[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: start-stop-daemon ulimit



On Thu, Mar 31, 2005 at 09:11:36AM +0200, Carlo Contavalli wrote:
>
> [...]
>
as usual, I forgot the attachment. Here it is..

Cheers, 
Carlo

-- 
  GPG Fingerprint: 2383 7B14 4D08 53A4 2C1A CA29 9E98 5431 1A68 6975
                        -------------
Old mail has arrived.
diff -x configure -x config.h.in -x 'config.h.in~' -Naur ./dpkg-1.10.27/configure.in ./dpkg-1.10.27.cc/configure.in
--- ./dpkg-1.10.27/configure.in	2005-02-10 16:25:43.000000000 +0100
+++ ./dpkg-1.10.27.cc/configure.in	2005-03-30 00:40:37.000000000 +0200
@@ -175,6 +175,8 @@
 AC_CHECK_FUNCS(vsnprintf lchown snprintf)
 AC_CHECK_HEADERS(sys/cdefs.h syslog.h stddef.h)
 AC_CHECK_HEADERS(error.h locale.h)
+AC_CHECK_FUNCS(setrlimit getrlimit)
+AC_CHECK_HEADERS(sys/time.h sys/resource.h unistd.h)
 AC_DECL_SYS_SIGLIST
 AC_CHECK_LIB(ihash, ihash_create, SSD_LIBS="-lihash $SSD_LIBS")
 AC_CHECK_LIB(ps, proc_stat_list_create, SSD_LIBS="-lps $SSD_LIBS")
diff -x configure -x config.h.in -x 'config.h.in~' -Naur ./dpkg-1.10.27/utils/start-stop-daemon.c ./dpkg-1.10.27.cc/utils/start-stop-daemon.c
--- ./dpkg-1.10.27/utils/start-stop-daemon.c	2005-02-10 16:24:18.000000000 +0100
+++ ./dpkg-1.10.27.cc/utils/start-stop-daemon.c	2005-03-30 03:03:42.000000000 +0200
@@ -90,6 +90,21 @@
 #  include <error.h>
 #endif
 
+  /* setrlimit */
+#ifndef SSD_SETRLIMIT_PATH 
+#  define SSD_SETRLIMIT_PATH "/etc/limits"
+#endif
+
+#if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && \
+    defined(HAVE_UNISTD_H) && defined(HAVE_SETRLIMIT) && defined(HAVE_GETRLIMIT)
+
+#  define SSD_SETRLIMIT 1
+
+#  include <sys/time.h>
+#  include <sys/resource.h>
+#endif
+
+
 static int testmode = 0;
 static int quietmode = 0;
 static int exitnodo = 1;
@@ -943,6 +958,272 @@
 }
 #endif /* OShpux */
 
+#ifdef SSD_SETRLIMIT
+static int
+do_getlimit(FILE * f, char ch, int * resource, const char ** rname) {
+  switch(ch) {
+    default:
+      break;
+
+      /* as */ 
+    case 'a':
+      if(fgetc(f) == 's') {
+	*resource=RLIMIT_AS;
+	*rname="as";
+	return 1; 
+      }
+      break;
+
+    case 'c':
+      /* core */ 
+      if((ch=fgetc(f)) == 'o' && fgetc(f) == 'r' && fgetc(f) == 'e') {
+	*resource=RLIMIT_CORE;
+	*rname="core";
+	return 1;
+      } 
+
+      /* cpu */
+      if(ch == 'p' && fgetc(f) == 'u') {
+	*resource=RLIMIT_CPU;
+	*rname="cpu";
+	return 1;
+      }
+      break;
+
+      /* data */ 
+    case 'd':
+      if(fgetc(f) == 'a' && fgetc(f) == 't' && fgetc(f) == 'a') {
+	*resource=RLIMIT_DATA;
+	*rname="data";
+	return 1;
+      }
+      break;
+
+    case 'f':
+      if(fgetc(f) == 's' && fgetc(f) == 'i' && fgetc(f) == 'z' && fgetc(f) == 'e') {
+	*resource=RLIMIT_FSIZE;
+	*rname="fsize";
+	return 1;
+      }
+      break;
+
+      /* locks */ 
+    case 'l':
+      if(fgetc(f) == 'o' && fgetc(f) == 'c' && fgetc(f) == 'k' && fgetc(f) == 's') {
+	*resource=RLIMIT_LOCKS;
+	*rname="locks";
+	return 1;
+      }
+      break;
+
+      /* memlock */ 
+    case 'm':
+      if(fgetc(f) == 'e' && fgetc(f) == 'm' && fgetc(f) == 'l' && 
+	 fgetc(f) == 'o' && fgetc(f) == 'c' && fgetc(f) == 'k') {
+	*resource=RLIMIT_MEMLOCK;
+	*rname="memlock";
+	return 1;
+      }
+      break;
+
+      /* nofile */ 
+    case 'n':
+      if((ch=fgetc(f)) == 'o' && fgetc(f) == 'f' && fgetc(f) == 'i' && 
+	 fgetc(f) == 'l' && fgetc(f) == 'e') {
+#if defined(OSOpenBSD) || defined(OSFreeBSD) || defined(OSNetBSD)
+	*resource=RLIMIT_OFILE;
+#else
+	*resource=RLIMIT_NOFILE;
+#endif
+
+	*rname="nofile";
+	return 1;
+      }
+
+      /* nproc */ 
+      if(ch == 'p' && fgetc(f) == 'r' && fgetc(f) == 'o' && fgetc(f) == 'c') {
+	*resource=RLIMIT_NPROC;
+	*rname="nproc";
+	return 1;
+      }
+      break;
+
+      /* rss */
+    case 'r':
+      if(fgetc(f) == 's' && fgetc(f) == 's') {
+	*resource=RLIMIT_RSS;
+	*rname="rss";
+	return 1;
+      }
+      break;
+
+      /* stack */ 
+    case 's':
+      if(fgetc(f) == 't' && fgetc(f) == 'a' &&
+	 fgetc(f) == 'c' && fgetc(f) == 'k') {
+	*resource=RLIMIT_STACK;
+	*rname="stack";
+	return 1;
+      }
+      break;
+  }
+
+  return 0;
+}
+
+
+static void
+do_loadlimits(void) 
+{
+  char * look=execname;
+
+  FILE * f;
+  char * name;
+  char * path;
+
+  char * lname;
+  struct rlimit limit;
+  rlim_t *lvalue;
+  unsigned long value;
+  int resource;
+
+
+  int line=1;
+  int ch;
+
+    /* Return immediately if we have nothing
+     * to run */
+  if(!look) {
+    if(!startas)
+      return;
+
+    look=startas;
+  }
+
+    /* Calculate name of configuration file */
+  name=strrchr(look, '/');
+  if(name)
+    name=name+1;
+  else
+    name=look;
+
+    /* Calculate name of the file to be read */
+  path=xmalloc(sizeof(SSD_SETRLIMIT_PATH)+strlen(name)+1);
+  sprintf(path, SSD_SETRLIMIT_PATH "/%s", name);
+
+    /* name of file/path to open */ 
+  f=fopen(path, "r");
+  if(!f) {
+    if(errno == ENOENT) {
+      if(quietmode < 0)
+        printf("%s: not loading limits -- missing file '%s'\n", name, path);
+      free(path);
+      return;
+    }
+
+    if(quietmode <= 0)
+      printf("%s: couldn't open limits file -- %s\n", name, strerror(errno)); 
+    free(path);
+    return;
+  }
+
+    /* Ok, file has been opened, read limits */
+  while(1) {
+
+      /* Skip blanks */
+    while((ch=fgetc(f)) != EOF && (ch == '\t' || ch == ' ' || ch == '\n'))  {
+      if(ch == '\n')
+	line++;
+    }
+      
+      /* Check if we found a comment */
+    if(ch == '#') {
+      while((ch=fgetc(f)) != EOF && ch != '\n')
+	;
+
+      if(ch == '\n')
+	line++;
+
+      continue;
+    }
+
+      /* Verify we didn't reach EOF */
+    if(ch == EOF) {
+      free(path);
+      return;
+    }
+
+      /* Get limit type */
+    if(do_getlimit(f, ch, &resource, (const char **)&lname)) {
+        /* Skip any blank */
+      while((ch=fgetc(f)) != EOF && (ch == '\t' || ch == ' ')) 
+	;
+
+        /* Set limit kind */
+      lvalue=NULL;
+      if(ch == 's' && fgetc(f) == 'o' && fgetc(f) == 'f' && fgetc(f) == 't')
+	lvalue=&(limit.rlim_cur);
+      else
+	if(ch == 'h' && fgetc(f) == 'a' && fgetc(f) == 'r' && fgetc(f) == 'd')
+	  lvalue=&(limit.rlim_max);
+
+        /* If we found some useful line value */
+      if(lvalue) {
+	while((ch=fgetc(f)) != EOF && (ch == ' ' || ch == '\t'))
+	  ;
+
+	  /* Try to get limit */
+	ungetc(ch, f);
+	if(fscanf(f, "%lu", &value) == 1) {
+	  if(!getrlimit(resource, &limit) == -1) {
+	    if(quietmode <= 0)
+	      printf("%s: getrlimit failed for %s (%s:%d): %s\n", name, lname, path, line, strerror(errno));
+	    continue;
+	  }
+
+	    /* Verify SOFT limit is <= of the HARD limit */
+	  *lvalue=(rlim_t)value;
+	  if(lvalue == &limit.rlim_max && limit.rlim_cur > limit.rlim_max) {
+	    if(quietmode < 0)
+	      printf("%s: SOFT LIMIT was > than HARD LIMIT -- decreased to HARD LIMIT value on %s:%d (%s)\n",
+		   name, path, line, lname);
+
+	    limit.rlim_cur=limit.rlim_max;
+	  }
+
+	      /* Skip Anything else until end of line */
+    	  while((ch=fgetc(f)) != EOF && ch != '\n')
+      	    ;
+	  line++;
+
+	  if(!setrlimit(resource, &limit) == -1) {
+	    if(quietmode <= 0)
+	      printf("%s: setrlimit failed for %s (%s:%d): %s\n", name, lname, path, line-1, strerror(errno));
+	    continue;
+	  } 
+
+	  if(quietmode < 0)
+	    printf("%s: setrlimit %s:%d: %s %s=%lu\n", name, path, line-1, lname, (lvalue == &(limit.rlim_cur) ? 
+		   "soft" : "hard"), value); 
+	  continue;
+	}
+      }
+    }
+
+    /* Something wrong into configuration file */
+    if(quietmode <= 0)
+      printf("%s: parse error in limits file on %s:%d\n", name, path, line);
+
+      /* Skip line */
+    while((ch=fgetc(f)) != EOF && ch != '\n')
+      ;
+    ungetc(ch, f);
+  }
+
+    /* should never be reached */
+  return;
+}
+#endif
 
 static void
 do_findprocs(void)
@@ -1191,6 +1472,9 @@
 		exit(i);
 	}
 
+#ifdef SSD_SETRLIMIT 
+	do_loadlimits();
+#endif
 	do_findprocs();
 
 	if (found) {

Reply to: