Upload Tizen:Base source
[external/eglibc.git] / sysdeps / mach / hurd / setpriority.c
1 /* Copyright (C) 1994,95,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 <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.  */
24 int
25 setpriority (enum __priority_which which, id_t 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 #ifdef POLICY_TIMESHARE_BASE_COUNT
48           {
49             /* XXX This assumes timeshare policy.  */
50             struct policy_timeshare_base base
51               = { NICE_TO_MACH_PRIORITY (prio) };
52             prierr = __task_policy (task, POLICY_TIMESHARE,
53                                     (policy_base_t) &base,
54                                     POLICY_TIMESHARE_BASE_COUNT,
55                                     0, 1);
56           }
57 #else
58           prierr = __task_priority (task, NICE_TO_MACH_PRIORITY (prio), 1);
59 #endif
60           __mach_port_deallocate (__mach_task_self (), task);
61           switch (prierr)
62             {
63             case KERN_FAILURE:
64               ++nacces;
65               break;
66             case KERN_SUCCESS:
67               ++nwin;
68               break;
69             case KERN_INVALID_ARGUMENT: /* Task died.  */
70               --npids;
71               --ntasks;
72               break;
73             default:
74               priloser = prierr;
75             }
76         }
77       return 0;
78     }
79
80   npids = ntasks = nwin = nperm = nacces = 0;
81   pidloser = priloser = 0;
82   err = _hurd_priority_which_map (which, who, setonepriority, 0);
83
84   if (!err && npids == 0)
85     /* No error, but no pids found.  */
86     err = ESRCH;
87   else if (nperm == npids)
88     /* Got EPERM from proc_task2pid for every process.  */
89     err = EPERM;
90   else if (nacces == ntasks)
91     /* Got KERN_FAILURE from task_priority for every task.  */
92     err = EACCES;
93   else if (nwin == 0)
94     err = pidloser ?: priloser;
95
96   return err ? __hurd_fail (err) : 0;
97 }
98 libc_hidden_def (setpriority)