]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-process-util.c
Merge pull request #3162 from keszybz/alias-refusal
[thirdparty/systemd.git] / src / test / test-process-util.c
CommitLineData
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
26fbedd7 21#include <sys/personality.h>
0b452006 22#include <sys/stat.h>
07630cea 23#include <sys/types.h>
0b452006
RC
24#include <sys/wait.h>
25#include <unistd.h>
26
b5efdb8a 27#include "alloc-util.h"
26fbedd7 28#include "architecture.h"
0b452006 29#include "log.h"
0b452006 30#include "macro.h"
07630cea
LP
31#include "process-util.h"
32#include "string-util.h"
288a74cc 33#include "terminal-util.h"
07630cea
LP
34#include "util.h"
35#include "virt.h"
0b452006
RC
36
37static void test_get_process_comm(void) {
38 struct stat st;
39 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL;
40 _cleanup_free_ char *env = NULL;
41 pid_t e;
42 uid_t u;
43 gid_t g;
44 dev_t h;
45 int r;
46 pid_t me;
47
48 if (stat("/proc/1/comm", &st) == 0) {
49 assert_se(get_process_comm(1, &a) >= 0);
50 log_info("pid1 comm: '%s'", a);
cfeaa44a 51 } else
0b452006 52 log_warning("/proc/1/comm does not exist.");
0b452006
RC
53
54 assert_se(get_process_cmdline(1, 0, true, &c) >= 0);
55 log_info("pid1 cmdline: '%s'", c);
56
57 assert_se(get_process_cmdline(1, 8, false, &d) >= 0);
58 log_info("pid1 cmdline truncated: '%s'", d);
59
6bc73acb 60 assert_se(get_process_ppid(1, &e) >= 0);
0b452006
RC
61 log_info("pid1 ppid: "PID_FMT, e);
62 assert_se(e == 0);
63
64 assert_se(is_kernel_thread(1) == 0);
65
66 r = get_process_exe(1, &f);
67 assert_se(r >= 0 || r == -EACCES);
68 log_info("pid1 exe: '%s'", strna(f));
69
70 assert_se(get_process_uid(1, &u) == 0);
71 log_info("pid1 uid: "UID_FMT, u);
72 assert_se(u == 0);
73
74 assert_se(get_process_gid(1, &g) == 0);
75 log_info("pid1 gid: "GID_FMT, g);
76 assert_se(g == 0);
77
78 me = getpid();
79
80 r = get_process_cwd(me, &cwd);
81 assert_se(r >= 0 || r == -EACCES);
82 log_info("pid1 cwd: '%s'", cwd);
83
84 r = get_process_root(me, &root);
85 assert_se(r >= 0 || r == -EACCES);
86 log_info("pid1 root: '%s'", root);
87
88 r = get_process_environ(me, &env);
89 assert_se(r >= 0 || r == -EACCES);
90 log_info("self strlen(environ): '%zu'", strlen(env));
91
75f86906 92 if (!detect_container())
cfeaa44a 93 assert_se(get_ctty_devnr(1, &h) == -ENXIO);
0b452006
RC
94
95 getenv_for_pid(1, "PATH", &i);
96 log_info("pid1 $PATH: '%s'", strna(i));
97}
98
99static void test_pid_is_unwaited(void) {
100 pid_t pid;
101
102 pid = fork();
103 assert_se(pid >= 0);
104 if (pid == 0) {
105 _exit(EXIT_SUCCESS);
106 } else {
107 int status;
108
109 waitpid(pid, &status, 0);
110 assert_se(!pid_is_unwaited(pid));
111 }
112 assert_se(pid_is_unwaited(getpid()));
113 assert_se(!pid_is_unwaited(-1));
114}
115
116static void test_pid_is_alive(void) {
117 pid_t pid;
118
119 pid = fork();
120 assert_se(pid >= 0);
121 if (pid == 0) {
122 _exit(EXIT_SUCCESS);
123 } else {
124 int status;
125
126 waitpid(pid, &status, 0);
127 assert_se(!pid_is_alive(pid));
128 }
129 assert_se(pid_is_alive(getpid()));
130 assert_se(!pid_is_alive(-1));
131}
132
26fbedd7
LP
133static void test_personality(void) {
134
135 assert_se(personality_to_string(PER_LINUX));
136 assert_se(!personality_to_string(PERSONALITY_INVALID));
137
138 assert_se(streq(personality_to_string(PER_LINUX), architecture_to_string(native_architecture())));
139
140 assert_se(personality_from_string(personality_to_string(PER_LINUX)) == PER_LINUX);
141 assert_se(personality_from_string(architecture_to_string(native_architecture())) == PER_LINUX);
142
143#ifdef __x86_64__
144 assert_se(streq_ptr(personality_to_string(PER_LINUX), "x86-64"));
145 assert_se(streq_ptr(personality_to_string(PER_LINUX32), "x86"));
146
147 assert_se(personality_from_string("x86-64") == PER_LINUX);
148 assert_se(personality_from_string("x86") == PER_LINUX32);
149 assert_se(personality_from_string("ia64") == PERSONALITY_INVALID);
150 assert_se(personality_from_string(NULL) == PERSONALITY_INVALID);
151
152 assert_se(personality_from_string(personality_to_string(PER_LINUX32)) == PER_LINUX32);
153#endif
154}
155
0b452006
RC
156int main(int argc, char *argv[]) {
157 log_parse_environment();
158 log_open();
159
160 test_get_process_comm();
161 test_pid_is_unwaited();
162 test_pid_is_alive();
26fbedd7 163 test_personality();
0b452006
RC
164
165 return 0;
166}