]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/process-util.c
util-lib: split out user/group/uid/gid calls into user-util.[ch]
[thirdparty/systemd.git] / src / basic / process-util.c
CommitLineData
0b452006
RC
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
0b452006 20#include <assert.h>
4f5dd394 21#include <ctype.h>
0b452006 22#include <errno.h>
0b452006 23#include <signal.h>
4f5dd394
LP
24#include <stdbool.h>
25#include <stdio.h>
26#include <string.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <unistd.h>
0b452006 30
4f5dd394 31#include "escape.h"
3ffd4af2 32#include "fd-util.h"
0b452006 33#include "fileio.h"
0b452006 34#include "log.h"
3ffd4af2 35#include "process-util.h"
24882e06 36#include "signal-util.h"
07630cea 37#include "string-util.h"
b1d4f8e1 38#include "user-util.h"
4f5dd394 39#include "util.h"
0b452006
RC
40
41int get_process_state(pid_t pid) {
42 const char *p;
43 char state;
44 int r;
45 _cleanup_free_ char *line = NULL;
46
47 assert(pid >= 0);
48
49 p = procfs_file_alloca(pid, "stat");
a644184a 50
0b452006 51 r = read_one_line_file(p, &line);
a644184a
LP
52 if (r == -ENOENT)
53 return -ESRCH;
0b452006
RC
54 if (r < 0)
55 return r;
56
57 p = strrchr(line, ')');
58 if (!p)
59 return -EIO;
60
61 p++;
62
63 if (sscanf(p, " %c", &state) != 1)
64 return -EIO;
65
66 return (unsigned char) state;
67}
68
69int get_process_comm(pid_t pid, char **name) {
70 const char *p;
71 int r;
72
73 assert(name);
74 assert(pid >= 0);
75
76 p = procfs_file_alloca(pid, "comm");
77
78 r = read_one_line_file(p, name);
79 if (r == -ENOENT)
80 return -ESRCH;
81
82 return r;
83}
84
85int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
86 _cleanup_fclose_ FILE *f = NULL;
87 char *r = NULL, *k;
88 const char *p;
89 int c;
90
91 assert(line);
92 assert(pid >= 0);
93
94 p = procfs_file_alloca(pid, "cmdline");
95
96 f = fopen(p, "re");
a644184a
LP
97 if (!f) {
98 if (errno == ENOENT)
99 return -ESRCH;
0b452006 100 return -errno;
a644184a 101 }
0b452006
RC
102
103 if (max_length == 0) {
104 size_t len = 0, allocated = 0;
105
106 while ((c = getc(f)) != EOF) {
107
108 if (!GREEDY_REALLOC(r, allocated, len+2)) {
109 free(r);
110 return -ENOMEM;
111 }
112
113 r[len++] = isprint(c) ? c : ' ';
114 }
115
116 if (len > 0)
117 r[len-1] = 0;
118
119 } else {
120 bool space = false;
121 size_t left;
122
123 r = new(char, max_length);
124 if (!r)
125 return -ENOMEM;
126
127 k = r;
128 left = max_length;
129 while ((c = getc(f)) != EOF) {
130
131 if (isprint(c)) {
132 if (space) {
133 if (left <= 4)
134 break;
135
136 *(k++) = ' ';
137 left--;
138 space = false;
139 }
140
141 if (left <= 4)
142 break;
143
144 *(k++) = (char) c;
145 left--;
146 } else
147 space = true;
148 }
149
150 if (left <= 4) {
151 size_t n = MIN(left-1, 3U);
152 memcpy(k, "...", n);
153 k[n] = 0;
154 } else
155 *k = 0;
156 }
157
158 /* Kernel threads have no argv[] */
159 if (isempty(r)) {
160 _cleanup_free_ char *t = NULL;
161 int h;
162
163 free(r);
164
165 if (!comm_fallback)
166 return -ENOENT;
167
168 h = get_process_comm(pid, &t);
169 if (h < 0)
170 return h;
171
172 r = strjoin("[", t, "]", NULL);
173 if (!r)
174 return -ENOMEM;
175 }
176
177 *line = r;
178 return 0;
179}
180
181int is_kernel_thread(pid_t pid) {
182 const char *p;
183 size_t count;
184 char c;
185 bool eof;
186 FILE *f;
187
a6149b93 188 if (pid == 0 || pid == 1) /* pid 1, and we ourselves certainly aren't a kernel thread */
0b452006
RC
189 return 0;
190
a6149b93 191 assert(pid > 1);
0b452006
RC
192
193 p = procfs_file_alloca(pid, "cmdline");
194 f = fopen(p, "re");
a644184a
LP
195 if (!f) {
196 if (errno == ENOENT)
197 return -ESRCH;
0b452006 198 return -errno;
a644184a 199 }
0b452006
RC
200
201 count = fread(&c, 1, 1, f);
202 eof = feof(f);
203 fclose(f);
204
205 /* Kernel threads have an empty cmdline */
206
207 if (count <= 0)
208 return eof ? 1 : -errno;
209
210 return 0;
211}
212
213int get_process_capeff(pid_t pid, char **capeff) {
214 const char *p;
a644184a 215 int r;
0b452006
RC
216
217 assert(capeff);
218 assert(pid >= 0);
219
220 p = procfs_file_alloca(pid, "status");
221
c4cd1d4d 222 r = get_proc_field(p, "CapEff", WHITESPACE, capeff);
a644184a
LP
223 if (r == -ENOENT)
224 return -ESRCH;
225
226 return r;
0b452006
RC
227}
228
229static int get_process_link_contents(const char *proc_file, char **name) {
230 int r;
231
232 assert(proc_file);
233 assert(name);
234
235 r = readlink_malloc(proc_file, name);
a644184a
LP
236 if (r == -ENOENT)
237 return -ESRCH;
0b452006 238 if (r < 0)
a644184a 239 return r;
0b452006
RC
240
241 return 0;
242}
243
244int get_process_exe(pid_t pid, char **name) {
245 const char *p;
246 char *d;
247 int r;
248
249 assert(pid >= 0);
250
251 p = procfs_file_alloca(pid, "exe");
252 r = get_process_link_contents(p, name);
253 if (r < 0)
254 return r;
255
256 d = endswith(*name, " (deleted)");
257 if (d)
258 *d = '\0';
259
260 return 0;
261}
262
263static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
264 _cleanup_fclose_ FILE *f = NULL;
265 char line[LINE_MAX];
266 const char *p;
267
268 assert(field);
269 assert(uid);
270
271 if (pid == 0)
272 return getuid();
273
274 p = procfs_file_alloca(pid, "status");
275 f = fopen(p, "re");
a644184a
LP
276 if (!f) {
277 if (errno == ENOENT)
278 return -ESRCH;
0b452006 279 return -errno;
a644184a 280 }
0b452006
RC
281
282 FOREACH_LINE(line, f, return -errno) {
283 char *l;
284
285 l = strstrip(line);
286
287 if (startswith(l, field)) {
288 l += strlen(field);
289 l += strspn(l, WHITESPACE);
290
291 l[strcspn(l, WHITESPACE)] = 0;
292
293 return parse_uid(l, uid);
294 }
295 }
296
297 return -EIO;
298}
299
300int get_process_uid(pid_t pid, uid_t *uid) {
301 return get_process_id(pid, "Uid:", uid);
302}
303
304int get_process_gid(pid_t pid, gid_t *gid) {
305 assert_cc(sizeof(uid_t) == sizeof(gid_t));
306 return get_process_id(pid, "Gid:", gid);
307}
308
309int get_process_cwd(pid_t pid, char **cwd) {
310 const char *p;
311
312 assert(pid >= 0);
313
314 p = procfs_file_alloca(pid, "cwd");
315
316 return get_process_link_contents(p, cwd);
317}
318
319int get_process_root(pid_t pid, char **root) {
320 const char *p;
321
322 assert(pid >= 0);
323
324 p = procfs_file_alloca(pid, "root");
325
326 return get_process_link_contents(p, root);
327}
328
329int get_process_environ(pid_t pid, char **env) {
330 _cleanup_fclose_ FILE *f = NULL;
331 _cleanup_free_ char *outcome = NULL;
332 int c;
333 const char *p;
334 size_t allocated = 0, sz = 0;
335
336 assert(pid >= 0);
337 assert(env);
338
339 p = procfs_file_alloca(pid, "environ");
340
341 f = fopen(p, "re");
a644184a
LP
342 if (!f) {
343 if (errno == ENOENT)
344 return -ESRCH;
0b452006 345 return -errno;
a644184a 346 }
0b452006
RC
347
348 while ((c = fgetc(f)) != EOF) {
349 if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
350 return -ENOMEM;
351
352 if (c == '\0')
353 outcome[sz++] = '\n';
354 else
355 sz += cescape_char(c, outcome + sz);
356 }
357
03c55bc0
LP
358 if (!outcome) {
359 outcome = strdup("");
360 if (!outcome)
361 return -ENOMEM;
362 } else
363 outcome[sz] = '\0';
de8763b6 364
0b452006
RC
365 *env = outcome;
366 outcome = NULL;
367
368 return 0;
369}
370
371int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
372 int r;
373 _cleanup_free_ char *line = NULL;
374 long unsigned ppid;
375 const char *p;
376
377 assert(pid >= 0);
378 assert(_ppid);
379
380 if (pid == 0) {
381 *_ppid = getppid();
382 return 0;
383 }
384
385 p = procfs_file_alloca(pid, "stat");
386 r = read_one_line_file(p, &line);
a644184a
LP
387 if (r == -ENOENT)
388 return -ESRCH;
0b452006
RC
389 if (r < 0)
390 return r;
391
392 /* Let's skip the pid and comm fields. The latter is enclosed
393 * in () but does not escape any () in its value, so let's
394 * skip over it manually */
395
396 p = strrchr(line, ')');
397 if (!p)
398 return -EIO;
399
400 p++;
401
402 if (sscanf(p, " "
403 "%*c " /* state */
404 "%lu ", /* ppid */
405 &ppid) != 1)
406 return -EIO;
407
408 if ((long unsigned) (pid_t) ppid != ppid)
409 return -ERANGE;
410
411 *_ppid = (pid_t) ppid;
412
413 return 0;
414}
415
416int wait_for_terminate(pid_t pid, siginfo_t *status) {
417 siginfo_t dummy;
418
419 assert(pid >= 1);
420
421 if (!status)
422 status = &dummy;
423
424 for (;;) {
425 zero(*status);
426
427 if (waitid(P_PID, pid, status, WEXITED) < 0) {
428
429 if (errno == EINTR)
430 continue;
431
432 return -errno;
433 }
434
435 return 0;
436 }
437}
438
439/*
440 * Return values:
441 * < 0 : wait_for_terminate() failed to get the state of the
442 * process, the process was terminated by a signal, or
443 * failed for an unknown reason.
444 * >=0 : The process terminated normally, and its exit code is
445 * returned.
446 *
447 * That is, success is indicated by a return value of zero, and an
448 * error is indicated by a non-zero value.
449 *
450 * A warning is emitted if the process terminates abnormally,
451 * and also if it returns non-zero unless check_exit_code is true.
452 */
453int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) {
454 int r;
455 siginfo_t status;
456
457 assert(name);
458 assert(pid > 1);
459
460 r = wait_for_terminate(pid, &status);
461 if (r < 0)
462 return log_warning_errno(r, "Failed to wait for %s: %m", name);
463
464 if (status.si_code == CLD_EXITED) {
465 if (status.si_status != 0)
466 log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG,
467 "%s failed with error code %i.", name, status.si_status);
468 else
469 log_debug("%s succeeded.", name);
470
471 return status.si_status;
472 } else if (status.si_code == CLD_KILLED ||
473 status.si_code == CLD_DUMPED) {
474
475 log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
476 return -EPROTO;
477 }
478
479 log_warning("%s failed due to unknown reason.", name);
480 return -EPROTO;
481}
482
483int kill_and_sigcont(pid_t pid, int sig) {
484 int r;
485
486 r = kill(pid, sig) < 0 ? -errno : 0;
487
488 if (r >= 0)
489 kill(pid, SIGCONT);
490
491 return r;
492}
493
494int getenv_for_pid(pid_t pid, const char *field, char **_value) {
495 _cleanup_fclose_ FILE *f = NULL;
496 char *value = NULL;
497 int r;
498 bool done = false;
499 size_t l;
500 const char *path;
501
502 assert(pid >= 0);
503 assert(field);
504 assert(_value);
505
506 path = procfs_file_alloca(pid, "environ");
507
508 f = fopen(path, "re");
a644184a
LP
509 if (!f) {
510 if (errno == ENOENT)
511 return -ESRCH;
0b452006 512 return -errno;
a644184a 513 }
0b452006
RC
514
515 l = strlen(field);
516 r = 0;
517
518 do {
519 char line[LINE_MAX];
520 unsigned i;
521
522 for (i = 0; i < sizeof(line)-1; i++) {
523 int c;
524
525 c = getc(f);
526 if (_unlikely_(c == EOF)) {
527 done = true;
528 break;
529 } else if (c == 0)
530 break;
531
532 line[i] = c;
533 }
534 line[i] = 0;
535
536 if (memcmp(line, field, l) == 0 && line[l] == '=') {
537 value = strdup(line + l + 1);
538 if (!value)
539 return -ENOMEM;
540
541 r = 1;
542 break;
543 }
544
545 } while (!done);
546
547 *_value = value;
548 return r;
549}
550
551bool pid_is_unwaited(pid_t pid) {
552 /* Checks whether a PID is still valid at all, including a zombie */
553
554 if (pid <= 0)
555 return false;
556
557 if (kill(pid, 0) >= 0)
558 return true;
559
560 return errno != ESRCH;
561}
562
563bool pid_is_alive(pid_t pid) {
564 int r;
565
566 /* Checks whether a PID is still valid and not a zombie */
567
568 if (pid <= 0)
569 return false;
570
571 r = get_process_state(pid);
a644184a 572 if (r == -ESRCH || r == 'Z')
0b452006
RC
573 return false;
574
575 return true;
576}