~binli/ubuntu/vivid/pulseaudio/load-extcon-module

« back to all changes in this revision

Viewing changes to .pc/0303-lfe-filter-change-the-crossover-frequency-as-a-param.patch/src/daemon/main.c

  • Committer: Bin Li
  • Date: 2016-01-23 15:04:48 UTC
  • Revision ID: bin.li@canonical.com-20160123150448-5ockvw4p5xxntda4
init the 1:6.0-0ubuntu9.15 from silo 12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***
 
2
  This file is part of PulseAudio.
 
3
 
 
4
  Copyright 2004-2006 Lennart Poettering
 
5
  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
 
6
 
 
7
  PulseAudio is free software; you can redistribute it and/or modify
 
8
  it under the terms of the GNU Lesser General Public License as published
 
9
  by the Free Software Foundation; either version 2.1 of the License,
 
10
  or (at your option) any later version.
 
11
 
 
12
  PulseAudio is distributed in the hope that it will be useful, but
 
13
  WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
  General Public License for more details.
 
16
 
 
17
  You should have received a copy of the GNU Lesser General Public License
 
18
  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
 
19
***/
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <unistd.h>
 
26
#include <errno.h>
 
27
#include <string.h>
 
28
#include <stdlib.h>
 
29
#include <stdio.h>
 
30
#include <signal.h>
 
31
#include <stddef.h>
 
32
#include <ltdl.h>
 
33
#include <limits.h>
 
34
#include <unistd.h>
 
35
#include <locale.h>
 
36
#include <sys/types.h>
 
37
#include <sys/stat.h>
 
38
 
 
39
#ifdef HAVE_SYS_MMAN_H
 
40
#include <sys/mman.h>
 
41
#endif
 
42
 
 
43
#ifdef HAVE_PWD_H
 
44
#include <pwd.h>
 
45
#endif
 
46
#ifdef HAVE_GRP_H
 
47
#include <grp.h>
 
48
#endif
 
49
 
 
50
#ifdef HAVE_LIBWRAP
 
51
#include <syslog.h>
 
52
#include <tcpd.h>
 
53
#endif
 
54
 
 
55
#ifdef HAVE_DBUS
 
56
#include <dbus/dbus.h>
 
57
#endif
 
58
 
 
59
#ifdef HAVE_SYSTEMD_DAEMON
 
60
#include <systemd/sd-daemon.h>
 
61
#endif
 
62
 
 
63
#include <pulse/client-conf.h>
 
64
#include <pulse/mainloop.h>
 
65
#include <pulse/mainloop-signal.h>
 
66
#include <pulse/timeval.h>
 
67
#include <pulse/xmalloc.h>
 
68
 
 
69
#include <pulsecore/i18n.h>
 
70
#include <pulsecore/lock-autospawn.h>
 
71
#include <pulsecore/socket.h>
 
72
#include <pulsecore/core-error.h>
 
73
#include <pulsecore/core-rtclock.h>
 
74
#include <pulsecore/core-scache.h>
 
75
#include <pulsecore/core.h>
 
76
#include <pulsecore/inotify-wrapper.h>
 
77
#include <pulsecore/module.h>
 
78
#include <pulsecore/cli-command.h>
 
79
#include <pulsecore/log.h>
 
80
#include <pulsecore/core-util.h>
 
81
#include <pulsecore/sioman.h>
 
82
#include <pulsecore/cli-text.h>
 
83
#include <pulsecore/pid.h>
 
84
#include <pulsecore/random.h>
 
85
#include <pulsecore/macro.h>
 
86
#include <pulsecore/shm.h>
 
87
#include <pulsecore/memtrap.h>
 
88
#include <pulsecore/strlist.h>
 
89
#ifdef HAVE_DBUS
 
90
#include <pulsecore/dbus-shared.h>
 
91
#endif
 
92
#include <pulsecore/cpu.h>
 
93
 
 
94
#include "cmdline.h"
 
95
#include "cpulimit.h"
 
96
#include "daemon-conf.h"
 
97
#include "dumpmodules.h"
 
98
#include "caps.h"
 
99
#include "ltdl-bind-now.h"
 
100
#include "server-lookup.h"
 
101
 
 
102
#ifdef HAVE_LIBWRAP
 
103
/* Only one instance of these variables */
 
104
int allow_severity = LOG_INFO;
 
105
int deny_severity = LOG_WARNING;
 
106
#endif
 
107
 
 
108
#ifdef HAVE_OSS_WRAPPER
 
109
/* padsp looks for this symbol in the running process and disables
 
110
 * itself if it finds it and it is set to 7 (which is actually a bit
 
111
 * mask). For details see padsp. */
 
112
int __padsp_disabled__ = 7;
 
113
#endif
 
114
 
 
115
static void signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
 
116
    pa_log_info("Got signal %s.", pa_sig2str(sig));
 
117
 
 
118
    switch (sig) {
 
119
#ifdef SIGUSR1
 
120
        case SIGUSR1:
 
121
            pa_module_load(userdata, "module-cli", NULL);
 
122
            break;
 
123
#endif
 
124
 
 
125
#ifdef SIGUSR2
 
126
        case SIGUSR2:
 
127
            pa_module_load(userdata, "module-cli-protocol-unix", NULL);
 
128
            break;
 
129
#endif
 
130
 
 
131
#ifdef SIGHUP
 
132
        case SIGHUP: {
 
133
            char *c = pa_full_status_string(userdata);
 
134
            pa_log_notice("%s", c);
 
135
            pa_xfree(c);
 
136
            return;
 
137
        }
 
138
#endif
 
139
 
 
140
        case SIGINT:
 
141
        case SIGTERM:
 
142
        default:
 
143
            pa_log_info("Exiting.");
 
144
            m->quit(m, 1);
 
145
            break;
 
146
    }
 
147
}
 
148
 
 
149
#if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
 
150
 
 
151
static int change_user(void) {
 
152
    struct passwd *pw;
 
153
    struct group * gr;
 
154
    int r;
 
155
 
 
156
    /* This function is called only in system-wide mode. It creates a
 
157
     * runtime dir in /var/run/ with proper UID/GID and drops privs
 
158
     * afterwards. */
 
159
 
 
160
    if (!(pw = getpwnam(PA_SYSTEM_USER))) {
 
161
        pa_log(_("Failed to find user '%s'."), PA_SYSTEM_USER);
 
162
        return -1;
 
163
    }
 
164
 
 
165
    if (!(gr = getgrnam(PA_SYSTEM_GROUP))) {
 
166
        pa_log(_("Failed to find group '%s'."), PA_SYSTEM_GROUP);
 
167
        return -1;
 
168
    }
 
169
 
 
170
    pa_log_info("Found user '%s' (UID %lu) and group '%s' (GID %lu).",
 
171
                PA_SYSTEM_USER, (unsigned long) pw->pw_uid,
 
172
                PA_SYSTEM_GROUP, (unsigned long) gr->gr_gid);
 
173
 
 
174
    if (pw->pw_gid != gr->gr_gid) {
 
175
        pa_log(_("GID of user '%s' and of group '%s' don't match."), PA_SYSTEM_USER, PA_SYSTEM_GROUP);
 
176
        return -1;
 
177
    }
 
178
 
 
179
    if (!pa_streq(pw->pw_dir, PA_SYSTEM_RUNTIME_PATH))
 
180
        pa_log_warn(_("Home directory of user '%s' is not '%s', ignoring."), PA_SYSTEM_USER, PA_SYSTEM_RUNTIME_PATH);
 
181
 
 
182
    if (pa_make_secure_dir(PA_SYSTEM_RUNTIME_PATH, 0755, pw->pw_uid, gr->gr_gid, true) < 0) {
 
183
        pa_log(_("Failed to create '%s': %s"), PA_SYSTEM_RUNTIME_PATH, pa_cstrerror(errno));
 
184
        return -1;
 
185
    }
 
186
 
 
187
    if (pa_make_secure_dir(PA_SYSTEM_STATE_PATH, 0700, pw->pw_uid, gr->gr_gid, true) < 0) {
 
188
        pa_log(_("Failed to create '%s': %s"), PA_SYSTEM_STATE_PATH, pa_cstrerror(errno));
 
189
        return -1;
 
190
    }
 
191
 
 
192
    /* We don't create the config dir here, because we don't need to write to it */
 
193
 
 
194
    if (initgroups(PA_SYSTEM_USER, gr->gr_gid) != 0) {
 
195
        pa_log(_("Failed to change group list: %s"), pa_cstrerror(errno));
 
196
        return -1;
 
197
    }
 
198
 
 
199
#if defined(HAVE_SETRESGID)
 
200
    r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
 
201
#elif defined(HAVE_SETEGID)
 
202
    if ((r = setgid(gr->gr_gid)) >= 0)
 
203
        r = setegid(gr->gr_gid);
 
204
#elif defined(HAVE_SETREGID)
 
205
    r = setregid(gr->gr_gid, gr->gr_gid);
 
206
#else
 
207
#error "No API to drop privileges"
 
208
#endif
 
209
 
 
210
    if (r < 0) {
 
211
        pa_log(_("Failed to change GID: %s"), pa_cstrerror(errno));
 
212
        return -1;
 
213
    }
 
214
 
 
215
#if defined(HAVE_SETRESUID)
 
216
    r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
 
217
#elif defined(HAVE_SETEUID)
 
218
    if ((r = setuid(pw->pw_uid)) >= 0)
 
219
        r = seteuid(pw->pw_uid);
 
220
#elif defined(HAVE_SETREUID)
 
221
    r = setreuid(pw->pw_uid, pw->pw_uid);
 
222
#else
 
223
#error "No API to drop privileges"
 
224
#endif
 
225
 
 
226
    if (r < 0) {
 
227
        pa_log(_("Failed to change UID: %s"), pa_cstrerror(errno));
 
228
        return -1;
 
229
    }
 
230
 
 
231
    pa_drop_caps();
 
232
 
 
233
    pa_set_env("USER", PA_SYSTEM_USER);
 
234
    pa_set_env("USERNAME", PA_SYSTEM_USER);
 
235
    pa_set_env("LOGNAME", PA_SYSTEM_USER);
 
236
    pa_set_env("HOME", PA_SYSTEM_RUNTIME_PATH);
 
237
 
 
238
    /* Relevant for pa_runtime_path() */
 
239
    if (!getenv("PULSE_RUNTIME_PATH"))
 
240
        pa_set_env("PULSE_RUNTIME_PATH", PA_SYSTEM_RUNTIME_PATH);
 
241
 
 
242
    if (!getenv("PULSE_CONFIG_PATH"))
 
243
        pa_set_env("PULSE_CONFIG_PATH", PA_SYSTEM_CONFIG_PATH);
 
244
 
 
245
    if (!getenv("PULSE_STATE_PATH"))
 
246
        pa_set_env("PULSE_STATE_PATH", PA_SYSTEM_STATE_PATH);
 
247
 
 
248
    pa_log_info("Successfully changed user to \"" PA_SYSTEM_USER "\".");
 
249
 
 
250
    return 0;
 
251
}
 
252
 
 
253
#else /* HAVE_PWD_H && HAVE_GRP_H */
 
254
 
 
255
static int change_user(void) {
 
256
    pa_log(_("System wide mode unsupported on this platform."));
 
257
    return -1;
 
258
}
 
259
 
 
260
#endif /* HAVE_PWD_H && HAVE_GRP_H */
 
261
 
 
262
#ifdef HAVE_SYS_RESOURCE_H
 
263
 
 
264
static int set_one_rlimit(const pa_rlimit *r, int resource, const char *name) {
 
265
    struct rlimit rl;
 
266
    pa_assert(r);
 
267
 
 
268
    if (!r->is_set)
 
269
        return 0;
 
270
 
 
271
    rl.rlim_cur = rl.rlim_max = r->value;
 
272
 
 
273
    if (setrlimit(resource, &rl) < 0) {
 
274
        pa_log_info("setrlimit(%s, (%u, %u)) failed: %s", name, (unsigned) r->value, (unsigned) r->value, pa_cstrerror(errno));
 
275
        return -1;
 
276
    }
 
277
 
 
278
    return 0;
 
279
}
 
280
 
 
281
static void set_all_rlimits(const pa_daemon_conf *conf) {
 
282
    set_one_rlimit(&conf->rlimit_fsize, RLIMIT_FSIZE, "RLIMIT_FSIZE");
 
283
    set_one_rlimit(&conf->rlimit_data, RLIMIT_DATA, "RLIMIT_DATA");
 
284
    set_one_rlimit(&conf->rlimit_stack, RLIMIT_STACK, "RLIMIT_STACK");
 
285
    set_one_rlimit(&conf->rlimit_core, RLIMIT_CORE, "RLIMIT_CORE");
 
286
#ifdef RLIMIT_RSS
 
287
    set_one_rlimit(&conf->rlimit_rss, RLIMIT_RSS, "RLIMIT_RSS");
 
288
#endif
 
289
#ifdef RLIMIT_NPROC
 
290
    set_one_rlimit(&conf->rlimit_nproc, RLIMIT_NPROC, "RLIMIT_NPROC");
 
291
#endif
 
292
#ifdef RLIMIT_NOFILE
 
293
    set_one_rlimit(&conf->rlimit_nofile, RLIMIT_NOFILE, "RLIMIT_NOFILE");
 
294
#endif
 
295
#ifdef RLIMIT_MEMLOCK
 
296
    set_one_rlimit(&conf->rlimit_memlock, RLIMIT_MEMLOCK, "RLIMIT_MEMLOCK");
 
297
#endif
 
298
#ifdef RLIMIT_AS
 
299
    set_one_rlimit(&conf->rlimit_as, RLIMIT_AS, "RLIMIT_AS");
 
300
#endif
 
301
#ifdef RLIMIT_LOCKS
 
302
    set_one_rlimit(&conf->rlimit_locks, RLIMIT_LOCKS, "RLIMIT_LOCKS");
 
303
#endif
 
304
#ifdef RLIMIT_SIGPENDING
 
305
    set_one_rlimit(&conf->rlimit_sigpending, RLIMIT_SIGPENDING, "RLIMIT_SIGPENDING");
 
306
#endif
 
307
#ifdef RLIMIT_MSGQUEUE
 
308
    set_one_rlimit(&conf->rlimit_msgqueue, RLIMIT_MSGQUEUE, "RLIMIT_MSGQUEUE");
 
309
#endif
 
310
#ifdef RLIMIT_NICE
 
311
    set_one_rlimit(&conf->rlimit_nice, RLIMIT_NICE, "RLIMIT_NICE");
 
312
#endif
 
313
#ifdef RLIMIT_RTPRIO
 
314
    set_one_rlimit(&conf->rlimit_rtprio, RLIMIT_RTPRIO, "RLIMIT_RTPRIO");
 
315
#endif
 
316
#ifdef RLIMIT_RTTIME
 
317
    set_one_rlimit(&conf->rlimit_rttime, RLIMIT_RTTIME, "RLIMIT_RTTIME");
 
318
#endif
 
319
}
 
320
#endif
 
321
 
 
322
static char *check_configured_address(void) {
 
323
    char *default_server = NULL;
 
324
    pa_client_conf *c = pa_client_conf_new();
 
325
 
 
326
    pa_client_conf_load(c, true, true);
 
327
 
 
328
    if (c->default_server && *c->default_server)
 
329
        default_server = pa_xstrdup(c->default_server);
 
330
 
 
331
    pa_client_conf_free(c);
 
332
 
 
333
    return default_server;
 
334
}
 
335
 
 
336
static bool valid_pid_file = false;
 
337
static void pid_file_deleted(void *userdata)
 
338
{
 
339
    pa_core *c = userdata;
 
340
    pa_log_info("Our pid file has been deleted (probably due to session logout), quitting...");
 
341
    valid_pid_file = false;
 
342
    pa_core_exit(c, true, 0);
 
343
}
 
344
 
 
345
#ifdef HAVE_DBUS
 
346
static pa_dbus_connection *register_dbus_name(pa_core *c, DBusBusType bus, const char* name) {
 
347
    DBusError error;
 
348
    pa_dbus_connection *conn;
 
349
 
 
350
    dbus_error_init(&error);
 
351
 
 
352
    if (!(conn = pa_dbus_bus_get(c, bus, &error)) || dbus_error_is_set(&error)) {
 
353
        pa_log_warn("Unable to contact D-Bus: %s: %s", error.name, error.message);
 
354
        goto fail;
 
355
    }
 
356
 
 
357
    if (dbus_bus_request_name(pa_dbus_connection_get(conn), name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
 
358
        pa_log_debug("Got %s!", name);
 
359
        return conn;
 
360
    }
 
361
 
 
362
    if (dbus_error_is_set(&error))
 
363
        pa_log_error("Failed to acquire %s: %s: %s", name, error.name, error.message);
 
364
    else
 
365
        pa_log_error("D-Bus name %s already taken.", name);
 
366
 
 
367
    /* PA cannot be started twice by the same user and hence we can
 
368
     * ignore mostly the case that a name is already taken. */
 
369
 
 
370
fail:
 
371
    if (conn)
 
372
        pa_dbus_connection_unref(conn);
 
373
 
 
374
    dbus_error_free(&error);
 
375
    return NULL;
 
376
}
 
377
#endif
 
378
 
 
379
int main(int argc, char *argv[]) {
 
380
    pa_core *c = NULL;
 
381
    pa_strbuf *buf = NULL;
 
382
    pa_daemon_conf *conf = NULL;
 
383
    pa_mainloop *mainloop = NULL;
 
384
    char *s;
 
385
    char *configured_address;
 
386
    int r = 0, retval = 1, d = 0;
 
387
    bool ltdl_init = false;
 
388
    int n_fds = 0, *passed_fds = NULL;
 
389
    const char *e;
 
390
#ifdef HAVE_FORK
 
391
    int daemon_pipe[2] = { -1, -1 };
 
392
    int daemon_pipe2[2] = { -1, -1 };
 
393
#endif
 
394
    pa_inotify *pid_monitor = NULL;
 
395
    int autospawn_fd = -1;
 
396
    bool autospawn_locked = false;
 
397
#ifdef HAVE_DBUS
 
398
    pa_dbusobj_server_lookup *server_lookup = NULL; /* /org/pulseaudio/server_lookup */
 
399
    pa_dbus_connection *lookup_service_bus = NULL; /* Always the user bus. */
 
400
    pa_dbus_connection *server_bus = NULL; /* The bus where we reserve org.pulseaudio.Server, either the user or the system bus. */
 
401
    bool start_server;
 
402
#endif
 
403
 
 
404
    pa_log_set_ident("pulseaudio");
 
405
    pa_log_set_level(PA_LOG_NOTICE);
 
406
    pa_log_set_flags(PA_LOG_COLORS|PA_LOG_PRINT_FILE|PA_LOG_PRINT_LEVEL, PA_LOG_RESET);
 
407
 
 
408
#if defined(__linux__) && defined(__OPTIMIZE__)
 
409
    /*
 
410
       Disable lazy relocations to make usage of external libraries
 
411
       more deterministic for our RT threads. We abuse __OPTIMIZE__ as
 
412
       a check whether we are a debug build or not. This all is
 
413
       admittedly a bit snake-oilish.
 
414
    */
 
415
 
 
416
    if (!getenv("LD_BIND_NOW")) {
 
417
        char *rp;
 
418
        char *canonical_rp;
 
419
 
 
420
        /* We have to execute ourselves, because the libc caches the
 
421
         * value of $LD_BIND_NOW on initialization. */
 
422
 
 
423
        pa_set_env("LD_BIND_NOW", "1");
 
424
 
 
425
        if ((canonical_rp = pa_realpath(PA_BINARY))) {
 
426
 
 
427
            if ((rp = pa_readlink("/proc/self/exe"))) {
 
428
 
 
429
                if (pa_streq(rp, canonical_rp))
 
430
                    pa_assert_se(execv(rp, argv) == 0);
 
431
                else
 
432
                    pa_log_warn("/proc/self/exe does not point to %s, cannot self execute. Are you playing games?", canonical_rp);
 
433
 
 
434
                pa_xfree(rp);
 
435
 
 
436
            } else
 
437
                pa_log_warn("Couldn't read /proc/self/exe, cannot self execute. Running in a chroot()?");
 
438
 
 
439
            pa_xfree(canonical_rp);
 
440
 
 
441
        } else
 
442
            pa_log_warn("Couldn't canonicalize binary path, cannot self execute.");
 
443
    }
 
444
#endif
 
445
 
 
446
#ifdef HAVE_SYSTEMD_DAEMON
 
447
    n_fds = sd_listen_fds(0);
 
448
    if (n_fds > 0) {
 
449
        int i = n_fds;
 
450
 
 
451
        passed_fds = pa_xnew(int, n_fds+2);
 
452
        passed_fds[n_fds] = passed_fds[n_fds+1] = -1;
 
453
        while (i--)
 
454
            passed_fds[i] = SD_LISTEN_FDS_START + i;
 
455
    }
 
456
#endif
 
457
 
 
458
    if (!passed_fds) {
 
459
        n_fds = 0;
 
460
        passed_fds = pa_xnew(int, 2);
 
461
        passed_fds[0] = passed_fds[1] = -1;
 
462
    }
 
463
 
 
464
    if ((e = getenv("PULSE_PASSED_FD"))) {
 
465
        int passed_fd = atoi(e);
 
466
        if (passed_fd > 2)
 
467
            passed_fds[n_fds] = passed_fd;
 
468
    }
 
469
 
 
470
    /* We might be autospawned, in which case have no idea in which
 
471
     * context we have been started. Let's cleanup our execution
 
472
     * context as good as possible */
 
473
 
 
474
    pa_reset_personality();
 
475
    pa_drop_root();
 
476
    pa_close_allv(passed_fds);
 
477
    pa_xfree(passed_fds);
 
478
    pa_reset_sigs(-1);
 
479
    pa_unblock_sigs(-1);
 
480
    pa_reset_priority();
 
481
 
 
482
    setlocale(LC_ALL, "");
 
483
    pa_init_i18n();
 
484
 
 
485
    conf = pa_daemon_conf_new();
 
486
 
 
487
    if (pa_daemon_conf_load(conf, NULL) < 0)
 
488
        goto finish;
 
489
 
 
490
    if (pa_daemon_conf_env(conf) < 0)
 
491
        goto finish;
 
492
 
 
493
    if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
 
494
        pa_log(_("Failed to parse command line."));
 
495
        goto finish;
 
496
    }
 
497
 
 
498
    if (conf->log_target)
 
499
        pa_log_set_target(conf->log_target);
 
500
    else {
 
501
        pa_log_target target = { .type = PA_LOG_STDERR, .file = NULL };
 
502
        pa_log_set_target(&target);
 
503
    }
 
504
 
 
505
    pa_log_set_level(conf->log_level);
 
506
    if (conf->log_meta)
 
507
        pa_log_set_flags(PA_LOG_PRINT_META, PA_LOG_SET);
 
508
    if (conf->log_time)
 
509
        pa_log_set_flags(PA_LOG_PRINT_TIME, PA_LOG_SET);
 
510
    pa_log_set_show_backtrace(conf->log_backtrace);
 
511
 
 
512
#ifdef HAVE_DBUS
 
513
    /* conf->system_instance and conf->local_server_type control almost the
 
514
     * same thing; make them agree about what is requested. */
 
515
    switch (conf->local_server_type) {
 
516
        case PA_SERVER_TYPE_UNSET:
 
517
            conf->local_server_type = conf->system_instance ? PA_SERVER_TYPE_SYSTEM : PA_SERVER_TYPE_USER;
 
518
            break;
 
519
        case PA_SERVER_TYPE_USER:
 
520
        case PA_SERVER_TYPE_NONE:
 
521
            conf->system_instance = false;
 
522
            break;
 
523
        case PA_SERVER_TYPE_SYSTEM:
 
524
            conf->system_instance = true;
 
525
            break;
 
526
        default:
 
527
            pa_assert_not_reached();
 
528
    }
 
529
 
 
530
    start_server = conf->local_server_type == PA_SERVER_TYPE_USER || (getuid() == 0 && conf->local_server_type == PA_SERVER_TYPE_SYSTEM);
 
531
 
 
532
    if (!start_server && conf->local_server_type == PA_SERVER_TYPE_SYSTEM) {
 
533
        pa_log_notice(_("System mode refused for non-root user. Only starting the D-Bus server lookup service."));
 
534
        conf->system_instance = false;
 
535
    }
 
536
#endif
 
537
 
 
538
    LTDL_SET_PRELOADED_SYMBOLS();
 
539
    pa_ltdl_init();
 
540
    ltdl_init = true;
 
541
 
 
542
    if (conf->dl_search_path)
 
543
        lt_dlsetsearchpath(conf->dl_search_path);
 
544
 
 
545
#ifdef OS_IS_WIN32
 
546
    {
 
547
        WSADATA data;
 
548
        WSAStartup(MAKEWORD(2, 0), &data);
 
549
    }
 
550
#endif
 
551
 
 
552
    pa_random_seed();
 
553
 
 
554
    switch (conf->cmd) {
 
555
        case PA_CMD_DUMP_MODULES:
 
556
            pa_dump_modules(conf, argc-d, argv+d);
 
557
            retval = 0;
 
558
            goto finish;
 
559
 
 
560
        case PA_CMD_DUMP_CONF: {
 
561
 
 
562
            if (d < argc) {
 
563
                pa_log("Too many arguments.\n");
 
564
                goto finish;
 
565
            }
 
566
 
 
567
            s = pa_daemon_conf_dump(conf);
 
568
            fputs(s, stdout);
 
569
            pa_xfree(s);
 
570
            retval = 0;
 
571
            goto finish;
 
572
        }
 
573
 
 
574
        case PA_CMD_DUMP_RESAMPLE_METHODS: {
 
575
            int i;
 
576
 
 
577
            if (d < argc) {
 
578
                pa_log("Too many arguments.\n");
 
579
                goto finish;
 
580
            }
 
581
 
 
582
            for (i = 0; i < PA_RESAMPLER_MAX; i++)
 
583
                if (pa_resample_method_supported(i))
 
584
                    printf("%s\n", pa_resample_method_to_string(i));
 
585
 
 
586
            retval = 0;
 
587
            goto finish;
 
588
        }
 
589
 
 
590
        case PA_CMD_HELP :
 
591
            pa_cmdline_help(argv[0]);
 
592
            retval = 0;
 
593
            goto finish;
 
594
 
 
595
        case PA_CMD_VERSION :
 
596
 
 
597
            if (d < argc) {
 
598
                pa_log("Too many arguments.\n");
 
599
                goto finish;
 
600
            }
 
601
 
 
602
            printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
 
603
            retval = 0;
 
604
            goto finish;
 
605
 
 
606
        case PA_CMD_CHECK: {
 
607
            pid_t pid;
 
608
 
 
609
            if (d < argc) {
 
610
                pa_log("Too many arguments.\n");
 
611
                goto finish;
 
612
            }
 
613
 
 
614
            if (pa_pid_file_check_running(&pid, "pulseaudio") < 0)
 
615
                pa_log_info("Daemon not running");
 
616
            else {
 
617
                pa_log_info("Daemon running as PID %u", pid);
 
618
                retval = 0;
 
619
            }
 
620
 
 
621
            goto finish;
 
622
 
 
623
        }
 
624
        case PA_CMD_KILL:
 
625
 
 
626
            if (d < argc) {
 
627
                pa_log("Too many arguments.\n");
 
628
                goto finish;
 
629
            }
 
630
 
 
631
            if (pa_pid_file_kill(SIGINT, NULL, "pulseaudio") < 0)
 
632
                pa_log(_("Failed to kill daemon: %s"), pa_cstrerror(errno));
 
633
            else
 
634
                retval = 0;
 
635
 
 
636
            goto finish;
 
637
 
 
638
        case PA_CMD_CLEANUP_SHM:
 
639
 
 
640
            if (d < argc) {
 
641
                pa_log("Too many arguments.\n");
 
642
                goto finish;
 
643
            }
 
644
 
 
645
            if (pa_shm_cleanup() >= 0)
 
646
                retval = 0;
 
647
 
 
648
            goto finish;
 
649
 
 
650
        default:
 
651
            pa_assert(conf->cmd == PA_CMD_DAEMON || conf->cmd == PA_CMD_START);
 
652
    }
 
653
 
 
654
    if (d < argc) {
 
655
        pa_log("Too many arguments.\n");
 
656
        goto finish;
 
657
    }
 
658
 
 
659
#ifdef HAVE_GETUID
 
660
    if (getuid() == 0 && !conf->system_instance)
 
661
        pa_log_warn(_("This program is not intended to be run as root (unless --system is specified)."));
 
662
#ifndef HAVE_DBUS /* A similar, only a notice worthy check was done earlier, if D-Bus is enabled. */
 
663
    else if (getuid() != 0 && conf->system_instance) {
 
664
        pa_log(_("Root privileges required."));
 
665
        goto finish;
 
666
    }
 
667
#endif
 
668
#endif  /* HAVE_GETUID */
 
669
 
 
670
    if (conf->cmd == PA_CMD_START && conf->system_instance) {
 
671
        pa_log(_("--start not supported for system instances."));
 
672
        goto finish;
 
673
    }
 
674
 
 
675
    if (conf->cmd == PA_CMD_START && (configured_address = check_configured_address())) {
 
676
        /* There is an server address in our config, but where did it come from?
 
677
         * By default a standard X11 login will load module-x11-publish which will
 
678
         * inject PULSE_SERVER X11 property. If the PA daemon crashes, we will end
 
679
         * up hitting this code path. So we have to check to see if our configured_address
 
680
         * is the same as the value that would go into this property so that we can
 
681
         * recover (i.e. autospawn) from a crash.
 
682
         */
 
683
        char *ufn;
 
684
        bool start_anyway = false;
 
685
 
 
686
        if ((ufn = pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET))) {
 
687
            char *id;
 
688
 
 
689
            if ((id = pa_machine_id())) {
 
690
                pa_strlist *server_list;
 
691
                char formatted_ufn[256];
 
692
 
 
693
                pa_snprintf(formatted_ufn, sizeof(formatted_ufn), "{%s}unix:%s", id, ufn);
 
694
                pa_xfree(id);
 
695
 
 
696
                if ((server_list = pa_strlist_parse(configured_address))) {
 
697
                    char *u = NULL;
 
698
 
 
699
                    /* We only need to check the first server */
 
700
                    server_list = pa_strlist_pop(server_list, &u);
 
701
                    pa_strlist_free(server_list);
 
702
 
 
703
                    start_anyway = (u && pa_streq(formatted_ufn, u));
 
704
                    pa_xfree(u);
 
705
                }
 
706
            }
 
707
            pa_xfree(ufn);
 
708
        }
 
709
 
 
710
        if (!start_anyway) {
 
711
            pa_log_notice(_("User-configured server at %s, refusing to start/autospawn."), configured_address);
 
712
            pa_xfree(configured_address);
 
713
            retval = 0;
 
714
            goto finish;
 
715
        }
 
716
 
 
717
        pa_log_notice(_("User-configured server at %s, which appears to be local. Probing deeper."), configured_address);
 
718
        pa_xfree(configured_address);
 
719
    }
 
720
 
 
721
    if (conf->system_instance && !conf->disallow_exit)
 
722
        pa_log_warn(_("Running in system mode, but --disallow-exit not set!"));
 
723
 
 
724
    if (conf->system_instance && !conf->disallow_module_loading)
 
725
        pa_log_warn(_("Running in system mode, but --disallow-module-loading not set!"));
 
726
 
 
727
    if (conf->system_instance && !conf->disable_shm) {
 
728
        pa_log_notice(_("Running in system mode, forcibly disabling SHM mode!"));
 
729
        conf->disable_shm = true;
 
730
    }
 
731
 
 
732
    if (conf->system_instance && conf->exit_idle_time >= 0) {
 
733
        pa_log_notice(_("Running in system mode, forcibly disabling exit idle time!"));
 
734
        conf->exit_idle_time = -1;
 
735
    }
 
736
 
 
737
    if (conf->cmd == PA_CMD_START) {
 
738
        /* If we shall start PA only when it is not running yet, we
 
739
         * first take the autospawn lock to make things
 
740
         * synchronous. */
 
741
 
 
742
        /* This locking and thread synchronisation code doesn't work reliably
 
743
         * on kFreeBSD (Debian bug #705435), or in upstream FreeBSD ports
 
744
         * (bug reference: ports/128947, patched in SVN r231972). */
 
745
#if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
 
746
        if ((autospawn_fd = pa_autospawn_lock_init()) < 0) {
 
747
            pa_log("Failed to initialize autospawn lock");
 
748
            goto finish;
 
749
        }
 
750
 
 
751
        if ((pa_autospawn_lock_acquire(true) < 0)) {
 
752
            pa_log("Failed to acquire autospawn lock");
 
753
            goto finish;
 
754
        }
 
755
 
 
756
        autospawn_locked = true;
 
757
#endif
 
758
    }
 
759
 
 
760
    if (conf->daemonize) {
 
761
#ifdef HAVE_FORK
 
762
        pid_t child;
 
763
#endif
 
764
 
 
765
        if (pa_stdio_acquire() < 0) {
 
766
            pa_log(_("Failed to acquire stdio."));
 
767
            goto finish;
 
768
        }
 
769
 
 
770
#ifdef HAVE_FORK
 
771
        if (pipe(daemon_pipe) < 0) {
 
772
            pa_log(_("pipe() failed: %s"), pa_cstrerror(errno));
 
773
            goto finish;
 
774
        }
 
775
 
 
776
        if ((child = fork()) < 0) {
 
777
            pa_log(_("fork() failed: %s"), pa_cstrerror(errno));
 
778
            pa_close_pipe(daemon_pipe);
 
779
            goto finish;
 
780
        }
 
781
 
 
782
        if (child != 0) {
 
783
            ssize_t n;
 
784
            /* Father */
 
785
 
 
786
            pa_assert_se(pa_close(daemon_pipe[1]) == 0);
 
787
            daemon_pipe[1] = -1;
 
788
 
 
789
            if ((n = pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL)) != sizeof(retval)) {
 
790
 
 
791
                if (n < 0)
 
792
                    pa_log(_("read() failed: %s"), pa_cstrerror(errno));
 
793
 
 
794
                retval = 1;
 
795
            }
 
796
 
 
797
            if (retval)
 
798
                pa_log(_("Daemon startup failed."));
 
799
            else
 
800
                pa_log_info("Daemon startup successful.");
 
801
 
 
802
            goto finish;
 
803
        }
 
804
 
 
805
        if (autospawn_fd >= 0) {
 
806
            /* The lock file is unlocked from the parent, so we need
 
807
             * to close it in the child */
 
808
 
 
809
            pa_autospawn_lock_release();
 
810
            pa_autospawn_lock_done(true);
 
811
 
 
812
            autospawn_locked = false;
 
813
            autospawn_fd = -1;
 
814
        }
 
815
 
 
816
        pa_assert_se(pa_close(daemon_pipe[0]) == 0);
 
817
        daemon_pipe[0] = -1;
 
818
#endif
 
819
 
 
820
        if (!conf->log_target) {
 
821
#ifdef HAVE_SYSTEMD_JOURNAL
 
822
            pa_log_target target = { .type = PA_LOG_JOURNAL, .file = NULL };
 
823
#else
 
824
            pa_log_target target = { .type = PA_LOG_SYSLOG, .file = NULL };
 
825
#endif
 
826
            pa_log_set_target(&target);
 
827
        }
 
828
 
 
829
#ifdef HAVE_SETSID
 
830
        if (setsid() < 0) {
 
831
            pa_log(_("setsid() failed: %s"), pa_cstrerror(errno));
 
832
            goto finish;
 
833
        }
 
834
#endif
 
835
 
 
836
#ifdef HAVE_FORK
 
837
        /* We now are a session and process group leader. Let's fork
 
838
         * again and let the father die, so that we'll become a
 
839
         * process that can never acquire a TTY again, in a session and
 
840
         * process group without leader */
 
841
 
 
842
        if (pipe(daemon_pipe2) < 0) {
 
843
            pa_log(_("pipe() failed: %s"), pa_cstrerror(errno));
 
844
            goto finish;
 
845
        }
 
846
 
 
847
        if ((child = fork()) < 0) {
 
848
            pa_log(_("fork() failed: %s"), pa_cstrerror(errno));
 
849
            pa_close_pipe(daemon_pipe2);
 
850
            goto finish;
 
851
        }
 
852
 
 
853
        if (child != 0) {
 
854
            ssize_t n;
 
855
            /* Father */
 
856
 
 
857
            pa_assert_se(pa_close(daemon_pipe2[1]) == 0);
 
858
            daemon_pipe2[1] = -1;
 
859
 
 
860
            if ((n = pa_loop_read(daemon_pipe2[0], &retval, sizeof(retval), NULL)) != sizeof(retval)) {
 
861
 
 
862
                if (n < 0)
 
863
                    pa_log(_("read() failed: %s"), pa_cstrerror(errno));
 
864
 
 
865
                retval = 1;
 
866
            }
 
867
 
 
868
            /* We now have to take care of signalling the first fork with
 
869
             * the return value we've received from this fork... */
 
870
            pa_assert(daemon_pipe[1] >= 0);
 
871
 
 
872
            pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
 
873
            pa_close(daemon_pipe[1]);
 
874
            daemon_pipe[1] = -1;
 
875
 
 
876
            goto finish;
 
877
        }
 
878
 
 
879
        pa_assert_se(pa_close(daemon_pipe2[0]) == 0);
 
880
        daemon_pipe2[0] = -1;
 
881
 
 
882
        /* We no longer need the (first) daemon_pipe as it's handled in our child above */
 
883
        pa_close_pipe(daemon_pipe);
 
884
#endif
 
885
 
 
886
#ifdef SIGTTOU
 
887
        signal(SIGTTOU, SIG_IGN);
 
888
#endif
 
889
#ifdef SIGTTIN
 
890
        signal(SIGTTIN, SIG_IGN);
 
891
#endif
 
892
#ifdef SIGTSTP
 
893
        signal(SIGTSTP, SIG_IGN);
 
894
#endif
 
895
 
 
896
        pa_nullify_stdfds();
 
897
    }
 
898
 
 
899
    pa_set_env_and_record("PULSE_INTERNAL", "1");
 
900
    pa_assert_se(chdir("/") == 0);
 
901
    umask(0022);
 
902
 
 
903
#ifdef HAVE_SYS_RESOURCE_H
 
904
    set_all_rlimits(conf);
 
905
#endif
 
906
    pa_rtclock_hrtimer_enable();
 
907
 
 
908
    if (conf->high_priority)
 
909
        pa_raise_priority(conf->nice_level);
 
910
 
 
911
    if (conf->system_instance)
 
912
        if (change_user() < 0)
 
913
            goto finish;
 
914
 
 
915
    pa_set_env_and_record("PULSE_SYSTEM", conf->system_instance ? "1" : "0");
 
916
 
 
917
    pa_log_info("This is PulseAudio %s", PACKAGE_VERSION);
 
918
    pa_log_debug("Compilation host: %s", CANONICAL_HOST);
 
919
    pa_log_debug("Compilation CFLAGS: %s", PA_CFLAGS);
 
920
 
 
921
#ifdef HAVE_LIBSAMPLERATE
 
922
    pa_log_warn("Compiled with DEPRECATED libsamplerate support!");
 
923
#endif
 
924
 
 
925
    s = pa_uname_string();
 
926
    pa_log_debug("Running on host: %s", s);
 
927
    pa_xfree(s);
 
928
 
 
929
    pa_log_debug("Found %u CPUs.", pa_ncpus());
 
930
 
 
931
    pa_log_info("Page size is %lu bytes", (unsigned long) PA_PAGE_SIZE);
 
932
 
 
933
#ifdef HAVE_VALGRIND_MEMCHECK_H
 
934
    pa_log_debug("Compiled with Valgrind support: yes");
 
935
#else
 
936
    pa_log_debug("Compiled with Valgrind support: no");
 
937
#endif
 
938
 
 
939
    pa_log_debug("Running in valgrind mode: %s", pa_yes_no(pa_in_valgrind()));
 
940
 
 
941
    pa_log_debug("Running in VM: %s", pa_yes_no(pa_running_in_vm()));
 
942
 
 
943
#ifdef __OPTIMIZE__
 
944
    pa_log_debug("Optimized build: yes");
 
945
#else
 
946
    pa_log_debug("Optimized build: no");
 
947
#endif
 
948
 
 
949
#ifdef NDEBUG
 
950
    pa_log_debug("NDEBUG defined, all asserts disabled.");
 
951
#elif defined(FASTPATH)
 
952
    pa_log_debug("FASTPATH defined, only fast path asserts disabled.");
 
953
#else
 
954
    pa_log_debug("All asserts enabled.");
 
955
#endif
 
956
 
 
957
    if (!(s = pa_machine_id())) {
 
958
        pa_log(_("Failed to get machine ID"));
 
959
        goto finish;
 
960
    }
 
961
    pa_log_info("Machine ID is %s.", s);
 
962
    pa_xfree(s);
 
963
 
 
964
    if ((s = pa_session_id())) {
 
965
        pa_log_info("Session ID is %s.", s);
 
966
        pa_xfree(s);
 
967
    }
 
968
 
 
969
    if (!(s = pa_get_runtime_dir()))
 
970
        goto finish;
 
971
    pa_log_info("Using runtime directory %s.", s);
 
972
    pa_xfree(s);
 
973
 
 
974
    if (!(s = pa_get_state_dir()))
 
975
        goto finish;
 
976
    pa_log_info("Using state directory %s.", s);
 
977
    pa_xfree(s);
 
978
 
 
979
    pa_log_info("Using modules directory %s.", conf->dl_search_path);
 
980
 
 
981
    pa_log_info("Running in system mode: %s", pa_yes_no(pa_in_system_mode()));
 
982
 
 
983
    if (pa_in_system_mode())
 
984
        pa_log_warn(_("OK, so you are running PA in system mode. Please note that you most likely shouldn't be doing that.\n"
 
985
                      "If you do it nonetheless then it's your own fault if things don't work as expected.\n"
 
986
                      "Please read http://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/WhatIsWrongWithSystemWide/ for an explanation why system mode is usually a bad idea."));
 
987
 
 
988
    if (conf->use_pid_file) {
 
989
        int z;
 
990
 
 
991
        if ((z = pa_pid_file_create("pulseaudio")) != 0) {
 
992
 
 
993
            if (conf->cmd == PA_CMD_START && z > 0) {
 
994
                /* If we are already running and with are run in
 
995
                 * --start mode, then let's return this as success. */
 
996
 
 
997
                retval = 0;
 
998
                goto finish;
 
999
            }
 
1000
 
 
1001
            pa_log(_("pa_pid_file_create() failed."));
 
1002
            goto finish;
 
1003
        }
 
1004
 
 
1005
        valid_pid_file = true;
 
1006
    }
 
1007
 
 
1008
    pa_disable_sigpipe();
 
1009
 
 
1010
    if (pa_rtclock_hrtimer())
 
1011
        pa_log_info("Fresh high-resolution timers available! Bon appetit!");
 
1012
    else
 
1013
        pa_log_info("Dude, your kernel stinks! The chef's recommendation today is Linux with high-resolution timers enabled!");
 
1014
 
 
1015
    if (conf->lock_memory) {
 
1016
#if defined(HAVE_SYS_MMAN_H) && !defined(__ANDROID__)
 
1017
        if (mlockall(MCL_FUTURE) < 0)
 
1018
            pa_log_warn("mlockall() failed: %s", pa_cstrerror(errno));
 
1019
        else
 
1020
            pa_log_info("Successfully locked process into memory.");
 
1021
#else
 
1022
        pa_log_warn("Memory locking requested but not supported on platform.");
 
1023
#endif
 
1024
    }
 
1025
 
 
1026
    pa_memtrap_install();
 
1027
 
 
1028
    pa_assert_se(mainloop = pa_mainloop_new());
 
1029
 
 
1030
    if (!(c = pa_core_new(pa_mainloop_get_api(mainloop), !conf->disable_shm, conf->shm_size))) {
 
1031
        pa_log(_("pa_core_new() failed."));
 
1032
        goto finish;
 
1033
    }
 
1034
 
 
1035
    if (valid_pid_file)
 
1036
        pid_monitor = pa_inotify_start(pa_pid_file_name(), c, pid_file_deleted, c);
 
1037
 
 
1038
    c->default_sample_spec = conf->default_sample_spec;
 
1039
    c->alternate_sample_rate = conf->alternate_sample_rate;
 
1040
    c->default_channel_map = conf->default_channel_map;
 
1041
    c->default_n_fragments = conf->default_n_fragments;
 
1042
    c->default_fragment_size_msec = conf->default_fragment_size_msec;
 
1043
    c->deferred_volume_safety_margin_usec = conf->deferred_volume_safety_margin_usec;
 
1044
    c->deferred_volume_extra_delay_usec = conf->deferred_volume_extra_delay_usec;
 
1045
    c->exit_idle_time = conf->exit_idle_time;
 
1046
    c->scache_idle_time = conf->scache_idle_time;
 
1047
    c->resample_method = conf->resample_method;
 
1048
    c->realtime_priority = conf->realtime_priority;
 
1049
    c->realtime_scheduling = conf->realtime_scheduling;
 
1050
    c->disable_remixing = conf->disable_remixing;
 
1051
    c->disable_lfe_remixing = conf->disable_lfe_remixing;
 
1052
    c->deferred_volume = conf->deferred_volume;
 
1053
    c->running_as_daemon = conf->daemonize;
 
1054
    c->disallow_exit = conf->disallow_exit;
 
1055
    c->flat_volumes = conf->flat_volumes;
 
1056
#ifdef HAVE_DBUS
 
1057
    c->server_type = conf->local_server_type;
 
1058
#endif
 
1059
 
 
1060
    pa_cpu_init(&c->cpu_info);
 
1061
 
 
1062
    pa_assert_se(pa_signal_init(pa_mainloop_get_api(mainloop)) == 0);
 
1063
    pa_signal_new(SIGINT, signal_callback, c);
 
1064
    pa_signal_new(SIGTERM, signal_callback, c);
 
1065
#ifdef SIGUSR1
 
1066
    pa_signal_new(SIGUSR1, signal_callback, c);
 
1067
#endif
 
1068
#ifdef SIGUSR2
 
1069
    pa_signal_new(SIGUSR2, signal_callback, c);
 
1070
#endif
 
1071
#ifdef SIGHUP
 
1072
    pa_signal_new(SIGHUP, signal_callback, c);
 
1073
#endif
 
1074
 
 
1075
    if (!conf->no_cpu_limit)
 
1076
        pa_assert_se(pa_cpu_limit_init(pa_mainloop_get_api(mainloop)) == 0);
 
1077
 
 
1078
    buf = pa_strbuf_new();
 
1079
 
 
1080
#ifdef HAVE_DBUS
 
1081
    pa_assert_se(dbus_threads_init_default());
 
1082
 
 
1083
    if (start_server) {
 
1084
#endif
 
1085
        if (conf->load_default_script_file) {
 
1086
            FILE *f;
 
1087
 
 
1088
            if ((f = pa_daemon_conf_open_default_script_file(conf))) {
 
1089
                r = pa_cli_command_execute_file_stream(c, f, buf, &conf->fail);
 
1090
                fclose(f);
 
1091
            }
 
1092
        }
 
1093
 
 
1094
        if (r >= 0)
 
1095
            r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
 
1096
 
 
1097
        pa_log_error("%s", s = pa_strbuf_tostring_free(buf));
 
1098
        pa_xfree(s);
 
1099
 
 
1100
        if (r < 0 && conf->fail) {
 
1101
            pa_log(_("Failed to initialize daemon."));
 
1102
            goto finish;
 
1103
        }
 
1104
 
 
1105
        if (!c->modules || pa_idxset_size(c->modules) == 0) {
 
1106
            pa_log(_("Daemon startup without any loaded modules, refusing to work."));
 
1107
            goto finish;
 
1108
        }
 
1109
#ifdef HAVE_DBUS
 
1110
    } else {
 
1111
        /* When we just provide the D-Bus server lookup service, we don't want
 
1112
         * any modules to be loaded. We haven't loaded any so far, so one might
 
1113
         * think there's no way to contact the server, but receiving certain
 
1114
         * signals could still cause modules to load. */
 
1115
        conf->disallow_module_loading = true;
 
1116
    }
 
1117
#endif
 
1118
 
 
1119
    /* We completed the initial module loading, so let's disable it
 
1120
     * from now on, if requested */
 
1121
    c->disallow_module_loading = conf->disallow_module_loading;
 
1122
 
 
1123
#ifdef HAVE_DBUS
 
1124
    if (!conf->system_instance) {
 
1125
        if ((server_lookup = pa_dbusobj_server_lookup_new(c))) {
 
1126
            if (!(lookup_service_bus = register_dbus_name(c, DBUS_BUS_SESSION, "org.PulseAudio1")))
 
1127
                goto finish;
 
1128
        }
 
1129
    }
 
1130
 
 
1131
    if (start_server)
 
1132
        server_bus = register_dbus_name(c, conf->system_instance ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, "org.pulseaudio.Server");
 
1133
#endif
 
1134
 
 
1135
#ifdef HAVE_FORK
 
1136
    if (daemon_pipe2[1] >= 0) {
 
1137
        int ok = 0;
 
1138
        pa_loop_write(daemon_pipe2[1], &ok, sizeof(ok), NULL);
 
1139
        pa_close(daemon_pipe2[1]);
 
1140
        daemon_pipe2[1] = -1;
 
1141
    }
 
1142
#endif
 
1143
 
 
1144
    pa_log_info("Daemon startup complete.");
 
1145
 
 
1146
    retval = 0;
 
1147
    if (pa_mainloop_run(mainloop, &retval) < 0)
 
1148
        goto finish;
 
1149
 
 
1150
    pa_log_info("Daemon shutdown initiated.");
 
1151
 
 
1152
finish:
 
1153
#ifdef HAVE_DBUS
 
1154
    if (server_bus)
 
1155
        pa_dbus_connection_unref(server_bus);
 
1156
    if (lookup_service_bus)
 
1157
        pa_dbus_connection_unref(lookup_service_bus);
 
1158
    if (server_lookup)
 
1159
        pa_dbusobj_server_lookup_free(server_lookup);
 
1160
#endif
 
1161
 
 
1162
    if (autospawn_fd >= 0) {
 
1163
        if (autospawn_locked)
 
1164
            pa_autospawn_lock_release();
 
1165
 
 
1166
        pa_autospawn_lock_done(false);
 
1167
    }
 
1168
 
 
1169
    if (pid_monitor)
 
1170
        pa_inotify_stop(pid_monitor);
 
1171
 
 
1172
    if (c) {
 
1173
        /* Ensure all the modules/samples are unloaded when the core is still ref'ed,
 
1174
         * as unlink callback hooks in modules may need the core to be ref'ed */
 
1175
        pa_module_unload_all(c);
 
1176
        pa_scache_free_all(c);
 
1177
 
 
1178
        pa_core_unref(c);
 
1179
        pa_log_info("Daemon terminated.");
 
1180
    }
 
1181
 
 
1182
    if (!conf->no_cpu_limit)
 
1183
        pa_cpu_limit_done();
 
1184
 
 
1185
    pa_signal_done();
 
1186
 
 
1187
#ifdef HAVE_FORK
 
1188
    /* If we have daemon_pipe[1] still open, this means we've failed after
 
1189
     * the first fork, but before the second. Therefore just write to it. */
 
1190
    if (daemon_pipe[1] >= 0)
 
1191
        pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
 
1192
    else if (daemon_pipe2[1] >= 0)
 
1193
        pa_loop_write(daemon_pipe2[1], &retval, sizeof(retval), NULL);
 
1194
 
 
1195
    pa_close_pipe(daemon_pipe2);
 
1196
    pa_close_pipe(daemon_pipe);
 
1197
#endif
 
1198
 
 
1199
    if (mainloop)
 
1200
        pa_mainloop_free(mainloop);
 
1201
 
 
1202
    if (conf)
 
1203
        pa_daemon_conf_free(conf);
 
1204
 
 
1205
    if (valid_pid_file)
 
1206
        pa_pid_file_remove();
 
1207
 
 
1208
    /* This has no real purpose except making things valgrind-clean */
 
1209
    pa_unset_env_recorded();
 
1210
 
 
1211
#ifdef OS_IS_WIN32
 
1212
    WSACleanup();
 
1213
#endif
 
1214
 
 
1215
    if (ltdl_init)
 
1216
        pa_ltdl_done();
 
1217
 
 
1218
#ifdef HAVE_DBUS
 
1219
    dbus_shutdown();
 
1220
#endif
 
1221
 
 
1222
    return retval;
 
1223
}