mirror of
https://github.com/systemd/systemd.git
synced 2025-03-31 14:50:15 +03:00
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.
This commit is contained in:
parent
a67c318df8
commit
b9da6a098b
@ -214,13 +214,11 @@ static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_c
|
||||
DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free);
|
||||
|
||||
static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
|
||||
struct udev_ctrl_msg_wire ctrl_msg_wire;
|
||||
int err = 0;
|
||||
|
||||
memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
|
||||
strcpy(ctrl_msg_wire.version, "udev-" PACKAGE_VERSION);
|
||||
ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
|
||||
ctrl_msg_wire.type = type;
|
||||
struct udev_ctrl_msg_wire ctrl_msg_wire = {
|
||||
.version = "udev-" STRINGIFY(PROJECT_VERSION),
|
||||
.magic = UDEV_CTRL_MAGIC,
|
||||
.type = type,
|
||||
};
|
||||
|
||||
if (buf)
|
||||
strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
|
||||
@ -228,43 +226,33 @@ static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int
|
||||
ctrl_msg_wire.intval = intval;
|
||||
|
||||
if (!uctrl->connected) {
|
||||
if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) {
|
||||
err = -errno;
|
||||
goto out;
|
||||
}
|
||||
if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0)
|
||||
return -errno;
|
||||
uctrl->connected = true;
|
||||
}
|
||||
if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
|
||||
err = -errno;
|
||||
goto out;
|
||||
}
|
||||
if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0)
|
||||
return -errno;
|
||||
|
||||
/* wait for peer message handling or disconnect */
|
||||
for (;;) {
|
||||
struct pollfd pfd[1];
|
||||
struct pollfd pfd = {
|
||||
.fd = uctrl->sock,
|
||||
.events = POLLIN,
|
||||
};
|
||||
int r;
|
||||
|
||||
pfd[0].fd = uctrl->sock;
|
||||
pfd[0].events = POLLIN;
|
||||
r = poll(pfd, 1, timeout * MSEC_PER_SEC);
|
||||
r = poll(&pfd, 1, timeout * MSEC_PER_SEC);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
err = -errno;
|
||||
break;
|
||||
return -errno;
|
||||
}
|
||||
|
||||
if (r > 0 && pfd[0].revents & POLLERR) {
|
||||
err = -EIO;
|
||||
break;
|
||||
}
|
||||
|
||||
if (r == 0)
|
||||
err = -ETIMEDOUT;
|
||||
break;
|
||||
return -ETIMEDOUT;
|
||||
if (pfd.revents & POLLERR)
|
||||
return -EIO;
|
||||
return 0;
|
||||
}
|
||||
out:
|
||||
return err;
|
||||
}
|
||||
|
||||
int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user