[PATCH] Fix -k startup option.

Tomash Brechko tomash.brechko at gmail.com
Fri Dec 14 17:43:22 UTC 2007


Seems like -k never worked: it requires privileges to lock the memory,
but they were dropped before the call, and return value was never tested
to see if the call succedded.

Also update -k documentation.
---
 trunk/server/memcached.c |   29 +++++++++++++++++++----------
 1 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/trunk/server/memcached.c b/trunk/server/memcached.c
index 8a7c498..15c090e 100644
--- a/trunk/server/memcached.c
+++ b/trunk/server/memcached.c
@@ -2694,7 +2694,12 @@ static void usage(void) {
            "-m <num>      max memory to use for items in megabytes, default is 64 MB\n"
            "-M            return error on memory exhausted (rather than removing items)\n"
            "-c <num>      max simultaneous connections, default is 1024\n"
-           "-k            lock down all paged memory\n"
+           "-k            lock down all paged memory.  Note that there is a\n"
+           "              limit on how much memory you may lock.  Trying to\n"
+           "              allocate more than that would fail, so be sure you\n"
+           "              set the limit correctly for the user you started\n"
+           "              the daemon with (not for -u <username> user;\n"
+           "              under sh this is done with 'ulimit -S -l NUM_KB').\n"
            "-v            verbose (print errors/warnings while in event loop)\n"
            "-vv           very verbose (also print client commands/reponses)\n"
            "-h            print this help and exit\n"
@@ -3001,6 +3006,19 @@ int main (int argc, char **argv) {
         }
     }
 
+    /* lock paged memory if needed */
+    if (lock_memory) {
+#ifdef HAVE_MLOCKALL
+        int res = mlockall(MCL_CURRENT | MCL_FUTURE);
+        if (res != 0) {
+            fprintf(stderr, "warning: mlockall() failed: %s\n",
+                    strerror(errno));
+        }
+#else
+        fprintf(stderr, "warning: mlockall() not supported on this platform.  proceeding without.\n");
+#endif
+    }
+
     /* lose root privileges if we have them */
     if (getuid() == 0 || geteuid() == 0) {
         if (username == 0 || *username == '\0') {
@@ -3060,15 +3078,6 @@ int main (int argc, char **argv) {
         memset(buckets, 0, sizeof(int) * MAX_BUCKETS);
     }
 
-    /* lock paged memory if needed */
-    if (lock_memory) {
-#ifdef HAVE_MLOCKALL
-        mlockall(MCL_CURRENT | MCL_FUTURE);
-#else
-        fprintf(stderr, "warning: mlockall() not supported on this platform.  proceeding without.\n");
-#endif
-    }
-
     /*
      * ignore SIGPIPE signals; we can use errno==EPIPE if we
      * need that information
-- 
1.5.3.6.961.gecf4


More information about the memcached mailing list