1/* Copyright (C) 1994-2022 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 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.
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 Lesser General Public License for more details.
13
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 <https://www.gnu.org/licenses/>. */
17
18#include <limits.h>
19#include <hurd.h>
20#include <hurd/resource.h>
21
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. */
26int
27__getpriority (enum __priority_which which, id_t who)
28{
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];
34
35 error_t getonepriority (pid_t pid, struct procinfo *pip)
36 {
37 if (pip)
38 onerr = 0;
39 else
40 {
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;
56 }
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;
65 }
66
67 onerr = 0;
68 err = _hurd_priority_which_map (which, who,
69 getonepriority, PI_FETCH_TASKINFO);
70
71 if (pi != pibuf)
72 __vm_deallocate (__mach_task_self (),
73 (vm_address_t) pi, pisize * sizeof pi[0]);
74
75 if (!err && maxpri == INT_MIN)
76 /* No error, but no pids found. */
77 err = onerr ?: ESRCH;
78
79 if (err)
80 return __hurd_fail (err);
81
82 return MACH_PRIORITY_TO_NICE (maxpri);
83}
84libc_hidden_def (__getpriority)
85weak_alias (__getpriority, getpriority)
86

source code of glibc/sysdeps/mach/hurd/getpriority.c