Skip to content

Commit b9da6a0

Browse files
committed
udev: modernize ctrl_send and use PROJECT_VERSION
PROJECT_VERSION is used in preparation for future changes. Let's simplify the code by using structured initialization. If the string written to .version ever became to long, the compiler will truncate it and tell us: ../src/udev/udev-ctrl.c: In function ‘ctrl_send’: ../src/udev/udev-ctrl.c:221:28: warning: initializer-string for array of chars is too long .version = "udev-" STRINGIFY(R_VERSION), ^~~~~~~ ../src/udev/udev-ctrl.c:221:28: note: (near initialization for ‘ctrl_msg_wire.version’) No functional change.
1 parent a67c318 commit b9da6a0

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/udev/udev-ctrl.c

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -214,57 +214,45 @@ static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_c
214214
DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free);
215215

216216
static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
217-
struct udev_ctrl_msg_wire ctrl_msg_wire;
218-
int err = 0;
219-
220-
memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
221-
strcpy(ctrl_msg_wire.version, "udev-" PACKAGE_VERSION);
222-
ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
223-
ctrl_msg_wire.type = type;
217+
struct udev_ctrl_msg_wire ctrl_msg_wire = {
218+
.version = "udev-" STRINGIFY(PROJECT_VERSION),
219+
.magic = UDEV_CTRL_MAGIC,
220+
.type = type,
221+
};
224222

225223
if (buf)
226224
strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
227225
else
228226
ctrl_msg_wire.intval = intval;
229227

230228
if (!uctrl->connected) {
231-
if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) {
232-
err = -errno;
233-
goto out;
234-
}
229+
if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0)
230+
return -errno;
235231
uctrl->connected = true;
236232
}
237-
if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
238-
err = -errno;
239-
goto out;
240-
}
233+
if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0)
234+
return -errno;
241235

242236
/* wait for peer message handling or disconnect */
243237
for (;;) {
244-
struct pollfd pfd[1];
238+
struct pollfd pfd = {
239+
.fd = uctrl->sock,
240+
.events = POLLIN,
241+
};
245242
int r;
246243

247-
pfd[0].fd = uctrl->sock;
248-
pfd[0].events = POLLIN;
249-
r = poll(pfd, 1, timeout * MSEC_PER_SEC);
244+
r = poll(&pfd, 1, timeout * MSEC_PER_SEC);
250245
if (r < 0) {
251246
if (errno == EINTR)
252247
continue;
253-
err = -errno;
254-
break;
255-
}
256-
257-
if (r > 0 && pfd[0].revents & POLLERR) {
258-
err = -EIO;
259-
break;
248+
return -errno;
260249
}
261-
262250
if (r == 0)
263-
err = -ETIMEDOUT;
264-
break;
251+
return -ETIMEDOUT;
252+
if (pfd.revents & POLLERR)
253+
return -EIO;
254+
return 0;
265255
}
266-
out:
267-
return err;
268256
}
269257

270258
int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) {

0 commit comments

Comments
 (0)