]>
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"
40 int get_process_state(pid_t pid
) {
44 _cleanup_free_
char *line
= NULL
;
48 p
= procfs_file_alloca(pid
, "stat");
50 r
= read_one_line_file(p
, &line
);
56 p
= strrchr(line
, ')');
62 if (sscanf(p
, " %c", &state
) != 1)
65 return (unsigned char) state
;
68 int get_process_comm(pid_t pid
, char **name
) {
75 p
= procfs_file_alloca(pid
, "comm");
77 r
= read_one_line_file(p
, name
);
84 int get_process_cmdline(pid_t pid
, size_t max_length
, bool comm_fallback
, char **line
) {
85 _cleanup_fclose_
FILE *f
= NULL
;
93 p
= procfs_file_alloca(pid
, "cmdline");
102 if (max_length
== 0) {
103 size_t len
= 0, allocated
= 0;
105 while ((c
= getc(f
)) != EOF
) {
107 if (!GREEDY_REALLOC(r
, allocated
, len
+2)) {
112 r
[len
++] = isprint(c
) ? c
: ' ';
122 r
= new(char, max_length
);
128 while ((c
= getc(f
)) != EOF
) {
150 size_t n
= MIN(left
-1, 3U);
157 /* Kernel threads have no argv[] */
159 _cleanup_free_
char *t
= NULL
;
167 h
= get_process_comm(pid
, &t
);
171 r
= strjoin("[", t
, "]", NULL
);
180 int is_kernel_thread(pid_t pid
) {
187 if (pid
== 0 || pid
== 1) /* pid 1, and we ourselves certainly aren't a kernel thread */
192 p
= procfs_file_alloca(pid
, "cmdline");
200 count
= fread(&c
, 1, 1, f
);
204 /* Kernel threads have an empty cmdline */
207 return eof
? 1 : -errno
;
212 int get_process_capeff(pid_t pid
, char **capeff
) {
219 p
= procfs_file_alloca(pid
, "status");
221 r
= get_proc_field(p
, "CapEff", WHITESPACE
, capeff
);
228 static int get_process_link_contents(const char *proc_file
, char **name
) {
234 r
= readlink_malloc(proc_file
, name
);
243 int get_process_exe(pid_t pid
, char **name
) {
250 p
= procfs_file_alloca(pid
, "exe");
251 r
= get_process_link_contents(p
, name
);
255 d
= endswith(*name
, " (deleted)");
262 static int get_process_id(pid_t pid
, const char *field
, uid_t
*uid
) {
263 _cleanup_fclose_
FILE *f
= NULL
;
273 p
= procfs_file_alloca(pid
, "status");
281 FOREACH_LINE(line
, f
, return -errno
) {
286 if (startswith(l
, field
)) {
288 l
+= strspn(l
, WHITESPACE
);
290 l
[strcspn(l
, WHITESPACE
)] = 0;
292 return parse_uid(l
, uid
);
299 int get_process_uid(pid_t pid
, uid_t
*uid
) {
300 return get_process_id(pid
, "Uid:", uid
);
303 int get_process_gid(pid_t pid
, gid_t
*gid
) {
304 assert_cc(sizeof(uid_t
) == sizeof(gid_t
));
305 return get_process_id(pid
, "Gid:", gid
);
308 int get_process_cwd(pid_t pid
, char **cwd
) {
313 p
= procfs_file_alloca(pid
, "cwd");
315 return get_process_link_contents(p
, cwd
);
318 int get_process_root(pid_t pid
, char **root
) {
323 p
= procfs_file_alloca(pid
, "root");
325 return get_process_link_contents(p
, root
);
328 int get_process_environ(pid_t pid
, char **env
) {
329 _cleanup_fclose_
FILE *f
= NULL
;
330 _cleanup_free_
char *outcome
= NULL
;
333 size_t allocated
= 0, sz
= 0;
338 p
= procfs_file_alloca(pid
, "environ");
347 while ((c
= fgetc(f
)) != EOF
) {
348 if (!GREEDY_REALLOC(outcome
, allocated
, sz
+ 5))
352 outcome
[sz
++] = '\n';
354 sz
+= cescape_char(c
, outcome
+ sz
);
358 outcome
= strdup("");
370 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
372 _cleanup_free_
char *line
= NULL
;
384 p
= procfs_file_alloca(pid
, "stat");
385 r
= read_one_line_file(p
, &line
);
391 /* Let's skip the pid and comm fields. The latter is enclosed
392 * in () but does not escape any () in its value, so let's
393 * skip over it manually */
395 p
= strrchr(line
, ')');
407 if ((long unsigned) (pid_t
) ppid
!= ppid
)
410 *_ppid
= (pid_t
) ppid
;
415 int wait_for_terminate(pid_t pid
, siginfo_t
*status
) {
426 if (waitid(P_PID
, pid
, status
, WEXITED
) < 0) {
440 * < 0 : wait_for_terminate() failed to get the state of the
441 * process, the process was terminated by a signal, or
442 * failed for an unknown reason.
443 * >=0 : The process terminated normally, and its exit code is
446 * That is, success is indicated by a return value of zero, and an
447 * error is indicated by a non-zero value.
449 * A warning is emitted if the process terminates abnormally,
450 * and also if it returns non-zero unless check_exit_code is true.
452 int wait_for_terminate_and_warn(const char *name
, pid_t pid
, bool check_exit_code
) {
459 r
= wait_for_terminate(pid
, &status
);
461 return log_warning_errno(r
, "Failed to wait for %s: %m", name
);
463 if (status
.si_code
== CLD_EXITED
) {
464 if (status
.si_status
!= 0)
465 log_full(check_exit_code
? LOG_WARNING
: LOG_DEBUG
,
466 "%s failed with error code %i.", name
, status
.si_status
);
468 log_debug("%s succeeded.", name
);
470 return status
.si_status
;
471 } else if (status
.si_code
== CLD_KILLED
||
472 status
.si_code
== CLD_DUMPED
) {
474 log_warning("%s terminated by signal %s.", name
, signal_to_string(status
.si_status
));
478 log_warning("%s failed due to unknown reason.", name
);
482 int kill_and_sigcont(pid_t pid
, int sig
) {
485 r
= kill(pid
, sig
) < 0 ? -errno
: 0;
493 int getenv_for_pid(pid_t pid
, const char *field
, char **_value
) {
494 _cleanup_fclose_
FILE *f
= NULL
;
505 path
= procfs_file_alloca(pid
, "environ");
507 f
= fopen(path
, "re");
521 for (i
= 0; i
< sizeof(line
)-1; i
++) {
525 if (_unlikely_(c
== EOF
)) {
535 if (memcmp(line
, field
, l
) == 0 && line
[l
] == '=') {
536 value
= strdup(line
+ l
+ 1);
550 bool pid_is_unwaited(pid_t pid
) {
551 /* Checks whether a PID is still valid at all, including a zombie */
556 if (kill(pid
, 0) >= 0)
559 return errno
!= ESRCH
;
562 bool pid_is_alive(pid_t pid
) {
565 /* Checks whether a PID is still valid and not a zombie */
570 r
= get_process_state(pid
);
571 if (r
== -ESRCH
|| r
== 'Z')