]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/setpriority.c
Tue Oct 31 00:07:29 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[thirdparty/glibc.git] / sysdeps / mach / hurd / setpriority.c
CommitLineData
28f540f4
RM
1/* Copyright (C) 1994 Free Software Foundation, Inc.
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
19#include <hurd.h>
20#include <hurd/resource.h>
21
22/* Set the priority of all processes specified by WHICH and WHO
23 to PRIO. Returns 0 on success, -1 on errors. */
24int
25setpriority (enum __priority_which which, int who, int prio)
26{
27 error_t err;
28 error_t pidloser, priloser;
29 unsigned int npids, ntasks, nwin, nperm, nacces;
30
31 error_t setonepriority (pid_t pid, struct procinfo *pi)
32 {
33 task_t task;
34 error_t piderr = __USEPORT (PROC, __proc_pid2task (port, pid, &task));
35 if (piderr == EPERM)
36 ++nperm;
37 if (piderr != ESRCH)
38 {
39 ++npids;
40 if (piderr && piderr != EPERM)
41 pidloser = piderr;
42 }
43 if (! piderr)
44 {
45 error_t prierr;
46 ++ntasks;
47 prierr = __task_priority (task, NICE_TO_MACH_PRIORITY (prio), 1);
48 __mach_port_deallocate (__mach_task_self (), task);
49 switch (prierr)
50 {
51 case KERN_FAILURE:
52 ++nacces;
53 break;
54 case KERN_SUCCESS:
55 ++nwin;
56 break;
57 case KERN_INVALID_ARGUMENT: /* Task died. */
58 --npids;
59 --ntasks;
60 break;
61 default:
62 priloser = prierr;
63 }
64 }
65 return 0;
66 }
67
68 npids = ntasks = nwin = nperm = nacces = 0;
69 pidloser = priloser = 0;
70 err = _hurd_priority_which_map (which, who, setonepriority);
71
72 if (!err && npids == 0)
73 /* No error, but no pids found. */
74 err = ESRCH;
75 else if (nperm == npids)
76 /* Got EPERM from proc_task2pid for every process. */
77 err = EPERM;
78 else if (nacces == ntasks)
79 /* Got KERN_FAILURE from task_priority for every task. */
80 err = EACCES;
81 else if (nwin == 0)
82 err = pidloser ?: priloser;
83
84 return err ? __hurd_fail (err) : 0;
85}