[65076] trunk/base/src

raimue at macports.org raimue at macports.org
Sat Mar 20 22:03:26 PDT 2010


Revision: 65076
          http://trac.macports.org/changeset/65076
Author:   raimue at macports.org
Date:     2010-03-20 22:03:24 -0700 (Sat, 20 Mar 2010)
Log Message:
-----------
port1.0: Use setpriority(2) in proc system instead of /usr/bin/nice, fixes #18304

Modified Paths:
--------------
    trunk/base/src/pextlib1.0/system.c
    trunk/base/src/port1.0/portbuild.tcl
    trunk/base/src/port1.0/portutil.tcl

Modified: trunk/base/src/pextlib1.0/system.c
===================================================================
--- trunk/base/src/pextlib1.0/system.c	2010-03-21 03:46:16 UTC (rev 65075)
+++ trunk/base/src/pextlib1.0/system.c	2010-03-21 05:03:24 UTC (rev 65076)
@@ -43,10 +43,12 @@
 
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/resource.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <limits.h>
 
 #include "system.h"
 #include "Pextlib.h"
@@ -69,6 +71,7 @@
     char *line;
 };
 
+/* usage: system ?-notty? ?-nice value? command */
 int SystemCmd(ClientData clientData UNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
 {
     char *buf;
@@ -80,29 +83,36 @@
     int fdset[2], nullfd;
     int fline, pos, ret;
     int osetsid = 0;
+    int oniceval = INT_MAX; /* magic value indicating no change */
     pid_t pid;
     uid_t euid;
     Tcl_Obj *tcl_result;
     int read_failed, status;
+    int i;
 
-    /* usage: system [-notty] command */
-    if (objc == 2) {
-        cmdstring = Tcl_GetString(objv[1]);
-    } else if (objc == 3) {
-        char *arg = Tcl_GetString(objv[1]);
-        cmdstring = Tcl_GetString(objv[2]);
+    if (objc < 2) {
+        Tcl_WrongNumArgs(interp, 1, objv, "?-notty? ?-nice value? command");
+        return TCL_ERROR;
+    }
 
+    cmdstring = Tcl_GetString(objv[objc - 1]);
+
+    for (i = 1; i < objc - 1; i++) {
+        char *arg = Tcl_GetString(objv[i]);
         if (strcmp(arg, "-notty") == 0) {
             osetsid = 1;
+        } else if (strcmp(arg, "-nice") == 0) {
+            i++;
+            if (Tcl_GetIntFromObj(interp, objv[i], &oniceval) != TCL_OK) {
+                Tcl_SetResult(interp, "invalid value for -nice", TCL_STATIC);
+                return TCL_ERROR;
+            }
         } else {
             tcl_result = Tcl_NewStringObj("bad option ", -1);
             Tcl_AppendObjToObj(tcl_result, Tcl_NewStringObj(arg, -1));
             Tcl_SetObjResult(interp, tcl_result);
             return TCL_ERROR;
         }
-    } else {
-        Tcl_WrongNumArgs(interp, 1, objv, "command");
-        return TCL_ERROR;
     }
 
     /*
@@ -131,6 +141,12 @@
             if (setsid() == -1)
                 _exit(1);
         }
+        /* change scheduling priority if requested */
+        if (oniceval != INT_MAX) {
+            if (setpriority(PRIO_PROCESS, getpid(), oniceval) != 0) {
+                /* ignore failure, just continue */
+            }
+        }
         /* drop privileges entirely for child */
         if (getuid() == 0 && (euid = geteuid()) != 0) {
             if (seteuid(0) || setuid(euid)) {

Modified: trunk/base/src/port1.0/portbuild.tcl
===================================================================
--- trunk/base/src/port1.0/portbuild.tcl	2010-03-21 03:46:16 UTC (rev 65075)
+++ trunk/base/src/port1.0/portbuild.tcl	2010-03-21 05:03:24 UTC (rev 65076)
@@ -43,7 +43,6 @@
 
 # define options
 options build.target
-options build.nice
 options build.jobs
 options build.asroot
 options use_parallel_build
@@ -103,17 +102,6 @@
     }
 }
 
-proc portbuild::build_getnicevalue {args} {
-    if {![exists build.nice] || [string match "* *" [option build.cmd]]} {
-        return ""
-    }
-    set nice [option build.nice]
-    if {![string is integer -strict $nice] || $nice <= 0} {
-        return ""
-    }
-    return "[findBinary nice $portutil::autoconf::nice_path] -n $nice "
-}
-
 proc portbuild::build_getjobs {args} {
     global buildmakejobs
     set jobs $buildmakejobs
@@ -161,11 +149,10 @@
 proc portbuild::build_main {args} {
     global build.cmd
 
-    set nice_prefix [build_getnicevalue]
     set jobs_suffix [build_getjobsarg]
 
     set realcmd ${build.cmd}
-    set build.cmd "$nice_prefix${build.cmd}$jobs_suffix"
+    set build.cmd "${build.cmd}$jobs_suffix"
     command_exec build
     set build.cmd ${realcmd}
     return 0

Modified: trunk/base/src/port1.0/portutil.tcl
===================================================================
--- trunk/base/src/port1.0/portutil.tcl	2010-03-21 03:46:16 UTC (rev 65075)
+++ trunk/base/src/port1.0/portutil.tcl	2010-03-21 05:03:24 UTC (rev 65076)
@@ -289,7 +289,7 @@
 # and used to form a standard set of command options.
 proc commands {args} {
     foreach option $args {
-        options use_${option} ${option}.dir ${option}.pre_args ${option}.args ${option}.post_args ${option}.env ${option}.type ${option}.cmd
+        options use_${option} ${option}.dir ${option}.pre_args ${option}.args ${option}.post_args ${option}.env ${option}.nice ${option}.type ${option}.cmd
     }
 }
 
@@ -328,14 +328,14 @@
 # command_prefix    additional command prefix (typically pipe command)
 # command_suffix    additional command suffix (typically redirection)
 proc command_exec {command args} {
-    global ${command}.env ${command}.env_array env
-    set notty 0
+    global ${command}.env ${command}.env_array ${command}.nice env
+    set notty ""
     set command_prefix ""
     set command_suffix ""
 
     if {[llength $args] > 0} {
         if {[lindex $args 0] == "-notty"} {
-            set notty 1
+            set notty "-notty"
             set args [lrange $args 1 end]
         }
 
@@ -370,6 +370,12 @@
     # Debug that.
     ui_debug "Environment: [environment_array_to_string ${command}.env_array]"
 
+    # Prepare nice value change
+    set nice ""
+    if {[info exists ${command}.nice] && [set ${command}.nice] != ""} {
+        set nice "-nice [set ${command}.nice]"
+    }
+
     # Get the command string.
     set cmdstring [command_string ${command}]
 
@@ -381,11 +387,8 @@
     array set env [array get ${command}.env_array]
     # Call the command.
     set fullcmdstring "$command_prefix $cmdstring $command_suffix"
-    if {$notty} {
-        set code [catch {system -notty $fullcmdstring} result]
-    } else {
-        set code [catch {system $fullcmdstring} result]
-    }
+    set code [catch {eval system $notty $nice \$fullcmdstring} result]
+
     # Unset the command array until next time.
     array unset ${command}.env_array
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20100320/7fb11363/attachment-0001.html>


More information about the macports-changes mailing list