Blame support/support_process_state.c

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