daemon: New option --priority for server and audiod.
authorAndre Noll <maan@tuebingen.mpg.de>
Thu, 8 Oct 2015 16:25:06 +0000 (16:25 +0000)
committerAndre Noll <maan@tuebingen.mpg.de>
Thu, 15 Oct 2015 21:10:57 +0000 (23:10 +0200)
On slow hardware it is useful to set the priority of the para_audiod
process to avoid sound artefacts due to buffer underruns.

This patch adds an option for this purpose which is available for
para_server and para_audiod.

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

index accf0244f957809b9c55b7e56b61127492264d62..e5f9c6be513103334f8a9203e6fbb5079fece853 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 94c4a8cba6b9aae8646491f3eeddfff016914c7b..478b0f47acc94d3098cc17576599032f007b43ca 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 ca3da140b812b8f5728d426e16bfdd8c40979c8f..621420a4340c75fc3eddbe37652c066c0772b8eb 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 732f80873cdf39529e61f158a4d6cc40c2ae0735..75c045817898e8b840dc4734cd547ad282e67580 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 916e88563ea32af2fc75d94c19a74f5c03ea0b64..6aca7bac7f41ddb9a37d4ec96e329a210e275f1f 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 7cb6e5200fa696471e7baaf5a4ee121cdef47755..71a9ec505f6d096a1dc589a9cdf6c1f484e5090d 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);