diff --git a/src/shared/clean-ipc.c b/src/shared/clean-ipc.c index 08dc7b9e43c..f9b05ff9249 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) {