*** empty log message ***
[glibc.git] / sysdeps / mach / hurd / getpriority.c
blob769e2bea80104cc9f02f37548dd46b52687de10e
1 /* Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <limits.h>
20 #include <hurd.h>
21 #include <hurd/resource.h>
23 /* Return the highest priority of any process specified by WHICH and WHO
24 (see <sys/resource.h>); if WHO is zero, the current process, process group,
25 or user (as specified by WHO) is used. A lower priority number means higher
26 priority. Priorities range from PRIO_MIN to PRIO_MAX. */
27 int
28 getpriority (enum __priority_which which, int who)
30 error_t err, onerr;
31 int maxpri = INT_MIN;
32 struct procinfo *pip; /* Just for sizeof. */
33 int pibuf[sizeof *pip + 2 * sizeof (pip->threadinfos[0])], *pi = pibuf;
34 unsigned int pisize = sizeof pibuf / sizeof pibuf[0];
36 error_t getonepriority (pid_t pid, struct procinfo *pip)
38 if (pip)
39 onerr = 0;
40 else
42 int *oldpi = pi;
43 unsigned int oldpisize = pisize;
44 char *tw = 0;
45 size_t twsz = 0;
46 int flags = PI_FETCH_TASKINFO;
47 onerr = __USEPORT (PROC, __proc_getprocinfo (port, pid, &flags,
48 &pi, &pisize,
49 &tw, &twsz));
50 if (twsz)
51 __vm_deallocate (__mach_task_self (), (vm_address_t) tw, twsz);
52 if (pi != oldpi && oldpi != pibuf)
53 /* Old buffer from last call was not reused; free it. */
54 __vm_deallocate (__mach_task_self (),
55 (vm_address_t) oldpi, oldpisize * sizeof pi[0]);
56 pip = (struct procinfo *) pi;
58 if (!onerr && pip->taskinfo.base_priority > maxpri)
59 maxpri = pip->taskinfo.base_priority;
60 return 0;
63 onerr = 0;
64 err = _hurd_priority_which_map (which, who,
65 getonepriority, PI_FETCH_TASKINFO);
67 if (pi != pibuf)
68 __vm_deallocate (__mach_task_self (),
69 (vm_address_t) pi, pisize * sizeof pi[0]);
71 if (!err && maxpri == INT_MIN)
72 /* No error, but no pids found. */
73 err = onerr ?: ESRCH;
75 if (err)
76 return __hurd_fail (err);
78 return MACH_PRIORITY_TO_NICE (maxpri);