[pulseaudio-commits] r1116 - in /trunk: src/daemon/daemon-conf.c src/daemon/daemon-conf.h src/daemon/daemon.conf.in src/daemon/main.c todo

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Wed Jul 19 18:25:38 PDT 2006


Author: lennart
Date: Thu Jul 20 03:25:37 2006
New Revision: 1116

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=1116&root=pulseaudio&view=rev
Log:
add support to set resource limits for the daemon and set some of them to some sane values

Modified:
    trunk/src/daemon/daemon-conf.c
    trunk/src/daemon/daemon-conf.h
    trunk/src/daemon/daemon.conf.in
    trunk/src/daemon/main.c
    trunk/todo

Modified: trunk/src/daemon/daemon-conf.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/daemon/daemon-conf.c?rev=1116&root=pulseaudio&r1=1115&r2=1116&view=diff
==============================================================================
--- trunk/src/daemon/daemon-conf.c (original)
+++ trunk/src/daemon/daemon-conf.c Thu Jul 20 03:25:37 2006
@@ -73,6 +73,20 @@
     .config_file = NULL,
     .use_pid_file = 1,
     .system_instance = 0
+#ifdef HAVE_SYS_RESOURCE_H
+    , .rlimit_as = { .value = 0, .is_set = 0 },
+    .rlimit_core = { .value = 0, .is_set = 0 },
+    .rlimit_data = { .value = 0, .is_set = 0 },
+    .rlimit_fsize = { .value = 0, .is_set = 0 },
+    .rlimit_nofile = { .value = 25, .is_set = 1 },
+    .rlimit_stack = { .value = 0, .is_set = 0 }
+#ifdef RLIMIT_NPROC
+    , .rlimit_nproc = { .value = 0, .is_set = 0 }
+#endif
+#ifdef RLIMIT_MEMLOCK
+    , .rlimit_memlock = { .value = 0, .is_set = 1 }
+#endif
+#endif
 };
 
 pa_daemon_conf* pa_daemon_conf_new(void) {
@@ -179,6 +193,30 @@
     if (pa_daemon_conf_set_resample_method(c, rvalue) < 0) {
         pa_log(__FILE__": [%s:%u] Inavalid resample method '%s'.", filename, line, rvalue);
         return -1;
+    }
+
+    return 0;
+}
+
+static int parse_rlimit(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
+    pa_rlimit *r = data;
+    assert(filename);
+    assert(lvalue);
+    assert(rvalue);
+    assert(r);
+
+    if (rvalue[strspn(rvalue, "\t ")] == 0) {
+        /* Empty string */
+        r->is_set = 0;
+        r->value = 0;
+    } else {
+        int32_t k;
+        if (pa_atoi(rvalue, &k) < 0) {
+            pa_log(__FILE__": [%s:%u] Inavalid rlimit '%s'.", filename, line, rvalue);
+            return -1;
+        }
+        r->is_set = k >= 0;
+        r->value = k >= 0 ? (rlim_t) k : 0;
     }
 
     return 0;
@@ -204,6 +242,20 @@
         { "resample-method",         parse_resample_method,   NULL },
         { "use-pid-file",            pa_config_parse_bool,    NULL },
         { "system-instance",         pa_config_parse_bool,    NULL },
+#ifdef HAVE_SYS_RESOURCE_H
+        { "rlimit-as",               parse_rlimit,            NULL },
+        { "rlimit-core",             parse_rlimit,            NULL },
+        { "rlimit-data",             parse_rlimit,            NULL },
+        { "rlimit-fsize",            parse_rlimit,            NULL },
+        { "rlimit-nofile",           parse_rlimit,            NULL },
+        { "rlimit-stack",            parse_rlimit,            NULL },
+#ifdef RLIMIT_NPROC
+        { "rlimit-nproc",            parse_rlimit,            NULL },
+#endif
+#ifdef RLIMIT_MEMLOCK
+        { "rlimit-memlock",          parse_rlimit,            NULL },
+#endif
+#endif
         { NULL,                      NULL,                    NULL },
     };
     
@@ -222,6 +274,24 @@
     table[12].data = c;
     table[13].data = &c->use_pid_file;
     table[14].data = &c->system_instance;
+#ifdef HAVE_SYS_RESOURCE_H
+    table[15].data = &c->rlimit_as;
+    table[16].data = &c->rlimit_core;
+    table[17].data = &c->rlimit_data;
+    table[18].data = &c->rlimit_fsize;
+    table[19].data = &c->rlimit_nofile;
+    table[20].data = &c->rlimit_stack;
+#ifdef RLIMIT_NPROC
+    table[21].data = &c->rlimit_nproc;
+#endif
+#ifdef RLIMIT_MEMLOCK
+#ifndef RLIMIT_NPROC
+#error "Houston, we have a numbering problem!"
+#endif
+    table[22].data = &c->rlimit_memlock;
+#endif
+#endif
+    
     
     pa_xfree(c->config_file);
     c->config_file = NULL;
@@ -289,6 +359,20 @@
     pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
     pa_strbuf_printf(s, "use-pid-file = %i\n", c->use_pid_file);
     pa_strbuf_printf(s, "system-instance = %i\n", !!c->system_instance);
+#ifdef HAVE_SYS_RESOURCE_H
+    pa_strbuf_printf(s, "rlimit-as = %li\n", c->rlimit_as.is_set ? (long int) c->rlimit_as.value : -1);
+    pa_strbuf_printf(s, "rlimit-core = %li\n", c->rlimit_core.is_set ? (long int) c->rlimit_core.value : -1);
+    pa_strbuf_printf(s, "rlimit-data = %li\n", c->rlimit_data.is_set ? (long int) c->rlimit_data.value : -1);
+    pa_strbuf_printf(s, "rlimit-fsize = %li\n", c->rlimit_fsize.is_set ? (long int) c->rlimit_fsize.value : -1);
+    pa_strbuf_printf(s, "rlimit-nofile = %li\n", c->rlimit_nofile.is_set ? (long int) c->rlimit_nofile.value : -1);
+    pa_strbuf_printf(s, "rlimit-stack = %li\n", c->rlimit_stack.is_set ? (long int) c->rlimit_stack.value : -1);
+#ifdef RLIMIT_NPROC
+    pa_strbuf_printf(s, "rlimit-nproc = %li\n", c->rlimit_nproc.is_set ? (long int) c->rlimit_nproc.value : -1);
+#endif
+#ifdef RLIMIT_MEMLOCK
+    pa_strbuf_printf(s, "rlimit-memlock = %li\n", c->rlimit_memlock.is_set ? (long int) c->rlimit_memlock.value : -1);
+#endif
+#endif
     
     return pa_strbuf_tostring_free(s);
 }

Modified: trunk/src/daemon/daemon-conf.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/daemon/daemon-conf.h?rev=1116&root=pulseaudio&r1=1115&r2=1116&view=diff
==============================================================================
--- trunk/src/daemon/daemon-conf.h (original)
+++ trunk/src/daemon/daemon-conf.h Thu Jul 20 03:25:37 2006
@@ -24,6 +24,10 @@
 
 #include <pulsecore/log.h>
 
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+
 /* The actual command to execute */
 typedef enum pa_daemon_conf_cmd {
     PA_CMD_DAEMON,  /* the default */
@@ -34,6 +38,13 @@
     PA_CMD_KILL,
     PA_CMD_CHECK
 } pa_daemon_conf_cmd_t;
+
+#ifdef HAVE_SYS_RESOURCE_H
+typedef struct pa_rlimit {
+    rlim_t value;
+    int is_set;
+} pa_rlimit;
+#endif
 
 /* A structure containing configuration data for the PulseAudio server . */
 typedef struct pa_daemon_conf {
@@ -53,6 +64,17 @@
     pa_log_level_t log_level;
     int resample_method;
     char *config_file;
+    
+#ifdef HAVE_SYS_RESOURCE_H
+    pa_rlimit rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
+#ifdef RLIMIT_NPROC
+    pa_rlimit rlimit_nproc;
+#endif
+#ifdef RLIMIT_MEMLOCK
+    pa_rlimit rlimit_memlock;
+#endif
+#endif
+    
 } pa_daemon_conf;
 
 /* Allocate a new structure and fill it with sane defaults */

Modified: trunk/src/daemon/daemon.conf.in
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/daemon/daemon.conf.in?rev=1116&root=pulseaudio&r1=1115&r2=1116&view=diff
==============================================================================
--- trunk/src/daemon/daemon.conf.in (original)
+++ trunk/src/daemon/daemon.conf.in Thu Jul 20 03:25:37 2006
@@ -81,3 +81,13 @@
 
 ## Run the daemon as system-wide instance, requires root priviliges
 ; system-instance = 0
+
+## Resource limits, see getrlimit(2) for more information
+; rlimit-as = -1
+; rlimit-core = -1
+; rlimit-data = -1
+; rlimit-fsize = -1
+; rlimit-nofile = 25
+; rlimit-stack = -1
+; rlimit-nproc = -1
+; rlimit-memlock = 25

Modified: trunk/src/daemon/main.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/daemon/main.c?rev=1116&root=pulseaudio&r1=1115&r2=1116&view=diff
==============================================================================
--- trunk/src/daemon/main.c (original)
+++ trunk/src/daemon/main.c Thu Jul 20 03:25:37 2006
@@ -258,6 +258,37 @@
     return 0;
 }
 
+#ifdef HAVE_SYS_RESOURCE_H
+
+static void set_one_rlimit(const pa_rlimit *r, int resource, const char *name) {
+    struct rlimit rl;
+    assert(r);
+
+    if (!r->is_set)
+        return;
+
+    rl.rlim_cur = rl.rlim_max = r->value;
+
+    if (setrlimit(resource, &rl) < 0)
+        pa_log_warn(__FILE__": setrlimit(%s, (%u, %u)) failed: %s", name, (unsigned) r->value, (unsigned) r->value, pa_cstrerror(errno));
+}
+
+static void set_all_rlimits(const pa_daemon_conf *conf) {
+    set_one_rlimit(&conf->rlimit_as, RLIMIT_AS, "RLIMIT_AS");
+    set_one_rlimit(&conf->rlimit_core, RLIMIT_CORE, "RLIMIT_CORE");
+    set_one_rlimit(&conf->rlimit_data, RLIMIT_DATA, "RLIMIT_DATA");
+    set_one_rlimit(&conf->rlimit_fsize, RLIMIT_FSIZE, "RLIMIT_FSIZE");
+    set_one_rlimit(&conf->rlimit_nofile, RLIMIT_NOFILE, "RLIMIT_NOFILE");
+    set_one_rlimit(&conf->rlimit_stack, RLIMIT_STACK, "RLIMIT_STACK");
+#ifdef RLIMIT_NPROC
+    set_one_rlimit(&conf->rlimit_nproc, RLIMIT_NPROC, "RLIMIT_NPROC");
+#endif
+#ifdef RLIMIT_MEMLOCK
+    set_one_rlimit(&conf->rlimit_memlock, RLIMIT_MEMLOCK, "RLIMIT_MEMLOCK");
+#endif
+}
+#endif
+
 int main(int argc, char *argv[]) {
     pa_core *c;
     pa_strbuf *buf = NULL;
@@ -335,7 +366,7 @@
 
     if (suid_root)
         pa_drop_root();
-    
+
     if (conf->dl_search_path)
         lt_dlsetsearchpath(conf->dl_search_path);
 
@@ -502,6 +533,10 @@
         valid_pid_file = 1;
     }
 
+#ifdef HAVE_SYS_RESOURCE_H
+    set_all_rlimits(conf);
+#endif
+    
 #ifdef SIGPIPE
     signal(SIGPIPE, SIG_IGN);
 #endif

Modified: trunk/todo
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/todo?rev=1116&root=pulseaudio&r1=1115&r2=1116&view=diff
==============================================================================
--- trunk/todo (original)
+++ trunk/todo Thu Jul 20 03:25:37 2006
@@ -35,7 +35,7 @@
 - key rings for auth
 - challenge response auth
 - sasl auth 
-- setrlimit
+- IP ACLs
 
 Long term:
 - pass meta info for hearing impaired




More information about the pulseaudio-commits mailing list