Skip to content

Commit

Permalink
nbd-client: Exit with error code other than EXIT_FAILURE
Browse files Browse the repository at this point in the history
When nbd-client tries to connect to nbd device, it talks to kernel
via netlink. If the nbd device is taken and locked by other process
like systemd-udevd, kernel will return EBUSY to nbd client. However,
nbd client just hide this error code with error message to check
dmesg logs.

Checking the dmesg logs is error-prone and not friendly for other
caller program. Instead, nbd-client should return the error code
to the caller to handle it properly.

Signed-off-by: Lin Liu <[email protected]>
  • Loading branch information
liulinC authored and yoe committed Dec 20, 2024
1 parent 7a64238 commit cf5a774
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
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

0 comments on commit cf5a774

Please sign in to comment.