Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / getpriority.c
blobfab3f8ec75a5788e13709b0aa573cf288e0ab7e2
1 /* Copyright (C) 1994-2014 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <limits.h>
19 #include <hurd.h>
20 #include <hurd/resource.h>
22 /* Return the highest priority of any process specified by WHICH and WHO
23 (see <sys/resource.h>); if WHO is zero, the current process, process group,
24 or user (as specified by WHO) is used. A lower priority number means higher
25 priority. Priorities range from PRIO_MIN to PRIO_MAX. */
26 int
27 getpriority (enum __priority_which which, id_t who)
29 error_t err, onerr;
30 int maxpri = INT_MIN;
31 struct procinfo *pip; /* Just for sizeof. */
32 int pibuf[sizeof *pip + 2 * sizeof (pip->threadinfos[0])], *pi = pibuf;
33 size_t pisize = sizeof pibuf / sizeof pibuf[0];
35 error_t getonepriority (pid_t pid, struct procinfo *pip)
37 if (pip)
38 onerr = 0;
39 else
41 int *oldpi = pi;
42 size_t oldpisize = pisize;
43 char *tw = 0;
44 size_t twsz = 0;
45 int flags = PI_FETCH_TASKINFO;
46 onerr = __USEPORT (PROC, __proc_getprocinfo (port, pid, &flags,
47 &pi, &pisize,
48 &tw, &twsz));
49 if (twsz)
50 __vm_deallocate (__mach_task_self (), (vm_address_t) tw, twsz);
51 if (pi != oldpi && oldpi != pibuf)
52 /* Old buffer from last call was not reused; free it. */
53 __vm_deallocate (__mach_task_self (),
54 (vm_address_t) oldpi, oldpisize * sizeof pi[0]);
55 pip = (struct procinfo *) pi;
57 #ifdef TASK_SCHED_TIMESHARE_INFO
58 if (!onerr && pip->timeshare_base_info.base_priority > maxpri)
59 maxpri = pip->timeshare_base_info.base_priority;
60 #else
61 if (!onerr && pip->taskinfo.base_priority > maxpri)
62 maxpri = pip->taskinfo.base_priority;
63 #endif
64 return 0;
67 onerr = 0;
68 err = _hurd_priority_which_map (which, who,
69 getonepriority, PI_FETCH_TASKINFO);
71 if (pi != pibuf)
72 __vm_deallocate (__mach_task_self (),
73 (vm_address_t) pi, pisize * sizeof pi[0]);
75 if (!err && maxpri == INT_MIN)
76 /* No error, but no pids found. */
77 err = onerr ?: ESRCH;
79 if (err)
80 return __hurd_fail (err);
82 return MACH_PRIORITY_TO_NICE (maxpri);
84 libc_hidden_def (getpriority)