summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-03-24 19:59:00 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-04-05 19:50:57 -0400
commitb92bea5d2a9481de69bb627a7b442a9f58fca43d (patch)
treed43f5e340014d5c3ce723eabb60cd74e3dd20a18 /src/shared
parent8c62ecf1a99ab4a3f69cb81be38715c504ef5723 (diff)
Use initalization instead of explicit zeroing
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.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/ask-password-api.c7
-rw-r--r--src/shared/dbus-loop.c20
-rw-r--r--src/shared/install.c24
-rw-r--r--src/shared/log.c43
-rw-r--r--src/shared/util.c109
-rw-r--r--src/shared/utmp-wtmp.c18
-rw-r--r--src/shared/virt.c3
7 files changed, 92 insertions, 132 deletions
diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c
index 277efd302..4557155d4 100644
--- a/src/shared/ask-password-api.c
+++ b/src/shared/ask-password-api.c
@@ -109,7 +109,6 @@ int ask_password_tty(
}
zero(pollfd);
-
pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
pollfd[POLL_TTY].events = POLLIN;
pollfd[POLL_INOTIFY].fd = notify;
@@ -248,7 +247,9 @@ static int create_socket(char **name) {
union {
struct sockaddr sa;
struct sockaddr_un un;
- } sa;
+ } sa = {
+ .un.sun_family = AF_UNIX,
+ };
int one = 1, r;
char *c;
@@ -260,8 +261,6 @@ static int create_socket(char **name) {
return -errno;
}
- zero(sa);
- sa.un.sun_family = AF_UNIX;
snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%llu", random_ull());
RUN_WITH_UMASK(0177) {
diff --git a/src/shared/dbus-loop.c b/src/shared/dbus-loop.c
index fec8998bc..b42ae1451 100644
--- a/src/shared/dbus-loop.c
+++ b/src/shared/dbus-loop.c
@@ -45,7 +45,7 @@ typedef struct EpollData {
static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
EpollData _cleanup_free_ *e = NULL;
- struct epoll_event ev;
+ struct epoll_event ev = { .data.ptr = e };
assert(watch);
@@ -57,9 +57,7 @@ static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
e->object = watch;
e->is_timeout = false;
- zero(ev);
ev.events = bus_flags_to_events(watch);
- ev.data.ptr = e;
if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) {
@@ -106,7 +104,7 @@ static void remove_watch(DBusWatch *watch, void *data) {
static void toggle_watch(DBusWatch *watch, void *data) {
EpollData *e;
- struct epoll_event ev;
+ struct epoll_event ev = {};
assert(watch);
@@ -114,21 +112,18 @@ static void toggle_watch(DBusWatch *watch, void *data) {
if (!e)
return;
- zero(ev);
- ev.events = bus_flags_to_events(watch);
ev.data.ptr = e;
+ ev.events = bus_flags_to_events(watch);
assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_MOD, e->fd, &ev) == 0);
}
static int timeout_arm(EpollData *e) {
- struct itimerspec its;
+ struct itimerspec its = {};
assert(e);
assert(e->is_timeout);
- zero(its);
-
if (dbus_timeout_get_enabled(e->object)) {
timespec_store(&its.it_value, dbus_timeout_get_interval(e->object) * USEC_PER_MSEC);
its.it_interval = its.it_value;
@@ -142,7 +137,7 @@ static int timeout_arm(EpollData *e) {
static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data) {
EpollData *e;
- struct epoll_event ev;
+ struct epoll_event ev = {};
assert(timeout);
@@ -160,7 +155,6 @@ static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data) {
if (timeout_arm(e) < 0)
goto fail;
- zero(ev);
ev.events = EPOLLIN;
ev.data.ptr = e;
@@ -227,13 +221,11 @@ int bus_loop_open(DBusConnection *c) {
int bus_loop_dispatch(int fd) {
int n;
- struct epoll_event event;
+ struct epoll_event event = {};
EpollData *d;
assert(fd >= 0);
- zero(event);
-
n = epoll_wait(fd, &event, 1, 0);
if (n < 0)
return errno == EAGAIN || errno == EINTR ? 0 : -errno;
diff --git a/src/shared/install.c b/src/shared/install.c
index 2555a36c0..9e870392f 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -700,7 +700,7 @@ int unit_file_link(
UnitFileChange **changes,
unsigned *n_changes) {
- LookupPaths _cleanup_lookup_paths_free_ paths = {NULL};
+ LookupPaths _cleanup_lookup_paths_free_ paths = {};
char **i;
char _cleanup_free_ *config_path = NULL;
int r, q;
@@ -1116,7 +1116,7 @@ static int unit_file_can_install(
const char *name,
bool allow_symlink) {
- InstallContext _cleanup_install_context_done_ c = {NULL};
+ InstallContext _cleanup_install_context_done_ c = {};
InstallInfo *i;
int r;
@@ -1452,8 +1452,8 @@ int unit_file_enable(
UnitFileChange **changes,
unsigned *n_changes) {
- LookupPaths _cleanup_lookup_paths_free_ paths = {NULL};
- InstallContext _cleanup_install_context_done_ c = {NULL};
+ LookupPaths _cleanup_lookup_paths_free_ paths = {};
+ InstallContext _cleanup_install_context_done_ c = {};
char **i;
char _cleanup_free_ *config_path = NULL;
int r;
@@ -1491,8 +1491,8 @@ int unit_file_disable(
UnitFileChange **changes,
unsigned *n_changes) {
- LookupPaths _cleanup_lookup_paths_free_ paths = {NULL};
- InstallContext _cleanup_install_context_done_ c = {NULL};
+ LookupPaths _cleanup_lookup_paths_free_ paths = {};
+ InstallContext _cleanup_install_context_done_ c = {};
char **i;
char _cleanup_free_ *config_path = NULL;
Set _cleanup_set_free_free_ *remove_symlinks_to = NULL;
@@ -1533,8 +1533,8 @@ int unit_file_reenable(
UnitFileChange **changes,
unsigned *n_changes) {
- LookupPaths _cleanup_lookup_paths_free_ paths = {NULL};
- InstallContext _cleanup_install_context_done_ c = {NULL};
+ LookupPaths _cleanup_lookup_paths_free_ paths = {};
+ InstallContext _cleanup_install_context_done_ c = {};
char **i;
char _cleanup_free_ *config_path = NULL;
Set _cleanup_set_free_free_ *remove_symlinks_to = NULL;
@@ -1576,7 +1576,7 @@ UnitFileState unit_file_get_state(
const char *root_dir,
const char *name) {
- LookupPaths _cleanup_lookup_paths_free_ paths = {NULL};
+ LookupPaths _cleanup_lookup_paths_free_ paths = {};
UnitFileState state = _UNIT_FILE_STATE_INVALID;
char **i;
char _cleanup_free_ *path = NULL;
@@ -1734,8 +1734,8 @@ int unit_file_preset(
UnitFileChange **changes,
unsigned *n_changes) {
- LookupPaths _cleanup_lookup_paths_free_ paths = {NULL};
- InstallContext _cleanup_install_context_done_ plus = {NULL}, minus = {NULL};
+ LookupPaths _cleanup_lookup_paths_free_ paths = {};
+ InstallContext _cleanup_install_context_done_ plus = {}, minus = {NULL};
char **i;
char _cleanup_free_ *config_path = NULL;
Set _cleanup_set_free_free_ *remove_symlinks_to = NULL;
@@ -1800,7 +1800,7 @@ int unit_file_get_list(
const char *root_dir,
Hashmap *h) {
- LookupPaths _cleanup_lookup_paths_free_ paths = {NULL};
+ LookupPaths _cleanup_lookup_paths_free_ paths = {};
char **i;
char _cleanup_free_ *buf = NULL;
DIR _cleanup_closedir_ *d = NULL;
diff --git a/src/shared/log.c b/src/shared/log.c
index 0dd04bc51..876f22dfc 100644
--- a/src/shared/log.c
+++ b/src/shared/log.c
@@ -129,16 +129,15 @@ static int create_log_socket(int type) {
}
static int log_open_syslog(void) {
- union sockaddr_union sa;
int r;
+ union sockaddr_union sa = {
+ .un.sun_family = AF_UNIX,
+ .un.sun_path = "/dev/log",
+ };
if (syslog_fd >= 0)
return 0;
- zero(sa);
- sa.un.sun_family = AF_UNIX;
- strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
-
syslog_fd = create_log_socket(SOCK_DGRAM);
if (syslog_fd < 0) {
r = syslog_fd;
@@ -183,7 +182,10 @@ void log_close_journal(void) {
}
static int log_open_journal(void) {
- union sockaddr_union sa;
+ union sockaddr_union sa = {
+ .un.sun_family = AF_UNIX,
+ .un.sun_path = "/run/systemd/journal/socket",
+ };
int r;
if (journal_fd >= 0)
@@ -195,10 +197,6 @@ static int log_open_journal(void) {
goto fail;
}
- zero(sa);
- sa.un.sun_family = AF_UNIX;
- strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
-
if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
r = -errno;
goto fail;
@@ -313,7 +311,7 @@ static int write_to_console(
const char *buffer) {
char location[64];
- struct iovec iovec[5];
+ struct iovec iovec[5] = {};
unsigned n = 0;
bool highlight;
@@ -322,8 +320,6 @@ static int write_to_console(
highlight = LOG_PRI(level) <= LOG_ERR && show_color;
- zero(iovec);
-
if (show_location) {
snprintf(location, sizeof(location), "(%s:%u) ", file, line);
char_array_0(location);
@@ -353,8 +349,11 @@ static int write_to_syslog(
const char *buffer) {
char header_priority[16], header_time[64], header_pid[16];
- struct iovec iovec[5];
- struct msghdr msghdr;
+ struct iovec iovec[5] = {};
+ struct msghdr msghdr = {
+ .msg_iov = iovec,
+ .msg_iovlen = ELEMENTSOF(iovec),
+ };
time_t t;
struct tm *tm;
@@ -375,7 +374,6 @@ static int write_to_syslog(
snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
char_array_0(header_pid);
- zero(iovec);
IOVEC_SET_STRING(iovec[0], header_priority);
IOVEC_SET_STRING(iovec[1], header_time);
IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
@@ -386,10 +384,6 @@ static int write_to_syslog(
if (syslog_is_stream)
iovec[4].iov_len++;
- zero(msghdr);
- msghdr.msg_iov = iovec;
- msghdr.msg_iovlen = ELEMENTSOF(iovec);
-
for (;;) {
ssize_t n;
@@ -417,7 +411,7 @@ static int write_to_kmsg(
const char *buffer) {
char header_priority[16], header_pid[16];
- struct iovec iovec[5];
+ struct iovec iovec[5] = {};
if (kmsg_fd < 0)
return 0;
@@ -428,7 +422,6 @@ static int write_to_kmsg(
snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
char_array_0(header_pid);
- zero(iovec);
IOVEC_SET_STRING(iovec[0], header_priority);
IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
IOVEC_SET_STRING(iovec[2], header_pid);
@@ -482,8 +475,8 @@ static int write_to_journal(
const char *buffer) {
char header[LINE_MAX];
- struct iovec iovec[4] = {{0}};
- struct msghdr mh = {0};
+ struct iovec iovec[4] = {};
+ struct msghdr mh = {};
if (journal_fd < 0)
return 0;
@@ -742,7 +735,7 @@ int log_struct_internal(
journal_fd >= 0) {
char header[LINE_MAX];
- struct iovec iovec[17] = {{0}};
+ struct iovec iovec[17] = {};
unsigned n = 0, i;
struct msghdr mh;
static const char nl = '\n';
diff --git a/src/shared/util.c b/src/shared/util.c
index 1bffd84d1..2f66597de 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -889,15 +889,14 @@ int reset_all_signal_handlers(void) {
int sig;
for (sig = 1; sig < _NSIG; sig++) {
- struct sigaction sa;
+ struct sigaction sa = {
+ .sa_handler = SIG_DFL,
+ .sa_flags = SA_RESTART,
+ };
if (sig == SIGKILL || sig == SIGSTOP)
continue;
- zero(sa);
- sa.sa_handler = SIG_DFL;
- sa.sa_flags = SA_RESTART;
-
/* On Linux the first two RT signals are reserved by
* glibc, and sigaction() will return EINVAL for them. */
if ((sigaction(sig, &sa, NULL) < 0))
@@ -1858,11 +1857,10 @@ int open_terminal(const char *name, int mode) {
}
int flush_fd(int fd) {
- struct pollfd pollfd;
-
- zero(pollfd);
- pollfd.fd = fd;
- pollfd.events = POLLIN;
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN,
+ };
for (;;) {
char buf[LINE_MAX];
@@ -1903,7 +1901,6 @@ int acquire_terminal(
int fd = -1, notify = -1, r = 0, wd = -1;
usec_t ts = 0;
- struct sigaction sa_old, sa_new;
assert(name);
@@ -1938,6 +1935,11 @@ int acquire_terminal(
}
for (;;) {
+ struct sigaction sa_old, sa_new = {
+ .sa_handler = SIG_IGN,
+ .sa_flags = SA_RESTART,
+ };
+
if (notify >= 0) {
r = flush_fd(notify);
if (r < 0)
@@ -1953,9 +1955,6 @@ int acquire_terminal(
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* if we already own the tty. */
- zero(sa_new);
- sa_new.sa_handler = SIG_IGN;
- sa_new.sa_flags = SA_RESTART;
assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
/* First, try to get the tty */
@@ -2063,7 +2062,10 @@ fail:
int release_terminal(void) {
int r = 0;
- struct sigaction sa_old, sa_new;
+ struct sigaction sa_old, sa_new = {
+ .sa_handler = SIG_IGN,
+ .sa_flags = SA_RESTART,
+ };
int _cleanup_close_ fd;
fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC);
@@ -2072,10 +2074,6 @@ int release_terminal(void) {
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* by our own TIOCNOTTY */
-
- zero(sa_new);
- sa_new.sa_handler = SIG_IGN;
- sa_new.sa_flags = SA_RESTART;
assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
if (ioctl(fd, TIOCNOTTY) < 0)
@@ -2100,13 +2098,13 @@ int sigaction_many(const struct sigaction *sa, ...) {
}
int ignore_signals(int sig, ...) {
- struct sigaction sa;
+ struct sigaction sa = {
+ .sa_handler = SIG_IGN,
+ .sa_flags = SA_RESTART,
+ };
va_list ap;
int r = 0;
- zero(sa);
- sa.sa_handler = SIG_IGN;
- sa.sa_flags = SA_RESTART;
if (sigaction(sig, &sa, NULL) < 0)
r = -errno;
@@ -2121,14 +2119,13 @@ int ignore_signals(int sig, ...) {
}
int default_signals(int sig, ...) {
- struct sigaction sa;
+ struct sigaction sa = {
+ .sa_handler = SIG_DFL,
+ .sa_flags = SA_RESTART,
+ };
va_list ap;
int r = 0;
- zero(sa);
- sa.sa_handler = SIG_DFL;
- sa.sa_flags = SA_RESTART;
-
if (sigaction(sig, &sa, NULL) < 0)
r = -errno;
@@ -2177,11 +2174,10 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
continue;
if (k < 0 && errno == EAGAIN && do_poll) {
- struct pollfd pollfd;
-
- zero(pollfd);
- pollfd.fd = fd;
- pollfd.events = POLLIN;
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN,
+ };
if (poll(&pollfd, 1, -1) < 0) {
if (errno == EINTR)
@@ -2226,11 +2222,10 @@ ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
continue;
if (k < 0 && errno == EAGAIN && do_poll) {
- struct pollfd pollfd;
-
- zero(pollfd);
- pollfd.fd = fd;
- pollfd.events = POLLOUT;
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLOUT,
+ };
if (poll(&pollfd, 1, -1) < 0) {
if (errno == EINTR)
@@ -2933,7 +2928,7 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char
static const char status_indent[] = " "; /* "[" STATUS "] " */
_cleanup_free_ char *s = NULL;
_cleanup_close_ int fd = -1;
- struct iovec iovec[6];
+ struct iovec iovec[6] = {};
int n = 0;
static bool prev_ephemeral;
@@ -2971,8 +2966,6 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char
}
}
- zero(iovec);
-
if (prev_ephemeral)
IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE);
prev_ephemeral = ephemeral;
@@ -3162,8 +3155,7 @@ char **replace_env_argv(char **argv, char **env) {
}
int fd_columns(int fd) {
- struct winsize ws;
- zero(ws);
+ struct winsize ws = {};
if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
return -errno;
@@ -3197,8 +3189,7 @@ unsigned columns(void) {
}
int fd_lines(int fd) {
- struct winsize ws;
- zero(ws);
+ struct winsize ws = {};
if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
return -errno;
@@ -3247,13 +3238,9 @@ bool on_tty(void) {
}
int running_in_chroot(void) {
- struct stat a, b;
-
- zero(a);
- zero(b);
+ struct stat a = {}, b = {};
/* Only works as root */
-
if (stat("/proc/1/root", &a) < 0)
return -errno;
@@ -3731,10 +3718,9 @@ void execute_directory(const char *directory, DIR *d, char *argv[]) {
while (!hashmap_isempty(pids)) {
pid_t pid = PTR_TO_UINT(hashmap_first_key(pids));
- siginfo_t si;
+ siginfo_t si = {};
char *path;
- zero(si);
if (waitid(P_PID, pid, &si, WEXITED) < 0) {
if (errno == EINTR)
@@ -3861,12 +3847,11 @@ char* hostname_cleanup(char *s) {
}
int pipe_eof(int fd) {
- struct pollfd pollfd;
int r;
-
- zero(pollfd);
- pollfd.fd = fd;
- pollfd.events = POLLIN|POLLHUP;
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN|POLLHUP,
+ };
r = poll(&pollfd, 1, 0);
if (r < 0)
@@ -3879,12 +3864,11 @@ int pipe_eof(int fd) {
}
int fd_wait_for_event(int fd, int event, usec_t t) {
- struct pollfd pollfd;
int r;
-
- zero(pollfd);
- pollfd.fd = fd;
- pollfd.events = event;
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = event,
+ };
r = poll(&pollfd, 1, t == (usec_t) -1 ? -1 : (int) (t / USEC_PER_MSEC));
if (r < 0)
@@ -4343,7 +4327,6 @@ int glob_exists(const char *path) {
assert(path);
- zero(g);
errno = 0;
k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c
index 3494b5690..5ee3d22e7 100644
--- a/src/shared/utmp-wtmp.c
+++ b/src/shared/utmp-wtmp.c
@@ -33,7 +33,7 @@
#include "utmp-wtmp.h"
int utmp_get_runlevel(int *runlevel, int *previous) {
- struct utmpx lookup, *found;
+ struct utmpx *found, lookup = { .ut_type = RUN_LVL };
int r;
const char *e;
@@ -66,9 +66,6 @@ int utmp_get_runlevel(int *runlevel, int *previous) {
setutxent();
- zero(lookup);
- lookup.ut_type = RUN_LVL;
-
if (!(found = getutxid(&lookup)))
r = -errno;
else {
@@ -102,14 +99,12 @@ static void init_timestamp(struct utmpx *store, usec_t t) {
}
static void init_entry(struct utmpx *store, usec_t t) {
- struct utsname uts;
+ struct utsname uts = {};
assert(store);
init_timestamp(store, t);
- zero(uts);
-
if (uname(&uts) >= 0)
strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
@@ -311,7 +306,10 @@ static int write_to_terminal(const char *tty, const char *message) {
while (left > 0) {
ssize_t n;
- struct pollfd pollfd;
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLOUT,
+ };
usec_t t;
int k;
@@ -320,10 +318,6 @@ static int write_to_terminal(const char *tty, const char *message) {
if (t >= end)
return -ETIME;
- zero(pollfd);
- pollfd.fd = fd;
- pollfd.events = POLLOUT;
-
k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
if (k < 0)
return -errno;
diff --git a/src/shared/virt.c b/src/shared/virt.c
index 78016eec8..fddb45d6e 100644
--- a/src/shared/virt.c
+++ b/src/shared/virt.c
@@ -62,7 +62,7 @@ int detect_vm(const char **id) {
union {
uint32_t sig32[3];
char text[13];
- } sig;
+ } sig = {};
unsigned i;
const char *j, *k;
bool hypervisor;
@@ -84,7 +84,6 @@ int detect_vm(const char **id) {
return r;
/* http://lwn.net/Articles/301888/ */
- zero(sig);
#if defined (__i386__)
#define REG_a "eax"