From: Andre Noll <maan@tuebingen.mpg.de>
Date: Thu, 8 Oct 2015 16:25:06 +0000 (+0000)
Subject: daemon: New option --priority for server and audiod.
X-Git-Tag: v0.5.6~84^2
X-Git-Url: https://git.tue.mpg.de/?a=commitdiff_plain;h=bbd70648da0f4168df34c1f1651d67af089e7be6;p=paraslash.git

daemon: New option --priority for server and audiod.

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.
---

diff --git a/audiod.c b/audiod.c
index accf0244..e5f9c6be 100644
--- 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,
diff --git a/daemon.c b/daemon.c
index 94c4a8cb..478b0f47 100644
--- 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.
  *
diff --git a/daemon.h b/daemon.h
index ca3da140..621420a4 100644
--- 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);
diff --git a/m4/gengetopt/audiod.m4 b/m4/gengetopt/audiod.m4
index 732f8087..75c04581 100644
--- a/m4/gengetopt/audiod.m4
+++ b/m4/gengetopt/audiod.m4
@@ -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
index 00000000..0b37dc06
--- /dev/null
+++ b/m4/gengetopt/priority.m4
@@ -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.
+"
diff --git a/m4/gengetopt/server.m4 b/m4/gengetopt/server.m4
index 916e8856..6aca7bac 100644
--- a/m4/gengetopt/server.m4
+++ b/m4/gengetopt/server.m4
@@ -18,6 +18,7 @@ include(color.m4)
 include(daemon.m4)
 include(user.m4)
 include(group.m4)
+include(priority.m4)
 
 <qu>
 option "port" p
diff --git a/server.c b/server.c
index 7cb6e520..71a9ec50 100644
--- 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);