Blame sysdeps/mach/hurd/getpriority.c

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 <limits.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/resource.h>
Packit 6c4009
Packit 6c4009
/* Return the highest priority of any process specified by WHICH and WHO
Packit 6c4009
   (see <sys/resource.h>); if WHO is zero, the current process, process group,
Packit 6c4009
   or user (as specified by WHO) is used.  A lower priority number means higher
Packit 6c4009
   priority.  Priorities range from PRIO_MIN to PRIO_MAX.  */
Packit 6c4009
int
Packit 6c4009
__getpriority (enum __priority_which which, id_t who)
Packit 6c4009
{
Packit 6c4009
  error_t err, onerr;
Packit 6c4009
  int maxpri = INT_MIN;
Packit 6c4009
  struct procinfo *pip;		/* Just for sizeof.  */
Packit 6c4009
  int pibuf[sizeof *pip + 2 * sizeof (pip->threadinfos[0])], *pi = pibuf;
Packit 6c4009
  size_t pisize = sizeof pibuf / sizeof pibuf[0];
Packit 6c4009
Packit 6c4009
  error_t getonepriority (pid_t pid, struct procinfo *pip)
Packit 6c4009
    {
Packit 6c4009
      if (pip)
Packit 6c4009
	onerr = 0;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  int *oldpi = pi;
Packit 6c4009
	  size_t oldpisize = pisize;
Packit 6c4009
	  char *tw = 0;
Packit 6c4009
	  size_t twsz = 0;
Packit 6c4009
	  int flags = PI_FETCH_TASKINFO;
Packit 6c4009
	  onerr = __USEPORT (PROC, __proc_getprocinfo (port, pid, &flags,
Packit 6c4009
						       &pi, &pisize,
Packit 6c4009
						       &tw, &twsz));
Packit 6c4009
	  if (twsz)
Packit 6c4009
	    __vm_deallocate (__mach_task_self (), (vm_address_t) tw, twsz);
Packit 6c4009
	  if (pi != oldpi && oldpi != pibuf)
Packit 6c4009
	    /* Old buffer from last call was not reused; free it.  */
Packit 6c4009
	    __vm_deallocate (__mach_task_self (),
Packit 6c4009
			     (vm_address_t) oldpi, oldpisize * sizeof pi[0]);
Packit 6c4009
	  pip = (struct procinfo *) pi;
Packit 6c4009
	}
Packit 6c4009
#ifdef TASK_SCHED_TIMESHARE_INFO
Packit 6c4009
      if (!onerr && pip->timeshare_base_info.base_priority > maxpri)
Packit 6c4009
	maxpri = pip->timeshare_base_info.base_priority;
Packit 6c4009
#else
Packit 6c4009
      if (!onerr && pip->taskinfo.base_priority > maxpri)
Packit 6c4009
	maxpri = pip->taskinfo.base_priority;
Packit 6c4009
#endif
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  onerr = 0;
Packit 6c4009
  err = _hurd_priority_which_map (which, who,
Packit 6c4009
				  getonepriority, PI_FETCH_TASKINFO);
Packit 6c4009
Packit 6c4009
  if (pi != pibuf)
Packit 6c4009
    __vm_deallocate (__mach_task_self (),
Packit 6c4009
		     (vm_address_t) pi, pisize * sizeof pi[0]);
Packit 6c4009
Packit 6c4009
  if (!err && maxpri == INT_MIN)
Packit 6c4009
    /* No error, but no pids found.  */
Packit 6c4009
    err = onerr ?: ESRCH;
Packit 6c4009
Packit 6c4009
  if (err)
Packit 6c4009
    return __hurd_fail (err);
Packit 6c4009
Packit 6c4009
  return MACH_PRIORITY_TO_NICE (maxpri);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__getpriority)
Packit 6c4009
weak_alias (__getpriority, getpriority)