diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-11-28 20:15:18 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-11-28 20:29:44 +0100 |
commit | 4d84bc2f5fd9134f13614add5e6b05732aba44ca (patch) | |
tree | f867931ccfe524bc164f57aa6dc51c4ded23c52b /src/journal/coredump.c | |
parent | cb2264aa0dcd5bb67338d25ee797ebec50ac7530 (diff) |
coredump: rework compose_open_fds()
Use FOREACH_DIRENT() and FOREACH_LINE() macros instead of manual loops.
Don't clobber return parameters on failure.
Simplify some other things.
Diffstat (limited to 'src/journal/coredump.c')
-rw-r--r-- | src/journal/coredump.c | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/src/journal/coredump.c b/src/journal/coredump.c index 26953cc5b0..50402068a0 100644 --- a/src/journal/coredump.c +++ b/src/journal/coredump.c @@ -459,12 +459,13 @@ static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_s * EOF */ static int compose_open_fds(pid_t pid, char **open_fds) { + _cleanup_closedir_ DIR *proc_fd_dir = NULL; + _cleanup_close_ int proc_fdinfo_fd = -1; + _cleanup_free_ char *buffer = NULL; _cleanup_fclose_ FILE *stream = NULL; - size_t ignored_size; const char *fddelim = "", *path; struct dirent *dent = NULL; - _cleanup_closedir_ DIR *proc_fd_dir = NULL; - _cleanup_close_ int proc_fdinfo_fd = -1; + size_t size = 0; int r = 0; assert(pid >= 0); @@ -475,23 +476,19 @@ static int compose_open_fds(pid_t pid, char **open_fds) { if (!proc_fd_dir) return -errno; - proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", - O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH); + proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH); if (proc_fdinfo_fd < 0) return -errno; - stream = open_memstream(open_fds, &ignored_size); + stream = open_memstream(&buffer, &size); if (!stream) return -ENOMEM; - for (dent = readdir(proc_fd_dir); dent != NULL; dent = readdir(proc_fd_dir)) { - _cleanup_free_ char *fdname = NULL; - int fd; + FOREACH_DIRENT(dent, proc_fd_dir, return -errno) { _cleanup_fclose_ FILE *fdinfo = NULL; + _cleanup_free_ char *fdname = NULL; char line[LINE_MAX]; - - if (dent->d_name[0] == '.' || strcmp(dent->d_name, "..") == 0) - continue; + int fd; r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname); if (r < 0) @@ -511,11 +508,23 @@ static int compose_open_fds(pid_t pid, char **open_fds) { continue; } - while(fgets(line, sizeof(line), fdinfo) != NULL) - fprintf(stream, "%s%s", - line, strchr(line, '\n') == NULL ? "\n" : ""); + FOREACH_LINE(line, fdinfo, break) { + fputs(line, stream); + if (!endswith(line, "\n")) + fputc('\n', stream); + } } + errno = 0; + fclose(stream); + stream = NULL; + + if (errno != 0) + return -errno; + + *open_fds = buffer; + buffer = NULL; + return 0; } |