]>
Commit | Line | Data |
---|---|---|
0b452006 RC |
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 | ||
0b452006 | 21 | #include <sys/stat.h> |
07630cea | 22 | #include <sys/types.h> |
0b452006 RC |
23 | #include <sys/wait.h> |
24 | #include <unistd.h> | |
25 | ||
0b452006 | 26 | #include "log.h" |
0b452006 | 27 | #include "macro.h" |
07630cea LP |
28 | #include "process-util.h" |
29 | #include "string-util.h" | |
288a74cc | 30 | #include "terminal-util.h" |
07630cea LP |
31 | #include "util.h" |
32 | #include "virt.h" | |
0b452006 RC |
33 | |
34 | static void test_get_process_comm(void) { | |
35 | struct stat st; | |
36 | _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL; | |
37 | _cleanup_free_ char *env = NULL; | |
38 | pid_t e; | |
39 | uid_t u; | |
40 | gid_t g; | |
41 | dev_t h; | |
42 | int r; | |
43 | pid_t me; | |
44 | ||
45 | if (stat("/proc/1/comm", &st) == 0) { | |
46 | assert_se(get_process_comm(1, &a) >= 0); | |
47 | log_info("pid1 comm: '%s'", a); | |
cfeaa44a | 48 | } else |
0b452006 | 49 | log_warning("/proc/1/comm does not exist."); |
0b452006 RC |
50 | |
51 | assert_se(get_process_cmdline(1, 0, true, &c) >= 0); | |
52 | log_info("pid1 cmdline: '%s'", c); | |
53 | ||
54 | assert_se(get_process_cmdline(1, 8, false, &d) >= 0); | |
55 | log_info("pid1 cmdline truncated: '%s'", d); | |
56 | ||
57 | assert_se(get_parent_of_pid(1, &e) >= 0); | |
58 | log_info("pid1 ppid: "PID_FMT, e); | |
59 | assert_se(e == 0); | |
60 | ||
61 | assert_se(is_kernel_thread(1) == 0); | |
62 | ||
63 | r = get_process_exe(1, &f); | |
64 | assert_se(r >= 0 || r == -EACCES); | |
65 | log_info("pid1 exe: '%s'", strna(f)); | |
66 | ||
67 | assert_se(get_process_uid(1, &u) == 0); | |
68 | log_info("pid1 uid: "UID_FMT, u); | |
69 | assert_se(u == 0); | |
70 | ||
71 | assert_se(get_process_gid(1, &g) == 0); | |
72 | log_info("pid1 gid: "GID_FMT, g); | |
73 | assert_se(g == 0); | |
74 | ||
75 | me = getpid(); | |
76 | ||
77 | r = get_process_cwd(me, &cwd); | |
78 | assert_se(r >= 0 || r == -EACCES); | |
79 | log_info("pid1 cwd: '%s'", cwd); | |
80 | ||
81 | r = get_process_root(me, &root); | |
82 | assert_se(r >= 0 || r == -EACCES); | |
83 | log_info("pid1 root: '%s'", root); | |
84 | ||
85 | r = get_process_environ(me, &env); | |
86 | assert_se(r >= 0 || r == -EACCES); | |
87 | log_info("self strlen(environ): '%zu'", strlen(env)); | |
88 | ||
75f86906 | 89 | if (!detect_container()) |
cfeaa44a | 90 | assert_se(get_ctty_devnr(1, &h) == -ENXIO); |
0b452006 RC |
91 | |
92 | getenv_for_pid(1, "PATH", &i); | |
93 | log_info("pid1 $PATH: '%s'", strna(i)); | |
94 | } | |
95 | ||
96 | static void test_pid_is_unwaited(void) { | |
97 | pid_t pid; | |
98 | ||
99 | pid = fork(); | |
100 | assert_se(pid >= 0); | |
101 | if (pid == 0) { | |
102 | _exit(EXIT_SUCCESS); | |
103 | } else { | |
104 | int status; | |
105 | ||
106 | waitpid(pid, &status, 0); | |
107 | assert_se(!pid_is_unwaited(pid)); | |
108 | } | |
109 | assert_se(pid_is_unwaited(getpid())); | |
110 | assert_se(!pid_is_unwaited(-1)); | |
111 | } | |
112 | ||
113 | static void test_pid_is_alive(void) { | |
114 | pid_t pid; | |
115 | ||
116 | pid = fork(); | |
117 | assert_se(pid >= 0); | |
118 | if (pid == 0) { | |
119 | _exit(EXIT_SUCCESS); | |
120 | } else { | |
121 | int status; | |
122 | ||
123 | waitpid(pid, &status, 0); | |
124 | assert_se(!pid_is_alive(pid)); | |
125 | } | |
126 | assert_se(pid_is_alive(getpid())); | |
127 | assert_se(!pid_is_alive(-1)); | |
128 | } | |
129 | ||
130 | int main(int argc, char *argv[]) { | |
131 | log_parse_environment(); | |
132 | log_open(); | |
133 | ||
134 | test_get_process_comm(); | |
135 | test_pid_is_unwaited(); | |
136 | test_pid_is_alive(); | |
137 | ||
138 | return 0; | |
139 | } |