Skip to content

Commit cf5a774

Browse files
liulinCyoe
authored andcommitted
nbd-client: Exit with error code other than EXIT_FAILURE
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]>
1 parent 7a64238 commit cf5a774

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

cliserv.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ void nbd_err(const char *s) {
7878
exit(EXIT_FAILURE);
7979
}
8080

81+
void nbd_err_code(const char *s, int code) {
82+
err_nonfatal(s);
83+
fprintf(stderr, "Exiting.\n");
84+
exit(abs(code));
85+
}
86+
8187
void logging(const char* name) {
8288
#ifdef ISSERVER
8389
openlog(name, LOG_PID, LOG_DAEMON);

cliserv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ void setmysockopt(int sock);
7777
void err_nonfatal(const char *s);
7878

7979
void nbd_err(const char *s) G_GNUC_NORETURN;
80+
void nbd_err_code(const char *s, int code) G_GNUC_NORETURN;
8081
#define err(S) nbd_err(S)
82+
#define err_code(S, C) nbd_err_code(S, C)
8183

8284
void logging(const char* name);
8385

nbd-client.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static void netlink_configure(int index, int *sockfds, int num_connects,
169169
struct nlattr *sock_attr;
170170
struct nl_msg *msg;
171171
int driver_id, i;
172+
int ret;
172173

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

202-
if (nl_send_sync(socket, msg) < 0) {
203+
ret = nl_send_sync(socket, msg);
204+
if (ret < 0) {
203205
if(geteuid() != 0) {
204-
err("Failed to setup device. Are you root?\n");
206+
err_code("Failed to setup device. Are you root?\n", ret);
205207
} else {
206-
err("Failed to setup device, check dmesg\n");
208+
err_code("Failed to setup device, check dmesg\n", ret);
207209
}
208210
}
209211
return;
@@ -215,6 +217,7 @@ static void netlink_disconnect(char *nbddev) {
215217
struct nl_sock *socket;
216218
struct nl_msg *msg;
217219
int driver_id;
220+
int ret;
218221

219222
int index = -1;
220223
if (nbddev) {
@@ -232,8 +235,9 @@ static void netlink_disconnect(char *nbddev) {
232235
genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, 0,
233236
NBD_CMD_DISCONNECT, 0);
234237
NLA_PUT_U32(msg, NBD_ATTR_INDEX, index);
235-
if (nl_send_sync(socket, msg) < 0)
236-
err("Failed to disconnect device, check dmsg\n");
238+
ret = nl_send_sync(socket, msg);
239+
if (ret < 0)
240+
err_code("Failed to disconnect device, check dmsg\n", ret);
237241
nl_socket_free(socket);
238242
return;
239243
nla_put_failure:

0 commit comments

Comments
 (0)