From d2b39cb6062d446b2542359cb31beff9eecff70c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:08:13 +0200 Subject: [PATCH 01/24] cgroup-util: FOREACH_LINE() excorcism --- src/basic/cgroup-util.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index b725a17761..e010fc7b5a 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -985,10 +985,9 @@ int cg_get_xattr(const char *controller, const char *path, const char *name, voi int cg_pid_get_path(const char *controller, pid_t pid, char **path) { _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; const char *fs, *controller_str; + int unified, r; size_t cs = 0; - int unified; assert(path); assert(pid >= 0); @@ -1018,10 +1017,15 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) { (void) __fsetlocking(f, FSETLOCKING_BYCALLER); - FOREACH_LINE(line, f, return -errno) { + for (;;) { + _cleanup_free_ char *line = NULL; char *e, *p; - truncate_nl(line); + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; if (unified) { e = startswith(line, "0:"); From f5af75eaec8175b52feb0deff30706a90c67a93c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:08:30 +0200 Subject: [PATCH 02/24] mount-util: FOREACH_LINE() excorcism --- src/basic/mount-util.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/basic/mount-util.c b/src/basic/mount-util.c index 38edef9823..358c8a9143 100644 --- a/src/basic/mount-util.c +++ b/src/basic/mount-util.c @@ -13,6 +13,7 @@ #include #include "alloc-util.h" +#include "def.h" #include "escape.h" #include "extract-word.h" #include "fd-util.h" @@ -954,8 +955,8 @@ int mount_option_mangle( int dev_is_devtmpfs(void) { _cleanup_fclose_ FILE *proc_self_mountinfo = NULL; - char line[LINE_MAX], *e; int mount_id, r; + char *e; r = path_get_mnt_id("/dev", &mount_id); if (r < 0) @@ -967,9 +968,16 @@ int dev_is_devtmpfs(void) { (void) __fsetlocking(proc_self_mountinfo, FSETLOCKING_BYCALLER); - FOREACH_LINE(line, proc_self_mountinfo, return -errno) { + for (;;) { + _cleanup_free_ char *line = NULL; int mid; + r = read_line(proc_self_mountinfo, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; + if (sscanf(line, "%i", &mid) != 1) continue; From 7e7a16a00e6702b04d424371a6a8b76050791f05 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:08:51 +0200 Subject: [PATCH 03/24] process-util: FOREACH_LINE excorcism --- src/basic/process-util.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index a02ceb79e5..b2aab853e2 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -25,6 +25,7 @@ #include "alloc-util.h" #include "architecture.h" +#include "def.h" #include "escape.h" #include "fd-util.h" #include "fileio.h" @@ -514,8 +515,8 @@ int get_process_exe(pid_t pid, char **name) { static int get_process_id(pid_t pid, const char *field, uid_t *uid) { _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; const char *p; + int r; assert(field); assert(uid); @@ -533,9 +534,16 @@ static int get_process_id(pid_t pid, const char *field, uid_t *uid) { (void) __fsetlocking(f, FSETLOCKING_BYCALLER); - FOREACH_LINE(line, f, return -errno) { + for (;;) { + _cleanup_free_ char *line = NULL; char *l; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; + l = strstrip(line); if (startswith(l, field)) { From 8d2b9d14c4d0db9d639987535efb3e7f671ce734 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:09:19 +0200 Subject: [PATCH 04/24] time-util: FOREACH_LINE excorcism --- src/basic/time-util.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 9ac739b42a..158e4aaf93 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -14,6 +14,7 @@ #include #include "alloc-util.h" +#include "def.h" #include "fd-util.h" #include "fileio.h" #include "fs-util.h" @@ -1222,6 +1223,7 @@ int get_timezones(char ***ret) { _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **zones = NULL; size_t n_zones = 0, n_allocated = 0; + int r; assert(ret); @@ -1234,13 +1236,18 @@ int get_timezones(char ***ret) { f = fopen("/usr/share/zoneinfo/zone.tab", "re"); if (f) { - char l[LINE_MAX]; - - FOREACH_LINE(l, f, return -errno) { + for (;;) { + _cleanup_free_ char *line = NULL; char *p, *w; size_t k; - p = strstrip(l); + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; + + p = strstrip(line); if (isempty(p) || *p == '#') continue; From c66e60a838a52eebdff2597c83d8edadbded27bf Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:09:53 +0200 Subject: [PATCH 05/24] cgroup: FOREACH_LINE excorcism --- src/core/cgroup.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/core/cgroup.c b/src/core/cgroup.c index c89f57a67f..1e5b564e17 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -441,7 +441,6 @@ static int whitelist_device(BPFProgram *prog, const char *path, const char *node static int whitelist_major(BPFProgram *prog, const char *path, const char *name, char type, const char *acc) { _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; char *p, *w; bool good = false; int r; @@ -454,10 +453,15 @@ static int whitelist_major(BPFProgram *prog, const char *path, const char *name, if (!f) return log_warning_errno(errno, "Cannot open /proc/devices to resolve %s (%c): %m", name, type); - FOREACH_LINE(line, f, goto fail) { + for (;;) { + _cleanup_free_ char *line = NULL; unsigned maj; - truncate_nl(line); + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return log_warning_errno(r, "Failed to read /proc/devices: %m"); + if (r == 0) + break; if (type == 'c' && streq(line, "Character devices:")) { good = true; @@ -520,9 +524,6 @@ static int whitelist_major(BPFProgram *prog, const char *path, const char *name, } return 0; - -fail: - return log_warning_errno(errno, "Failed to read /proc/devices: %m"); } static bool cgroup_context_has_cpu_weight(CGroupContext *c) { From 2452419b3ac4926c27861b694439f70964d6f5c6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:10:37 +0200 Subject: [PATCH 06/24] ima-setup: FOREACH_LINE excorcism --- src/core/ima-setup.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/core/ima-setup.c b/src/core/ima-setup.c index 013d6c5de3..0f9805acb3 100644 --- a/src/core/ima-setup.c +++ b/src/core/ima-setup.c @@ -7,6 +7,8 @@ #include #include +#include "alloc-util.h" +#include "def.h" #include "fd-util.h" #include "fileio.h" #include "ima-setup.h" @@ -22,7 +24,7 @@ int ima_setup(void) { _cleanup_fclose_ FILE *input = NULL; _cleanup_close_ int imafd = -1; unsigned lineno = 0; - char line[page_size()]; + int r; if (access(IMA_SECFS_DIR, F_OK) < 0) { log_debug("IMA support is disabled in the kernel, ignoring."); @@ -64,10 +66,16 @@ int ima_setup(void) { return 0; } - FOREACH_LINE(line, input, - return log_error_errno(errno, "Failed to read the IMA custom policy file "IMA_POLICY_PATH": %m")) { + for (;;) { + _cleanup_free_ char *line = NULL; size_t len; + r = read_line(input, LONG_LINE_MAX, &line); + if (r < 0) + return log_error_errno(r, "Failed to read the IMA custom policy file "IMA_POLICY_PATH": %m"); + if (r == 0) + break; + len = strlen(line); lineno++; From b435812c642b11ae3650f08bcbc546d459733196 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:11:54 +0200 Subject: [PATCH 07/24] ima-setup: include error cause in failure messages --- src/core/ima-setup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/ima-setup.c b/src/core/ima-setup.c index 0f9805acb3..d6a49ce100 100644 --- a/src/core/ima-setup.c +++ b/src/core/ima-setup.c @@ -27,17 +27,17 @@ int ima_setup(void) { int r; if (access(IMA_SECFS_DIR, F_OK) < 0) { - log_debug("IMA support is disabled in the kernel, ignoring."); + log_debug_errno(errno, "IMA support is disabled in the kernel, ignoring: %m"); return 0; } if (access(IMA_SECFS_POLICY, W_OK) < 0) { - log_warning("Another IMA custom policy has already been loaded, ignoring."); + log_warning_errno(errno, "Another IMA custom policy has already been loaded, ignoring: %m"); return 0; } if (access(IMA_POLICY_PATH, F_OK) < 0) { - log_debug("No IMA custom policy file "IMA_POLICY_PATH", ignoring."); + log_debug_errno(errno, "No IMA custom policy file "IMA_POLICY_PATH", ignoring: %m"); return 0; } From 1fb89422842ad2810ff361233a3462c3fda6f3c4 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:12:06 +0200 Subject: [PATCH 08/24] ima-setup: prefer safe_close() over plain close() --- src/core/ima-setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/ima-setup.c b/src/core/ima-setup.c index d6a49ce100..d9e5cf4f05 100644 --- a/src/core/ima-setup.c +++ b/src/core/ima-setup.c @@ -58,7 +58,7 @@ int ima_setup(void) { return 0; } - close(imafd); + safe_close(imafd); imafd = open(IMA_SECFS_POLICY, O_WRONLY|O_CLOEXEC); if (imafd < 0) { From a34f0dae01817d34f77d4494191f8f5483618c2f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:12:40 +0200 Subject: [PATCH 09/24] shutdown: FOREACH_LINE() excorcism --- src/core/shutdown.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/core/shutdown.c b/src/core/shutdown.c index 5bf332d9a9..62d953659c 100644 --- a/src/core/shutdown.c +++ b/src/core/shutdown.c @@ -171,16 +171,23 @@ static int switch_root_initramfs(void) { */ static bool sync_making_progress(unsigned long long *prev_dirty) { _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; - bool r = false; unsigned long long val = 0; + bool r = false; f = fopen("/proc/meminfo", "re"); if (!f) return log_warning_errno(errno, "Failed to open /proc/meminfo: %m"); - FOREACH_LINE(line, f, log_warning_errno(errno, "Failed to parse /proc/meminfo: %m")) { + for (;;) { + _cleanup_free_ char *line = NULL; unsigned long long ull = 0; + int q; + + q = read_line(f, LONG_LINE_MAX, &line); + if (q < 0) + return log_warning_errno(q, "Failed to parse /proc/meminfo: %m"); + if (q == 0) + break; if (!first_word(line, "NFS_Unstable:") && !first_word(line, "Writeback:") && !first_word(line, "Dirty:")) continue; From ea8b6526b91ca1bc948fd53db3a8b4ad6ee17285 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:14:12 +0200 Subject: [PATCH 10/24] smcak-setup: FOREACH_LINE excorcism --- src/core/smack-setup.c | 55 ++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c index 50115c0454..216176145c 100644 --- a/src/core/smack-setup.c +++ b/src/core/smack-setup.c @@ -14,6 +14,7 @@ #include #include "alloc-util.h" +#include "def.h" #include "dirent-util.h" #include "fd-util.h" #include "fileio.h" @@ -29,7 +30,6 @@ static int write_access2_rules(const char* srcdir) { _cleanup_close_ int load2_fd = -1, change_fd = -1; _cleanup_closedir_ DIR *dir = NULL; struct dirent *entry; - char buf[NAME_MAX]; int dfd = -1; int r = 0; @@ -83,13 +83,17 @@ static int write_access2_rules(const char* srcdir) { } /* load2 write rules in the kernel require a line buffered stream */ - FOREACH_LINE(buf, policy, - log_error_errno(errno, "Failed to read line from '%s': %m", - entry->d_name)) { + for (;;) { + _cleanup_free_ char *buf = NULL, *sbj = NULL, *obj = NULL, *acc1 = NULL, *acc2 = NULL; + int q; - _cleanup_free_ char *sbj = NULL, *obj = NULL, *acc1 = NULL, *acc2 = NULL; + q = read_line(policy, NAME_MAX, &buf); + if (q < 0) + return log_error_errno(q, "Failed to read line from '%s': %m", entry->d_name); + if (q == 0) + break; - if (isempty(truncate_nl(buf)) || strchr(COMMENTS, *buf)) + if (isempty(buf) || strchr(COMMENTS, buf[0])) continue; /* if 3 args -> load rule : subject object access1 */ @@ -115,7 +119,6 @@ static int write_cipso2_rules(const char* srcdir) { _cleanup_close_ int cipso2_fd = -1; _cleanup_closedir_ DIR *dir = NULL; struct dirent *entry; - char buf[NAME_MAX]; int dfd = -1; int r = 0; @@ -162,11 +165,17 @@ static int write_cipso2_rules(const char* srcdir) { } /* cipso2 write rules in the kernel require a line buffered stream */ - FOREACH_LINE(buf, policy, - log_error_errno(errno, "Failed to read line from '%s': %m", - entry->d_name)) { + for (;;) { + _cleanup_free_ char *buf = NULL; + int q; - if (isempty(truncate_nl(buf)) || strchr(COMMENTS, *buf)) + q = read_line(policy, NAME_MAX, &buf); + if (q < 0) + return log_error_errno(q, "Failed to read line from '%s': %m", entry->d_name); + if (q == 0) + break; + + if (isempty(buf) || strchr(COMMENTS, buf[0])) continue; if (write(cipso2_fd, buf, strlen(buf)) < 0) { @@ -186,7 +195,6 @@ static int write_netlabel_rules(const char* srcdir) { _cleanup_fclose_ FILE *dst = NULL; _cleanup_closedir_ DIR *dir = NULL; struct dirent *entry; - char buf[NAME_MAX]; int dfd = -1; int r = 0; @@ -232,11 +240,16 @@ static int write_netlabel_rules(const char* srcdir) { (void) __fsetlocking(policy, FSETLOCKING_BYCALLER); /* load2 write rules in the kernel require a line buffered stream */ - FOREACH_LINE(buf, policy, - log_error_errno(errno, "Failed to read line from %s: %m", entry->d_name)) { - + for (;;) { + _cleanup_free_ char *buf = NULL; int q; + q = read_line(policy, NAME_MAX, &buf); + if (q < 0) + return log_error_errno(q, "Failed to read line from %s: %m", entry->d_name); + if (q == 0) + break; + if (!fputs(buf, dst)) { if (r == 0) r = -EINVAL; @@ -261,7 +274,6 @@ static int write_onlycap_list(void) { _cleanup_free_ char *list = NULL; _cleanup_fclose_ FILE *f = NULL; size_t len = 0, allocated = 0; - char buf[LINE_MAX]; int r; f = fopen("/etc/smack/onlycap", "re"); @@ -271,10 +283,17 @@ static int write_onlycap_list(void) { return errno == ENOENT ? ENOENT : -errno; } - FOREACH_LINE(buf, f, return -errno) { + for (;;) { + _cleanup_free_ char *buf = NULL; size_t l; - if (isempty(truncate_nl(buf)) || strchr(COMMENTS, *buf)) + r = read_line(f, LONG_LINE_MAX, &buf); + if (r < 0) + return log_error_errno(r, "Failed to read line from /etc/smack/onlycap: %m"); + if (r == 0) + break; + + if (isempty(buf) || strchr(COMMENTS, *buf)) continue; l = strlen(buf); From 9fd0b02913685fa387967c47185e0ce3696646cc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:14:25 +0200 Subject: [PATCH 11/24] smack-setup: include error cause in log message --- src/core/smack-setup.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c index 216176145c..4aa839ea22 100644 --- a/src/core/smack-setup.c +++ b/src/core/smack-setup.c @@ -279,7 +279,8 @@ static int write_onlycap_list(void) { f = fopen("/etc/smack/onlycap", "re"); if (!f) { if (errno != ENOENT) - log_warning_errno(errno, "Failed to read '/etc/smack/onlycap'"); + log_warning_errno(errno, "Failed to read '/etc/smack/onlycap': %m"); + return errno == ENOENT ? ENOENT : -errno; } @@ -304,7 +305,7 @@ static int write_onlycap_list(void) { len += l + 1; } - if (!len) + if (len == 0) return 0; list[len - 1] = 0; @@ -312,13 +313,13 @@ static int write_onlycap_list(void) { onlycap_fd = open("/sys/fs/smackfs/onlycap", O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); if (onlycap_fd < 0) { if (errno != ENOENT) - log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/onlycap'"); + log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/onlycap': %m"); return -errno; /* negative error */ } r = write(onlycap_fd, list, len); if (r < 0) - return log_error_errno(errno, "Failed to write onlycap list(%s) to '/sys/fs/smackfs/onlycap'", list); + return log_error_errno(errno, "Failed to write onlycap list(%s) to '/sys/fs/smackfs/onlycap': %m", list); return 0; } From 7b26ea6f06c27edd6371a52f86872e21182244cf Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:14:42 +0200 Subject: [PATCH 12/24] coredump: FOREACH_LINE excorcism --- src/coredump/coredump.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 31274ee602..d3fc059fbb 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -511,7 +511,7 @@ static int compose_open_fds(pid_t pid, char **open_fds) { const char *fddelim = "", *path; struct dirent *dent = NULL; size_t size = 0; - int r = 0; + int r; assert(pid >= 0); assert(open_fds != NULL); @@ -534,7 +534,6 @@ static int compose_open_fds(pid_t pid, char **open_fds) { FOREACH_DIRENT(dent, proc_fd_dir, return -errno) { _cleanup_fclose_ FILE *fdinfo = NULL; _cleanup_free_ char *fdname = NULL; - char line[LINE_MAX]; int fd; r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname); @@ -555,10 +554,17 @@ static int compose_open_fds(pid_t pid, char **open_fds) { continue; } - FOREACH_LINE(line, fdinfo, break) { + for (;;) { + _cleanup_free_ char *line = NULL; + + r = read_line(fdinfo, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; + fputs(line, stream); - if (!endswith(line, "\n")) - fputc('\n', stream); + fputc('\n', stream); } } From 8ae29228a8bae50867d3729c1183b0734176c7d3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:14:59 +0200 Subject: [PATCH 13/24] bus-creds: FOREACH_LINE excorcism --- src/libsystemd/sd-bus/bus-creds.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libsystemd/sd-bus/bus-creds.c b/src/libsystemd/sd-bus/bus-creds.c index a6dda16876..81d97ff968 100644 --- a/src/libsystemd/sd-bus/bus-creds.c +++ b/src/libsystemd/sd-bus/bus-creds.c @@ -803,10 +803,15 @@ int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) { else if (!IN_SET(errno, EPERM, EACCES)) return -errno; } else { - char line[LINE_MAX]; - FOREACH_LINE(line, f, return -errno) { - truncate_nl(line); + for (;;) { + _cleanup_free_ char *line = NULL; + + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; if (missing & SD_BUS_CREDS_PPID) { p = startswith(line, "PPid:"); From 710bf2aeb2821df42a6e3f51aa7fa55ab07378f3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:15:18 +0200 Subject: [PATCH 14/24] sd-path: FOREACH_LINE exorcism --- src/libsystemd/sd-path/sd-path.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/libsystemd/sd-path/sd-path.c b/src/libsystemd/sd-path/sd-path.c index ec6e597ec0..d590c90f2e 100644 --- a/src/libsystemd/sd-path/sd-path.c +++ b/src/libsystemd/sd-path/sd-path.c @@ -4,6 +4,7 @@ #include "alloc-util.h" #include "architecture.h" +#include "def.h" #include "fd-util.h" #include "fileio.h" #include "fs-util.h" @@ -75,7 +76,6 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) { _cleanup_free_ char *b = NULL; _cleanup_free_ const char *fn = NULL; const char *c = NULL; - char line[LINE_MAX]; size_t n; int r; @@ -103,9 +103,16 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) { * xdg-user-dirs does upstream */ n = strlen(field); - FOREACH_LINE(line, f, return -errno) { + for (;;) { + _cleanup_free_ char *line = NULL; char *l, *p, *e; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; + l = strstrip(line); if (!strneq(l, field, n)) From 271c8ec50f48fae5523f8355b5233fa1db5c6a42 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:15:35 +0200 Subject: [PATCH 15/24] localectl: FOREACH_LINE excorcism --- src/locale/localectl.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/locale/localectl.c b/src/locale/localectl.c index 88fd077e09..e2a9415138 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -280,7 +280,6 @@ static int set_x11_keymap(int argc, char **argv, void *userdata) { static int list_x11_keymaps(int argc, char **argv, void *userdata) { _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **list = NULL; - char line[LINE_MAX]; enum { NONE, MODELS, @@ -305,9 +304,16 @@ static int list_x11_keymaps(int argc, char **argv, void *userdata) { else assert_not_reached("Wrong parameter"); - FOREACH_LINE(line, f, break) { + for (;;) { + _cleanup_free_ char *line = NULL; char *l, *w; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return log_error_errno(r, "Failed to read keyboard mapping list: %m"); + if (r == 0) + break; + l = strstrip(line); if (isempty(l)) From 0a6488b441251e14ea23bf9564da5bc9fce7c6c6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:16:06 +0200 Subject: [PATCH 16/24] resolved-dns-trust-anchor: FOREACH_LINE excorcism Also, properly ignore these read errors, and say so. --- src/resolve/resolved-dns-trust-anchor.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/resolve/resolved-dns-trust-anchor.c b/src/resolve/resolved-dns-trust-anchor.c index dc6b746189..c5ec93b724 100644 --- a/src/resolve/resolved-dns-trust-anchor.c +++ b/src/resolve/resolved-dns-trust-anchor.c @@ -433,7 +433,6 @@ static int dns_trust_anchor_load_files( STRV_FOREACH(f, files) { _cleanup_fclose_ FILE *g = NULL; - char line[LINE_MAX]; unsigned n = 0; g = fopen(*f, "r"); @@ -441,13 +440,22 @@ static int dns_trust_anchor_load_files( if (errno == ENOENT) continue; - log_warning_errno(errno, "Failed to open %s: %m", *f); + log_warning_errno(errno, "Failed to open '%s', ignoring: %m", *f); continue; } - FOREACH_LINE(line, g, log_warning_errno(errno, "Failed to read %s, ignoring: %m", *f)) { + for (;;) { + _cleanup_free_ char *line = NULL; char *l; + r = read_line(g, LONG_LINE_MAX, &line); + if (r < 0) { + log_warning_errno(r, "Failed to read '%s', ignoring: %m", *f); + break; + } + if (r == 0) + break; + n++; l = strstrip(line); From 7c777a7773790375ce648d2a45f97fb2ce3cbd8b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:16:52 +0200 Subject: [PATCH 17/24] resolved-etc-hosts: FOREACH_LINE excorcism --- src/resolve/resolved-etc-hosts.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/resolve/resolved-etc-hosts.c b/src/resolve/resolved-etc-hosts.c index 5be62cc3a8..8074612fb4 100644 --- a/src/resolve/resolved-etc-hosts.c +++ b/src/resolve/resolved-etc-hosts.c @@ -1,10 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include "def.h" #include "fd-util.h" #include "fileio.h" #include "hostname-util.h" -#include "resolved-etc-hosts.h" #include "resolved-dns-synthesize.h" +#include "resolved-etc-hosts.h" #include "string-util.h" #include "strv.h" #include "time-util.h" @@ -161,13 +162,19 @@ static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) { int etc_hosts_parse(EtcHosts *hosts, FILE *f) { _cleanup_(etc_hosts_free) EtcHosts t = {}; - char line[LINE_MAX]; unsigned nr = 0; int r; - FOREACH_LINE(line, f, return log_error_errno(errno, "Failed to read /etc/hosts: %m")) { + for (;;) { + _cleanup_free_ char *line = NULL; char *l; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return log_error_errno(r, "Failed to read /etc/hosts: %m"); + if (r == 0) + break; + nr++; l = strstrip(line); From e1b9fc23eb0fb2f67cd9e4a1209db0da2eaa0c9f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:17:23 +0200 Subject: [PATCH 18/24] resolved-resolv-conf: FOREACH_LINE excorcism --- src/resolve/resolved-resolv-conf.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/resolve/resolved-resolv-conf.c b/src/resolve/resolved-resolv-conf.c index 1e19586b92..6b74daf776 100644 --- a/src/resolve/resolved-resolv-conf.c +++ b/src/resolve/resolved-resolv-conf.c @@ -4,6 +4,7 @@ #include #include "alloc-util.h" +#include "def.h" #include "dns-domain.h" #include "fd-util.h" #include "fileio-label.h" @@ -89,7 +90,6 @@ static bool file_is_our_own(const struct stat *st) { int manager_read_resolv_conf(Manager *m) { _cleanup_fclose_ FILE *f = NULL; struct stat st; - char line[LINE_MAX]; unsigned n = 0; int r; @@ -137,10 +137,19 @@ int manager_read_resolv_conf(Manager *m) { dns_server_mark_all(m->dns_servers); dns_search_domain_mark_all(m->search_domains); - FOREACH_LINE(line, f, r = -errno; goto clear) { + for (;;) { + _cleanup_free_ char *line = NULL; const char *a; char *l; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) { + log_error_errno(r, "Failed to read /etc/resolv.conf: %m"); + goto clear; + } + if (r == 0) + break; + n++; l = strstrip(line); From d5b3c07da6997cde496cfb09ae47693ed13e79e2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:18:01 +0200 Subject: [PATCH 19/24] clean-ipc: FOREACH_LINE excorcism --- src/shared/clean-ipc.c | 52 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/shared/clean-ipc.c b/src/shared/clean-ipc.c index 08dc7b9e43..f9b05ff924 100644 --- a/src/shared/clean-ipc.c +++ b/src/shared/clean-ipc.c @@ -16,6 +16,7 @@ #include #include "clean-ipc.h" +#include "def.h" #include "dirent-util.h" #include "fd-util.h" #include "fileio.h" @@ -39,9 +40,8 @@ static bool match_uid_gid(uid_t subject_uid, gid_t subject_gid, uid_t delete_uid static int clean_sysvipc_shm(uid_t delete_uid, gid_t delete_gid, bool rm) { _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; bool first = true; - int ret = 0; + int ret = 0, r; f = fopen("/proc/sysvipc/shm", "re"); if (!f) { @@ -51,20 +51,25 @@ static int clean_sysvipc_shm(uid_t delete_uid, gid_t delete_gid, bool rm) { return log_warning_errno(errno, "Failed to open /proc/sysvipc/shm: %m"); } - FOREACH_LINE(line, f, goto fail) { + for (;;) { + _cleanup_free_ char *line = NULL; unsigned n_attached; pid_t cpid, lpid; uid_t uid, cuid; gid_t gid, cgid; int shmid; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return log_warning_errno(errno, "Failed to read /proc/sysvipc/shm: %m"); + if (r == 0) + break; + if (first) { first = false; continue; } - truncate_nl(line); - if (sscanf(line, "%*i %i %*o %*u " PID_FMT " " PID_FMT " %u " UID_FMT " " GID_FMT " " UID_FMT " " GID_FMT, &shmid, &cpid, &lpid, &n_attached, &uid, &gid, &cuid, &cgid) != 8) continue; @@ -95,16 +100,12 @@ static int clean_sysvipc_shm(uid_t delete_uid, gid_t delete_gid, bool rm) { } return ret; - -fail: - return log_warning_errno(errno, "Failed to read /proc/sysvipc/shm: %m"); } static int clean_sysvipc_sem(uid_t delete_uid, gid_t delete_gid, bool rm) { _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; bool first = true; - int ret = 0; + int ret = 0, r; f = fopen("/proc/sysvipc/sem", "re"); if (!f) { @@ -114,18 +115,23 @@ static int clean_sysvipc_sem(uid_t delete_uid, gid_t delete_gid, bool rm) { return log_warning_errno(errno, "Failed to open /proc/sysvipc/sem: %m"); } - FOREACH_LINE(line, f, goto fail) { + for (;;) { + _cleanup_free_ char *line = NULL; uid_t uid, cuid; gid_t gid, cgid; int semid; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return log_warning_errno(r, "Failed to read /proc/sysvipc/sem: %m"); + if (r == 0) + break; + if (first) { first = false; continue; } - truncate_nl(line); - if (sscanf(line, "%*i %i %*o %*u " UID_FMT " " GID_FMT " " UID_FMT " " GID_FMT, &semid, &uid, &gid, &cuid, &cgid) != 5) continue; @@ -153,16 +159,12 @@ static int clean_sysvipc_sem(uid_t delete_uid, gid_t delete_gid, bool rm) { } return ret; - -fail: - return log_warning_errno(errno, "Failed to read /proc/sysvipc/sem: %m"); } static int clean_sysvipc_msg(uid_t delete_uid, gid_t delete_gid, bool rm) { _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; bool first = true; - int ret = 0; + int ret = 0, r; f = fopen("/proc/sysvipc/msg", "re"); if (!f) { @@ -172,19 +174,24 @@ static int clean_sysvipc_msg(uid_t delete_uid, gid_t delete_gid, bool rm) { return log_warning_errno(errno, "Failed to open /proc/sysvipc/msg: %m"); } - FOREACH_LINE(line, f, goto fail) { + for (;;) { + _cleanup_free_ char *line = NULL; uid_t uid, cuid; gid_t gid, cgid; pid_t cpid, lpid; int msgid; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return log_warning_errno(r, "Failed to read /proc/sysvipc/msg: %m"); + if (r == 0) + break; + if (first) { first = false; continue; } - truncate_nl(line); - if (sscanf(line, "%*i %i %*o %*u %*u " PID_FMT " " PID_FMT " " UID_FMT " " GID_FMT " " UID_FMT " " GID_FMT, &msgid, &cpid, &lpid, &uid, &gid, &cuid, &cgid) != 7) continue; @@ -212,9 +219,6 @@ static int clean_sysvipc_msg(uid_t delete_uid, gid_t delete_gid, bool rm) { } return ret; - -fail: - return log_warning_errno(errno, "Failed to read /proc/sysvipc/msg: %m"); } static int clean_posix_shm_internal(DIR *dir, uid_t uid, gid_t gid, bool rm) { From bef77f378c4391e1baacd106944853639aa68517 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:18:20 +0200 Subject: [PATCH 20/24] install: FOREACH_LINE excorcism --- src/shared/install.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/shared/install.c b/src/shared/install.c index ccb999998c..61b713c6b6 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -15,6 +15,7 @@ #include "alloc-util.h" #include "conf-files.h" #include "conf-parser.h" +#include "def.h" #include "dirent-util.h" #include "extract-word.h" #include "fd-util.h" @@ -2841,7 +2842,6 @@ static int read_presets(UnitFileScope scope, const char *root_dir, Presets *pres STRV_FOREACH(p, files) { _cleanup_fclose_ FILE *f; - char line[LINE_MAX]; int n = 0; f = fopen(*p, "re"); @@ -2852,11 +2852,18 @@ static int read_presets(UnitFileScope scope, const char *root_dir, Presets *pres return -errno; } - FOREACH_LINE(line, f, return -errno) { + for (;;) { + _cleanup_free_ char *line = NULL; PresetRule rule = {}; const char *parameter; char *l; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; + l = strstrip(line); n++; From 8ac3c9ab1bd2f26a45b3a8703d35652dd3dffab1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:18:35 +0200 Subject: [PATCH 21/24] install: pass encountered error number to log_syntax() --- src/shared/install.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/install.c b/src/shared/install.c index 61b713c6b6..d806bc69ff 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -2880,7 +2880,7 @@ static int read_presets(UnitFileScope scope, const char *root_dir, Presets *pres /* Unit_name will remain the same as parameter when no instances are specified */ r = split_pattern_into_name_and_instances(parameter, &unit_name, &instances); if (r < 0) { - log_syntax(NULL, LOG_WARNING, *p, n, 0, "Couldn't parse line '%s'. Ignoring.", line); + log_syntax(NULL, LOG_WARNING, *p, n, r, "Couldn't parse line '%s'. Ignoring.", line); continue; } From 050ca2996388d681d12b4be78511764b1839a92f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:18:52 +0200 Subject: [PATCH 22/24] sysusers: FOREACH_LINE excorcism --- src/sysusers/sysusers.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index 91722408da..0c9902adf6 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -1681,7 +1681,6 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { static int read_config_file(const char *fn, bool ignore_enoent) { _cleanup_fclose_ FILE *rf = NULL; FILE *f = NULL; - char line[LINE_MAX]; unsigned v = 0; int r = 0; @@ -1701,10 +1700,17 @@ static int read_config_file(const char *fn, bool ignore_enoent) { f = rf; } - FOREACH_LINE(line, f, break) { + for (;;) { + _cleanup_free_ char *line = NULL; char *l; int k; + k = read_line(f, LONG_LINE_MAX, &line); + if (k < 0) + return log_error_errno(k, "Failed to read '%s': %m", fn); + if (k == 0) + break; + v++; l = strstrip(line); From e393eff6224792fe3e4339ffdd7cead92feee551 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:20:43 +0200 Subject: [PATCH 23/24] sysv-generator: FOREACH_LINE excorcism --- src/sysv-generator/sysv-generator.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c index 2b1e267c08..65313d4416 100644 --- a/src/sysv-generator/sysv-generator.c +++ b/src/sysv-generator/sysv-generator.c @@ -5,6 +5,7 @@ #include #include "alloc-util.h" +#include "def.h" #include "dirent-util.h" #include "exit-status.h" #include "fd-util.h" @@ -432,7 +433,6 @@ static int load_sysv(SysvStub *s) { _cleanup_free_ char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL; char *description; bool supports_reload = false; - char l[LINE_MAX]; assert(s); @@ -446,9 +446,16 @@ static int load_sysv(SysvStub *s) { log_debug("Loading SysV script %s", s->path); - FOREACH_LINE(l, f, goto fail) { + for (;;) { + _cleanup_free_ char *l = NULL; char *t; + r = read_line(f, LONG_LINE_MAX, &l); + if (r < 0) + return log_error_errno(r, "Failed to read configuration file '%s': %m", s->path); + if (r == 0) + break; + line++; t = strstrip(l); @@ -456,7 +463,7 @@ static int load_sysv(SysvStub *s) { /* Try to figure out whether this init script supports * the reload operation. This heuristic looks for * "Usage" lines which include the reload option. */ - if ( state == USAGE_CONTINUATION || + if (state == USAGE_CONTINUATION || (state == NORMAL && strcasestr(t, "usage"))) { if (usage_contains_reload(t)) { supports_reload = true; @@ -644,9 +651,6 @@ static int load_sysv(SysvStub *s) { s->loaded = true; return 0; - -fail: - return log_error_errno(errno, "Failed to read configuration file '%s': %m", s->path); } static int fix_order(SysvStub *s, Hashmap *all_services) { From 2a98ae4a05d764d4280abb9e9a13335d4f2be3c3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 16:21:05 +0200 Subject: [PATCH 24/24] tmpfiles: FOREACH_LINE excorcism --- src/tmpfiles/tmpfiles.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index d7e02c400e..7819d96c67 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -2979,10 +2979,9 @@ static int parse_argv(int argc, char *argv[]) { static int read_config_file(char **config_dirs, const char *fn, bool ignore_enoent, bool *invalid_config) { _cleanup_fclose_ FILE *_f = NULL; - FILE *f; - char line[LINE_MAX]; Iterator iterator; unsigned v = 0; + FILE *f; Item *i; int r = 0; @@ -3006,10 +3005,17 @@ static int read_config_file(char **config_dirs, const char *fn, bool ignore_enoe f = _f; } - FOREACH_LINE(line, f, break) { + for (;;) { + _cleanup_free_ char *line = NULL; + bool invalid_line = false; char *l; int k; - bool invalid_line = false; + + k = read_line(f, LONG_LINE_MAX, &line); + if (k < 0) + return log_error_errno(k, "Failed to read '%s': %m", fn); + if (k == 0) + break; v++;