diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./config/config.h.in ./config/config.h.in
--- ./config/config.h.in	2006-01-26 01:25:24.000000000 +0100
+++ ./config/config.h.in	2006-02-03 21:31:28.000000000 +0100
@@ -162,3 +162,15 @@
 
 /* Define to 1 if you want to use CryptoPP */
 #undef HAVE_CRYPTOPP
+
+/* Define to 1 if you have <sys/time.h> */
+#undef HAVE_SYS_TIME_H
+
+/* Define to 1 if you have <sys/resource.h> */
+#undef HAVE_SYS_RESOURCE_H
+
+/* Define to 1 if you have getrlimit() */
+#undef HAVE_GETRLIMIT
+
+/* Define to 1 if you have setrlimit() */
+#undef HAVE_SETRLIMIT
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./config/configure.in ./config/configure.in
--- ./config/configure.in	2006-02-03 01:32:22.000000000 +0100
+++ ./config/configure.in	2006-02-03 21:31:28.000000000 +0100
@@ -512,11 +512,12 @@
 AC_CHECK_SIZEOF(short)
 AC_CHECK_SIZEOF(float)
 AC_CHECK_SIZEOF(double)
+AC_CHECK_FUNCS(setrlimit getrlimit)
 
 # various header files
 AC_CHECK_HEADERS([sys/utsname.h])
 AC_CHECK_HEADERS(arpa/inet.h,,)
-AC_CHECK_HEADERS([sys/types.h netinet/in_systm.h netinet/in.h netinet/ip.h],,,
+AC_CHECK_HEADERS([sys/types.h sys/time.h sys/resource.h netinet/in_systm.h netinet/in.h netinet/ip.h],,,
 [#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./distrib/Authors.txt ./distrib/Authors.txt
--- ./distrib/Authors.txt	2006-01-12 12:44:34.000000000 +0100
+++ ./distrib/Authors.txt	2006-02-03 21:31:28.000000000 +0100
@@ -37,6 +37,10 @@
 su_blanc
 bogeyman
 
+rlimit code taken from Ocaml-annexlib:
+Shawn Wagner <raevnos@pennmush.org>
+http://raevnos.pennmush.org/code/extlib/
+
 Fasttrack plugin from giFT-fasttrack:
 Markus Kern
 
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/daemon/common/commonOptions.ml ./src/daemon/common/commonOptions.ml
--- ./src/daemon/common/commonOptions.ml	2006-02-03 04:12:14.000000000 +0100
+++ ./src/daemon/common/commonOptions.ml	2006-02-03 21:50:31.000000000 +0100
@@ -108,15 +108,20 @@
 
 let _ =
   lprintf_nl "Starting MLDonkey %s ... " Autoconf.current_version;
+  let ulof_old = Unix2.c_getdtablesize () in
   lprintf_nl "Language %s, locale %s, ulimit for open files %d"
-    Charset.default_language Charset.locstr (Unix2.c_getdtablesize ());
-  lprintf_nl "MLDonkey is working in %s" file_basedir;
-
+    Charset.default_language Charset.locstr ulof_old;
+  let nofile = Unix2.ml_getrlimit Unix2.RLIMIT_NOFILE in
+    Unix2.ml_setrlimit Unix2.RLIMIT_NOFILE nofile.Unix2.rlim_max;
   let ulof = Unix2.c_getdtablesize () in
   if ulof < 150 then begin
     lprintf_nl "ulimit for open files is set to %d, at least 150 is required, exiting..." ulof;
     exit 2
   end;
+  if ulof_old <> ulof then
+    lprintf_nl "raised ulimit for open files from %d to %d" ulof_old ulof;
+
+  lprintf_nl "MLDonkey is working in %s" file_basedir;
   if not (Sys.file_exists file_basedir) then begin
     lprint_newline ();
     lprintf_nl "creating new MLDonkey base directory in %s\n" file_basedir;
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/daemon/driver/driverCommands.ml ./src/daemon/driver/driverCommands.ml
--- ./src/daemon/driver/driverCommands.ml	2006-02-03 02:10:45.000000000 +0100
+++ ./src/daemon/driver/driverCommands.ml	2006-02-03 21:31:28.000000000 +0100
@@ -1973,6 +1973,31 @@
          _s ""
      ), "debug command (example: disk .)";
 
+     "rlimit", Arg_none (fun o ->
+         let buf = o.conn_buf in
+	 let cpu = Unix2.ml_getrlimit Unix2.RLIMIT_CPU in
+	 let fsize = Unix2.ml_getrlimit Unix2.RLIMIT_FSIZE in
+	 let data = Unix2.ml_getrlimit Unix2.RLIMIT_DATA in
+	 let stack = Unix2.ml_getrlimit Unix2.RLIMIT_STACK in
+	 let core = Unix2.ml_getrlimit Unix2.RLIMIT_CORE in
+	 let rss = Unix2.ml_getrlimit Unix2.RLIMIT_RSS in
+	 let nprof = Unix2.ml_getrlimit Unix2.RLIMIT_NPROF in
+	 let nofile = Unix2.ml_getrlimit Unix2.RLIMIT_NOFILE in
+	 let memlock = Unix2.ml_getrlimit Unix2.RLIMIT_MEMLOCK in
+         let rlimit_as = Unix2.ml_getrlimit Unix2.RLIMIT_AS in
+         Printf.bprintf buf "cpu %d %d\n" cpu.Unix2.rlim_cur cpu.Unix2.rlim_max;
+         Printf.bprintf buf "fsize %d %d\n" fsize.Unix2.rlim_cur fsize.Unix2.rlim_max;
+         Printf.bprintf buf "data %d %d\n" data.Unix2.rlim_cur data.Unix2.rlim_max;
+         Printf.bprintf buf "stack %d %d\n" stack.Unix2.rlim_cur stack.Unix2.rlim_max;
+         Printf.bprintf buf "core %d %d\n" core.Unix2.rlim_cur core.Unix2.rlim_max;
+         Printf.bprintf buf "rss %d %d\n" rss.Unix2.rlim_cur rss.Unix2.rlim_max;
+         Printf.bprintf buf "nprof %d %d\n" nprof.Unix2.rlim_cur nprof.Unix2.rlim_max;
+         Printf.bprintf buf "nofile %d %d\n" nofile.Unix2.rlim_cur nofile.Unix2.rlim_max;
+         Printf.bprintf buf "memlock %d %d\n" memlock.Unix2.rlim_cur memlock.Unix2.rlim_max;
+         Printf.bprintf buf "as %d %d\n" rlimit_as.Unix2.rlim_cur rlimit_as.Unix2.rlim_max;
+         _s ""
+     ), "debug command";
+
     "shares", Arg_none (fun o ->
 
         let buf = o.conn_buf in
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/utils/cdk/unix2.ml ./src/utils/cdk/unix2.ml
--- ./src/utils/cdk/unix2.ml	2006-01-29 19:42:02.000000000 +0100
+++ ./src/utils/cdk/unix2.ml	2006-02-03 21:31:28.000000000 +0100
@@ -173,3 +173,42 @@
         lprintf_nl "%s does not exist and can not be created, exiting..." dirname; exit 73)
   | Sys_error s -> lprintf_nl "%s for directory %s" s dirname; exit 73
   | e -> lprintf_nl "%s for directory %s" (Printexc2.to_string e) dirname; exit 73
+
+(** The resource type to query or set with [getrlimit] or [setrlimit] *)
+type rlimit_resource = RLIMIT_CPU (** CPU time in seconds *)
+                       | RLIMIT_FSIZE (** Maximum file size *)
+                       | RLIMIT_DATA (** Max data size *)
+                       | RLIMIT_STACK (** Max stack size *)
+                       | RLIMIT_CORE (** Max core file size *)
+                       | RLIMIT_RSS (** Max resident set size *)
+                       | RLIMIT_NPROF (** Max number of processes *)
+                       | RLIMIT_NOFILE (** Max number of open files *)
+                       | RLIMIT_MEMLOCK (** Max locked-in-memory address space *)
+                       | RLIMIT_AS (** Address space limit *)
+
+type rlimit = {
+  rlim_cur: int;
+  rlim_max: int
+}
+
+let dummy_rlimit = {
+  rlim_cur = -1;
+  rlim_max = -1
+}
+
+external getrlimit: rlimit_resource -> rlimit = "ml_getrlimit"
+external setrlimit: rlimit_resource -> rlimit -> unit = "ml_setrlimit"
+
+let ml_getrlimit resource =
+  try
+    getrlimit resource
+  with _ -> dummy_rlimit
+
+let ml_setrlimit resource n =
+  let new_rlimit = {
+    rlim_cur = n;
+    rlim_max = n
+  } in
+  try
+    setrlimit resource new_rlimit
+  with _ -> ()
diff -a -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/utils/lib/stubs_c.c ./src/utils/lib/stubs_c.c
--- ./src/utils/lib/stubs_c.c	2006-02-02 01:33:28.000000000 +0100
+++ ./src/utils/lib/stubs_c.c	2006-02-03 21:31:28.000000000 +0100
@@ -28,6 +28,14 @@
 #include <inttypes.h>
 #endif
 
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+
 #define lseek XXXXXXXXX
 #define read XXXXXXXXX
 #define ftruncate XXXXXXXXX
@@ -1225,3 +1233,199 @@
 #endif
   CAMLreturn (v);
 }
+
+value ml_getrlimit(value resource) {
+#ifdef HAVE_GETRLIMIT
+  CAMLparam1(resource);
+  CAMLlocal1(retval);
+  int r;
+  struct rlimit lim;
+
+  switch (Int_val(resource)) {
+  case 0:
+#ifdef RLIMIT_CPU
+    r = RLIMIT_CPU;
+#else
+    r = -1;
+#endif
+    break;
+  case 1:
+#ifdef RLIMIT_FSIZE
+    r = RLIMIT_FSIZE;
+#else
+    r = -1;
+#endif
+    break;
+  case 2:
+#ifdef RLIMIT_DATA
+    r = RLIMIT_DATA;
+#else
+    r = -1;
+#endif
+    break;
+  case 3:
+#ifdef RLIMIT_STACK
+    r = RLIMIT_STACK;
+#else
+    r = -1;
+#endif
+    break;
+  case 4:
+#ifdef RLIMIT_CORE
+    r = RLIMIT_CORE;
+#else
+    r = -1;
+#endif
+    break;
+  case 5:
+#ifdef RLIMIT_RSS
+    r = RLIMIT_RSS;
+#else
+    r = -1;
+#endif
+    break;
+  case 6:
+#ifdef RLIMIT_NPROC
+    r = RLIMIT_NPROC;
+#else
+    r = -1;
+#endif
+    break;
+  case 7:
+#ifdef RLIMIT_NOFILE
+    r = RLIMIT_NOFILE;
+#elif RLIMIT_OFILE
+    r = RLIMIT_OFILE:
+#else
+    r = -1;
+#endif
+    break;
+  case 8:
+#ifdef RLIMIT_MEMLOCK
+    r = RLIMIT_MEMLOCK;
+#else
+    r = -1;
+#endif
+    break;
+  case 9:
+#ifdef RLIMIT_AS
+    r = RLIMIT_AS;
+#else
+    r = -1;
+#endif
+    break;
+  default:
+    errno = EINVAL;
+    uerror("getrlimit", Nothing);    
+  }
+
+  if (getrlimit(r, &lim) < 0) 
+    uerror("getrlimit", Nothing);
+
+  
+  retval = alloc_tuple(2);
+  Field(retval, 0) = Val_int(lim.rlim_cur);
+  Field(retval, 1) = Val_int(lim.rlim_max);
+
+  CAMLreturn(retval);
+
+#else
+  failwith("getrlimit unimplemented");
+#endif
+}
+
+value ml_setrlimit(value resource, value rlimit) {
+#ifdef HAVE_SETRLIMIT
+  int r;
+  struct rlimit lim;
+
+  switch (Int_val(resource)) {
+  case 0:
+#ifdef RLIMIT_CPU
+    r = RLIMIT_CPU;
+#else
+    r = -1;
+#endif
+    break;
+  case 1:
+#ifdef RLIMIT_FSIZE
+    r = RLIMIT_FSIZE;
+#else
+    r = -1;
+#endif
+    break;
+  case 2:
+#ifdef RLIMIT_DATA
+    r = RLIMIT_DATA;
+#else
+    r = -1;
+#endif
+    break;
+  case 3:
+#ifdef RLIMIT_STACK
+    r = RLIMIT_STACK;
+#else
+    r = -1;
+#endif
+    break;
+  case 4:
+#ifdef RLIMIT_CORE
+    r = RLIMIT_CORE;
+#else
+    r = -1;
+#endif
+    break;
+  case 5:
+#ifdef RLIMIT_RSS
+    r = RLIMIT_RSS;
+#else
+    r = -1;
+#endif
+    break;
+  case 6:
+#ifdef RLIMIT_NPROC
+    r = RLIMIT_NPROC;
+#else
+    r = -1;
+#endif
+    break;
+  case 7:
+#ifdef RLIMIT_NOFILE
+    r = RLIMIT_NOFILE;
+#elif RLIMIT_OFILE
+    r = RLIMIT_OFILE:
+#else
+    r = -1;
+#endif
+    break;
+  case 8:
+#ifdef RLIMIT_MEMLOCK
+    r = RLIMIT_MEMLOCK;
+#else
+    r = -1;
+#endif
+    break;
+  case 9:
+#ifdef RLIMIT_AS
+    r = RLIMIT_AS;
+#else
+    r = -1;
+#endif
+    break;
+  default:
+    errno = EINVAL;
+    uerror("getrlimit", Nothing);    
+  }
+
+  lim.rlim_cur = Int_val(Field(rlimit, 0));
+  lim.rlim_max = Int_val(Field(rlimit, 1));
+
+  if (setrlimit(r, &lim) < 0) 
+    uerror("setrlimit", Nothing);
+
+  return Val_unit;
+
+#else
+  failwith("getrlimit unimplemented");
+#endif
+}
