From 5519f19a27f4b5e78f3018953166ccd70c319667 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Tue, 26 May 2015 12:15:17 -0400 Subject: udevd: modernize error handling We never return magic exit codes, but just EXIT_FAILUER or EXIT_SUCCESS. Signed-off-by: Anthony G. Basile --- src/udev/udevd.c | 74 ++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 42 deletions(-) diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 64ef25f38a..89681caf82 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -225,7 +225,7 @@ static void worker_spawn(struct event *event) { int fd_monitor; struct epoll_event ep_signal, ep_monitor; sigset_t mask; - int rc = EXIT_SUCCESS; + int r = 0; /* take initial device from queue */ dev = event->dev; @@ -242,15 +242,13 @@ static void worker_spawn(struct event *event) { sigfillset(&mask); fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); if (fd_signal < 0) { - log_error_errno(errno, "error creating signalfd %m"); - rc = 2; + r = log_error_errno(errno, "error creating signalfd %m"); goto out; } fd_ep = epoll_create1(EPOLL_CLOEXEC); if (fd_ep < 0) { - log_error_errno(errno, "error creating epoll fd: %m"); - rc = 3; + r = log_error_errno(errno, "error creating epoll fd: %m"); goto out; } @@ -265,8 +263,7 @@ static void worker_spawn(struct event *event) { if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 || epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) { - log_error_errno(errno, "fail to add fds to epoll: %m"); - rc = 4; + r = log_error_errno(errno, "fail to add fds to epoll: %m"); goto out; } @@ -279,12 +276,12 @@ static void worker_spawn(struct event *event) { for (;;) { struct udev_event *udev_event; struct worker_message msg; - int fd_lock = -1, r; + int fd_lock = -1; log_debug("seq %llu running", udev_device_get_seqnum(dev)); udev_event = udev_event_new(dev); if (udev_event == NULL) { - rc = 5; + r = -ENOMEM; goto out; } @@ -316,6 +313,7 @@ static void worker_spawn(struct event *event) { if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) { log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d)); fd_lock = safe_close(fd_lock); + r = -EAGAIN; goto skip; } } @@ -373,7 +371,7 @@ skip: if (fdcount < 0) { if (errno == EINTR) continue; - log_error_errno(errno, "failed to poll: %m"); + r = log_error_errno(errno, "failed to poll: %m"); goto out; } @@ -406,7 +404,7 @@ out: udev_builtin_exit(udev); udev_unref(udev); log_close(); - exit(rc); + exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); } case -1: event->state = EVENT_QUEUED; @@ -1138,11 +1136,13 @@ int main(int argc, char *argv[]) { struct epoll_event ep_netlink = { .events = EPOLLIN }; struct epoll_event ep_worker = { .events = EPOLLIN }; struct udev_ctrl_connection *ctrl_conn = NULL; - int rc = 1, r, one = 1; + int r = 0, one = 1; udev = udev_new(); - if (udev == NULL) + if (!udev) { + r = log_error_errno(errno, "could not allocate udev context: %m"); goto exit; + } log_set_target(LOG_TARGET_AUTO); log_open(); @@ -1159,7 +1159,7 @@ int main(int argc, char *argv[]) { log_set_max_level(LOG_DEBUG); if (getuid() != 0) { - log_error("root privileges required"); + r = log_error_errno(EPERM, "root privileges required"); goto exit; } @@ -1172,7 +1172,7 @@ int main(int argc, char *argv[]) { /* set umask before creating any file/directory */ r = chdir("/"); if (r < 0) { - log_error_errno(errno, "could not change dir to /: %m"); + r = log_error_errno(errno, "could not change dir to /: %m"); goto exit; } @@ -1182,7 +1182,7 @@ int main(int argc, char *argv[]) { r = mkdir(UDEV_ROOT_RUN "/udev", 0755); if (r < 0 && errno != EEXIST) { - log_error_errno(errno, "could not create " UDEV_ROOT_RUN "/udev: %m"); + r = log_error_errno(errno, "could not create " UDEV_ROOT_RUN "/udev: %m"); goto exit; } @@ -1207,30 +1207,26 @@ int main(int argc, char *argv[]) { /* open control and netlink socket */ udev_ctrl = udev_ctrl_new(udev); - if (udev_ctrl == NULL) { - log_error("error initializing udev control socket"); - rc = 1; + if (!udev_ctrl) { + r = log_error_errno(EINVAL, "error initializing udev control socket"); goto exit; } fd_ctrl = udev_ctrl_get_fd(udev_ctrl); monitor = udev_monitor_new_from_netlink(udev, "kernel"); - if (monitor == NULL) { - log_error("error initializing netlink socket"); - rc = 3; + if (!monitor) { + r = log_error_errno(EINVAL, "error initializing netlink socket"); goto exit; } fd_netlink = udev_monitor_get_fd(monitor); if (udev_monitor_enable_receiving(monitor) < 0) { - log_error("error binding netlink socket"); - rc = 3; + r = log_error_errno(EINVAL, "error binding netlink socket"); goto exit; } if (udev_ctrl_enable_receiving(udev_ctrl) < 0) { - log_error("error binding udev control socket"); - rc = 1; + r = log_error_errno(EINVAL, "error binding udev control socket"); goto exit; } @@ -1241,14 +1237,14 @@ int main(int argc, char *argv[]) { udev_builtin_init(udev); rules = udev_rules_new(udev, arg_resolve_names); - if (rules == NULL) { - log_error("error reading rules"); + if (!rules) { + r = log_error_errno(ENOMEM, "error reading rules"); goto exit; } - rc = udev_rules_apply_static_dev_perms(rules); - if (rc < 0) - log_error_errno(rc, "failed to apply permissions on static device nodes - %m"); + r = udev_rules_apply_static_dev_perms(rules); + if (r < 0) + log_error_errno(r, "failed to apply permissions on static device nodes: %m"); if (arg_daemonize) { pid_t pid; @@ -1258,11 +1254,9 @@ int main(int argc, char *argv[]) { case 0: break; case -1: - log_error_errno(errno, "fork of daemon failed: %m"); - rc = 4; + r = log_error_errno(errno, "fork of daemon failed: %m"); goto exit; default: - rc = EXIT_SUCCESS; goto exit_daemonize; } @@ -1287,8 +1281,7 @@ int main(int argc, char *argv[]) { fd_inotify = udev_watch_init(udev); if (fd_inotify < 0) { - log_error("error initializing inotify"); - rc = 4; + r = log_error_errno(ENOMEM, "error initializing inotify"); goto exit; } /* watch rules.d paths for changes */ @@ -1310,15 +1303,13 @@ int main(int argc, char *argv[]) { sigprocmask(SIG_SETMASK, &mask, &sigmask_orig); fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); if (fd_signal < 0) { - log_error("error creating signalfd"); - rc = 5; + r = log_error_errno(errno, "error creating signalfd"); goto exit; } /* unnamed socket from workers to the main daemon */ if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) { - log_error("error creating socketpair"); - rc = 6; + r = log_error_errno(errno, "error creating socketpair"); goto exit; } fd_worker = worker_watch[READ_END]; @@ -1549,7 +1540,6 @@ int main(int argc, char *argv[]) { ctrl_conn = handle_ctrl_msg(udev_ctrl); } - rc = EXIT_SUCCESS; exit: udev_ctrl_cleanup(udev_ctrl); unlink(UDEV_ROOT_RUN "/udev/queue"); @@ -1572,5 +1562,5 @@ exit_daemonize: mac_selinux_finish(); udev_unref(udev); log_close(); - return rc; + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; } -- cgit v1.2.3-54-g00ecf