|
Packit Service |
7f6d86 |
/* Wait for process state.
|
|
Packit Service |
7f6d86 |
Copyright (C) 2020 Free Software Foundation, Inc.
|
|
Packit Service |
7f6d86 |
This file is part of the GNU C Library.
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
7f6d86 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
7f6d86 |
License as published by the Free Software Foundation; either
|
|
Packit Service |
7f6d86 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
7f6d86 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
7f6d86 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
7f6d86 |
Lesser General Public License for more details.
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
7f6d86 |
License along with the GNU C Library; if not, see
|
|
Packit Service |
7f6d86 |
<https://www.gnu.org/licenses/>. */
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
#include <stdlib.h>
|
|
Packit Service |
7f6d86 |
#include <time.h>
|
|
Packit Service |
7f6d86 |
#include <string.h>
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
#include <array_length.h>
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
#include <support/process_state.h>
|
|
Packit Service |
7f6d86 |
#include <support/xstdio.h>
|
|
Packit Service |
7f6d86 |
#include <support/check.h>
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
void
|
|
Packit Service |
7f6d86 |
support_process_state_wait (pid_t pid, enum support_process_state state)
|
|
Packit Service |
7f6d86 |
{
|
|
Packit Service |
7f6d86 |
#ifdef __linux__
|
|
Packit Service |
7f6d86 |
/* For Linux it does a polling check on /proc/<pid>/status checking on
|
|
Packit Service |
7f6d86 |
third field. */
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
/* It mimics the kernel states from fs/proc/array.c */
|
|
Packit Service |
7f6d86 |
static const struct process_states
|
|
Packit Service |
7f6d86 |
{
|
|
Packit Service |
7f6d86 |
enum support_process_state s;
|
|
Packit Service |
7f6d86 |
char v;
|
|
Packit Service |
7f6d86 |
} process_states[] = {
|
|
Packit Service |
7f6d86 |
{ support_process_state_running, 'R' },
|
|
Packit Service |
7f6d86 |
{ support_process_state_sleeping, 'S' },
|
|
Packit Service |
7f6d86 |
{ support_process_state_disk_sleep, 'D' },
|
|
Packit Service |
7f6d86 |
{ support_process_state_stopped, 'T' },
|
|
Packit Service |
7f6d86 |
{ support_process_state_tracing_stop, 't' },
|
|
Packit Service |
7f6d86 |
{ support_process_state_dead, 'X' },
|
|
Packit Service |
7f6d86 |
{ support_process_state_zombie, 'Z' },
|
|
Packit Service |
7f6d86 |
{ support_process_state_parked, 'P' },
|
|
Packit Service |
7f6d86 |
};
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
char spath[sizeof ("/proc/" + 3) * sizeof (pid_t) + sizeof ("/status") + 1];
|
|
Packit Service |
7f6d86 |
snprintf (spath, sizeof (spath), "/proc/%i/status", pid);
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
FILE *fstatus = xfopen (spath, "r");
|
|
Packit Service |
7f6d86 |
char *line = NULL;
|
|
Packit Service |
7f6d86 |
size_t linesiz = 0;
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
for (;;)
|
|
Packit Service |
7f6d86 |
{
|
|
Packit Service |
7f6d86 |
char cur_state = -1;
|
|
Packit Service |
7f6d86 |
while (xgetline (&line, &linesiz, fstatus) != -1)
|
|
Packit Service |
7f6d86 |
if (strncmp (line, "State:", strlen ("State:")) == 0)
|
|
Packit Service |
7f6d86 |
{
|
|
Packit Service |
7f6d86 |
TEST_COMPARE (sscanf (line, "%*s %c", &cur_state), 1);
|
|
Packit Service |
7f6d86 |
break;
|
|
Packit Service |
7f6d86 |
}
|
|
Packit Service |
7f6d86 |
/* Fallback to nanosleep for invalid state. */
|
|
Packit Service |
7f6d86 |
if (cur_state == -1)
|
|
Packit Service |
7f6d86 |
break;
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
for (size_t i = 0; i < array_length (process_states); ++i)
|
|
Packit Service |
7f6d86 |
if (state & process_states[i].s && cur_state == process_states[i].v)
|
|
Packit Service |
7f6d86 |
{
|
|
Packit Service |
7f6d86 |
free (line);
|
|
Packit Service |
7f6d86 |
xfclose (fstatus);
|
|
Packit Service |
7f6d86 |
return;
|
|
Packit Service |
7f6d86 |
}
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
rewind (fstatus);
|
|
Packit Service |
7f6d86 |
fflush (fstatus);
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
if (nanosleep (&(struct timespec) { 0, 10000000 }, NULL) != 0)
|
|
Packit Service |
7f6d86 |
FAIL_EXIT1 ("nanosleep: %m");
|
|
Packit Service |
7f6d86 |
}
|
|
Packit Service |
7f6d86 |
|
|
Packit Service |
7f6d86 |
free (line);
|
|
Packit Service |
7f6d86 |
xfclose (fstatus);
|
|
Packit Service |
7f6d86 |
/* Fallback to nanosleep if an invalid state is found. */
|
|
Packit Service |
7f6d86 |
#endif
|
|
Packit Service |
7f6d86 |
nanosleep (&(struct timespec) { 2, 0 }, NULL);
|
|
Packit Service |
7f6d86 |
}
|