]>
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>
36 #include "process-util.h"
37 #include "signal-util.h"
38 #include "string-util.h"
39 #include "user-util.h"
42 int get_process_state(pid_t pid
) {
46 _cleanup_free_
char *line
= NULL
;
50 p
= procfs_file_alloca(pid
, "stat");
52 r
= read_one_line_file(p
, &line
);
58 p
= strrchr(line
, ')');
64 if (sscanf(p
, " %c", &state
) != 1)
67 return (unsigned char) state
;
70 int get_process_comm(pid_t pid
, char **name
) {
77 p
= procfs_file_alloca(pid
, "comm");
79 r
= read_one_line_file(p
, name
);
86 int get_process_cmdline(pid_t pid
, size_t max_length
, bool comm_fallback
, char **line
) {
87 _cleanup_fclose_
FILE *f
= NULL
;
95 p
= procfs_file_alloca(pid
, "cmdline");
104 if (max_length
== 0) {
105 size_t len
= 0, allocated
= 0;
107 while ((c
= getc(f
)) != EOF
) {
109 if (!GREEDY_REALLOC(r
, allocated
, len
+2)) {
114 r
[len
++] = isprint(c
) ? c
: ' ';
124 r
= new(char, max_length
);
130 while ((c
= getc(f
)) != EOF
) {
152 size_t n
= MIN(left
-1, 3U);
159 /* Kernel threads have no argv[] */
161 _cleanup_free_
char *t
= NULL
;
169 h
= get_process_comm(pid
, &t
);
173 r
= strjoin("[", t
, "]", NULL
);
182 int is_kernel_thread(pid_t pid
) {
189 if (pid
== 0 || pid
== 1) /* pid 1, and we ourselves certainly aren't a kernel thread */
194 p
= procfs_file_alloca(pid
, "cmdline");
202 count
= fread(&c
, 1, 1, f
);
206 /* Kernel threads have an empty cmdline */
209 return eof
? 1 : -errno
;
214 int get_process_capeff(pid_t pid
, char **capeff
) {
221 p
= procfs_file_alloca(pid
, "status");
223 r
= get_proc_field(p
, "CapEff", WHITESPACE
, capeff
);
230 static int get_process_link_contents(const char *proc_file
, char **name
) {
236 r
= readlink_malloc(proc_file
, name
);
245 int get_process_exe(pid_t pid
, char **name
) {
252 p
= procfs_file_alloca(pid
, "exe");
253 r
= get_process_link_contents(p
, name
);
257 d
= endswith(*name
, " (deleted)");
264 static int get_process_id(pid_t pid
, const char *field
, uid_t
*uid
) {
265 _cleanup_fclose_
FILE *f
= NULL
;
275 p
= procfs_file_alloca(pid
, "status");
283 FOREACH_LINE(line
, f
, return -errno
) {
288 if (startswith(l
, field
)) {
290 l
+= strspn(l
, WHITESPACE
);
292 l
[strcspn(l
, WHITESPACE
)] = 0;
294 return parse_uid(l
, uid
);
301 int get_process_uid(pid_t pid
, uid_t
*uid
) {
302 return get_process_id(pid
, "Uid:", uid
);
305 int get_process_gid(pid_t pid
, gid_t
*gid
) {
306 assert_cc(sizeof(uid_t
) == sizeof(gid_t
));
307 return get_process_id(pid
, "Gid:", gid
);
310 int get_process_cwd(pid_t pid
, char **cwd
) {
315 p
= procfs_file_alloca(pid
, "cwd");
317 return get_process_link_contents(p
, cwd
);
320 int get_process_root(pid_t pid
, char **root
) {
325 p
= procfs_file_alloca(pid
, "root");
327 return get_process_link_contents(p
, root
);
330 int get_process_environ(pid_t pid
, char **env
) {
331 _cleanup_fclose_
FILE *f
= NULL
;
332 _cleanup_free_
char *outcome
= NULL
;
335 size_t allocated
= 0, sz
= 0;
340 p
= procfs_file_alloca(pid
, "environ");
349 while ((c
= fgetc(f
)) != EOF
) {
350 if (!GREEDY_REALLOC(outcome
, allocated
, sz
+ 5))
354 outcome
[sz
++] = '\n';
356 sz
+= cescape_char(c
, outcome
+ sz
);
360 outcome
= strdup("");
372 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
374 _cleanup_free_
char *line
= NULL
;
386 p
= procfs_file_alloca(pid
, "stat");
387 r
= read_one_line_file(p
, &line
);
393 /* Let's skip the pid and comm fields. The latter is enclosed
394 * in () but does not escape any () in its value, so let's
395 * skip over it manually */
397 p
= strrchr(line
, ')');
409 if ((long unsigned) (pid_t
) ppid
!= ppid
)
412 *_ppid
= (pid_t
) ppid
;
417 int wait_for_terminate(pid_t pid
, siginfo_t
*status
) {
428 if (waitid(P_PID
, pid
, status
, WEXITED
) < 0) {
442 * < 0 : wait_for_terminate() failed to get the state of the
443 * process, the process was terminated by a signal, or
444 * failed for an unknown reason.
445 * >=0 : The process terminated normally, and its exit code is
448 * That is, success is indicated by a return value of zero, and an
449 * error is indicated by a non-zero value.
451 * A warning is emitted if the process terminates abnormally,
452 * and also if it returns non-zero unless check_exit_code is true.
454 int wait_for_terminate_and_warn(const char *name
, pid_t pid
, bool check_exit_code
) {
461 r
= wait_for_terminate(pid
, &status
);
463 return log_warning_errno(r
, "Failed to wait for %s: %m", name
);
465 if (status
.si_code
== CLD_EXITED
) {
466 if (status
.si_status
!= 0)
467 log_full(check_exit_code
? LOG_WARNING
: LOG_DEBUG
,
468 "%s failed with error code %i.", name
, status
.si_status
);
470 log_debug("%s succeeded.", name
);
472 return status
.si_status
;
473 } else if (status
.si_code
== CLD_KILLED
||
474 status
.si_code
== CLD_DUMPED
) {
476 log_warning("%s terminated by signal %s.", name
, signal_to_string(status
.si_status
));
480 log_warning("%s failed due to unknown reason.", name
);
484 void sigkill_wait(pid_t
*pid
) {
490 if (kill(*pid
, SIGKILL
) > 0)
491 (void) wait_for_terminate(*pid
, NULL
);
494 int kill_and_sigcont(pid_t pid
, int sig
) {
497 r
= kill(pid
, sig
) < 0 ? -errno
: 0;
505 int getenv_for_pid(pid_t pid
, const char *field
, char **_value
) {
506 _cleanup_fclose_
FILE *f
= NULL
;
517 path
= procfs_file_alloca(pid
, "environ");
519 f
= fopen(path
, "re");
533 for (i
= 0; i
< sizeof(line
)-1; i
++) {
537 if (_unlikely_(c
== EOF
)) {
547 if (memcmp(line
, field
, l
) == 0 && line
[l
] == '=') {
548 value
= strdup(line
+ l
+ 1);
562 bool pid_is_unwaited(pid_t pid
) {
563 /* Checks whether a PID is still valid at all, including a zombie */
568 if (kill(pid
, 0) >= 0)
571 return errno
!= ESRCH
;
574 bool pid_is_alive(pid_t pid
) {
577 /* Checks whether a PID is still valid and not a zombie */
582 r
= get_process_state(pid
);
583 if (r
== -ESRCH
|| r
== 'Z')