Blame support/support_process_state.c

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