diff options
author | Lennart Poettering <lennart@poettering.net> | 2017-12-11 19:50:30 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2017-12-14 10:42:25 +0100 |
commit | 0d5366733428b657e1b5b7700b461e878e00b578 (patch) | |
tree | e2bcdb9f85e01c79221a9f4797e11fae60166c48 /src/login | |
parent | 966c04cf012f48686cf5359067a7b26c080f44ea (diff) | |
download | systemd-0d5366733428b657e1b5b7700b461e878e00b578.tar.gz |
tree-wide: use __fsetlocking() instead of fxyz_unlocked()
Let's replace usage of fputc_unlocked() and friends by __fsetlocking(f,
FSETLOCKING_BYCALLER). This turns off locking for the entire FILE*,
instead of doing individual per-call decision whether to use normal
calls or _unlocked() calls.
This has various benefits:
1. It's easier to read and easier not to forget
2. It's more comprehensive, as fprintf() and friends are covered too
(as these functions have no _unlocked() counterpart)
3. Philosophically, it's a bit more correct, because it's more a
property of the file handle really whether we ever pass it on to another
thread, not of the operations we then apply to it.
This patch reworks all pieces of codes that so far used fxyz_unlocked()
calls to use __fsetlocking() instead. It also reworks all places that
use open_memstream(), i.e. use stdio FILE* for string manipulations.
Note that this in some way a revert of 4b61c8751135c58be043d86b9fef4c8ec7aadf18.
Diffstat (limited to 'src/login')
-rw-r--r-- | src/login/logind-seat.c | 8 | ||||
-rw-r--r-- | src/login/logind-user.c | 42 |
2 files changed, 27 insertions, 23 deletions
diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c index f4aa9a6651..b99e7abf91 100644 --- a/src/login/logind-seat.c +++ b/src/login/logind-seat.c @@ -20,6 +20,7 @@ #include <errno.h> #include <fcntl.h> +#include <stdio_ext.h> #include <string.h> #include <unistd.h> @@ -102,7 +103,8 @@ int seat_save(Seat *s) { if (r < 0) goto fail; - fchmod(fileno(f), 0644); + (void) __fsetlocking(f, FSETLOCKING_BYCALLER); + (void) fchmod(fileno(f), 0644); fprintf(f, "# This is private data. Do not parse.\n" @@ -128,7 +130,7 @@ int seat_save(Seat *s) { if (s->sessions) { Session *i; - fputs_unlocked("SESSIONS=", f); + fputs("SESSIONS=", f); LIST_FOREACH(sessions_by_seat, i, s->sessions) { fprintf(f, "%s%c", @@ -136,7 +138,7 @@ int seat_save(Seat *s) { i->sessions_by_seat_next ? ' ' : '\n'); } - fputs_unlocked("UIDS=", f); + fputs("UIDS=", f); LIST_FOREACH(sessions_by_seat, i, s->sessions) fprintf(f, UID_FMT"%c", diff --git a/src/login/logind-user.c b/src/login/logind-user.c index 01469438b1..94e250b94a 100644 --- a/src/login/logind-user.c +++ b/src/login/logind-user.c @@ -22,6 +22,7 @@ #include <string.h> #include <sys/mount.h> #include <unistd.h> +#include <stdio_ext.h> #include "alloc-util.h" #include "bus-common-errors.h" @@ -150,7 +151,8 @@ static int user_save_internal(User *u) { if (r < 0) goto fail; - fchmod(fileno(f), 0644); + (void) __fsetlocking(f, FSETLOCKING_BYCALLER); + (void) fchmod(fileno(f), 0644); fprintf(f, "# This is private data. Do not parse.\n" @@ -183,18 +185,18 @@ static int user_save_internal(User *u) { Session *i; bool first; - fputs_unlocked("SESSIONS=", f); + fputs("SESSIONS=", f); first = true; LIST_FOREACH(sessions_by_user, i, u->sessions) { if (first) first = false; else - fputc_unlocked(' ', f); + fputc(' ', f); - fputs_unlocked(i->id, f); + fputs(i->id, f); } - fputs_unlocked("\nSEATS=", f); + fputs("\nSEATS=", f); first = true; LIST_FOREACH(sessions_by_user, i, u->sessions) { if (!i->seat) @@ -203,12 +205,12 @@ static int user_save_internal(User *u) { if (first) first = false; else - fputc_unlocked(' ', f); + fputc(' ', f); - fputs_unlocked(i->seat->id, f); + fputs(i->seat->id, f); } - fputs_unlocked("\nACTIVE_SESSIONS=", f); + fputs("\nACTIVE_SESSIONS=", f); first = true; LIST_FOREACH(sessions_by_user, i, u->sessions) { if (!session_is_active(i)) @@ -217,12 +219,12 @@ static int user_save_internal(User *u) { if (first) first = false; else - fputc_unlocked(' ', f); + fputc(' ', f); - fputs_unlocked(i->id, f); + fputs(i->id, f); } - fputs_unlocked("\nONLINE_SESSIONS=", f); + fputs("\nONLINE_SESSIONS=", f); first = true; LIST_FOREACH(sessions_by_user, i, u->sessions) { if (session_get_state(i) == SESSION_CLOSING) @@ -231,12 +233,12 @@ static int user_save_internal(User *u) { if (first) first = false; else - fputc_unlocked(' ', f); + fputc(' ', f); - fputs_unlocked(i->id, f); + fputs(i->id, f); } - fputs_unlocked("\nACTIVE_SEATS=", f); + fputs("\nACTIVE_SEATS=", f); first = true; LIST_FOREACH(sessions_by_user, i, u->sessions) { if (!session_is_active(i) || !i->seat) @@ -245,12 +247,12 @@ static int user_save_internal(User *u) { if (first) first = false; else - fputc_unlocked(' ', f); + fputc(' ', f); - fputs_unlocked(i->seat->id, f); + fputs(i->seat->id, f); } - fputs_unlocked("\nONLINE_SEATS=", f); + fputs("\nONLINE_SEATS=", f); first = true; LIST_FOREACH(sessions_by_user, i, u->sessions) { if (session_get_state(i) == SESSION_CLOSING || !i->seat) @@ -259,11 +261,11 @@ static int user_save_internal(User *u) { if (first) first = false; else - fputc_unlocked(' ', f); + fputc(' ', f); - fputs_unlocked(i->seat->id, f); + fputs(i->seat->id, f); } - fputc_unlocked('\n', f); + fputc('\n', f); } r = fflush_and_check(f); |