* hurd/Versions (libc: GLIBC_2.0): Add _S_catch_exception_raise_state,
[jlayton/glibc.git] / sysdeps / mach / hurd / getpriority.c
1 /* Copyright (C) 1994,95,96,97,2000,02 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, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 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, id_t 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           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;
57         }
58 #ifdef TASK_SCHED_TIMESHARE_INFO
59       if (!onerr && pip->timeshare_base_info.base_priority > maxpri)
60         maxpri = pip->timeshare_base_info.base_priority;
61 #else
62       if (!onerr && pip->taskinfo.base_priority > maxpri)
63         maxpri = pip->taskinfo.base_priority;
64 #endif
65       return 0;
66     }
67
68   onerr = 0;
69   err = _hurd_priority_which_map (which, who,
70                                   getonepriority, PI_FETCH_TASKINFO);
71
72   if (pi != pibuf)
73     __vm_deallocate (__mach_task_self (),
74                      (vm_address_t) pi, pisize * sizeof pi[0]);
75
76   if (!err && maxpri == INT_MIN)
77     /* No error, but no pids found.  */
78     err = onerr ?: ESRCH;
79
80   if (err)
81     return __hurd_fail (err);
82
83   return MACH_PRIORITY_TO_NICE (maxpri);
84 }