]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
global/signal_handler: dump cmdline instead of arg[0] 10345/head
authorKefu Chai <kchai@redhat.com>
Tue, 19 Jul 2016 04:27:33 +0000 (12:27 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 19 Jul 2016 10:04:30 +0000 (18:04 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/global/signal_handler.cc

index f1b32c92aec6d91a4bb2c04e4d803962049ce523..7f0cb33b62d8efc52f69b589a917cb598f2c2bb8 100644 (file)
@@ -152,7 +152,7 @@ void install_standard_sighandlers(void)
 #include "common/Thread.h"
 #include <errno.h>
 
-void get_name_by_pid(pid_t pid, char *task_name)
+string get_name_by_pid(pid_t pid)
 {
   char proc_pid_path[PATH_MAX] = {0};
   snprintf(proc_pid_path, PATH_MAX, "/proc/%d/cmdline", pid);
@@ -163,18 +163,22 @@ void get_name_by_pid(pid_t pid, char *task_name)
     derr << "Fail to open '" << proc_pid_path 
          << "' error = " << cpp_strerror(fd) 
          << dendl;
-    return;
+    return "<unknown>";
   }
-
-  int ret = read(fd, task_name, PATH_MAX-1);
+  // assuming the cmdline length does not exceed PATH_MAX. if it
+  // really does, it's fine to return a truncated version.
+  char buf[PATH_MAX] = {0};
+  int ret = read(fd, buf, sizeof(buf));
+  close(fd);
   if (ret < 0) {
     ret = -errno;
-    derr << "Fail to read '" << proc_pid_path 
-         << "' error = " << cpp_strerror(ret) 
+    derr << "Fail to read '" << proc_pid_path
+         << "' error = " << cpp_strerror(ret)
          << dendl;
+    return "<unknown>";
   }
-   
-  close(fd);
+  std::replace(buf, buf + ret, '\0', ' ');
+  return string(buf, ret);
 }
 
  
@@ -283,14 +287,13 @@ struct SignalHandler : public Thread {
          if (handlers[signum]) {
            r = read(handlers[signum]->pipefd[0], &v, 1);
            if (r == 1) {
-             char task_name[PATH_MAX] = "unknown";
              siginfo_t * siginfo = &handlers[signum]->info_t;
-             get_name_by_pid(siginfo->si_pid, task_name);
-             derr << "received  signal: " << sys_siglist[signum] 
-                  << " from " << " PID: " << siginfo->si_pid 
-                  << " task name: " << task_name 
-                  << " UID: " << siginfo->si_uid 
-                  << dendl;
+             string task_name = get_name_by_pid(siginfo->si_pid);
+             derr << "received  signal: " << sys_siglist[signum]
+                  << " from " << " PID: " << siginfo->si_pid
+                  << " task name: " << task_name
+                  << " UID: " << siginfo->si_uid
+                  << dendl;
              handlers[signum]->handler(signum);
            }
          }