]>
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/>.
21 #include <sys/types.h>
34 #include "signal-util.h"
35 #include "process-util.h"
37 int get_process_state(pid_t pid
) {
41 _cleanup_free_
char *line
= NULL
;
45 p
= procfs_file_alloca(pid
, "stat");
47 r
= read_one_line_file(p
, &line
);
53 p
= strrchr(line
, ')');
59 if (sscanf(p
, " %c", &state
) != 1)
62 return (unsigned char) state
;
65 int get_process_comm(pid_t pid
, char **name
) {
72 p
= procfs_file_alloca(pid
, "comm");
74 r
= read_one_line_file(p
, name
);
81 int get_process_cmdline(pid_t pid
, size_t max_length
, bool comm_fallback
, char **line
) {
82 _cleanup_fclose_
FILE *f
= NULL
;
90 p
= procfs_file_alloca(pid
, "cmdline");
99 if (max_length
== 0) {
100 size_t len
= 0, allocated
= 0;
102 while ((c
= getc(f
)) != EOF
) {
104 if (!GREEDY_REALLOC(r
, allocated
, len
+2)) {
109 r
[len
++] = isprint(c
) ? c
: ' ';
119 r
= new(char, max_length
);
125 while ((c
= getc(f
)) != EOF
) {
147 size_t n
= MIN(left
-1, 3U);
154 /* Kernel threads have no argv[] */
156 _cleanup_free_
char *t
= NULL
;
164 h
= get_process_comm(pid
, &t
);
168 r
= strjoin("[", t
, "]", NULL
);
177 int is_kernel_thread(pid_t pid
) {
184 if (pid
== 0 || pid
== 1) /* pid 1, and we ourselves certainly aren't a kernel thread */
189 p
= procfs_file_alloca(pid
, "cmdline");
197 count
= fread(&c
, 1, 1, f
);
201 /* Kernel threads have an empty cmdline */
204 return eof
? 1 : -errno
;
209 int get_process_capeff(pid_t pid
, char **capeff
) {
216 p
= procfs_file_alloca(pid
, "status");
218 r
= get_status_field(p
, "\nCapEff:", capeff
);
225 static int get_process_link_contents(const char *proc_file
, char **name
) {
231 r
= readlink_malloc(proc_file
, name
);
240 int get_process_exe(pid_t pid
, char **name
) {
247 p
= procfs_file_alloca(pid
, "exe");
248 r
= get_process_link_contents(p
, name
);
252 d
= endswith(*name
, " (deleted)");
259 static int get_process_id(pid_t pid
, const char *field
, uid_t
*uid
) {
260 _cleanup_fclose_
FILE *f
= NULL
;
270 p
= procfs_file_alloca(pid
, "status");
278 FOREACH_LINE(line
, f
, return -errno
) {
283 if (startswith(l
, field
)) {
285 l
+= strspn(l
, WHITESPACE
);
287 l
[strcspn(l
, WHITESPACE
)] = 0;
289 return parse_uid(l
, uid
);
296 int get_process_uid(pid_t pid
, uid_t
*uid
) {
297 return get_process_id(pid
, "Uid:", uid
);
300 int get_process_gid(pid_t pid
, gid_t
*gid
) {
301 assert_cc(sizeof(uid_t
) == sizeof(gid_t
));
302 return get_process_id(pid
, "Gid:", gid
);
305 int get_process_cwd(pid_t pid
, char **cwd
) {
310 p
= procfs_file_alloca(pid
, "cwd");
312 return get_process_link_contents(p
, cwd
);
315 int get_process_root(pid_t pid
, char **root
) {
320 p
= procfs_file_alloca(pid
, "root");
322 return get_process_link_contents(p
, root
);
325 int get_process_environ(pid_t pid
, char **env
) {
326 _cleanup_fclose_
FILE *f
= NULL
;
327 _cleanup_free_
char *outcome
= NULL
;
330 size_t allocated
= 0, sz
= 0;
335 p
= procfs_file_alloca(pid
, "environ");
344 while ((c
= fgetc(f
)) != EOF
) {
345 if (!GREEDY_REALLOC(outcome
, allocated
, sz
+ 5))
349 outcome
[sz
++] = '\n';
351 sz
+= cescape_char(c
, outcome
+ sz
);
355 outcome
= strdup("");
367 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
369 _cleanup_free_
char *line
= NULL
;
381 p
= procfs_file_alloca(pid
, "stat");
382 r
= read_one_line_file(p
, &line
);
388 /* Let's skip the pid and comm fields. The latter is enclosed
389 * in () but does not escape any () in its value, so let's
390 * skip over it manually */
392 p
= strrchr(line
, ')');
404 if ((long unsigned) (pid_t
) ppid
!= ppid
)
407 *_ppid
= (pid_t
) ppid
;
412 int wait_for_terminate(pid_t pid
, siginfo_t
*status
) {
423 if (waitid(P_PID
, pid
, status
, WEXITED
) < 0) {
437 * < 0 : wait_for_terminate() failed to get the state of the
438 * process, the process was terminated by a signal, or
439 * failed for an unknown reason.
440 * >=0 : The process terminated normally, and its exit code is
443 * That is, success is indicated by a return value of zero, and an
444 * error is indicated by a non-zero value.
446 * A warning is emitted if the process terminates abnormally,
447 * and also if it returns non-zero unless check_exit_code is true.
449 int wait_for_terminate_and_warn(const char *name
, pid_t pid
, bool check_exit_code
) {
456 r
= wait_for_terminate(pid
, &status
);
458 return log_warning_errno(r
, "Failed to wait for %s: %m", name
);
460 if (status
.si_code
== CLD_EXITED
) {
461 if (status
.si_status
!= 0)
462 log_full(check_exit_code
? LOG_WARNING
: LOG_DEBUG
,
463 "%s failed with error code %i.", name
, status
.si_status
);
465 log_debug("%s succeeded.", name
);
467 return status
.si_status
;
468 } else if (status
.si_code
== CLD_KILLED
||
469 status
.si_code
== CLD_DUMPED
) {
471 log_warning("%s terminated by signal %s.", name
, signal_to_string(status
.si_status
));
475 log_warning("%s failed due to unknown reason.", name
);
479 int kill_and_sigcont(pid_t pid
, int sig
) {
482 r
= kill(pid
, sig
) < 0 ? -errno
: 0;
490 int getenv_for_pid(pid_t pid
, const char *field
, char **_value
) {
491 _cleanup_fclose_
FILE *f
= NULL
;
502 path
= procfs_file_alloca(pid
, "environ");
504 f
= fopen(path
, "re");
518 for (i
= 0; i
< sizeof(line
)-1; i
++) {
522 if (_unlikely_(c
== EOF
)) {
532 if (memcmp(line
, field
, l
) == 0 && line
[l
] == '=') {
533 value
= strdup(line
+ l
+ 1);
547 bool pid_is_unwaited(pid_t pid
) {
548 /* Checks whether a PID is still valid at all, including a zombie */
553 if (kill(pid
, 0) >= 0)
556 return errno
!= ESRCH
;
559 bool pid_is_alive(pid_t pid
) {
562 /* Checks whether a PID is still valid and not a zombie */
567 r
= get_process_state(pid
);
568 if (r
== -ESRCH
|| r
== 'Z')