Merge pull request #10450 from poettering/foreach-line-excorcism
FOREACH_LINE excorcism
This commit is contained in:
commit
17acb7ef9a
|
@ -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:");
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <libmount.h>
|
||||
|
||||
#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;
|
||||
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "def.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "ima-setup.h"
|
||||
|
@ -22,20 +24,20 @@ 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.");
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -56,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) {
|
||||
|
@ -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++;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include <string.h>
|
||||
|
||||
#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,20 +274,27 @@ 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");
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -285,7 +305,7 @@ static int write_onlycap_list(void) {
|
|||
len += l + 1;
|
||||
}
|
||||
|
||||
if (!len)
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
list[len - 1] = 0;
|
||||
|
@ -293,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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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:");
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <stdio_ext.h>
|
||||
|
||||
#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);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#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) {
|
||||
|
|
|
@ -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++;
|
||||
|
||||
|
@ -2873,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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#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) {
|
||||
|
|
|
@ -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++;
|
||||
|
||||
|
|
Loading…
Reference in New Issue