]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-process-util.c
shared: add process-util.[ch]
[thirdparty/systemd.git] / src / test / test-process-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5 Copyright 2013 Thomas H.P. Andersen
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25
26 #include "process-util.h"
27 #include "log.h"
28 #include "util.h"
29 #include "macro.h"
30 #include "virt.h"
31
32 static void test_get_process_comm(void) {
33 struct stat st;
34 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL;
35 _cleanup_free_ char *env = NULL;
36 pid_t e;
37 uid_t u;
38 gid_t g;
39 dev_t h;
40 int r;
41 pid_t me;
42
43 if (stat("/proc/1/comm", &st) == 0) {
44 assert_se(get_process_comm(1, &a) >= 0);
45 log_info("pid1 comm: '%s'", a);
46 } else {
47 log_warning("/proc/1/comm does not exist.");
48 }
49
50 assert_se(get_process_cmdline(1, 0, true, &c) >= 0);
51 log_info("pid1 cmdline: '%s'", c);
52
53 assert_se(get_process_cmdline(1, 8, false, &d) >= 0);
54 log_info("pid1 cmdline truncated: '%s'", d);
55
56 assert_se(get_parent_of_pid(1, &e) >= 0);
57 log_info("pid1 ppid: "PID_FMT, e);
58 assert_se(e == 0);
59
60 assert_se(is_kernel_thread(1) == 0);
61
62 r = get_process_exe(1, &f);
63 assert_se(r >= 0 || r == -EACCES);
64 log_info("pid1 exe: '%s'", strna(f));
65
66 assert_se(get_process_uid(1, &u) == 0);
67 log_info("pid1 uid: "UID_FMT, u);
68 assert_se(u == 0);
69
70 assert_se(get_process_gid(1, &g) == 0);
71 log_info("pid1 gid: "GID_FMT, g);
72 assert_se(g == 0);
73
74 me = getpid();
75
76 r = get_process_cwd(me, &cwd);
77 assert_se(r >= 0 || r == -EACCES);
78 log_info("pid1 cwd: '%s'", cwd);
79
80 r = get_process_root(me, &root);
81 assert_se(r >= 0 || r == -EACCES);
82 log_info("pid1 root: '%s'", root);
83
84 r = get_process_environ(me, &env);
85 assert_se(r >= 0 || r == -EACCES);
86 log_info("self strlen(environ): '%zu'", strlen(env));
87
88 if (!detect_container(NULL))
89 assert_se(get_ctty_devnr(1, &h) == -ENOENT);
90
91 getenv_for_pid(1, "PATH", &i);
92 log_info("pid1 $PATH: '%s'", strna(i));
93 }
94
95 static void test_pid_is_unwaited(void) {
96 pid_t pid;
97
98 pid = fork();
99 assert_se(pid >= 0);
100 if (pid == 0) {
101 _exit(EXIT_SUCCESS);
102 } else {
103 int status;
104
105 waitpid(pid, &status, 0);
106 assert_se(!pid_is_unwaited(pid));
107 }
108 assert_se(pid_is_unwaited(getpid()));
109 assert_se(!pid_is_unwaited(-1));
110 }
111
112 static void test_pid_is_alive(void) {
113 pid_t pid;
114
115 pid = fork();
116 assert_se(pid >= 0);
117 if (pid == 0) {
118 _exit(EXIT_SUCCESS);
119 } else {
120 int status;
121
122 waitpid(pid, &status, 0);
123 assert_se(!pid_is_alive(pid));
124 }
125 assert_se(pid_is_alive(getpid()));
126 assert_se(!pid_is_alive(-1));
127 }
128
129 int main(int argc, char *argv[]) {
130 log_parse_environment();
131 log_open();
132
133 test_get_process_comm();
134 test_pid_is_unwaited();
135 test_pid_is_alive();
136
137 return 0;
138 }