From b92bea5d2a9481de69bb627a7b442a9f58fca43d Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Sun, 24 Mar 2013 19:59:00 -0400 Subject: Use initalization instead of explicit zeroing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before, we would initialize many fields twice: first by filling the structure with zeros, and then a second time with the real values. We can let the compiler do the job for us, avoiding one copy. A downside of this patch is that text gets slightly bigger. This is because all zero() calls are effectively inlined: $ size build/.libs/systemd text data bss dec hex filename before 897737 107300 2560 1007597 f5fed build/.libs/systemd after 897873 107300 2560 1007733 f6075 build/.libs/systemd … actually less than 1‰. A few asserts that the parameter is not null had to be removed. I don't think this changes much, because first, it is quite unlikely for the assert to fail, and second, an immediate SEGV is almost as good as an assert. --- src/core/dbus-execute.c | 6 +-- src/core/dbus.c | 4 +- src/core/device.c | 3 +- src/core/execute.c | 22 ++++----- src/core/job.c | 22 ++++----- src/core/locale-setup.c | 4 +- src/core/loopback-setup.c | 65 +++++++++++++------------- src/core/main.c | 29 ++++++------ src/core/manager.c | 116 ++++++++++++++++++++-------------------------- src/core/mount.c | 15 +++--- src/core/service.c | 4 +- src/core/swap.c | 10 ++-- src/core/umount.c | 16 +++---- src/core/unit.c | 22 ++++----- 14 files changed, 146 insertions(+), 192 deletions(-) (limited to 'src/core') diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 0a53207e59..2a8a0e1ac5 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -160,9 +160,8 @@ int bus_execute_append_cpu_sched_priority(DBusMessageIter *i, const char *proper if (c->cpu_sched_set) n = c->cpu_sched_priority; else { - struct sched_param p; + struct sched_param p = {}; - zero(p); if (sched_getparam(0, &p) >= 0) n = p.sched_priority; else @@ -280,9 +279,8 @@ int bus_execute_append_rlimits(DBusMessageIter *i, const char *property, void *d if (c->rlimit[r]) u = (uint64_t) c->rlimit[r]->rlim_max; else { - struct rlimit rl; + struct rlimit rl = {}; - zero(rl); getrlimit(r, &rl); u = (uint64_t) rl.rlim_max; diff --git a/src/core/dbus.c b/src/core/dbus.c index b92c7d0b99..67ab1ae7f7 100644 --- a/src/core/dbus.c +++ b/src/core/dbus.c @@ -203,13 +203,11 @@ static void bus_toggle_watch(DBusWatch *bus_watch, void *data) { } static int bus_timeout_arm(Manager *m, Watch *w) { - struct itimerspec its; + struct itimerspec its = {}; assert(m); assert(w); - zero(its); - if (dbus_timeout_get_enabled(w->data.bus_timeout)) { timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC); its.it_interval = its.it_value; diff --git a/src/core/device.c b/src/core/device.c index 0b01718ad4..fb948688b6 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -477,7 +477,6 @@ static void device_shutdown(Manager *m) { } static int device_enumerate(Manager *m) { - struct epoll_event ev; int r; struct udev_enumerate *e = NULL; struct udev_list_entry *item = NULL, *first = NULL; @@ -485,6 +484,8 @@ static int device_enumerate(Manager *m) { assert(m); if (!m->udev) { + struct epoll_event ev; + if (!(m->udev = udev_new())) return -ENOMEM; diff --git a/src/core/execute.c b/src/core/execute.c index 2c13d1f9f6..c51049767d 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -205,7 +205,10 @@ static int open_null_as(int flags, int nfd) { static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, const char *unit_id, int nfd) { int fd, r; - union sockaddr_union sa; + union sockaddr_union sa = { + .un.sun_family = AF_UNIX, + .un.sun_path = "/run/systemd/journal/stdout", + }; assert(context); assert(output < _EXEC_OUTPUT_MAX); @@ -216,10 +219,6 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons if (fd < 0) return -errno; - zero(sa); - sa.un.sun_family = AF_UNIX; - strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path)); - r = connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)); if (r < 0) { close_nointr_nofail(fd); @@ -938,7 +937,7 @@ static int apply_seccomp(uint32_t *syscall_filter) { int i; unsigned n; struct sock_filter *f; - struct sock_fprog prog; + struct sock_fprog prog = {}; assert(syscall_filter); @@ -970,7 +969,6 @@ static int apply_seccomp(uint32_t *syscall_filter) { memcpy(f + (ELEMENTSOF(header) + 2*n), footer, sizeof(footer)); /* Third: install the filter */ - zero(prog); prog.len = ELEMENTSOF(header) + ELEMENTSOF(footer) + 2*n; prog.filter = f; if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) @@ -1210,10 +1208,9 @@ int exec_spawn(ExecCommand *command, } if (context->cpu_sched_set) { - struct sched_param param; - - zero(param); - param.sched_priority = context->cpu_sched_priority; + struct sched_param param = { + .sched_priority = context->cpu_sched_priority, + }; r = sched_setscheduler(0, context->cpu_sched_policy | @@ -1701,7 +1698,7 @@ int exec_context_load_environment(const ExecContext *c, char ***l) { int k; bool ignore = false; char **p; - glob_t pglob; + glob_t pglob = {}; int count, n; fn = *i; @@ -1721,7 +1718,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) { } /* Filename supports globbing, take all matching files */ - zero(pglob); errno = 0; if (glob(fn, 0, NULL, &pglob) != 0) { globfree(&pglob); diff --git a/src/core/job.c b/src/core/job.c index d5b7987a00..af5855b71d 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -858,10 +858,12 @@ finish: } int job_start_timer(Job *j) { - struct itimerspec its; - struct epoll_event ev; + struct itimerspec its = {}; + struct epoll_event ev = { + .data.ptr = &j->timer_watch, + .events = EPOLLIN, + }; int fd, r; - assert(j); if (j->unit->job_timeout <= 0 || j->timer_watch.type == WATCH_JOB_TIMER) @@ -874,7 +876,6 @@ int job_start_timer(Job *j) { goto fail; } - zero(its); timespec_store(&its.it_value, j->unit->job_timeout); if (timerfd_settime(fd, 0, &its, NULL) < 0) { @@ -882,10 +883,6 @@ int job_start_timer(Job *j) { goto fail; } - zero(ev); - ev.data.ptr = &j->timer_watch; - ev.events = EPOLLIN; - if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { r = -errno; goto fail; @@ -1064,15 +1061,14 @@ int job_deserialize(Job *j, FILE *f, FDSet *fds) { } int job_coldplug(Job *j) { - struct epoll_event ev; + struct epoll_event ev = { + .data.ptr = &j->timer_watch, + .events = EPOLLIN, + }; if (j->timer_watch.type != WATCH_JOB_TIMER) return 0; - zero(ev); - ev.data.ptr = &j->timer_watch; - ev.events = EPOLLIN; - if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, j->timer_watch.fd, &ev) < 0) return -errno; diff --git a/src/core/locale-setup.c b/src/core/locale-setup.c index eefb256ac5..d7113b9795 100644 --- a/src/core/locale-setup.c +++ b/src/core/locale-setup.c @@ -68,11 +68,9 @@ static const char * const variable_names[_VARIABLE_MAX] = { }; int locale_setup(void) { - char *variables[_VARIABLE_MAX]; + char *variables[_VARIABLE_MAX] = {}; int r = 0, i; - zero(variables); - if (detect_container(NULL) <= 0) { r = parse_env_file("/proc/cmdline", WHITESPACE, "locale.LANG", &variables[VARIABLE_LANG], diff --git a/src/core/loopback-setup.c b/src/core/loopback-setup.c index bfd0d93204..f98a29806e 100644 --- a/src/core/loopback-setup.c +++ b/src/core/loopback-setup.c @@ -88,25 +88,26 @@ static int add_adresses(int fd, int if_loopback, unsigned *requests) { union { struct sockaddr sa; struct sockaddr_nl nl; - } sa; + } sa = { + .nl.nl_family = AF_NETLINK, + }; + union { struct nlmsghdr header; uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg)) + RTA_LENGTH(sizeof(struct in6_addr))]; - } request; + } request = { + .header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), + .header.nlmsg_type = RTM_NEWADDR, + .header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK, + .header.nlmsg_seq = *requests + 1, + }; struct ifaddrmsg *ifaddrmsg; uint32_t ipv4_address = htonl(INADDR_LOOPBACK); int r; - zero(request); - - request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); - request.header.nlmsg_type = RTM_NEWADDR; - request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK; - request.header.nlmsg_seq = *requests + 1; - ifaddrmsg = NLMSG_DATA(&request.header); ifaddrmsg->ifa_family = AF_INET; ifaddrmsg->ifa_prefixlen = 8; @@ -114,13 +115,11 @@ static int add_adresses(int fd, int if_loopback, unsigned *requests) { ifaddrmsg->ifa_scope = RT_SCOPE_HOST; ifaddrmsg->ifa_index = if_loopback; - r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &ipv4_address, sizeof(ipv4_address)); + r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, + &ipv4_address, sizeof(ipv4_address)); if (r < 0) return r; - zero(sa); - sa.nl.nl_family = AF_NETLINK; - if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0) return -errno; (*requests)++; @@ -134,7 +133,8 @@ static int add_adresses(int fd, int if_loopback, unsigned *requests) { ifaddrmsg->ifa_family = AF_INET6; ifaddrmsg->ifa_prefixlen = 128; - r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &in6addr_loopback, sizeof(in6addr_loopback)); + r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, + &in6addr_loopback, sizeof(in6addr_loopback)); if (r < 0) return r; @@ -149,31 +149,29 @@ static int start_interface(int fd, int if_loopback, unsigned *requests) { union { struct sockaddr sa; struct sockaddr_nl nl; - } sa; + } sa = { + .nl.nl_family = AF_NETLINK, + }; + union { struct nlmsghdr header; uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))]; - } request; + } request = { + .header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), + .header.nlmsg_type = RTM_NEWLINK, + .header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK, + .header.nlmsg_seq = *requests + 1, + }; struct ifinfomsg *ifinfomsg; - zero(request); - - request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); - request.header.nlmsg_type = RTM_NEWLINK; - request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; - request.header.nlmsg_seq = *requests + 1; - ifinfomsg = NLMSG_DATA(&request.header); ifinfomsg->ifi_family = AF_UNSPEC; ifinfomsg->ifi_index = if_loopback; ifinfomsg->ifi_flags = IFF_UP; ifinfomsg->ifi_change = IFF_UP; - zero(sa); - sa.nl.nl_family = AF_NETLINK; - if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0) return -errno; @@ -234,7 +232,10 @@ static int check_loopback(void) { union { struct sockaddr sa; struct sockaddr_in in; - } sa; + } sa = { + .in.sin_family = AF_INET, + .in.sin_addr.s_addr = INADDR_LOOPBACK, + }; /* If we failed to set up the loop back device, check whether * it might already be set up */ @@ -243,10 +244,6 @@ static int check_loopback(void) { if (fd < 0) return -errno; - zero(sa); - sa.in.sin_family = AF_INET; - sa.in.sin_addr.s_addr = INADDR_LOOPBACK; - if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0) r = 1; else @@ -260,7 +257,9 @@ int loopback_setup(void) { union { struct sockaddr sa; struct sockaddr_nl nl; - } sa; + } sa = { + .nl.nl_family = AF_NETLINK, + }; unsigned requests = 0, i; int _cleanup_close_ fd = -1; bool eperm = false; @@ -274,8 +273,6 @@ int loopback_setup(void) { if (fd < 0) return -errno; - zero(sa); - sa.nl.nl_family = AF_NETLINK; if (bind(fd, &sa.sa, sizeof(sa)) < 0) { r = -errno; goto error; diff --git a/src/core/main.c b/src/core/main.c index 92f066c707..4d21dd97a3 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -107,13 +107,13 @@ _noreturn_ static void crash(int sig) { if (!arg_dump_core) log_error("Caught <%s>, not dumping core.", signal_to_string(sig)); else { - struct sigaction sa; + struct sigaction sa = { + .sa_handler = nop_handler, + .sa_flags = SA_NOCLDSTOP|SA_RESTART, + }; pid_t pid; /* We want to wait for the core process, hence let's enable SIGCHLD */ - zero(sa); - sa.sa_handler = nop_handler; - sa.sa_flags = SA_NOCLDSTOP|SA_RESTART; assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); pid = fork(); @@ -121,7 +121,7 @@ _noreturn_ static void crash(int sig) { log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno)); else if (pid == 0) { - struct rlimit rl; + struct rlimit rl = {}; /* Enable default signal handler for core dump */ zero(sa); @@ -129,7 +129,6 @@ _noreturn_ static void crash(int sig) { assert_se(sigaction(sig, &sa, NULL) == 0); /* Don't limit the core dump size */ - zero(rl); rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; setrlimit(RLIMIT_CORE, &rl); @@ -162,16 +161,16 @@ _noreturn_ static void crash(int sig) { chvt(arg_crash_chvt); if (arg_crash_shell) { - struct sigaction sa; + struct sigaction sa = { + .sa_handler = SIG_IGN, + .sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART, + }; pid_t pid; log_info("Executing crash shell in 10s..."); sleep(10); /* Let the kernel reap children for us */ - zero(sa); - sa.sa_handler = SIG_IGN; - sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART; assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); pid = fork(); @@ -193,12 +192,10 @@ _noreturn_ static void crash(int sig) { } static void install_crash_handler(void) { - struct sigaction sa; - - zero(sa); - - sa.sa_handler = crash; - sa.sa_flags = SA_NODEFER; + struct sigaction sa = { + .sa_handler = crash, + .sa_flags = SA_NODEFER, + }; sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1); } diff --git a/src/core/manager.c b/src/core/manager.c index e26522a4dd..549153e057 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -93,12 +93,15 @@ static int manager_setup_notify(Manager *m) { union { struct sockaddr sa; struct sockaddr_un un; - } sa; - struct epoll_event ev; + } sa = { + .sa.sa_family = AF_UNIX, + }; + struct epoll_event ev = { + .events = EPOLLIN, + .data.ptr = &m->notify_watch, + }; int one = 1, r; - assert(m); - m->notify_watch.type = WATCH_NOTIFY; m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); if (m->notify_watch.fd < 0) { @@ -106,9 +109,6 @@ static int manager_setup_notify(Manager *m) { return -errno; } - zero(sa); - sa.sa.sa_family = AF_UNIX; - if (getpid() != 1 || detect_container(NULL) > 0) snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET "/%llu", random_ull()); else @@ -129,10 +129,6 @@ static int manager_setup_notify(Manager *m) { return -errno; } - zero(ev); - ev.events = EPOLLIN; - ev.data.ptr = &m->notify_watch; - r = epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev); if (r < 0) { log_error("Failed to add notification socket fd to epoll: %m"); @@ -150,16 +146,14 @@ static int manager_setup_notify(Manager *m) { } static int manager_jobs_in_progress_mod_timer(Manager *m) { - struct itimerspec its; + struct itimerspec its = { + .it_value.tv_sec = JOBS_IN_PROGRESS_WAIT_SEC, + .it_interval.tv_sec = JOBS_IN_PROGRESS_PERIOD_SEC, + }; if (m->jobs_in_progress_watch.type != WATCH_JOBS_IN_PROGRESS) return 0; - zero(its); - - its.it_value.tv_sec = JOBS_IN_PROGRESS_WAIT_SEC; - its.it_interval.tv_sec = JOBS_IN_PROGRESS_PERIOD_SEC; - if (timerfd_settime(m->jobs_in_progress_watch.fd, 0, &its, NULL) < 0) return -errno; @@ -167,11 +161,12 @@ static int manager_jobs_in_progress_mod_timer(Manager *m) { } static int manager_watch_jobs_in_progress(Manager *m) { - struct epoll_event ev; + struct epoll_event ev = { + .events = EPOLLIN, + .data.ptr = &m->jobs_in_progress_watch, + }; int r; - assert(m); - if (m->jobs_in_progress_watch.type != WATCH_INVALID) return 0; @@ -189,10 +184,6 @@ static int manager_watch_jobs_in_progress(Manager *m) { goto err; } - zero(ev); - ev.events = EPOLLIN; - ev.data.ptr = &m->jobs_in_progress_watch; - if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->jobs_in_progress_watch.fd, &ev) < 0) { log_error("Failed to add jobs progress timer fd to epoll: %m"); r = -errno; @@ -287,10 +278,18 @@ static void manager_print_jobs_in_progress(Manager *m) { } static int manager_setup_time_change(Manager *m) { - struct epoll_event ev; - struct itimerspec its; + struct epoll_event ev = { + .events = EPOLLIN, + .data.ptr = &m->time_change_watch, + }; + + /* We only care for the cancellation event, hence we set the + * timeout to the latest possible value. */ + struct itimerspec its = { + .it_value.tv_sec = TIME_T_MAX, + }; + assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX)); - assert(m); assert(m->time_change_watch.type == WATCH_INVALID); /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever @@ -303,13 +302,6 @@ static int manager_setup_time_change(Manager *m) { return -errno; } - zero(its); - - /* We only care for the cancellation event, hence we set the - * timeout to the latest possible value. */ - assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX)); - its.it_value.tv_sec = TIME_T_MAX; - if (timerfd_settime(m->time_change_watch.fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) { log_debug("Failed to set up TFD_TIMER_CANCEL_ON_SET, ignoring: %m"); close_nointr_nofail(m->time_change_watch.fd); @@ -317,10 +309,6 @@ static int manager_setup_time_change(Manager *m) { return 0; } - zero(ev); - ev.events = EPOLLIN; - ev.data.ptr = &m->time_change_watch; - if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->time_change_watch.fd, &ev) < 0) { log_error("Failed to add timer change fd to epoll: %m"); return -errno; @@ -360,15 +348,18 @@ static int enable_special_signals(Manager *m) { static int manager_setup_signals(Manager *m) { sigset_t mask; - struct epoll_event ev; - struct sigaction sa; + struct epoll_event ev = { + .events = EPOLLIN, + .data.ptr = &m->signal_watch, + }; + struct sigaction sa = { + .sa_handler = SIG_DFL, + .sa_flags = SA_NOCLDSTOP|SA_RESTART, + }; assert(m); /* We are not interested in SIGSTOP and friends. */ - zero(sa); - sa.sa_handler = SIG_DFL; - sa.sa_flags = SA_NOCLDSTOP|SA_RESTART; assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); assert_se(sigemptyset(&mask) == 0); @@ -410,10 +401,6 @@ static int manager_setup_signals(Manager *m) { if (m->signal_watch.fd < 0) return -errno; - zero(ev); - ev.events = EPOLLIN; - ev.data.ptr = &m->signal_watch; - if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0) return -errno; @@ -1184,30 +1171,29 @@ static int manager_process_notify_fd(Manager *m) { for (;;) { char buf[4096]; - struct msghdr msghdr; - struct iovec iovec; - struct ucred *ucred; + struct iovec iovec = { + .iov_base = buf, + .iov_len = sizeof(buf)-1, + }; + union { struct cmsghdr cmsghdr; uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; - } control; + } control = {}; + + struct msghdr msghdr = { + .msg_iov = &iovec, + .msg_iovlen = 1, + .msg_control = &control, + .msg_controllen = sizeof(control), + }; + struct ucred *ucred; Unit *u; char _cleanup_strv_free_ **tags = NULL; - zero(iovec); - iovec.iov_base = buf; - iovec.iov_len = sizeof(buf)-1; - - zero(control); - zero(msghdr); - msghdr.msg_iov = &iovec; - msghdr.msg_iovlen = 1; - msghdr.msg_control = &control; - msghdr.msg_controllen = sizeof(control); - n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT); if (n <= 0) { - if (n >= 0) + if (n == 0) return -EIO; if (errno == EAGAIN || errno == EINTR) @@ -1254,12 +1240,10 @@ static int manager_dispatch_sigchld(Manager *m) { assert(m); for (;;) { - siginfo_t si; + siginfo_t si = {}; Unit *u; int r; - zero(si); - /* First we call waitd() for a PID and do not reap the * zombie. That way we can still access /proc/$PID for * it while it is a zombie. */ diff --git a/src/core/mount.c b/src/core/mount.c index 51a81a9488..4f245555f2 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -293,7 +293,7 @@ static int mount_add_requires_mounts_links(Mount *m) { } static char* mount_test_option(const char *haystack, const char *needle) { - struct mntent me; + struct mntent me = { .mnt_opts = (char*) haystack }; assert(needle); @@ -303,9 +303,6 @@ static char* mount_test_option(const char *haystack, const char *needle) { if (!haystack) return NULL; - zero(me); - me.mnt_opts = (char*) haystack; - return hasmntopt(&me, needle); } @@ -1706,10 +1703,14 @@ static void mount_shutdown(Manager *m) { static int mount_enumerate(Manager *m) { int r; - struct epoll_event ev; assert(m); if (!m->proc_self_mountinfo) { + struct epoll_event ev = { + .events = EPOLLPRI, + .data.ptr = &m->mount_watch, + }; + m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"); if (!m->proc_self_mountinfo) return -errno; @@ -1717,10 +1718,6 @@ static int mount_enumerate(Manager *m) { m->mount_watch.type = WATCH_MOUNT; m->mount_watch.fd = fileno(m->proc_self_mountinfo); - zero(ev); - ev.events = EPOLLPRI; - ev.data.ptr = &m->mount_watch; - if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0) return -errno; } diff --git a/src/core/service.c b/src/core/service.c index 98266a5a90..a104b300c3 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -3446,7 +3446,7 @@ static int service_enumerate(Manager *m) { unsigned i; DIR _cleanup_closedir_ *d = NULL; char _cleanup_free_ *path = NULL, *fpath = NULL, *name = NULL; - Set *runlevel_services[ELEMENTSOF(rcnd_table)]; + Set *runlevel_services[ELEMENTSOF(rcnd_table)] = {}; Set _cleanup_set_free_ *shutdown_services = NULL; Unit *service; Iterator j; @@ -3457,8 +3457,6 @@ static int service_enumerate(Manager *m) { if (m->running_as != SYSTEMD_SYSTEM) return 0; - zero(runlevel_services); - STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path) for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) { struct dirent *de; diff --git a/src/core/swap.c b/src/core/swap.c index dc98f47387..b363c5ec09 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -1242,10 +1242,14 @@ static void swap_shutdown(Manager *m) { static int swap_enumerate(Manager *m) { int r; - struct epoll_event ev; assert(m); if (!m->proc_swaps) { + struct epoll_event ev = { + .events = EPOLLPRI, + .data.ptr = &m->swap_watch, + }; + m->proc_swaps = fopen("/proc/swaps", "re"); if (!m->proc_swaps) return (errno == ENOENT) ? 0 : -errno; @@ -1253,10 +1257,6 @@ static int swap_enumerate(Manager *m) { m->swap_watch.type = WATCH_SWAP; m->swap_watch.fd = fileno(m->proc_swaps); - zero(ev); - ev.events = EPOLLPRI; - ev.data.ptr = &m->swap_watch; - if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0) return -errno; } diff --git a/src/core/umount.c b/src/core/umount.c index 95e47e3c5a..8c69e74caf 100644 --- a/src/core/umount.c +++ b/src/core/umount.c @@ -381,7 +381,13 @@ static int delete_loopback(const char *device) { static int delete_dm(dev_t devnum) { int _cleanup_close_ fd = -1; int r; - struct dm_ioctl dm; + struct dm_ioctl dm = { + .version = {DM_VERSION_MAJOR, + DM_VERSION_MINOR, + DM_VERSION_PATCHLEVEL}, + .data_size = sizeof(dm), + .dev = devnum, + }; assert(major(devnum) != 0); @@ -389,14 +395,6 @@ static int delete_dm(dev_t devnum) { if (fd < 0) return -errno; - zero(dm); - dm.version[0] = DM_VERSION_MAJOR; - dm.version[1] = DM_VERSION_MINOR; - dm.version[2] = DM_VERSION_PATCHLEVEL; - - dm.data_size = sizeof(dm); - dm.dev = devnum; - r = ioctl(fd, DM_DEV_REMOVE, &dm); return r >= 0 ? 0 : -errno; } diff --git a/src/core/unit.c b/src/core/unit.c index dbb1882965..63e42b2330 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -1520,17 +1520,16 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su } int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) { - struct epoll_event ev; + struct epoll_event ev = { + .data.ptr = w, + .events = events, + }; assert(u); assert(fd >= 0); assert(w); assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u)); - zero(ev); - ev.data.ptr = w; - ev.events = events; - if (epoll_ctl(u->manager->epoll_fd, w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, fd, @@ -1578,7 +1577,7 @@ void unit_unwatch_pid(Unit *u, pid_t pid) { } int unit_watch_timer(Unit *u, clockid_t clock_id, bool relative, usec_t usec, Watch *w) { - struct itimerspec its; + struct itimerspec its = {}; int flags, fd; bool ours; @@ -1603,8 +1602,6 @@ int unit_watch_timer(Unit *u, clockid_t clock_id, bool relative, usec_t usec, Wa } else assert_not_reached("Invalid watch type"); - zero(its); - if (usec <= 0) { /* Set absolute time in the past, but not 0, since we * don't want to disarm the timer */ @@ -1622,11 +1619,10 @@ int unit_watch_timer(Unit *u, clockid_t clock_id, bool relative, usec_t usec, Wa goto fail; if (w->type == WATCH_INVALID) { - struct epoll_event ev; - - zero(ev); - ev.data.ptr = w; - ev.events = EPOLLIN; + struct epoll_event ev = { + .data.ptr = w, + .events = EPOLLIN, + }; if (epoll_ctl(u->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) goto fail; -- cgit v1.2.3-54-g00ecf