]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/getpriority.c
initial import
[thirdparty/glibc.git] / sysdeps / mach / hurd / getpriority.c
1 /* Copyright (C) 1994 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
19 #include <limits.h>
20 #include <hurd.h>
21 #include <hurd/resource.h>
22
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)
29 {
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];
35
36 error_t getonepriority (pid_t pid, struct procinfo *pip)
37 {
38 if (pip)
39 onerr = 0;
40 else
41 {
42 int *oldpi = pi;
43 unsigned int oldpisize = pisize;
44 onerr = __USEPORT (PROC, __proc_getprocinfo (port,
45 pid,
46 &pi, &pisize));
47 if (pi != oldpi && oldpi != pibuf)
48 /* Old buffer from last call was not reused; free it. */
49 __vm_deallocate (__mach_task_self (),
50 (vm_address_t) oldpi, oldpisize * sizeof pi[0]);
51 pip = (struct procinfo *) pi;
52 }
53 if (!onerr && pip->taskinfo.base_priority > maxpri)
54 maxpri = pip->taskinfo.base_priority;
55 return 0;
56 }
57
58 onerr = 0;
59 err = _hurd_priority_which_map (which, who, getonepriority);
60
61 if (pi != pibuf)
62 __vm_deallocate (__mach_task_self (),
63 (vm_address_t) pi, pisize * sizeof pi[0]);
64
65 if (!err && maxpri == INT_MIN)
66 /* No error, but no pids found. */
67 err = onerr ?: ESRCH;
68
69 if (err)
70 return __hurd_fail (err);
71
72 return MACH_PRIORITY_TO_NICE (maxpri);
73 }