]>
Commit | Line | Data |
---|---|---|
dfe9aa91 AZ |
1 | /* Wait for process state. |
2 | Copyright (C) 2020 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <https://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <stdlib.h> | |
20 | #include <time.h> | |
21 | #include <string.h> | |
22 | ||
23 | #include <array_length.h> | |
24 | ||
25 | #include <support/process_state.h> | |
26 | #include <support/xstdio.h> | |
27 | #include <support/check.h> | |
28 | ||
29 | void | |
30 | support_process_state_wait (pid_t pid, enum support_process_state state) | |
31 | { | |
32 | #ifdef __linux__ | |
33 | /* For Linux it does a polling check on /proc/<pid>/status checking on | |
34 | third field. */ | |
35 | ||
36 | /* It mimics the kernel states from fs/proc/array.c */ | |
37 | static const struct process_states | |
38 | { | |
39 | enum support_process_state s; | |
40 | char v; | |
41 | } process_states[] = { | |
42 | { support_process_state_running, 'R' }, | |
43 | { support_process_state_sleeping, 'S' }, | |
44 | { support_process_state_disk_sleep, 'D' }, | |
45 | { support_process_state_stopped, 'T' }, | |
46 | { support_process_state_tracing_stop, 't' }, | |
47 | { support_process_state_dead, 'X' }, | |
48 | { support_process_state_zombie, 'Z' }, | |
49 | { support_process_state_parked, 'P' }, | |
50 | }; | |
51 | ||
52 | char spath[sizeof ("/proc/" + 3) * sizeof (pid_t) + sizeof ("/status") + 1]; | |
53 | snprintf (spath, sizeof (spath), "/proc/%i/status", pid); | |
54 | ||
55 | FILE *fstatus = xfopen (spath, "r"); | |
56 | char *line = NULL; | |
57 | size_t linesiz = 0; | |
58 | ||
59 | for (;;) | |
60 | { | |
61 | char cur_state = -1; | |
cea56af1 | 62 | while (xgetline (&line, &linesiz, fstatus) > 0) |
dfe9aa91 AZ |
63 | if (strncmp (line, "State:", strlen ("State:")) == 0) |
64 | { | |
65 | TEST_COMPARE (sscanf (line, "%*s %c", &cur_state), 1); | |
66 | break; | |
67 | } | |
68 | /* Fallback to nanosleep for invalid state. */ | |
69 | if (cur_state == -1) | |
70 | break; | |
71 | ||
72 | for (size_t i = 0; i < array_length (process_states); ++i) | |
73 | if (state & process_states[i].s && cur_state == process_states[i].v) | |
74 | { | |
75 | free (line); | |
76 | xfclose (fstatus); | |
77 | return; | |
78 | } | |
79 | ||
80 | rewind (fstatus); | |
81 | fflush (fstatus); | |
82 | ||
83 | if (nanosleep (&(struct timespec) { 0, 10000000 }, NULL) != 0) | |
84 | FAIL_EXIT1 ("nanosleep: %m"); | |
85 | } | |
86 | ||
87 | free (line); | |
88 | xfclose (fstatus); | |
89 | /* Fallback to nanosleep if an invalid state is found. */ | |
90 | #endif | |
f9c3cf28 | 91 | nanosleep (&(struct timespec) { 1, 0 }, NULL); |
dfe9aa91 | 92 | } |