* Add patch to fix 'cpulimit drops priority, then can't get back up. Fix debian/1.1-10
authorgregor herrmann <gregoa@debian.org>
Sat, 3 Mar 2007 21:14:38 +0000 (21:14 -0000)
committergregor herrmann <gregoa@debian.org>
Sat, 3 Mar 2007 21:14:38 +0000 (21:14 -0000)
  included'. Thanks to Ariel for providing the patch. (Closes: #412421)

debian/changelog
debian/control
debian/patches/00list [new file with mode: 0644]
debian/patches/01_priority.dpatch [new file with mode: 0755]
debian/rules

index 27ad4d8..77738e3 100644 (file)
@@ -1,3 +1,10 @@
+cpulimit (1.1-10) unstable; urgency=low
+
+  * Add patch to fix 'cpulimit drops priority, then can't get back up. Fix
+    included'. Thanks to Ariel for providing the patch. (Closes: #412421)
+
+ -- gregor herrmann <gregor+debian@comodo.priv.at>  Sat,  3 Mar 2007 21:59:06 +0100
+
 cpulimit (1.1-9) unstable; urgency=low
 
   * Change watch file, update Homepage: field in debian/control, and mention 
index 822399a..37ccefe 100644 (file)
@@ -3,7 +3,7 @@ Section: admin
 Priority: optional
 Maintainer: gregor herrmann <gregor+debian@comodo.priv.at>
 Uploaders: tony mancill <tmancill@debian.org>
-Build-Depends: debhelper (>= 5.0.0)
+Build-Depends: debhelper (>= 5.0.0), dpatch
 Standards-Version: 3.7.2
 XS-Vcs-Svn: http://svn.toastfreeware.priv.at/debian/cpulimit/trunk/
 XS-Vcs-Browser: http://svn.toastfreeware.priv.at/wsvn/ToastfreewareDebian/cpulimit/trunk/
diff --git a/debian/patches/00list b/debian/patches/00list
new file mode 100644 (file)
index 0000000..ba325ca
--- /dev/null
@@ -0,0 +1 @@
+01_priority
diff --git a/debian/patches/01_priority.dpatch b/debian/patches/01_priority.dpatch
new file mode 100755 (executable)
index 0000000..758aa01
--- /dev/null
@@ -0,0 +1,112 @@
+#! /bin/sh /usr/share/dpatch/dpatch-run
+## 01_priority.dpatch by  <gregor+debian@comodo.priv.at>
+##
+## All lines beginning with `## DP:' are a description of the patch.
+## DP: only drop priority if it can be raised too.
+## DP: and implement RLIMIT_NICE option.
+
+@DPATCH@
+diff -urNad cpulimit~/cpulimit.c cpulimit/cpulimit.c
+--- cpulimit~/cpulimit.c       2006-04-03 22:33:07.000000000 +0200
++++ cpulimit/cpulimit.c        2007-03-03 22:06:14.000000000 +0100
+@@ -42,6 +42,7 @@
+ #include <dirent.h>
+ #include <errno.h>
+ #include <string.h>
++#include <limits.h> //for INT_MAX, could also hardcode some large number
+ //kernel time resolution (inverse of one jiffy interval) in Hertz
+ //i don't know how to detect it, then define to the default (not very clean!)
+@@ -59,6 +60,8 @@
+ int verbose=0;
+ //lazy mode
+ int lazy=0;
++//is higher priority nice possible?
++int nice_lim;
+ //reverse byte search
+ void *memrchr(const void *s, int c, size_t n);
+@@ -71,7 +74,7 @@
+ int waitforpid(int pid) {
+       //switch to low priority
+-      if (setpriority(PRIO_PROCESS,getpid(),19)!=0) {
++      if (nice_lim < INT_MAX && setpriority(PRIO_PROCESS,getpid(),19)!=0) {
+               printf("Warning: cannot renice\n");
+       }
+@@ -127,8 +130,8 @@
+ done:
+       printf("Process %d detected\n",pid);
+       //now set high priority, if possible
+-      if (setpriority(PRIO_PROCESS,getpid(),-20)!=0) {
+-              printf("Warning: cannot renice.\nTo work better you should run this program as root.\n");
++      if (nice_lim < INT_MAX && setpriority(PRIO_PROCESS,getpid(),nice_lim)!=0) {
++              printf("Warning: cannot renice.\n");
+       }
+       return 0;
+@@ -143,7 +146,7 @@
+ int getpidof(const char *process) {
+       //set low priority
+-      if (setpriority(PRIO_PROCESS,getpid(),19)!=0) {
++      if (nice_lim < INT_MAX && setpriority(PRIO_PROCESS,getpid(),19)!=0) {
+               printf("Warning: cannot renice\n");
+       }
+@@ -219,8 +222,8 @@
+ done:
+       printf("Process %d detected\n",pid);
+       //now set high priority, if possible
+-      if (setpriority(PRIO_PROCESS,getpid(),-20)!=0) {
+-              printf("Warning: cannot renice.\nTo work better you should run this program as root.\n");
++      if (nice_lim < INT_MAX && setpriority(PRIO_PROCESS,getpid(),nice_lim)!=0) {
++              printf("Warning: cannot renice.\n");
+       }
+       return pid;
+@@ -373,6 +376,7 @@
+       int pid_ok=0;
+       int process_ok=0;
+       int limit_ok=0;
++      struct rlimit maxlimit;
+       do {
+               next_option = getopt_long (argc, argv, short_options,long_options, NULL);
+@@ -437,6 +441,35 @@
+       signal(SIGINT,quit);
+       signal(SIGTERM,quit);
++      //now test high priority
++      if (setpriority(PRIO_PROCESS,getpid(),-20)!=0) {
++              //if that failed, check if we have a limit by how much we can raise the priority
++#ifdef RLIMIT_NICE //check if non-root can even make changes (ifdef because it's only available in linux >= 2.6.13)
++              nice_lim=getpriority(PRIO_PROCESS,getpid());
++              getrlimit(RLIMIT_NICE, &maxlimit);
++
++              if( (20 - (signed)maxlimit.rlim_cur) < nice_lim &&  //if we can do better then current
++                  setpriority(PRIO_PROCESS,getpid(),20 - (signed)maxlimit.rlim_cur)==0 //and it actually works
++                ) {
++
++                      //if we can do better, but not by much, warn about it
++                      if( (nice_lim - (20 - (signed)maxlimit.rlim_cur)) < 9) {
++                              printf("Warning, can only increase priority by %d.\n", nice_lim - (20 - (signed)maxlimit.rlim_cur));
++                      }
++
++                      nice_lim = 20 - (signed)maxlimit.rlim_cur; //our new limit
++
++              } else //otherwise don't try to change priority. The below will also run if it's not possible for non-root to change priority
++#endif
++              {
++                      printf("Warning: cannot renice.\nTo work better you should run this program as root, or adjust RLIMIT_NICE.\nFor example in /etc/security/limits.conf add a line with: * - nice -10\n\n");
++                      nice_lim=INT_MAX;
++              }
++      } else {
++              nice_lim=-20;
++      }
++      //don't bother putting setpriority back down, since getpidof and waitforpid twiddle it anyway
++
+       //time quantum in microseconds. it's splitted in a working period and a sleeping one
+       int period=100000;
+       struct timespec twork,tsleep;   //working and sleeping intervals
index 7fea5cf..bf418ed 100755 (executable)
@@ -9,6 +9,7 @@
 # Uncomment this to turn on verbose mode.
 #export DH_VERBOSE=1
 
+include /usr/share/dpatch/dpatch.make
 
 CFLAGS = -Wall -g
 
@@ -21,26 +22,21 @@ endif
 configure: configure-stamp
 configure-stamp:
        dh_testdir
-
        touch configure-stamp
 
 
 build: build-stamp
 
-build-stamp: configure-stamp 
+build-stamp: configure-stamp patch
        dh_testdir
-
        $(MAKE)
-
        touch build-stamp
 
-clean:
+clean: unpatch
        dh_testdir
        dh_testroot
-       rm -f build-stamp configure-stamp
-
+       rm -f build-stamp configure-stamp patch-stamp
        [ ! -f Makefile ] || $(MAKE) clean
-
        dh_clean 
 
 install: build
@@ -48,7 +44,6 @@ install: build
        dh_testroot
        dh_clean -k 
        dh_installdirs
-
        install cpulimit $(CURDIR)/debian/cpulimit/usr/bin