From: Zbigniew Jędrzejewski-Szmek Date: Fri, 21 Dec 2018 12:36:26 +0000 (+0100) Subject: udev: modernize ctrl_send and use PROJECT_VERSION X-Git-Tag: v241-rc1~131^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b9da6a098b08d4eafec239fc05e0fd2073115504;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/udev/udev-ctrl.c b/src/udev/udev-ctrl.c index d90ebb72599..cb36c7e537d 100644 --- a/src/udev/udev-ctrl.c +++ b/src/udev/udev-ctrl.c @@ -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; - } - - if (r > 0 && pfd[0].revents & POLLERR) { - err = -EIO; - break; + return -errno; } - 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) {