]> code.delx.au - pulseaudio/blobdiff - src/daemon/main.c
daemon: optionally call mlockall() on startup
[pulseaudio] / src / daemon / main.c
index 47ca267366aaf41f7f370d17e4f8bed848694f0c..58f8d66031481e4286ad32b0694d0e2929039b54 100644 (file)
 
 #include <liboil/liboil.h>
 
+#ifdef HAVE_SYS_MMAN_H
+#include <sys/mman.h>
+#endif
+
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #endif
 #include <pulsecore/thread.h>
 #include <pulsecore/once.h>
 #include <pulsecore/shm.h>
+#include <pulsecore/memtrap.h>
+#ifdef HAVE_DBUS
+#include <pulsecore/dbus-shared.h>
+#endif
 
 #include "cmdline.h"
 #include "cpulimit.h"
@@ -330,6 +338,42 @@ static void set_all_rlimits(const pa_daemon_conf *conf) {
 }
 #endif
 
+#ifdef HAVE_DBUS
+static pa_dbus_connection *register_dbus(pa_core *c) {
+    DBusError error;
+    pa_dbus_connection *conn;
+
+    dbus_error_init(&error);
+
+    if (!(conn = pa_dbus_bus_get(c, pa_in_system_mode() ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
+        pa_log_warn("Unable to contact D-Bus: %s: %s", error.name, error.message);
+        goto fail;
+    }
+
+    if (dbus_bus_request_name(pa_dbus_connection_get(conn), "org.pulseaudio.Server", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
+        pa_log_debug("Got org.pulseaudio.Server!");
+        return conn;
+    }
+
+    if (dbus_error_is_set(&error))
+        pa_log_warn("Failed to acquire org.pulseaudio.Server: %s: %s", error.name, error.message);
+    else
+        pa_log_warn("D-Bus name org.pulseaudio.Server already taken. Weird shit!");
+
+    /* PA cannot be started twice by the same user and hence we can
+     * ignore mostly the case that org.pulseaudio.Server is already
+     * taken. */
+
+fail:
+
+    if (conn)
+        pa_dbus_connection_unref(conn);
+
+    dbus_error_free(&error);
+    return NULL;
+}
+#endif
+
 int main(int argc, char *argv[]) {
     pa_core *c = NULL;
     pa_strbuf *buf = NULL;
@@ -352,9 +396,12 @@ int main(int argc, char *argv[]) {
 #endif
     int autospawn_fd = -1;
     pa_bool_t autospawn_locked = FALSE;
+#ifdef HAVE_DBUS
+    pa_dbus_connection *dbus = NULL;
+#endif
 
     pa_log_set_ident("pulseaudio");
-    pa_log_set_level(PA_LOG_INFO);
+    pa_log_set_level(PA_LOG_NOTICE);
     pa_log_set_flags(PA_LOG_COLORS|PA_LOG_PRINT_FILE|PA_LOG_PRINT_LEVEL, PA_LOG_RESET);
 
 #if defined(__linux__) && defined(__OPTIMIZE__)
@@ -399,7 +446,7 @@ int main(int argc, char *argv[]) {
         pa_limit_caps();
 
         /* When capabilities are not supported we will not be able to
-         * aquire RT sched anymore. But yes, that's the way it is. It
+         * acquire RT sched anymore. But yes, that's the way it is. It
          * is just too risky tun let PA run as root all the time. */
     }
 
@@ -510,7 +557,7 @@ int main(int argc, char *argv[]) {
 
         if ((conf->high_priority && !allow_high_priority) ||
             (conf->realtime_scheduling && !allow_realtime))
-            pa_log_notice(_("Called SUID root and real-time and/or high-priority scheduling was requested in the configuration. However, we lack the necessary privileges:\n"
+            pa_log_info(_("Called SUID root and real-time and/or high-priority scheduling was requested in the configuration. However, we lack the necessary privileges:\n"
                             "We are not in group '%s', PolicyKit refuse to grant us the requested privileges and we have no increase RLIMIT_NICE/RLIMIT_RTPRIO resource limits.\n"
                             "For enabling real-time/high-priority scheduling please acquire the appropriate PolicyKit privileges, or become a member of '%s', or increase the RLIMIT_NICE/RLIMIT_RTPRIO resource limits for this user."),
                           PA_REALTIME_GROUP, PA_REALTIME_GROUP);
@@ -535,7 +582,7 @@ int main(int argc, char *argv[]) {
 #endif
 
     if (conf->high_priority && !pa_can_high_priority()) {
-        pa_log_warn(_("High-priority scheduling enabled in configuration but not allowed by policy."));
+        pa_log_info(_("High-priority scheduling enabled in configuration but not allowed by policy."));
         conf->high_priority = FALSE;
     }
 
@@ -581,7 +628,7 @@ int main(int argc, char *argv[]) {
     }
 
     if (conf->realtime_scheduling && !pa_can_realtime()) {
-        pa_log_warn(_("Real-time scheduling enabled in configuration but not allowed by policy."));
+        pa_log_info(_("Real-time scheduling enabled in configuration but not allowed by policy."));
         conf->realtime_scheduling = FALSE;
     }
 
@@ -866,6 +913,11 @@ int main(int argc, char *argv[]) {
     pa_log_info(_("Machine ID is %s."), s);
     pa_xfree(s);
 
+    if ((s = pa_session_id())) {
+            pa_log_info(_("Session ID is %s."), s);
+            pa_xfree(s);
+    }
+
     if (!(s = pa_get_runtime_dir()))
         goto finish;
     pa_log_info(_("Using runtime directory %s."), s);
@@ -898,9 +950,7 @@ int main(int argc, char *argv[]) {
         valid_pid_file = TRUE;
     }
 
-#ifdef SIGPIPE
-    signal(SIGPIPE, SIG_IGN);
-#endif
+    pa_disable_sigpipe();
 
     if (pa_rtclock_hrtimer())
         pa_log_info(_("Fresh high-resolution timers available! Bon appetit!"));
@@ -914,6 +964,19 @@ int main(int argc, char *argv[]) {
     pa_rtsig_configure(SIGRTMIN, SIGRTMAX-1);
 #endif
 
+    if (conf->lock_memory) {
+#ifdef HAVE_SYS_MMAN_H
+        if (mlockall(MCL_FUTURE) < 0)
+            pa_log_warn("mlockall() failed: %s", pa_cstrerror(errno));
+        else
+            pa_log_info("Sucessfully locked process into memory.");
+#else
+        pa_log_warn("Memory locking requested but not supported on platform.");
+#endif
+    }
+
+    pa_memtrap_install();
+
     pa_assert_se(mainloop = pa_mainloop_new());
 
     if (!(c = pa_core_new(pa_mainloop_get_api(mainloop), !conf->disable_shm, conf->shm_size))) {
@@ -997,6 +1060,10 @@ int main(int argc, char *argv[]) {
     }
 #endif
 
+#ifdef HAVE_DBUS
+    dbus = register_dbus(c);
+#endif
+
     pa_log_info(_("Daemon startup complete."));
 
     retval = 0;
@@ -1006,6 +1073,10 @@ int main(int argc, char *argv[]) {
     pa_log_info(_("Daemon shutdown initiated."));
 
 finish:
+#ifdef HAVE_DBUS
+    if (dbus)
+        pa_dbus_connection_unref(dbus);
+#endif
 
     if (autospawn_fd >= 0) {
         if (autospawn_locked)