chiark / gitweb /
treewide: auto-convert the simple cases to log_*_errno()
[elogind.git] / src / journal / coredump.c
index e820c6f71acb8eaeca2950da7c12e759c993f1b1..af155b487f3ee091cf06df2b3fb8d2cccc571072 100644 (file)
@@ -313,7 +313,7 @@ static int save_external_coredump(
 
         r = make_filename(info, &fn);
         if (r < 0) {
-                log_error("Failed to determine coredump file name: %s", strerror(-r));
+                log_error_errno(-r, "Failed to determine coredump file name: %m");
                 return r;
         }
 
@@ -337,7 +337,7 @@ static int save_external_coredump(
                 log_error("Not enough disk space for coredump of %s (%s), refusing.", info[INFO_PID], info[INFO_COMM]);
                 goto fail;
         } else if (r < 0) {
-                log_error("Failed to dump coredump to file: %s", strerror(-r));
+                log_error_errno(-r, "Failed to dump coredump to file: %m");
                 goto fail;
         }
 
@@ -379,7 +379,7 @@ static int save_external_coredump(
 
                 r = compress_stream(fd, fd_compressed, -1);
                 if (r < 0) {
-                        log_error("Failed to compress %s: %s", tmp_compressed, strerror(-r));
+                        log_error_errno(-r, "Failed to compress %s: %m", tmp_compressed);
                         goto fail_compressed;
                 }
 
@@ -446,7 +446,7 @@ static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_s
 
         n = read(fd, field + 9, size);
         if (n < 0) {
-                log_error("Failed to read core data: %s", strerror(-n));
+                log_error_errno(-n, "Failed to read core data: %m");
                 return (int) n;
         }
         if ((size_t) n < size) {
@@ -478,21 +478,25 @@ static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_s
  */
 static int compose_open_fds(pid_t pid, char **open_fds) {
         _cleanup_fclose_ FILE *stream = NULL;
-        char path[PATH_MAX], line[LINE_MAX];
         size_t ignored_size;
-        const char *fddelim = "";
+        const char *fddelim = "", *path;
         struct dirent *dent = NULL;
         _cleanup_closedir_ DIR *proc_fd_dir = NULL;
+        _cleanup_close_ int proc_fdinfo_fd = -1;
         int r = 0;
 
         assert(pid >= 0);
         assert(open_fds != NULL);
 
-        sprintf(path, "/proc/"PID_FMT"/fd", pid);
+        path = procfs_file_alloca(pid, "fd");
         proc_fd_dir = opendir(path);
+        if (!proc_fd_dir)
+                return -errno;
 
-        if (proc_fd_dir == NULL)
-                return -ENOENT;
+        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);
         if (!stream)
@@ -500,18 +504,14 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
 
         for (dent = readdir(proc_fd_dir); dent != NULL; dent = readdir(proc_fd_dir)) {
                 _cleanup_free_ char *fdname = NULL;
+                int fd;
                 _cleanup_fclose_ FILE *fdinfo = NULL;
+                char line[LINE_MAX];
 
                 if (dent->d_name[0] == '.' || strcmp(dent->d_name, "..") == 0)
                         continue;
 
-                /* Too long path is unlikely a path to valid file descriptor in /proc/[pid]/fd */
-                /* Skip it. */
-                r = snprintf(path, sizeof(path), "/proc/"PID_FMT"/fd/%s", pid, dent->d_name);
-                if (r >= (int)sizeof(path))
-                        continue;
-
-                r = readlink_malloc(path, &fdname);
+                r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname);
                 if (r < 0)
                         return r;
 
@@ -519,16 +519,15 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
                 fddelim = "\n";
 
                 /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
-
-                /* Too long path is unlikely a path to valid file descriptor info in /proc/[pid]/fdinfo */
-                /* Skip it. */
-                r = snprintf(path, sizeof(path), "/proc/"PID_FMT"/fdinfo/%s", pid, dent->d_name);
-                if (r >= (int)sizeof(path))
+                fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY);
+                if (fd < 0)
                         continue;
 
-                fdinfo = fopen(path, "re");
-                if (fdinfo == NULL)
+                fdinfo = fdopen(fd, "re");
+                if (fdinfo == NULL) {
+                        close(fd);
                         continue;
+                }
 
                 while(fgets(line, sizeof(line), fdinfo) != NULL)
                         fprintf(stream, "%s%s",
@@ -842,7 +841,7 @@ int main(int argc, char* argv[]) {
                 else if (r == -EINVAL)
                         log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
                 else
-                        log_warning("Failed to generate stack trace: %s", strerror(-r));
+                        log_warning_errno(-r, "Failed to generate stack trace: %m");
         }
 
         if (!core_message)
@@ -869,7 +868,7 @@ log:
 
         r = sd_journal_sendv(iovec, j);
         if (r < 0)
-                log_error("Failed to log coredump: %s", strerror(-r));
+                log_error_errno(-r, "Failed to log coredump: %m");
 
 finish:
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;