summaryrefslogtreecommitdiff
path: root/src/login
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/login
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/login')
-rw-r--r--src/login/loginctl.c10
-rw-r--r--src/login/logind-inhibit.c3
-rw-r--r--src/login/logind-session.c3
-rw-r--r--src/login/logind.c38
-rw-r--r--src/login/pam-module.c28
5 files changed, 31 insertions, 51 deletions
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
index 6c229c64e3..36c65bc8d0 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -834,17 +834,13 @@ static int show_one(const char *verb, DBusConnection *bus, const char *path, boo
const char *interface = "";
int r;
DBusMessageIter iter, sub, sub2, sub3;
- SessionStatusInfo session_info;
- UserStatusInfo user_info;
- SeatStatusInfo seat_info;
+ SessionStatusInfo session_info = {};
+ UserStatusInfo user_info = {};
+ SeatStatusInfo seat_info = {};
assert(path);
assert(new_line);
- zero(session_info);
- zero(user_info);
- zero(seat_info);
-
r = bus_method_call_with_reply(
bus,
"org.freedesktop.login1",
diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c
index c43ae23acf..e77088364a 100644
--- a/src/login/logind-inhibit.c
+++ b/src/login/logind-inhibit.c
@@ -292,7 +292,7 @@ int inhibitor_create_fifo(Inhibitor *i) {
/* Open reading side */
if (i->fifo_fd < 0) {
- struct epoll_event ev;
+ struct epoll_event ev = {};
i->fifo_fd = open(i->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
if (i->fifo_fd < 0)
@@ -302,7 +302,6 @@ int inhibitor_create_fifo(Inhibitor *i) {
if (r < 0)
return r;
- zero(ev);
ev.events = 0;
ev.data.u32 = FD_OTHER_BASE + i->fifo_fd;
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
index 71c79127e9..97c24d094b 100644
--- a/src/login/logind-session.c
+++ b/src/login/logind-session.c
@@ -898,7 +898,7 @@ int session_create_fifo(Session *s) {
/* Open reading side */
if (s->fifo_fd < 0) {
- struct epoll_event ev;
+ struct epoll_event ev = {};
s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
if (s->fifo_fd < 0)
@@ -908,7 +908,6 @@ int session_create_fifo(Session *s) {
if (r < 0)
return r;
- zero(ev);
ev.events = 0;
ev.data.u32 = FD_OTHER_BASE + s->fifo_fd;
diff --git a/src/login/logind.c b/src/login/logind.c
index f72aac5e61..c9e492fc4f 100644
--- a/src/login/logind.c
+++ b/src/login/logind.c
@@ -1174,7 +1174,10 @@ static void manager_dispatch_other(Manager *m, int fd) {
static int manager_connect_bus(Manager *m) {
DBusError error;
int r;
- struct epoll_event ev;
+ struct epoll_event ev = {
+ .events = EPOLLIN,
+ .data.u32 = FD_BUS,
+ };
assert(m);
assert(!m->bus);
@@ -1230,10 +1233,6 @@ static int manager_connect_bus(Manager *m) {
goto fail;
}
- zero(ev);
- ev.events = EPOLLIN;
- ev.data.u32 = FD_BUS;
-
if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->bus_fd, &ev) < 0)
goto fail;
@@ -1246,7 +1245,10 @@ fail:
}
static int manager_connect_console(Manager *m) {
- struct epoll_event ev;
+ struct epoll_event ev = {
+ .events = 0,
+ .data.u32 = FD_CONSOLE,
+ };
assert(m);
assert(m->console_active_fd < 0);
@@ -1271,10 +1273,6 @@ static int manager_connect_console(Manager *m) {
return -errno;
}
- zero(ev);
- ev.events = 0;
- ev.data.u32 = FD_CONSOLE;
-
if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->console_active_fd, &ev) < 0)
return -errno;
@@ -1282,8 +1280,11 @@ static int manager_connect_console(Manager *m) {
}
static int manager_connect_udev(Manager *m) {
- struct epoll_event ev;
int r;
+ struct epoll_event ev = {
+ .events = EPOLLIN,
+ .data.u32 = FD_SEAT_UDEV,
+ };
assert(m);
assert(!m->udev_seat_monitor);
@@ -1304,9 +1305,6 @@ static int manager_connect_udev(Manager *m) {
m->udev_seat_fd = udev_monitor_get_fd(m->udev_seat_monitor);
- zero(ev);
- ev.events = EPOLLIN;
- ev.data.u32 = FD_SEAT_UDEV;
if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_seat_fd, &ev) < 0)
return -errno;
@@ -1447,7 +1445,7 @@ int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
int manager_dispatch_idle_action(Manager *m) {
struct dual_timestamp since;
- struct itimerspec its;
+ struct itimerspec its = {};
int r;
usec_t n;
@@ -1459,7 +1457,6 @@ int manager_dispatch_idle_action(Manager *m) {
goto finish;
}
- zero(its);
n = now(CLOCK_MONOTONIC);
r = manager_get_idle_hint(m, &since);
@@ -1482,7 +1479,10 @@ int manager_dispatch_idle_action(Manager *m) {
}
if (m->idle_action_fd < 0) {
- struct epoll_event ev;
+ struct epoll_event ev = {
+ .events = EPOLLIN,
+ .data.u32 = FD_IDLE_ACTION,
+ };
m->idle_action_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
if (m->idle_action_fd < 0) {
@@ -1491,10 +1491,6 @@ int manager_dispatch_idle_action(Manager *m) {
goto finish;
}
- zero(ev);
- ev.events = EPOLLIN;
- ev.data.u32 = FD_IDLE_ACTION;
-
if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->idle_action_fd, &ev) < 0) {
log_error("Failed to add idle action timer to epoll: %m");
r = -errno;
diff --git a/src/login/pam-module.c b/src/login/pam-module.c
index 0d4f7991e7..c8f4dae77e 100644
--- a/src/login/pam-module.c
+++ b/src/login/pam-module.c
@@ -256,13 +256,15 @@ static bool check_user_lists(
}
static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
- char *p = NULL;
+ char _cleanup_free_ *p = NULL;
int r;
- int fd;
- union sockaddr_union sa;
+ int _cleanup_close_ fd = -1;
+ union sockaddr_union sa = {
+ .un.sun_family = AF_UNIX,
+ };
struct ucred ucred;
socklen_t l;
- char *tty;
+ char _cleanup_free_ *tty = NULL;
int v;
assert(display);
@@ -277,27 +279,17 @@ static int get_seat_from_display(const char *display, const char **seat, uint32_
r = socket_from_display(display, &p);
if (r < 0)
return r;
+ strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
- if (fd < 0) {
- free(p);
+ if (fd < 0)
return -errno;
- }
-
- zero(sa);
- sa.un.sun_family = AF_UNIX;
- strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
- free(p);
- if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
- close_nointr_nofail(fd);
+ if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0)
return -errno;
- }
l = sizeof(ucred);
r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l);
- close_nointr_nofail(fd);
-
if (r < 0)
return -errno;
@@ -306,8 +298,6 @@ static int get_seat_from_display(const char *display, const char **seat, uint32_
return r;
v = vtnr_from_tty(tty);
- free(tty);
-
if (v < 0)
return v;
else if (v == 0)