Merge branch 'refs/heads/t/priority'
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 13 Dec 2015 11:21:44 +0000 (12:21 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 13 Dec 2015 11:22:59 +0000 (12:22 +0100)
A single commit which was cooking in next for two months.

* refs/heads/t/priority:
  daemon: New option --priority for server and audiod.

NEWS
audiod.c
daemon.c
daemon.h
m4/gengetopt/audiod.m4
m4/gengetopt/priority.m4 [new file with mode: 0644]
m4/gengetopt/server.m4
server.c

diff --git a/NEWS b/NEWS
index 7faa0dd..a5bef9f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,7 @@ current master branch "cascading gradient"
        - The check command now also checks the attribute table for
          inconsistencies.
        - New -v flag for the version command (print verbose version string)
+       - New option --priority for para_server and para_audiod.
 
 
 --------------------------------------
index accf024..e5f9c6b 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -1474,6 +1474,7 @@ int main(int argc, char *argv[])
        writer_init();
        if (conf.help_given || conf.detailed_help_given)
                print_help_and_die();
+       daemon_set_priority(conf.priority_arg);
        daemon_drop_privileges_or_die(conf.user_arg, conf.group_arg);
        parse_config_or_die();
        daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no,
index 94c4a8c..478b0f4 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -11,6 +11,7 @@
 #include <sys/types.h> /* getgrnam() */
 #include <grp.h>
 #include <signal.h>
+#include <sys/resource.h>
 
 #include "para.h"
 #include "daemon.h"
@@ -260,6 +261,21 @@ void daemon_log_welcome(const char *name)
        PARA_INFO_LOG("welcome to para_%s-" PACKAGE_VERSION " \n", name);
 }
 
+/**
+ * Renice the calling process.
+ *
+ * \param prio The priority value to set.
+ *
+ * Errors are not considered fatal, but a warning message is logged if the
+ * underlying call to setpriority(2) fails.
+ */
+void daemon_set_priority(int prio)
+{
+       if (setpriority(PRIO_PROCESS, 0, prio) < 0)
+               PARA_WARNING_LOG("could not set priority to %d: %s\n", prio,
+                       strerror(errno));
+}
+
 /**
  * Give up superuser privileges.
  *
index ca3da14..621420a 100644 (file)
--- a/daemon.h
+++ b/daemon.h
@@ -5,6 +5,7 @@ void daemonize(bool parent_waits);
 void daemon_open_log_or_die(void);
 void daemon_close_log(void);
 void daemon_log_welcome(const char *whoami);
+void daemon_set_priority(int prio);
 void daemon_drop_privileges_or_die(const char *username, const char *groupname);
 void daemon_set_start_time(void);
 time_t daemon_get_uptime(const struct timeval *current_time);
index 732f808..75c0458 100644 (file)
@@ -20,6 +20,7 @@ include(log_timing.m4)
 include(daemon.m4)
 include(user.m4)
 include(group.m4)
+include(priority.m4)
 
 <qu>
 ########################
diff --git a/m4/gengetopt/priority.m4 b/m4/gengetopt/priority.m4
new file mode 100644 (file)
index 0000000..0b37dc0
--- /dev/null
@@ -0,0 +1,16 @@
+option "priority" -
+#~~~~~~~~~~~~~~~~~~
+"adjust scheduling priority"
+int typestr = "prio"
+default = "0"
+optional
+details = "
+       The priority (also known as nice value) is a value in the range -20
+       to 19. Lower priorities cause more favorable scheduling. Since only
+       privileged processes may request a negative priority, specifying
+       a negative value works only if the daemon is started with root
+       privileges.
+
+       Failure to set the given priority value is not considered an error
+       but a log message is printed in this case.
+"
index 916e885..6aca7ba 100644 (file)
@@ -18,6 +18,7 @@ include(color.m4)
 include(daemon.m4)
 include(user.m4)
 include(group.m4)
+include(priority.m4)
 
 <qu>
 option "port" p
index 7cb6e52..71a9ec5 100644 (file)
--- a/server.c
+++ b/server.c
@@ -463,6 +463,7 @@ static void server_init(int argc, char **argv)
        version_handle_flag("server", conf.version_given);
        if (conf.help_given || conf.detailed_help_given)
                print_help_and_die();
+       daemon_set_priority(conf.priority_arg);
        daemon_drop_privileges_or_die(conf.user_arg, conf.group_arg);
        /* parse config file, open log and set defaults */
        parse_config_or_die(0);