Back to index

glibc  2.9
getpriority.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994,95,96,97,2000,02 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <limits.h>
00020 #include <hurd.h>
00021 #include <hurd/resource.h>
00022 
00023 /* Return the highest priority of any process specified by WHICH and WHO
00024    (see <sys/resource.h>); if WHO is zero, the current process, process group,
00025    or user (as specified by WHO) is used.  A lower priority number means higher
00026    priority.  Priorities range from PRIO_MIN to PRIO_MAX.  */
00027 int
00028 getpriority (enum __priority_which which, id_t who)
00029 {
00030   error_t err, onerr;
00031   int maxpri = INT_MIN;
00032   struct procinfo *pip;            /* Just for sizeof.  */
00033   int pibuf[sizeof *pip + 2 * sizeof (pip->threadinfos[0])], *pi = pibuf;
00034   size_t pisize = sizeof pibuf / sizeof pibuf[0];
00035 
00036   error_t getonepriority (pid_t pid, struct procinfo *pip)
00037     {
00038       if (pip)
00039        onerr = 0;
00040       else
00041        {
00042          int *oldpi = pi;
00043          size_t oldpisize = pisize;
00044          char *tw = 0;
00045          size_t twsz = 0;
00046          int flags = PI_FETCH_TASKINFO;
00047          onerr = __USEPORT (PROC, __proc_getprocinfo (port, pid, &flags,
00048                                                  &pi, &pisize,
00049                                                  &tw, &twsz));
00050          if (twsz)
00051            __vm_deallocate (__mach_task_self (), (vm_address_t) tw, twsz);
00052          if (pi != oldpi && oldpi != pibuf)
00053            /* Old buffer from last call was not reused; free it.  */
00054            __vm_deallocate (__mach_task_self (),
00055                           (vm_address_t) oldpi, oldpisize * sizeof pi[0]);
00056          pip = (struct procinfo *) pi;
00057        }
00058 #ifdef TASK_SCHED_TIMESHARE_INFO
00059       if (!onerr && pip->timeshare_base_info.base_priority > maxpri)
00060        maxpri = pip->timeshare_base_info.base_priority;
00061 #else
00062       if (!onerr && pip->taskinfo.base_priority > maxpri)
00063        maxpri = pip->taskinfo.base_priority;
00064 #endif
00065       return 0;
00066     }
00067 
00068   onerr = 0;
00069   err = _hurd_priority_which_map (which, who,
00070                               getonepriority, PI_FETCH_TASKINFO);
00071 
00072   if (pi != pibuf)
00073     __vm_deallocate (__mach_task_self (),
00074                    (vm_address_t) pi, pisize * sizeof pi[0]);
00075 
00076   if (!err && maxpri == INT_MIN)
00077     /* No error, but no pids found.  */
00078     err = onerr ?: ESRCH;
00079 
00080   if (err)
00081     return __hurd_fail (err);
00082 
00083   return MACH_PRIORITY_TO_NICE (maxpri);
00084 }
00085 libc_hidden_def (getpriority)