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