From 53bba2fb1da3ca26b1cf96c461598d53df2040cc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 24 Jan 2019 15:09:40 +0900 Subject: [PATCH] udev-ctrl: refactor udev_ctrl_enable_receiving() --- src/udev/udev-ctrl.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/udev/udev-ctrl.c b/src/udev/udev-ctrl.c index 0ed59c0..7d8ff6d 100644 --- a/src/udev/udev-ctrl.c +++ b/src/udev/udev-ctrl.c @@ -116,25 +116,28 @@ int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) { } int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) { - int err; - - if (!uctrl->bound) { - err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); - if (err < 0 && errno == EADDRINUSE) { - (void) sockaddr_un_unlink(&uctrl->saddr.un); - err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); - } + int r; - if (err < 0) - return log_error_errno(errno, "Failed to bind socket: %m"); + assert(uctrl); - err = listen(uctrl->sock, 0); - if (err < 0) - return log_error_errno(errno, "Failed to listen: %m"); + if (uctrl->bound) + return 0; - uctrl->bound = true; - uctrl->cleanup_socket = true; + r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); + if (r < 0 && errno == EADDRINUSE) { + (void) sockaddr_un_unlink(&uctrl->saddr.un); + r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); } + + if (r < 0) + return log_error_errno(errno, "Failed to bind udev control socket: %m"); + + if (listen(uctrl->sock, 0) < 0) + return log_error_errno(errno, "Failed to listen udev control socket: %m"); + + uctrl->bound = true; + uctrl->cleanup_socket = true; + return 0; } -- 2.7.4