chiark / gitweb /
core/killall: use procfs_file_alloca
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 17 Apr 2013 21:19:38 +0000 (17:19 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 19 Apr 2013 14:10:41 +0000 (10:10 -0400)
src/core/killall.c
src/shared/util.c
src/shared/util.h

index e078012c1b9cc7d3ef9da23d30b6fea12e395075..a0f57455fb41ffb25062f1d8b33e533f3974215f 100644 (file)
@@ -32,8 +32,7 @@
 #define TIMEOUT_USEC (10 * USEC_PER_SEC)
 
 static bool ignore_proc(pid_t pid) {
 #define TIMEOUT_USEC (10 * USEC_PER_SEC)
 
 static bool ignore_proc(pid_t pid) {
-        char buf[PATH_MAX];
-        FILE *f;
+        _cleanup_fclose_ FILE *f = NULL;
         char c;
         size_t count;
         uid_t uid;
         char c;
         size_t count;
         uid_t uid;
@@ -51,15 +50,11 @@ static bool ignore_proc(pid_t pid) {
         if (uid != 0)
                 return false;
 
         if (uid != 0)
                 return false;
 
-        snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long) pid);
-        char_array_0(buf);
-
-        f = fopen(buf, "re");
+        f = fopen(procfs_file_alloca(pid, "cmdline"), "re");
         if (!f)
                 return true; /* not really, but has the desired effect */
 
         count = fread(&c, 1, 1, f);
         if (!f)
                 return true; /* not really, but has the desired effect */
 
         count = fread(&c, 1, 1, f);
-        fclose(f);
 
         /* Kernel threads have an empty cmdline */
         if (count <= 0)
 
         /* Kernel threads have an empty cmdline */
         if (count <= 0)
index 1fc6c5aa1adf63476e1c7406b527b51f2896bd1f..a6ec79a29222042be9467d2cbc3410c71899a8a8 100644 (file)
@@ -80,15 +80,6 @@ char **saved_argv = NULL;
 static volatile unsigned cached_columns = 0;
 static volatile unsigned cached_lines = 0;
 
 static volatile unsigned cached_columns = 0;
 static volatile unsigned cached_lines = 0;
 
-#define procfs_file_alloca(pid, field)                                  \
-        ({                                                              \
-                pid_t _pid_ = (pid);                                    \
-                char *_r_;                                              \
-                _r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
-                sprintf(_r_, "/proc/%lu/" field, (unsigned long) _pid_); \
-                _r_;                                                    \
-        })
-
 size_t page_size(void) {
         static __thread size_t pgsz = 0;
         long r;
 size_t page_size(void) {
         static __thread size_t pgsz = 0;
         long r;
index cfb54939cd5d21730b4adcc3cf96a915a9dd58d8..6575f5681199108799b54dfa5fb5059538dce9b8 100644 (file)
@@ -21,6 +21,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <alloca.h>
 #include <inttypes.h>
 #include <time.h>
 #include <sys/time.h>
 #include <inttypes.h>
 #include <time.h>
 #include <sys/time.h>
@@ -696,3 +697,12 @@ int unlink_noerrno(const char *path);
                 strcpy(stpcpy(_c_, _a_), _b_);          \
                 _c_;                                    \
         })
                 strcpy(stpcpy(_c_, _a_), _b_);          \
                 _c_;                                    \
         })
+
+#define procfs_file_alloca(pid, field)                                  \
+        ({                                                              \
+                pid_t _pid_ = (pid);                                    \
+                char *_r_;                                              \
+                _r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
+                sprintf(_r_, "/proc/%lu/" field, (unsigned long) _pid_); \
+                _r_;                                                    \
+        })