Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nbd-client: Exit with error code other than EXIT_FAILURE #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cliserv.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ void nbd_err(const char *s) {
exit(EXIT_FAILURE);
}

void nbd_err_code(const char *s, int code) {
err_nonfatal(s);
fprintf(stderr, "Exiting.\n");
exit(abs(code));
}

void logging(const char* name) {
#ifdef ISSERVER
openlog(name, LOG_PID, LOG_DAEMON);
Expand Down
2 changes: 2 additions & 0 deletions cliserv.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ void setmysockopt(int sock);
void err_nonfatal(const char *s);

void nbd_err(const char *s) G_GNUC_NORETURN;
void nbd_err_code(const char *s, int code) G_GNUC_NORETURN;
#define err(S) nbd_err(S)
#define err_code(S, C) nbd_err_code(S, C)

void logging(const char* name);

Expand Down
14 changes: 9 additions & 5 deletions nbd-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ static void netlink_configure(int index, int *sockfds, int num_connects,
struct nlattr *sock_attr;
struct nl_msg *msg;
int driver_id, i;
int ret;

socket = get_nbd_socket(&driver_id);
nl_socket_modify_cb(socket, NL_CB_VALID, NL_CB_CUSTOM, callback, NULL);
Expand Down Expand Up @@ -199,11 +200,12 @@ static void netlink_configure(int index, int *sockfds, int num_connects,
}
nla_nest_end(msg, sock_attr);

if (nl_send_sync(socket, msg) < 0) {
ret = nl_send_sync(socket, msg);
if (ret < 0) {
if(geteuid() != 0) {
err("Failed to setup device. Are you root?\n");
err_code("Failed to setup device. Are you root?\n", ret);
} else {
err("Failed to setup device, check dmesg\n");
err_code("Failed to setup device, check dmesg\n", ret);
}
}
return;
Expand All @@ -215,6 +217,7 @@ static void netlink_disconnect(char *nbddev) {
struct nl_sock *socket;
struct nl_msg *msg;
int driver_id;
int ret;

int index = -1;
if (nbddev) {
Expand All @@ -232,8 +235,9 @@ static void netlink_disconnect(char *nbddev) {
genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, 0,
NBD_CMD_DISCONNECT, 0);
NLA_PUT_U32(msg, NBD_ATTR_INDEX, index);
if (nl_send_sync(socket, msg) < 0)
err("Failed to disconnect device, check dmsg\n");
ret = nl_send_sync(socket, msg);
if (ret < 0)
err_code("Failed to disconnect device, check dmsg\n", ret);
nl_socket_free(socket);
return;
nla_put_failure:
Expand Down