diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/Makefile.am | 2 | ||||
-rw-r--r-- | src/shared/cgroup-util.c | 1 | ||||
-rw-r--r-- | src/shared/log.c | 1 | ||||
-rw-r--r-- | src/shared/process-util.c | 176 | ||||
-rw-r--r-- | src/shared/process-util.h | 46 | ||||
-rw-r--r-- | src/shared/smack-util.c | 1 | ||||
-rw-r--r-- | src/shared/util.c | 145 | ||||
-rw-r--r-- | src/shared/util.h | 18 | ||||
-rw-r--r-- | src/shared/virt.c | 1 |
9 files changed, 231 insertions, 160 deletions
diff --git a/src/shared/Makefile.am b/src/shared/Makefile.am index ac40f48c07..9f2df41e70 100644 --- a/src/shared/Makefile.am +++ b/src/shared/Makefile.am @@ -19,6 +19,7 @@ libudev_shared_la_SOURCES=\ mkdir-label.c \ MurmurHash2.c \ path-util.c \ + process-util.c \ selinux-util.c \ siphash24.c \ smack-util.c \ @@ -51,6 +52,7 @@ noinst_HEADERS = \ mkdir.h \ MurmurHash2.h \ path-util.h \ + process-util.h \ selinux-util.h \ set.h \ siphash24.h \ diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 2076eff4d2..326b75f763 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -25,6 +25,7 @@ #include "cgroup-util.h" #include "set.h" +#include "process-util.h" #include "path-util.h" int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) { diff --git a/src/shared/log.c b/src/shared/log.c index d4df4dffd3..bcbf4a34bd 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -33,6 +33,7 @@ #include "macro.h" #include "socket-util.h" #include "time-util.h" +#include "process-util.h" #define SNDBUF_SIZE (8*1024*1024) diff --git a/src/shared/process-util.c b/src/shared/process-util.c new file mode 100644 index 0000000000..0c9d1e409c --- /dev/null +++ b/src/shared/process-util.c @@ -0,0 +1,176 @@ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <stdbool.h> +#include <sys/types.h> +#include <string.h> +#include <stdio.h> +#include <assert.h> +#include <errno.h> +#include <unistd.h> +#include <sys/wait.h> +#include <signal.h> +#include <ctype.h> + +#include "process-util.h" +#include "fileio.h" +#include "util.h" +#include "log.h" + +int get_process_comm(pid_t pid, char **name) { + const char *p; + int r; + + assert(name); + assert(pid >= 0); + + p = procfs_file_alloca(pid, "comm"); + + r = read_one_line_file(p, name); + if (r == -ENOENT) + return -ESRCH; + + return r; +} + +int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { + _cleanup_fclose_ FILE *f = NULL; + char *r = NULL, *k; + const char *p; + int c; + + assert(line); + assert(pid >= 0); + + p = procfs_file_alloca(pid, "cmdline"); + + f = fopen(p, "re"); + if (!f) + return -errno; + + if (max_length == 0) { + size_t len = 0, allocated = 0; + + while ((c = getc(f)) != EOF) { + + if (!GREEDY_REALLOC(r, allocated, len+2)) { + free(r); + return -ENOMEM; + } + + r[len++] = isprint(c) ? c : ' '; + } + + if (len > 0) + r[len-1] = 0; + + } else { + bool space = false; + size_t left; + + r = new(char, max_length); + if (!r) + return -ENOMEM; + + k = r; + left = max_length; + while ((c = getc(f)) != EOF) { + + if (isprint(c)) { + if (space) { + if (left <= 4) + break; + + *(k++) = ' '; + left--; + space = false; + } + + if (left <= 4) + break; + + *(k++) = (char) c; + left--; + } else + space = true; + } + + if (left <= 4) { + size_t n = MIN(left-1, 3U); + memcpy(k, "...", n); + k[n] = 0; + } else + *k = 0; + } + + /* Kernel threads have no argv[] */ + if (isempty(r)) { + _cleanup_free_ char *t = NULL; + int h; + + free(r); + + if (!comm_fallback) + return -ENOENT; + + h = get_process_comm(pid, &t); + if (h < 0) + return h; + + r = strjoin("[", t, "]", NULL); + if (!r) + return -ENOMEM; + } + + *line = r; + return 0; +} + +int get_process_environ(pid_t pid, char **env) { + _cleanup_fclose_ FILE *f = NULL; + _cleanup_free_ char *outcome = NULL; + int c; + const char *p; + size_t allocated = 0, sz = 0; + + assert(pid >= 0); + assert(env); + + p = procfs_file_alloca(pid, "environ"); + + f = fopen(p, "re"); + if (!f) + return -errno; + + while ((c = fgetc(f)) != EOF) { + if (!GREEDY_REALLOC(outcome, allocated, sz + 5)) + return -ENOMEM; + + if (c == '\0') + outcome[sz++] = '\n'; + else + sz += cescape_char(c, outcome + sz); + } + + outcome[sz] = '\0'; + *env = outcome; + outcome = NULL; + + return 0; +} diff --git a/src/shared/process-util.h b/src/shared/process-util.h new file mode 100644 index 0000000000..bb420ec06a --- /dev/null +++ b/src/shared/process-util.h @@ -0,0 +1,46 @@ +#pragma once + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <stdbool.h> +#include <sys/types.h> +#include <alloca.h> +#include <stdio.h> +#include <string.h> +#include <signal.h> + +#include "formats-util.h" + +#define procfs_file_alloca(pid, field) \ + ({ \ + pid_t _pid_ = (pid); \ + const char *_r_; \ + if (_pid_ == 0) { \ + _r_ = ("/proc/self/" field); \ + } else { \ + _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \ + sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \ + } \ + _r_; \ + }) + +int get_process_comm(pid_t pid, char **name); +int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line); +int get_process_environ(pid_t pid, char **environ); diff --git a/src/shared/smack-util.c b/src/shared/smack-util.c index f715fc1084..92477811aa 100644 --- a/src/shared/smack-util.c +++ b/src/shared/smack-util.c @@ -24,6 +24,7 @@ #endif #include "util.h" +#include "process-util.h" #include "path-util.h" #include "fileio.h" #include "smack-util.h" diff --git a/src/shared/util.c b/src/shared/util.c index e70984d36e..d22ab8631f 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -69,6 +69,7 @@ #include "hashmap.h" #include "fileio.h" #include "virt.h" +#include "process-util.h" /* Put this test here for a lack of better place */ assert_cc(EAGAIN == EWOULDBLOCK); @@ -127,7 +128,7 @@ char* endswith(const char *s, const char *postfix) { return (char*) s + sl - pl; } -static size_t cescape_char(char c, char *buf) { +size_t cescape_char(char c, char *buf) { char * buf_old = buf; switch (c) { @@ -427,39 +428,6 @@ char *truncate_nl(char *s) { return s; } -int get_process_environ(pid_t pid, char **env) { - _cleanup_fclose_ FILE *f = NULL; - _cleanup_free_ char *outcome = NULL; - int c; - const char *p; - size_t allocated = 0, sz = 0; - - assert(pid >= 0); - assert(env); - - p = procfs_file_alloca(pid, "environ"); - - f = fopen(p, "re"); - if (!f) - return -errno; - - while ((c = fgetc(f)) != EOF) { - if (!GREEDY_REALLOC(outcome, allocated, sz + 5)) - return -ENOMEM; - - if (c == '\0') - outcome[sz++] = '\n'; - else - sz += cescape_char(c, outcome + sz); - } - - outcome[sz] = '\0'; - *env = outcome; - outcome = NULL; - - return 0; -} - char *strnappend(const char *s, const char *suffix, size_t b) { size_t a; char *r; @@ -541,115 +509,6 @@ int rmdir_parents(const char *path, const char *stop) { return 0; } -int get_process_comm(pid_t pid, char **name) { - const char *p; - int r; - - assert(name); - assert(pid >= 0); - - p = procfs_file_alloca(pid, "comm"); - - r = read_one_line_file(p, name); - if (r == -ENOENT) - return -ESRCH; - - return r; -} - -int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { - _cleanup_fclose_ FILE *f = NULL; - char *r = NULL, *k; - const char *p; - int c; - - assert(line); - assert(pid >= 0); - - p = procfs_file_alloca(pid, "cmdline"); - - f = fopen(p, "re"); - if (!f) - return -errno; - - if (max_length == 0) { - size_t len = 0, allocated = 0; - - while ((c = getc(f)) != EOF) { - - if (!GREEDY_REALLOC(r, allocated, len+2)) { - free(r); - return -ENOMEM; - } - - r[len++] = isprint(c) ? c : ' '; - } - - if (len > 0) - r[len-1] = 0; - - } else { - bool space = false; - size_t left; - - r = new(char, max_length); - if (!r) - return -ENOMEM; - - k = r; - left = max_length; - while ((c = getc(f)) != EOF) { - - if (isprint(c)) { - if (space) { - if (left <= 4) - break; - - *(k++) = ' '; - left--; - space = false; - } - - if (left <= 4) - break; - - *(k++) = (char) c; - left--; - } else - space = true; - } - - if (left <= 4) { - size_t n = MIN(left-1, 3U); - memcpy(k, "...", n); - k[n] = 0; - } else - *k = 0; - } - - /* Kernel threads have no argv[] */ - if (isempty(r)) { - _cleanup_free_ char *t = NULL; - int h; - - free(r); - - if (!comm_fallback) - return -ENOENT; - - h = get_process_comm(pid, &t); - if (h < 0) - return h; - - r = strjoin("[", t, "]", NULL); - if (!r) - return -ENOMEM; - } - - *line = r; - return 0; -} - char hexchar(int x) { static const char table[16] = "0123456789abcdef"; diff --git a/src/shared/util.h b/src/shared/util.h index 4808f04efe..0f97873d8b 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -149,14 +149,11 @@ char *truncate_nl(char *s); int rmdir_parents(const char *path, const char *stop); -int get_process_comm(pid_t pid, char **name); -int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line); -int get_process_environ(pid_t pid, char **environ); - char hexchar(int x) _const_; char octchar(int x) _const_; char *cescape(const char *s); +size_t cescape_char(char c, char *buf); char *xescape(const char *s, const char *bad); bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_; @@ -397,19 +394,6 @@ int unlink_noerrno(const char *path); _d_; \ }) -#define procfs_file_alloca(pid, field) \ - ({ \ - pid_t _pid_ = (pid); \ - const char *_r_; \ - if (_pid_ == 0) { \ - _r_ = ("/proc/self/" field); \ - } else { \ - _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \ - sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \ - } \ - _r_; \ - }) - static inline void qsort_safe(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { if (nmemb) { diff --git a/src/shared/virt.c b/src/shared/virt.c index 358b4403d2..e39ad963c6 100644 --- a/src/shared/virt.c +++ b/src/shared/virt.c @@ -22,6 +22,7 @@ #include <unistd.h> #include "util.h" +#include "process-util.h" #include "virt.h" #include "fileio.h" |