1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-02 10:51:20 +03:00

clean-ipc: FOREACH_LINE excorcism

This commit is contained in:
Lennart Poettering 2018-10-18 16:18:01 +02:00
parent e1b9fc23eb
commit d5b3c07da6

View File

@ -16,6 +16,7 @@
#include <unistd.h> #include <unistd.h>
#include "clean-ipc.h" #include "clean-ipc.h"
#include "def.h"
#include "dirent-util.h" #include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.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) { static int clean_sysvipc_shm(uid_t delete_uid, gid_t delete_gid, bool rm) {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
char line[LINE_MAX];
bool first = true; bool first = true;
int ret = 0; int ret = 0, r;
f = fopen("/proc/sysvipc/shm", "re"); f = fopen("/proc/sysvipc/shm", "re");
if (!f) { 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"); 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; unsigned n_attached;
pid_t cpid, lpid; pid_t cpid, lpid;
uid_t uid, cuid; uid_t uid, cuid;
gid_t gid, cgid; gid_t gid, cgid;
int shmid; 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) { if (first) {
first = false; first = false;
continue; continue;
} }
truncate_nl(line);
if (sscanf(line, "%*i %i %*o %*u " PID_FMT " " PID_FMT " %u " UID_FMT " " GID_FMT " " UID_FMT " " GID_FMT, 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) &shmid, &cpid, &lpid, &n_attached, &uid, &gid, &cuid, &cgid) != 8)
continue; continue;
@ -95,16 +100,12 @@ static int clean_sysvipc_shm(uid_t delete_uid, gid_t delete_gid, bool rm) {
} }
return ret; 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) { static int clean_sysvipc_sem(uid_t delete_uid, gid_t delete_gid, bool rm) {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
char line[LINE_MAX];
bool first = true; bool first = true;
int ret = 0; int ret = 0, r;
f = fopen("/proc/sysvipc/sem", "re"); f = fopen("/proc/sysvipc/sem", "re");
if (!f) { 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"); 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; uid_t uid, cuid;
gid_t gid, cgid; gid_t gid, cgid;
int semid; 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) { if (first) {
first = false; first = false;
continue; continue;
} }
truncate_nl(line);
if (sscanf(line, "%*i %i %*o %*u " UID_FMT " " GID_FMT " " UID_FMT " " GID_FMT, if (sscanf(line, "%*i %i %*o %*u " UID_FMT " " GID_FMT " " UID_FMT " " GID_FMT,
&semid, &uid, &gid, &cuid, &cgid) != 5) &semid, &uid, &gid, &cuid, &cgid) != 5)
continue; continue;
@ -153,16 +159,12 @@ static int clean_sysvipc_sem(uid_t delete_uid, gid_t delete_gid, bool rm) {
} }
return ret; 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) { static int clean_sysvipc_msg(uid_t delete_uid, gid_t delete_gid, bool rm) {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
char line[LINE_MAX];
bool first = true; bool first = true;
int ret = 0; int ret = 0, r;
f = fopen("/proc/sysvipc/msg", "re"); f = fopen("/proc/sysvipc/msg", "re");
if (!f) { 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"); 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; uid_t uid, cuid;
gid_t gid, cgid; gid_t gid, cgid;
pid_t cpid, lpid; pid_t cpid, lpid;
int msgid; 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) { if (first) {
first = false; first = false;
continue; continue;
} }
truncate_nl(line);
if (sscanf(line, "%*i %i %*o %*u %*u " PID_FMT " " PID_FMT " " UID_FMT " " GID_FMT " " UID_FMT " " GID_FMT, 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) &msgid, &cpid, &lpid, &uid, &gid, &cuid, &cgid) != 7)
continue; continue;
@ -212,9 +219,6 @@ static int clean_sysvipc_msg(uid_t delete_uid, gid_t delete_gid, bool rm) {
} }
return ret; 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) { static int clean_posix_shm_internal(DIR *dir, uid_t uid, gid_t gid, bool rm) {