hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame hurd/hurdprio.c

Packit 6c4009
/* Support code for dealing with priorities in the Hurd.
Packit 6c4009
   Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/resource.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
error_t
Packit 6c4009
_hurd_priority_which_map (enum __priority_which which, int who,
Packit 6c4009
			  error_t (*function) (pid_t, struct procinfo *),
Packit 6c4009
			  int pi_flags)
Packit 6c4009
{
Packit 6c4009
  mach_msg_type_number_t npids = 64, i;
Packit 6c4009
  pid_t pidbuf[npids], *pids = pidbuf;
Packit 6c4009
  error_t err;
Packit 6c4009
  struct procinfo *pip;
Packit 6c4009
  int pibuf[sizeof *pip + 5 * sizeof (pip->threadinfos[0])], *pi = pibuf;
Packit 6c4009
  mach_msg_type_number_t pisize = sizeof (pibuf) / sizeof (int);
Packit 6c4009
Packit 6c4009
  switch (which)
Packit 6c4009
    {
Packit 6c4009
    default:
Packit 6c4009
      return EINVAL;
Packit 6c4009
Packit 6c4009
    case PRIO_PROCESS:
Packit 6c4009
      err = (*function) (who ?: getpid (), 0); /* XXX special-case self? */
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case PRIO_PGRP:
Packit 6c4009
      err = __USEPORT (PROC, __proc_getpgrppids (port, who, &pids, &npids));
Packit 6c4009
      for (i = 0; !err && i < npids; ++i)
Packit 6c4009
	err = (*function) (pids[i], 0);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case PRIO_USER:
Packit 6c4009
      if (who == 0)
Packit 6c4009
	who = __geteuid ();
Packit 6c4009
      err = __USEPORT (PROC, __proc_getallpids (port, &pids, &npids));
Packit 6c4009
      for (i = 0; !err && i < npids; ++i)
Packit 6c4009
	{
Packit 6c4009
	  /* Get procinfo to check the owner.  */
Packit 6c4009
	  int *oldpi = pi;
Packit 6c4009
	  mach_msg_type_number_t oldpisize = pisize;
Packit 6c4009
	  char *tw = 0;
Packit 6c4009
	  size_t twsz = 0;
Packit 6c4009
	  err = __USEPORT (PROC, __proc_getprocinfo (port, pids[i],
Packit 6c4009
						     &pi_flags,
Packit 6c4009
						     &pi, &pisize,
Packit 6c4009
						     &tw, &twsz));
Packit 6c4009
	  if (!err)
Packit 6c4009
	    {
Packit 6c4009
	      if (twsz)		/* Gratuitous.  */
Packit 6c4009
		__munmap (tw, twsz);
Packit 6c4009
	      if (pi != oldpi && oldpi != pibuf)
Packit 6c4009
		/* Old buffer from last call was not reused; free it.  */
Packit 6c4009
		__munmap (oldpi, oldpisize * sizeof pi[0]);
Packit 6c4009
Packit 6c4009
	      pip = (struct procinfo *) pi;
Packit 6c4009
	      if (pip->owner == (uid_t) who)
Packit 6c4009
		err = (*function) (pids[i], pip);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pids != pidbuf)
Packit 6c4009
    __munmap (pids, npids * sizeof pids[0]);
Packit 6c4009
  if (pi != pibuf)
Packit 6c4009
    __munmap (pi, pisize * sizeof pi[0]);
Packit 6c4009
Packit 6c4009
  return err;
Packit 6c4009
}