From: Diego Nieto Cid <[email protected]>

    Check for task_max_priority RPC

      * config.h.in: add #undef for HAVE_MACH_TASK_MAX_PRIORITY.
      * sysdeps/mach/configure.ac: use mach_RPC_CHECK to check for
        task_max_priority RPC in mach_host.defs.
      * sysdeps/mach/configure: regenerate file.

    Use task_max_priority when setpriority is called by root

      * sysdeps/mach/hurd/setpriority.c: clamp the prio argument
        to the range [-NZERO, NZERO-1] and use task_max_priority
        when called by root.
---
 config.h.in                     |  3 +++
 sysdeps/mach/configure          | 30 ++++++++++++++++++++++++
 sysdeps/mach/configure.ac       |  2 ++
 sysdeps/mach/hurd/setpriority.c | 41 +++++++++++++++++++++++++++++++--
 4 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/config.h.in b/config.h.in
index b53731c393..87befaa9c6 100644
--- a/config.h.in
+++ b/config.h.in
@@ -170,6 +170,9 @@
 /* Mach specific: define if the `vm_set_size_limit' RPC is available.  */
 #undef  HAVE_MACH_VM_SET_SIZE_LIMIT
 
+/* Mach specific: define if the `task_max_priority' RPC is available.  */
+#undef  HAVE_MACH_TASK_MAX_PRIORITY
+
 /* Mach/i386 specific: define if the `i386_io_perm_*' RPCs are available.  */
 #undef HAVE_I386_IO_PERM_MODIFY
 
diff --git a/sysdeps/mach/configure b/sysdeps/mach/configure
index 0161937ab4..a725cd1c7d 100755
--- a/sysdeps/mach/configure
+++ b/sysdeps/mach/configure
@@ -641,6 +641,36 @@ if test $libc_cv_mach_rpc_vm_get_size_limit = yes; then
 
 fi
 
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for task_max_priority 
in mach_host.defs" >&5
+printf %s "checking for task_max_priority in mach_host.defs... " >&6; }
+if test ${libc_cv_mach_rpc_task_max_priority+y}
+then :
+  printf %s "(cached) " >&6
+else case e in #(
+  e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <mach/mach_host.defs>
+
+_ACEOF
+if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
+  $EGREP_TRADITIONAL "task_max_priority" >/dev/null 2>&1
+then :
+  libc_cv_mach_rpc_task_max_priority=yes
+else case e in #(
+  e) libc_cv_mach_rpc_task_max_priority=no ;;
+esac
+fi
+rm -rf conftest*
+ ;;
+esac
+fi
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: 
$libc_cv_mach_rpc_task_max_priority" >&5
+printf "%s\n" "$libc_cv_mach_rpc_task_max_priority" >&6; }
+if test $libc_cv_mach_rpc_task_max_priority = yes; then
+  printf "%s\n" "#define HAVE_MACH_TASK_MAX_PRIORITY 1" >>confdefs.h
+
+fi
+
 
 ac_fn_c_check_header_preproc "$LINENO" "mach/machine/ndr_def.h" 
"ac_cv_header_mach_machine_ndr_def_h"
 if test "x$ac_cv_header_mach_machine_ndr_def_h" = xyes
diff --git a/sysdeps/mach/configure.ac b/sysdeps/mach/configure.ac
index 237b8be937..b591e6fd9b 100644
--- a/sysdeps/mach/configure.ac
+++ b/sysdeps/mach/configure.ac
@@ -104,6 +104,8 @@ mach_RPC_CHECK(gnumach.defs, vm_set_size_limit,
                HAVE_MACH_VM_SET_SIZE_LIMIT)
 mach_RPC_CHECK(gnumach.defs, vm_get_size_limit,
                HAVE_MACH_VM_GET_SIZE_LIMIT)
+mach_RPC_CHECK(mach_host.defs, task_max_priority,
+              HAVE_MACH_TASK_MAX_PRIORITY)
 
 AC_CHECK_HEADER(mach/machine/ndr_def.h, [dnl
   DEFINES="$DEFINES -DNDR_DEF_HEADER='<mach/machine/ndr_def.h>'"], [dnl
diff --git a/sysdeps/mach/hurd/setpriority.c b/sysdeps/mach/hurd/setpriority.c
index 23d678bf40..bda13b0d7f 100644
--- a/sysdeps/mach/hurd/setpriority.c
+++ b/sysdeps/mach/hurd/setpriority.c
@@ -28,7 +28,30 @@ __setpriority (enum __priority_which which, id_t who, int 
prio)
   error_t pidloser, priloser;
   unsigned int npids, ntasks, nwin, nperm, nacces;
 
-  prio = MAX (0, MIN (2 * NZERO - 1, prio));
+  /*   * man getpriority(2) (i.e. man7.org)
+   *
+   *     << The prio argument is a value in the range -20 to 19. >>
+   *
+   *   * man nice(2)  (i.e. man7.org)
+   *
+   *     << Attempts to set a nice value outside the range are
+   *       clamped to the range. >>
+   *
+   *   * The Open Group Base Specifications Issue 8
+   *     getpriority ( https://pubs.opengroup.org/onlinepubs/9799919799/ )
+   *
+   *     << The nice value is in the range [0,2*{NZERO} -1], while the
+   *        return value for getpriority() and the third parameter for
+   *        setpriority() are in the range [-{NZERO},{NZERO} -1]. >>
+   *
+   *  So given that NZERO is defined to 20, we can use it to clamp to
+   *  the range specified by POSIX, in concordance with the Linux man pages.
+   *
+   *  That range is then mapped to something similar to [0, 2*{NZERO}-1], as
+   *  specified by POSIX, through the usage of the macro NICE_TO_MACH_PRIORITY
+   *  and MACK_PRIORITY_TO_NICE. (i.e. [5, 45] )
+   */
+  prio = MAX ((-NZERO), MIN ((NZERO - 1), prio));
 
   error_t setonepriority (pid_t pid, struct procinfo *pi)
     {
@@ -57,11 +80,25 @@ __setpriority (enum __priority_which which, id_t who, int 
prio)
                                    0, 1);
          }
 #else
-         prierr = __task_priority (task, NICE_TO_MACH_PRIORITY (prio), 1);
+         do {
+#ifdef HAVE_MACH_TASK_MAX_PRIORITY
+           mach_port_t host = MACH_PORT_NULL;
+           error_t priverr = __get_privileged_ports (&host, NULL);
+           if (!priverr)
+             {
+               prierr = __task_max_priority (host, task, NICE_TO_MACH_PRIORITY 
(prio), 1, 1);
+               __mach_port_deallocate (__mach_task_self (), host);
+               if (prierr != MIG_BAD_ID)
+                 break;
+             }
+#endif
+           prierr = __task_priority (task, NICE_TO_MACH_PRIORITY (prio), 1);
+         } while (0);
 #endif
          __mach_port_deallocate (__mach_task_self (), task);
          switch (prierr)
            {
+           case KERN_NO_ACCESS:
            case KERN_FAILURE:
              ++nacces;
              break;
-- 
2.53.0


Reply via email to