]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/hurdprio.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / hurd / hurdprio.c
CommitLineData
28f540f4 1/* Support code for dealing with priorities in the Hurd.
04277e02 2 Copyright (C) 1994-2019 Free Software Foundation, Inc.
c84142e8
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
c84142e8
UD
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
c84142e8 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
28f540f4 18
28f540f4 19#include <hurd.h>
aefc5849
RM
20#include <hurd/resource.h>
21#include <sys/mman.h>
f7db9ce5 22#include <unistd.h>
28f540f4
RM
23
24error_t
25_hurd_priority_which_map (enum __priority_which which, int who,
80b55d32
RM
26 error_t (*function) (pid_t, struct procinfo *),
27 int pi_flags)
28f540f4
RM
28{
29 mach_msg_type_number_t npids = 64, i;
70aabf75 30 pid_t pidbuf[npids], *pids = pidbuf;
28f540f4
RM
31 error_t err;
32 struct procinfo *pip;
33 int pibuf[sizeof *pip + 5 * sizeof (pip->threadinfos[0])], *pi = pibuf;
34 mach_msg_type_number_t pisize = sizeof (pibuf) / sizeof (int);
35
36 switch (which)
37 {
aefc5849
RM
38 default:
39 return EINVAL;
40
28f540f4 41 case PRIO_PROCESS:
aefc5849 42 err = (*function) (who ?: getpid (), 0); /* XXX special-case self? */
28f540f4
RM
43 break;
44
45 case PRIO_PGRP:
46 err = __USEPORT (PROC, __proc_getpgrppids (port, who, &pids, &npids));
aefc5849
RM
47 for (i = 0; !err && i < npids; ++i)
48 err = (*function) (pids[i], 0);
28f540f4
RM
49 break;
50
51 case PRIO_USER:
1a658b79 52 if (who == 0)
dba2bdbe 53 who = __geteuid ();
28f540f4 54 err = __USEPORT (PROC, __proc_getallpids (port, &pids, &npids));
aefc5849 55 for (i = 0; !err && i < npids; ++i)
28f540f4
RM
56 {
57 /* Get procinfo to check the owner. */
58 int *oldpi = pi;
59 mach_msg_type_number_t oldpisize = pisize;
853f0eea
RM
60 char *tw = 0;
61 size_t twsz = 0;
aefc5849
RM
62 err = __USEPORT (PROC, __proc_getprocinfo (port, pids[i],
63 &pi_flags,
64 &pi, &pisize,
65 &tw, &twsz));
66 if (!err)
67 {
68 if (twsz) /* Gratuitous. */
69 __munmap (tw, twsz);
70 if (pi != oldpi && oldpi != pibuf)
71 /* Old buffer from last call was not reused; free it. */
72 __munmap (oldpi, oldpisize * sizeof pi[0]);
28f540f4 73
aefc5849
RM
74 pip = (struct procinfo *) pi;
75 if (pip->owner == (uid_t) who)
76 err = (*function) (pids[i], pip);
77 }
28f540f4 78 }
aefc5849 79 break;
28f540f4
RM
80 }
81
82 if (pids != pidbuf)
aefc5849 83 __munmap (pids, npids * sizeof pids[0]);
28f540f4 84 if (pi != pibuf)
aefc5849 85 __munmap (pi, pisize * sizeof pi[0]);
28f540f4
RM
86
87 return err;
88}