]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/process-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
35 #include "process-util.h"
36 #include "signal-util.h"
37 #include "string-util.h"
38 #include "user-util.h"
41 int get_process_state(pid_t pid
) {
45 _cleanup_free_
char *line
= NULL
;
49 p
= procfs_file_alloca(pid
, "stat");
51 r
= read_one_line_file(p
, &line
);
57 p
= strrchr(line
, ')');
63 if (sscanf(p
, " %c", &state
) != 1)
66 return (unsigned char) state
;
69 int get_process_comm(pid_t pid
, char **name
) {
76 p
= procfs_file_alloca(pid
, "comm");
78 r
= read_one_line_file(p
, name
);
85 int get_process_cmdline(pid_t pid
, size_t max_length
, bool comm_fallback
, char **line
) {
86 _cleanup_fclose_
FILE *f
= NULL
;
94 p
= procfs_file_alloca(pid
, "cmdline");
103 if (max_length
== 0) {
104 size_t len
= 0, allocated
= 0;
106 while ((c
= getc(f
)) != EOF
) {
108 if (!GREEDY_REALLOC(r
, allocated
, len
+2)) {
113 r
[len
++] = isprint(c
) ? c
: ' ';
123 r
= new(char, max_length
);
129 while ((c
= getc(f
)) != EOF
) {
151 size_t n
= MIN(left
-1, 3U);
158 /* Kernel threads have no argv[] */
160 _cleanup_free_
char *t
= NULL
;
168 h
= get_process_comm(pid
, &t
);
172 r
= strjoin("[", t
, "]", NULL
);
181 int is_kernel_thread(pid_t pid
) {
188 if (pid
== 0 || pid
== 1) /* pid 1, and we ourselves certainly aren't a kernel thread */
193 p
= procfs_file_alloca(pid
, "cmdline");
201 count
= fread(&c
, 1, 1, f
);
205 /* Kernel threads have an empty cmdline */
208 return eof
? 1 : -errno
;
213 int get_process_capeff(pid_t pid
, char **capeff
) {
220 p
= procfs_file_alloca(pid
, "status");
222 r
= get_proc_field(p
, "CapEff", WHITESPACE
, capeff
);
229 static int get_process_link_contents(const char *proc_file
, char **name
) {
235 r
= readlink_malloc(proc_file
, name
);
244 int get_process_exe(pid_t pid
, char **name
) {
251 p
= procfs_file_alloca(pid
, "exe");
252 r
= get_process_link_contents(p
, name
);
256 d
= endswith(*name
, " (deleted)");
263 static int get_process_id(pid_t pid
, const char *field
, uid_t
*uid
) {
264 _cleanup_fclose_
FILE *f
= NULL
;
274 p
= procfs_file_alloca(pid
, "status");
282 FOREACH_LINE(line
, f
, return -errno
) {
287 if (startswith(l
, field
)) {
289 l
+= strspn(l
, WHITESPACE
);
291 l
[strcspn(l
, WHITESPACE
)] = 0;
293 return parse_uid(l
, uid
);
300 int get_process_uid(pid_t pid
, uid_t
*uid
) {
301 return get_process_id(pid
, "Uid:", uid
);
304 int get_process_gid(pid_t pid
, gid_t
*gid
) {
305 assert_cc(sizeof(uid_t
) == sizeof(gid_t
));
306 return get_process_id(pid
, "Gid:", gid
);
309 int get_process_cwd(pid_t pid
, char **cwd
) {
314 p
= procfs_file_alloca(pid
, "cwd");
316 return get_process_link_contents(p
, cwd
);
319 int get_process_root(pid_t pid
, char **root
) {
324 p
= procfs_file_alloca(pid
, "root");
326 return get_process_link_contents(p
, root
);
329 int get_process_environ(pid_t pid
, char **env
) {
330 _cleanup_fclose_
FILE *f
= NULL
;
331 _cleanup_free_
char *outcome
= NULL
;
334 size_t allocated
= 0, sz
= 0;
339 p
= procfs_file_alloca(pid
, "environ");
348 while ((c
= fgetc(f
)) != EOF
) {
349 if (!GREEDY_REALLOC(outcome
, allocated
, sz
+ 5))
353 outcome
[sz
++] = '\n';
355 sz
+= cescape_char(c
, outcome
+ sz
);
359 outcome
= strdup("");
371 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
373 _cleanup_free_
char *line
= NULL
;
385 p
= procfs_file_alloca(pid
, "stat");
386 r
= read_one_line_file(p
, &line
);
392 /* Let's skip the pid and comm fields. The latter is enclosed
393 * in () but does not escape any () in its value, so let's
394 * skip over it manually */
396 p
= strrchr(line
, ')');
408 if ((long unsigned) (pid_t
) ppid
!= ppid
)
411 *_ppid
= (pid_t
) ppid
;
416 int wait_for_terminate(pid_t pid
, siginfo_t
*status
) {
427 if (waitid(P_PID
, pid
, status
, WEXITED
) < 0) {
441 * < 0 : wait_for_terminate() failed to get the state of the
442 * process, the process was terminated by a signal, or
443 * failed for an unknown reason.
444 * >=0 : The process terminated normally, and its exit code is
447 * That is, success is indicated by a return value of zero, and an
448 * error is indicated by a non-zero value.
450 * A warning is emitted if the process terminates abnormally,
451 * and also if it returns non-zero unless check_exit_code is true.
453 int wait_for_terminate_and_warn(const char *name
, pid_t pid
, bool check_exit_code
) {
460 r
= wait_for_terminate(pid
, &status
);
462 return log_warning_errno(r
, "Failed to wait for %s: %m", name
);
464 if (status
.si_code
== CLD_EXITED
) {
465 if (status
.si_status
!= 0)
466 log_full(check_exit_code
? LOG_WARNING
: LOG_DEBUG
,
467 "%s failed with error code %i.", name
, status
.si_status
);
469 log_debug("%s succeeded.", name
);
471 return status
.si_status
;
472 } else if (status
.si_code
== CLD_KILLED
||
473 status
.si_code
== CLD_DUMPED
) {
475 log_warning("%s terminated by signal %s.", name
, signal_to_string(status
.si_status
));
479 log_warning("%s failed due to unknown reason.", name
);
483 void sigkill_wait(pid_t
*pid
) {
489 if (kill(*pid
, SIGKILL
) > 0)
490 (void) wait_for_terminate(*pid
, NULL
);
493 int kill_and_sigcont(pid_t pid
, int sig
) {
496 r
= kill(pid
, sig
) < 0 ? -errno
: 0;
504 int getenv_for_pid(pid_t pid
, const char *field
, char **_value
) {
505 _cleanup_fclose_
FILE *f
= NULL
;
516 path
= procfs_file_alloca(pid
, "environ");
518 f
= fopen(path
, "re");
532 for (i
= 0; i
< sizeof(line
)-1; i
++) {
536 if (_unlikely_(c
== EOF
)) {
546 if (memcmp(line
, field
, l
) == 0 && line
[l
] == '=') {
547 value
= strdup(line
+ l
+ 1);
561 bool pid_is_unwaited(pid_t pid
) {
562 /* Checks whether a PID is still valid at all, including a zombie */
567 if (kill(pid
, 0) >= 0)
570 return errno
!= ESRCH
;
573 bool pid_is_alive(pid_t pid
) {
576 /* Checks whether a PID is still valid and not a zombie */
581 r
= get_process_state(pid
);
582 if (r
== -ESRCH
|| r
== 'Z')