~diwic/ubuntu/lucid/pulseaudio/bugfixes

« back to all changes in this revision

Viewing changes to src/daemon/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich
  • Date: 2008-11-04 15:46:00 UTC
  • mfrom: (1.2.1 upstream) (1.1.6 lenny)
  • Revision ID: james.westby@ubuntu.com-20081104154600-hlzknpcazaam0nxm
Tags: 0.9.13-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Don't build against, and create jack package. Jack is not in main.
  - Remove --disable-per-user-esound-socket from configure flags, as we still
    want per user esound sockets.
  - Remove stop links from rc0 and rc6.
  - Change default resample algorithm and bubffer size.
  - Add alsa configuration files to route alsa applications via pulseaudio.
  - Move libasound2-plugins from Recommends to Depends.
* debian/pulseaudio.preinst: When upgrading from intrepid, remove
  /etc/X11/Xsession.d/70pulseaudio, as this was used to minimize a race
  condition when starting GNOME in intrepid. This race should not exist in
  jaunty once libcanberra is built to use pulseaudio as a backend.
* Do not spawn a pulseaudio server if clients fail to find a running server.
* Remove explicit version dependency for libspeex-dev to allow the package
  to be built for now.
* Regenerate autotools files to work with Ubuntu's newer libtool/libltdl.
* debian/control: libpulsecore5 -> libpulsecore8 to match the library
  soname.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: main.c 2187 2008-03-28 18:09:25Z lennart $ */
2
 
 
3
1
/***
4
2
  This file is part of PulseAudio.
5
3
 
66
64
#include <pulse/mainloop-signal.h>
67
65
#include <pulse/timeval.h>
68
66
#include <pulse/xmalloc.h>
 
67
#include <pulse/i18n.h>
69
68
 
 
69
#include <pulsecore/lock-autospawn.h>
70
70
#include <pulsecore/winsock.h>
71
71
#include <pulsecore/core-error.h>
72
72
#include <pulsecore/core.h>
111
111
 
112
112
#ifdef OS_IS_WIN32
113
113
 
114
 
static void message_cb(pa_mainloop_api*a, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
 
114
static void message_cb(pa_mainloop_api*a, pa_time_event*e, const struct timeval *tv, void *userdata) {
115
115
    MSG msg;
116
116
    struct timeval tvnext;
117
117
 
118
 
    while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
 
118
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
119
119
        if (msg.message == WM_QUIT)
120
120
            raise(SIGTERM);
121
121
        else {
130
130
 
131
131
#endif
132
132
 
133
 
static void signal_callback(pa_mainloop_api*m, PA_GCC_UNUSED pa_signal_event *e, int sig, void *userdata) {
134
 
    pa_log_info("Got signal %s.", pa_sig2str(sig));
 
133
static void signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
 
134
    pa_log_info(_("Got signal %s."), pa_sig2str(sig));
135
135
 
136
136
    switch (sig) {
137
137
#ifdef SIGUSR1
158
158
        case SIGINT:
159
159
        case SIGTERM:
160
160
        default:
161
 
            pa_log_info("Exiting.");
 
161
            pa_log_info(_("Exiting."));
162
162
            m->quit(m, 1);
163
163
            break;
164
164
    }
165
165
}
166
166
 
167
 
#define set_env(key, value) putenv(pa_sprintf_malloc("%s=%s", (key), (value)))
168
 
 
169
167
#if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
170
168
 
171
169
static int change_user(void) {
178
176
     * afterwards. */
179
177
 
180
178
    if (!(pw = getpwnam(PA_SYSTEM_USER))) {
181
 
        pa_log("Failed to find user '%s'.", PA_SYSTEM_USER);
 
179
        pa_log(_("Failed to find user '%s'."), PA_SYSTEM_USER);
182
180
        return -1;
183
181
    }
184
182
 
185
183
    if (!(gr = getgrnam(PA_SYSTEM_GROUP))) {
186
 
        pa_log("Failed to find group '%s'.", PA_SYSTEM_GROUP);
 
184
        pa_log(_("Failed to find group '%s'."), PA_SYSTEM_GROUP);
187
185
        return -1;
188
186
    }
189
187
 
190
 
    pa_log_info("Found user '%s' (UID %lu) and group '%s' (GID %lu).",
 
188
    pa_log_info(_("Found user '%s' (UID %lu) and group '%s' (GID %lu)."),
191
189
                PA_SYSTEM_USER, (unsigned long) pw->pw_uid,
192
190
                PA_SYSTEM_GROUP, (unsigned long) gr->gr_gid);
193
191
 
194
192
    if (pw->pw_gid != gr->gr_gid) {
195
 
        pa_log("GID of user '%s' and of group '%s' don't match.", PA_SYSTEM_USER, PA_SYSTEM_GROUP);
 
193
        pa_log(_("GID of user '%s' and of group '%s' don't match."), PA_SYSTEM_USER, PA_SYSTEM_GROUP);
196
194
        return -1;
197
195
    }
198
196
 
199
197
    if (strcmp(pw->pw_dir, PA_SYSTEM_RUNTIME_PATH) != 0)
200
 
        pa_log_warn("Warning: home directory of user '%s' is not '%s', ignoring.", PA_SYSTEM_USER, PA_SYSTEM_RUNTIME_PATH);
 
198
        pa_log_warn(_("Home directory of user '%s' is not '%s', ignoring."), PA_SYSTEM_USER, PA_SYSTEM_RUNTIME_PATH);
201
199
 
202
200
    if (pa_make_secure_dir(PA_SYSTEM_RUNTIME_PATH, 0755, pw->pw_uid, gr->gr_gid) < 0) {
203
 
        pa_log("Failed to create '%s': %s", PA_SYSTEM_RUNTIME_PATH, pa_cstrerror(errno));
204
 
        return -1;
205
 
    }
 
201
        pa_log(_("Failed to create '%s': %s"), PA_SYSTEM_RUNTIME_PATH, pa_cstrerror(errno));
 
202
        return -1;
 
203
    }
 
204
 
 
205
    if (pa_make_secure_dir(PA_SYSTEM_STATE_PATH, 0700, pw->pw_uid, gr->gr_gid) < 0) {
 
206
        pa_log(_("Failed to create '%s': %s"), PA_SYSTEM_STATE_PATH, pa_cstrerror(errno));
 
207
        return -1;
 
208
    }
 
209
 
 
210
    /* We don't create the config dir here, because we don't need to write to it */
206
211
 
207
212
    if (initgroups(PA_SYSTEM_USER, gr->gr_gid) != 0) {
208
 
        pa_log("Failed to change group list: %s", pa_cstrerror(errno));
 
213
        pa_log(_("Failed to change group list: %s"), pa_cstrerror(errno));
209
214
        return -1;
210
215
    }
211
216
 
221
226
#endif
222
227
 
223
228
    if (r < 0) {
224
 
        pa_log("Failed to change GID: %s", pa_cstrerror(errno));
 
229
        pa_log(_("Failed to change GID: %s"), pa_cstrerror(errno));
225
230
        return -1;
226
231
    }
227
232
 
237
242
#endif
238
243
 
239
244
    if (r < 0) {
240
 
        pa_log("Failed to change UID: %s", pa_cstrerror(errno));
 
245
        pa_log(_("Failed to change UID: %s"), pa_cstrerror(errno));
241
246
        return -1;
242
247
    }
243
248
 
244
 
    set_env("USER", PA_SYSTEM_USER);
245
 
    set_env("USERNAME", PA_SYSTEM_USER);
246
 
    set_env("LOGNAME", PA_SYSTEM_USER);
247
 
    set_env("HOME", PA_SYSTEM_RUNTIME_PATH);
 
249
    pa_set_env("USER", PA_SYSTEM_USER);
 
250
    pa_set_env("USERNAME", PA_SYSTEM_USER);
 
251
    pa_set_env("LOGNAME", PA_SYSTEM_USER);
 
252
    pa_set_env("HOME", PA_SYSTEM_RUNTIME_PATH);
248
253
 
249
254
    /* Relevant for pa_runtime_path() */
250
 
    set_env("PULSE_RUNTIME_PATH", PA_SYSTEM_RUNTIME_PATH);
251
 
    set_env("PULSE_CONFIG_PATH", PA_SYSTEM_RUNTIME_PATH);
 
255
    pa_set_env("PULSE_RUNTIME_PATH", PA_SYSTEM_RUNTIME_PATH);
 
256
    pa_set_env("PULSE_CONFIG_PATH", PA_SYSTEM_CONFIG_PATH);
 
257
    pa_set_env("PULSE_STATE_PATH", PA_SYSTEM_STATE_PATH);
252
258
 
253
 
    pa_log_info("Successfully dropped root privileges.");
 
259
    pa_log_info(_("Successfully dropped root privileges."));
254
260
 
255
261
    return 0;
256
262
}
258
264
#else /* HAVE_PWD_H && HAVE_GRP_H */
259
265
 
260
266
static int change_user(void) {
261
 
    pa_log("System wide mode unsupported on this platform.");
 
267
    pa_log(_("System wide mode unsupported on this platform."));
262
268
    return -1;
263
269
}
264
270
 
265
271
#endif /* HAVE_PWD_H && HAVE_GRP_H */
266
272
 
267
 
static int create_runtime_dir(void) {
268
 
    char fn[PATH_MAX];
269
 
 
270
 
    pa_runtime_path(NULL, fn, sizeof(fn));
271
 
 
272
 
    /* This function is called only when the daemon is started in
273
 
     * per-user mode. We create the runtime directory somewhere in
274
 
     * /tmp/ with the current UID/GID */
275
 
 
276
 
    if (pa_make_secure_dir(fn, 0700, (uid_t)-1, (gid_t)-1) < 0) {
277
 
        pa_log("Failed to create '%s': %s", fn, pa_cstrerror(errno));
278
 
        return -1;
279
 
    }
280
 
 
281
 
    return 0;
282
 
}
283
 
 
284
273
#ifdef HAVE_SYS_RESOURCE_H
285
274
 
286
275
static int set_one_rlimit(const pa_rlimit *r, int resource, const char *name) {
293
282
    rl.rlim_cur = rl.rlim_max = r->value;
294
283
 
295
284
    if (setrlimit(resource, &rl) < 0) {
296
 
        pa_log_warn("setrlimit(%s, (%u, %u)) failed: %s", name, (unsigned) r->value, (unsigned) r->value, pa_cstrerror(errno));
 
285
        pa_log_info(_("setrlimit(%s, (%u, %u)) failed: %s"), name, (unsigned) r->value, (unsigned) r->value, pa_cstrerror(errno));
297
286
        return -1;
298
287
    }
299
288
 
301
290
}
302
291
 
303
292
static void set_all_rlimits(const pa_daemon_conf *conf) {
304
 
    set_one_rlimit(&conf->rlimit_as, RLIMIT_AS, "RLIMIT_AS");
305
 
    set_one_rlimit(&conf->rlimit_core, RLIMIT_CORE, "RLIMIT_CORE");
 
293
    set_one_rlimit(&conf->rlimit_fsize, RLIMIT_FSIZE, "RLIMIT_FSIZE");
306
294
    set_one_rlimit(&conf->rlimit_data, RLIMIT_DATA, "RLIMIT_DATA");
307
 
    set_one_rlimit(&conf->rlimit_fsize, RLIMIT_FSIZE, "RLIMIT_FSIZE");
308
 
    set_one_rlimit(&conf->rlimit_nofile, RLIMIT_NOFILE, "RLIMIT_NOFILE");
309
295
    set_one_rlimit(&conf->rlimit_stack, RLIMIT_STACK, "RLIMIT_STACK");
 
296
    set_one_rlimit(&conf->rlimit_core, RLIMIT_CORE, "RLIMIT_CORE");
 
297
    set_one_rlimit(&conf->rlimit_rss, RLIMIT_RSS, "RLIMIT_RSS");
310
298
#ifdef RLIMIT_NPROC
311
299
    set_one_rlimit(&conf->rlimit_nproc, RLIMIT_NPROC, "RLIMIT_NPROC");
312
300
#endif
 
301
    set_one_rlimit(&conf->rlimit_nofile, RLIMIT_NOFILE, "RLIMIT_NOFILE");
313
302
#ifdef RLIMIT_MEMLOCK
314
303
    set_one_rlimit(&conf->rlimit_memlock, RLIMIT_MEMLOCK, "RLIMIT_MEMLOCK");
315
304
#endif
 
305
    set_one_rlimit(&conf->rlimit_as, RLIMIT_AS, "RLIMIT_AS");
 
306
#ifdef RLIMIT_LOCKS
 
307
    set_one_rlimit(&conf->rlimit_locks, RLIMIT_LOCKS, "RLIMIT_LOCKS");
 
308
#endif
 
309
#ifdef RLIMIT_SIGPENDING
 
310
    set_one_rlimit(&conf->rlimit_sigpending, RLIMIT_SIGPENDING, "RLIMIT_SIGPENDING");
 
311
#endif
 
312
#ifdef RLIMIT_MSGQUEUE
 
313
    set_one_rlimit(&conf->rlimit_msgqueue, RLIMIT_MSGQUEUE, "RLIMIT_MSGQUEUE");
 
314
#endif
316
315
#ifdef RLIMIT_NICE
317
316
    set_one_rlimit(&conf->rlimit_nice, RLIMIT_NICE, "RLIMIT_NICE");
318
317
#endif
319
318
#ifdef RLIMIT_RTPRIO
320
319
    set_one_rlimit(&conf->rlimit_rtprio, RLIMIT_RTPRIO, "RLIMIT_RTPRIO");
321
320
#endif
 
321
#ifdef RLIMIT_RTTIME
 
322
    set_one_rlimit(&conf->rlimit_rttime, RLIMIT_RTTIME, "RLIMIT_RTTIME");
 
323
#endif
322
324
}
323
325
#endif
324
326
 
329
331
    pa_mainloop *mainloop = NULL;
330
332
    char *s;
331
333
    int r = 0, retval = 1, d = 0;
332
 
    int daemon_pipe[2] = { -1, -1 };
333
334
    pa_bool_t suid_root, real_root;
334
 
    int valid_pid_file = 0;
 
335
    pa_bool_t valid_pid_file = FALSE;
335
336
    gid_t gid = (gid_t) -1;
336
 
    pa_bool_t allow_realtime, allow_high_priority;
337
337
    pa_bool_t ltdl_init = FALSE;
338
 
 
 
338
    int passed_fd = -1;
 
339
    const char *e;
 
340
#ifdef HAVE_FORK
 
341
    int daemon_pipe[2] = { -1, -1 };
 
342
#endif
339
343
#ifdef OS_IS_WIN32
340
 
    pa_time_event *timer;
341
 
    struct timeval tv;
 
344
    pa_time_event *win32_timer;
 
345
    struct timeval win32_tv;
342
346
#endif
 
347
    char *lf = NULL;
 
348
    int autospawn_fd = -1;
 
349
    pa_bool_t autospawn_locked = FALSE;
343
350
 
 
351
    pa_log_set_maximal_level(PA_LOG_INFO);
 
352
    pa_log_set_ident("pulseaudio");
344
353
 
345
354
#if defined(__linux__) && defined(__OPTIMIZE__)
346
355
    /*
355
364
        /* We have to execute ourselves, because the libc caches the
356
365
         * value of $LD_BIND_NOW on initialization. */
357
366
 
358
 
        putenv(pa_xstrdup("LD_BIND_NOW=1"));
 
367
        pa_set_env("LD_BIND_NOW", "1");
359
368
        pa_assert_se(rp = pa_readlink("/proc/self/exe"));
360
369
        pa_assert_se(execv(rp, argv) == 0);
361
370
    }
369
378
    suid_root = FALSE;
370
379
#endif
371
380
 
372
 
    if (suid_root) {
 
381
    if (!real_root) {
373
382
        /* Drop all capabilities except CAP_SYS_NICE  */
374
383
        pa_limit_caps();
375
384
 
385
394
         * is just too risky tun let PA run as root all the time. */
386
395
    }
387
396
 
 
397
    if ((e = getenv("PULSE_PASSED_FD"))) {
 
398
        passed_fd = atoi(e);
 
399
 
 
400
        if (passed_fd <= 2)
 
401
            passed_fd = -1;
 
402
    }
 
403
 
 
404
    pa_close_all(passed_fd, -1);
 
405
 
 
406
    pa_reset_sigs(-1);
 
407
    pa_unblock_sigs(-1);
 
408
 
388
409
    /* At this point, we are a normal user, possibly with CAP_NICE if
389
410
     * we were started SUID. If we are started as normal root, than we
390
411
     * still are normal root. */
391
412
 
392
413
    setlocale(LC_ALL, "");
393
 
    pa_log_set_maximal_level(PA_LOG_INFO);
394
 
    pa_log_set_ident("pulseaudio");
 
414
    pa_init_i18n();
395
415
 
396
416
    conf = pa_daemon_conf_new();
397
417
 
402
422
        goto finish;
403
423
 
404
424
    if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
405
 
        pa_log("Failed to parse command line.");
 
425
        pa_log(_("Failed to parse command line."));
406
426
        goto finish;
407
427
    }
408
428
 
409
429
    pa_log_set_maximal_level(conf->log_level);
410
430
    pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
411
431
 
412
 
    if (suid_root) {
413
 
        /* Ok, we're suid root, so let's better not enable high prio
414
 
         * or RT by default */
415
 
 
416
 
        allow_high_priority = allow_realtime = FALSE;
 
432
    pa_log_debug("Started as real root: %s, suid root: %s", pa_yes_no(real_root), pa_yes_no(suid_root));
 
433
 
 
434
    if (!real_root && pa_have_caps()) {
 
435
        pa_bool_t allow_high_priority = FALSE, allow_realtime = FALSE;
 
436
 
 
437
        /* Let's better not enable high prio or RT by default */
 
438
 
 
439
        if (conf->high_priority && !allow_high_priority) {
 
440
            if (pa_own_uid_in_group(PA_REALTIME_GROUP, &gid) > 0) {
 
441
                pa_log_info(_("We're in the group '%s', allowing high-priority scheduling."), PA_REALTIME_GROUP);
 
442
                allow_high_priority = TRUE;
 
443
            }
 
444
        }
 
445
 
 
446
        if (conf->realtime_scheduling && !allow_realtime) {
 
447
            if (pa_own_uid_in_group(PA_REALTIME_GROUP, &gid) > 0) {
 
448
                pa_log_info(_("We're in the group '%s', allowing real-time scheduling."), PA_REALTIME_GROUP);
 
449
                allow_realtime = TRUE;
 
450
            }
 
451
        }
417
452
 
418
453
#ifdef HAVE_POLKIT
419
 
        if (conf->high_priority) {
 
454
        if (conf->high_priority && !allow_high_priority) {
420
455
            if (pa_polkit_check("org.pulseaudio.acquire-high-priority") > 0) {
421
 
                pa_log_info("PolicyKit grants us acquire-high-priority privilige.");
 
456
                pa_log_info(_("PolicyKit grants us acquire-high-priority privilege."));
422
457
                allow_high_priority = TRUE;
423
458
            } else
424
 
                pa_log_info("PolicyKit refuses acquire-high-priority privilige.");
 
459
                pa_log_info(_("PolicyKit refuses acquire-high-priority privilege."));
425
460
        }
426
461
 
427
 
        if (conf->realtime_scheduling) {
 
462
        if (conf->realtime_scheduling && !allow_realtime) {
428
463
            if (pa_polkit_check("org.pulseaudio.acquire-real-time") > 0) {
429
 
                pa_log_info("PolicyKit grants us acquire-real-time privilige.");
 
464
                pa_log_info(_("PolicyKit grants us acquire-real-time privilege."));
430
465
                allow_realtime = TRUE;
431
466
            } else
432
 
                pa_log_info("PolicyKit refuses acquire-real-time privilige.");
 
467
                pa_log_info(_("PolicyKit refuses acquire-real-time privilege."));
433
468
        }
434
469
#endif
435
470
 
436
 
        if ((conf->high_priority || conf->realtime_scheduling) && pa_own_uid_in_group(PA_REALTIME_GROUP, &gid) > 0) {
437
 
            pa_log_info("We're in the group '"PA_REALTIME_GROUP"', allowing real-time and high-priority scheduling.");
438
 
            allow_realtime = conf->realtime_scheduling;
439
 
            allow_high_priority = conf->high_priority;
440
 
        }
441
 
 
442
471
        if (!allow_high_priority && !allow_realtime) {
443
472
 
444
473
            /* OK, there's no further need to keep CAP_NICE. Hence
445
474
             * let's give it up early */
446
475
 
447
476
            pa_drop_caps();
448
 
            pa_drop_root();
449
 
            suid_root = real_root = FALSE;
450
477
 
451
478
            if (conf->high_priority || conf->realtime_scheduling)
452
 
                pa_log_notice("Called SUID root and real-time/high-priority scheduling was requested in the configuration. However, we lack the necessary priviliges:\n"
453
 
                              "We are not in group '"PA_REALTIME_GROUP"' and PolicyKit refuse to grant us priviliges. Dropping SUID again.\n"
454
 
                              "For enabling real-time scheduling please acquire the appropriate PolicyKit priviliges, or become a member of '"PA_REALTIME_GROUP"', or increase the RLIMIT_NICE/RLIMIT_RTPRIO resource limits for this user.");
 
479
                pa_log_notice(_("Called SUID root and real-time/high-priority scheduling was requested in the configuration. However, we lack the necessary priviliges:\n"
 
480
                                "We are not in group '"PA_REALTIME_GROUP"' and PolicyKit refuse to grant us priviliges. Dropping SUID again.\n"
 
481
                                "For enabling real-time scheduling please acquire the appropriate PolicyKit priviliges, or become a member of '"PA_REALTIME_GROUP"', or increase the RLIMIT_NICE/RLIMIT_RTPRIO resource limits for this user."));
455
482
        }
456
 
 
457
 
    } else {
458
 
 
459
 
        /* OK, we're a normal user, so let's allow the user evrything
460
 
         * he asks for, it's now the kernel's job to enforce limits,
461
 
         * not ours anymore */
462
 
        allow_high_priority = allow_realtime = TRUE;
463
 
    }
464
 
 
465
 
    if (conf->high_priority && !allow_high_priority) {
466
 
        pa_log_info("High-priority scheduling enabled in configuration but now allowed by policy. Disabling forcibly.");
467
 
        conf->high_priority = FALSE;
468
 
    }
469
 
 
470
 
    if (conf->realtime_scheduling && !allow_realtime) {
471
 
        pa_log_info("Real-time scheduling enabled in configuration but now allowed by policy. Disabling forcibly.");
472
 
        conf->realtime_scheduling = FALSE;
473
 
    }
474
 
 
475
 
    if (conf->high_priority && conf->cmd == PA_CMD_DAEMON)
 
483
    }
 
484
 
 
485
#ifdef HAVE_SYS_RESOURCE_H
 
486
    /* Reset resource limits. If we are run as root (for system mode)
 
487
     * this might end up increasing the limits, which is intended
 
488
     * behaviour. For all other cases, i.e. started as normal user, or
 
489
     * SUID root at this point we should have no CAP_SYS_RESOURCE and
 
490
     * increasing the limits thus should fail. Which is, too, intended
 
491
     * behaviour */
 
492
 
 
493
    set_all_rlimits(conf);
 
494
#endif
 
495
 
 
496
    if (conf->high_priority && !pa_can_high_priority())
 
497
        pa_log_warn(_("High-priority scheduling enabled in configuration but not allowed by policy."));
 
498
 
 
499
    if (conf->high_priority && (conf->cmd == PA_CMD_DAEMON || conf->cmd == PA_CMD_START))
476
500
        pa_raise_priority(conf->nice_level);
477
501
 
478
 
    if (suid_root) {
 
502
    if (!real_root && pa_have_caps()) {
479
503
        pa_bool_t drop;
480
504
 
481
 
        drop = conf->cmd != PA_CMD_DAEMON || !conf->realtime_scheduling;
 
505
        drop = (conf->cmd != PA_CMD_DAEMON && conf->cmd != PA_CMD_START) || !conf->realtime_scheduling;
482
506
 
483
507
#ifdef RLIMIT_RTPRIO
484
508
        if (!drop) {
485
 
 
 
509
            struct rlimit rl;
486
510
            /* At this point we still have CAP_NICE if we were loaded
487
511
             * SUID root. If possible let's acquire RLIMIT_RTPRIO
488
512
             * instead and give CAP_NICE up. */
489
513
 
490
 
            const pa_rlimit rl = { 9, TRUE };
491
 
 
492
 
            if (set_one_rlimit(&rl, RLIMIT_RTPRIO, "RLIMIT_RTPRIO") >= 0) {
493
 
                pa_log_info("Successfully increased RLIMIT_RTPRIO, giving up CAP_NICE.");
494
 
                drop = TRUE;
495
 
            } else
496
 
                pa_log_warn("RLIMIT_RTPRIO failed: %s", pa_cstrerror(errno));
 
514
            if (getrlimit(RLIMIT_RTPRIO, &rl) >= 0) {
 
515
 
 
516
                if (rl.rlim_cur >= 9)
 
517
                    drop = TRUE;
 
518
                else {
 
519
                    rl.rlim_max = rl.rlim_cur = 9;
 
520
 
 
521
                    if (setrlimit(RLIMIT_RTPRIO, &rl) >= 0) {
 
522
                        pa_log_info(_("Successfully increased RLIMIT_RTPRIO"));
 
523
                        drop = TRUE;
 
524
                    } else
 
525
                        pa_log_warn(_("RLIMIT_RTPRIO failed: %s"), pa_cstrerror(errno));
 
526
                }
 
527
            }
497
528
        }
498
529
#endif
499
530
 
500
531
        if (drop)  {
 
532
            pa_log_info(_("Giving up CAP_NICE"));
501
533
            pa_drop_caps();
502
 
            pa_drop_root();
503
 
            suid_root = real_root = FALSE;
 
534
            suid_root = FALSE;
504
535
        }
505
536
    }
506
537
 
 
538
    if (conf->realtime_scheduling && !pa_can_realtime())
 
539
        pa_log_warn(_("Real-time scheduling enabled in configuration but not allowed by policy."));
 
540
 
 
541
    pa_log_debug("Can realtime: %s, can high-priority: %s", pa_yes_no(pa_can_realtime()), pa_yes_no(pa_can_high_priority()));
 
542
 
507
543
    LTDL_SET_PRELOADED_SYMBOLS();
508
544
    pa_ltdl_init();
509
545
    ltdl_init = TRUE;
558
594
            pid_t pid;
559
595
 
560
596
            if (pa_pid_file_check_running(&pid, "pulseaudio") < 0)
561
 
                pa_log_info("Daemon not running");
 
597
                pa_log_info(_("Daemon not running"));
562
598
            else {
563
 
                pa_log_info("Daemon running as PID %u", pid);
 
599
                pa_log_info(_("Daemon running as PID %u"), pid);
564
600
                retval = 0;
565
601
            }
566
602
 
570
606
        case PA_CMD_KILL:
571
607
 
572
608
            if (pa_pid_file_kill(SIGINT, NULL, "pulseaudio") < 0)
573
 
                pa_log("Failed to kill daemon.");
 
609
                pa_log(_("Failed to kill daemon: %s"), pa_cstrerror(errno));
574
610
            else
575
611
                retval = 0;
576
612
 
584
620
            goto finish;
585
621
 
586
622
        default:
587
 
            pa_assert(conf->cmd == PA_CMD_DAEMON);
 
623
            pa_assert(conf->cmd == PA_CMD_DAEMON || conf->cmd == PA_CMD_START);
588
624
    }
589
625
 
590
626
    if (real_root && !conf->system_instance)
591
 
        pa_log_warn("This program is not intended to be run as root (unless --system is specified).");
 
627
        pa_log_warn(_("This program is not intended to be run as root (unless --system is specified)."));
592
628
    else if (!real_root && conf->system_instance) {
593
 
        pa_log("Root priviliges required.");
594
 
        goto finish;
 
629
        pa_log(_("Root priviliges required."));
 
630
        goto finish;
 
631
    }
 
632
 
 
633
    if (conf->cmd == PA_CMD_START && conf->system_instance) {
 
634
        pa_log(_("--start not supported for system instances."));
 
635
        goto finish;
 
636
    }
 
637
 
 
638
    if (conf->system_instance && !conf->disallow_exit)
 
639
        pa_log_warn(_("Running in system mode, but --disallow-exit not set!"));
 
640
 
 
641
    if (conf->system_instance && !conf->disallow_module_loading)
 
642
        pa_log_warn(_("Running in system mode, but --disallow-module-loading not set!"));
 
643
 
 
644
    if (conf->system_instance && !conf->disable_shm) {
 
645
        pa_log_notice(_("Running in system mode, forcibly disabling SHM mode!"));
 
646
        conf->disable_shm = TRUE;
 
647
    }
 
648
 
 
649
    if (conf->system_instance && conf->exit_idle_time > 0) {
 
650
        pa_log_notice(_("Running in system mode, forcibly disabling exit idle time!"));
 
651
        conf->exit_idle_time = 0;
 
652
    }
 
653
 
 
654
    if (conf->cmd == PA_CMD_START) {
 
655
        /* If we shall start PA only when it is not running yet, we
 
656
         * first take the autospawn lock to make things
 
657
         * synchronous. */
 
658
 
 
659
        if ((autospawn_fd = pa_autospawn_lock_init()) < 0) {
 
660
            pa_log("Failed to initialize autospawn lock");
 
661
            goto finish;
 
662
        }
 
663
 
 
664
        if ((pa_autospawn_lock_acquire(TRUE) < 0)) {
 
665
            pa_log("Failed to acquire autospawn lock");
 
666
            goto finish;
 
667
        }
 
668
 
 
669
        autospawn_locked = TRUE;
595
670
    }
596
671
 
597
672
    if (conf->daemonize) {
599
674
        int tty_fd;
600
675
 
601
676
        if (pa_stdio_acquire() < 0) {
602
 
            pa_log("Failed to acquire stdio.");
 
677
            pa_log(_("Failed to acquire stdio."));
603
678
            goto finish;
604
679
        }
605
680
 
606
681
#ifdef HAVE_FORK
607
682
        if (pipe(daemon_pipe) < 0) {
608
 
            pa_log("Failed to create pipe.");
 
683
            pa_log(_("pipe failed: %s"), pa_cstrerror(errno));
609
684
            goto finish;
610
685
        }
611
686
 
612
687
        if ((child = fork()) < 0) {
613
 
            pa_log("fork() failed: %s", pa_cstrerror(errno));
 
688
            pa_log(_("fork() failed: %s"), pa_cstrerror(errno));
614
689
            goto finish;
615
690
        }
616
691
 
617
692
        if (child != 0) {
 
693
            ssize_t n;
618
694
            /* Father */
619
695
 
620
696
            pa_assert_se(pa_close(daemon_pipe[1]) == 0);
621
697
            daemon_pipe[1] = -1;
622
698
 
623
 
            if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL) != sizeof(retval)) {
624
 
                pa_log("read() failed: %s", pa_cstrerror(errno));
 
699
            if ((n = pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL)) != sizeof(retval)) {
 
700
 
 
701
                if (n < 0)
 
702
                    pa_log(_("read() failed: %s"), pa_cstrerror(errno));
 
703
 
625
704
                retval = 1;
626
705
            }
627
706
 
628
707
            if (retval)
629
 
                pa_log("daemon startup failed.");
 
708
                pa_log(_("Daemon startup failed."));
630
709
            else
631
 
                pa_log_info("daemon startup successful.");
 
710
                pa_log_info(_("Daemon startup successful."));
632
711
 
633
712
            goto finish;
634
713
        }
635
714
 
 
715
        if (autospawn_fd >= 0) {
 
716
            /* The lock file is unlocked from the parent, so we need
 
717
             * to close it in the child */
 
718
 
 
719
            pa_autospawn_lock_release();
 
720
            pa_autospawn_lock_done(TRUE);
 
721
 
 
722
            autospawn_locked = FALSE;
 
723
            autospawn_fd = -1;
 
724
        }
 
725
 
636
726
        pa_assert_se(pa_close(daemon_pipe[0]) == 0);
637
727
        daemon_pipe[0] = -1;
638
728
#endif
652
742
        pa_close(1);
653
743
        pa_close(2);
654
744
 
655
 
        open("/dev/null", O_RDONLY);
656
 
        open("/dev/null", O_WRONLY);
657
 
        open("/dev/null", O_WRONLY);
 
745
        pa_assert_se(open("/dev/null", O_RDONLY) == 0);
 
746
        pa_assert_se(open("/dev/null", O_WRONLY) == 1);
 
747
        pa_assert_se(open("/dev/null", O_WRONLY) == 2);
658
748
#else
659
749
        FreeConsole();
660
750
#endif
677
767
#endif
678
768
    }
679
769
 
 
770
    pa_set_env("PULSE_INTERNAL", "1");
680
771
    pa_assert_se(chdir("/") == 0);
681
772
    umask(0022);
682
773
 
683
 
    if (conf->system_instance) {
 
774
    if (conf->system_instance)
684
775
        if (change_user() < 0)
685
776
            goto finish;
686
 
    } else if (create_runtime_dir() < 0)
687
 
        goto finish;
 
777
 
 
778
    pa_set_env("PULSE_SYSTEM", conf->system_instance ? "1" : "0");
 
779
 
 
780
    pa_log_info(_("This is PulseAudio %s"), PACKAGE_VERSION);
 
781
    pa_log_debug(_("Compilation host: %s"), CANONICAL_HOST);
 
782
    pa_log_debug(_("Compilation CFLAGS: %s"), PA_CFLAGS);
 
783
 
 
784
    s = pa_uname_string();
 
785
    pa_log_debug(_("Running on host: %s"), s);
 
786
    pa_xfree(s);
 
787
 
 
788
    pa_log_info(_("Page size is %lu bytes"), (unsigned long) PA_PAGE_SIZE);
 
789
 
 
790
#ifdef HAVE_VALGRIND_MEMCHECK_H
 
791
    pa_log_debug(_("Compiled with Valgrind support: yes"));
 
792
#else
 
793
    pa_log_debug(_("Compiled with Valgrind support: no"));
 
794
#endif
 
795
 
 
796
    pa_log_debug(_("Running in valgrind mode: %s"), pa_yes_no(pa_in_valgrind()));
 
797
 
 
798
#ifdef __OPTIMIZE__
 
799
    pa_log_debug(_("Optimized build: yes"));
 
800
#else
 
801
    pa_log_debug(_("Optimized build: no"));
 
802
#endif
 
803
 
 
804
    if (!(s = pa_machine_id())) {
 
805
        pa_log(_("Failed to get machine ID"));
 
806
        goto finish;
 
807
    }
 
808
    pa_log_info(_("Machine ID is %s."), s);
 
809
    pa_xfree(s);
 
810
 
 
811
    if (!(s = pa_get_runtime_dir()))
 
812
        goto finish;
 
813
    pa_log_info(_("Using runtime directory %s."), s);
 
814
    pa_xfree(s);
 
815
 
 
816
    if (!(s = pa_get_state_dir()))
 
817
        goto finish;
 
818
    pa_log_info(_("Using state directory %s."), s);
 
819
    pa_xfree(s);
 
820
 
 
821
    pa_log_info(_("Running in system mode: %s"), pa_yes_no(pa_in_system_mode()));
688
822
 
689
823
    if (conf->use_pid_file) {
690
 
        if (pa_pid_file_create() < 0) {
691
 
            pa_log("pa_pid_file_create() failed.");
692
 
#ifdef HAVE_FORK
693
 
            if (conf->daemonize)
694
 
                pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
695
 
#endif
 
824
        int z;
 
825
 
 
826
        if ((z = pa_pid_file_create("pulseaudio")) != 0) {
 
827
 
 
828
            if (conf->cmd == PA_CMD_START && z > 0) {
 
829
                /* If we are already running and with are run in
 
830
                 * --start mode, then let's return this as success. */
 
831
 
 
832
                retval = 0;
 
833
                goto finish;
 
834
            }
 
835
 
 
836
            pa_log(_("pa_pid_file_create() failed."));
696
837
            goto finish;
697
838
        }
698
839
 
699
 
        valid_pid_file = 1;
 
840
        valid_pid_file = TRUE;
700
841
    }
701
842
 
702
 
#ifdef HAVE_SYS_RESOURCE_H
703
 
    set_all_rlimits(conf);
704
 
#endif
705
 
 
706
843
#ifdef SIGPIPE
707
844
    signal(SIGPIPE, SIG_IGN);
708
845
#endif
709
846
 
710
 
    pa_log_info("This is PulseAudio " PACKAGE_VERSION);
711
 
    pa_log_info("Page size is %lu bytes", (unsigned long) PA_PAGE_SIZE);
712
 
 
713
847
    if (pa_rtclock_hrtimer())
714
 
        pa_log_info("Fresh high-resolution timers available! Bon appetit!");
 
848
        pa_log_info(_("Fresh high-resolution timers available! Bon appetit!"));
715
849
    else
716
 
        pa_log_info("Dude, your kernel stinks! The chef's recommendation today is Linux with high-resolution timers enabled!");
 
850
        pa_log_info(_("Dude, your kernel stinks! The chef's recommendation today is Linux with high-resolution timers enabled!"));
717
851
 
718
852
#ifdef SIGRTMIN
719
853
    /* Valgrind uses SIGRTMAX. To easy debugging we don't use it here */
722
856
 
723
857
    pa_assert_se(mainloop = pa_mainloop_new());
724
858
 
725
 
    if (!(c = pa_core_new(pa_mainloop_get_api(mainloop), !conf->disable_shm))) {
726
 
        pa_log("pa_core_new() failed.");
 
859
    if (!(c = pa_core_new(pa_mainloop_get_api(mainloop), !conf->disable_shm, conf->shm_size))) {
 
860
        pa_log(_("pa_core_new() failed."));
727
861
        goto finish;
728
862
    }
729
863
 
730
 
    c->is_system_instance = !!conf->system_instance;
731
864
    c->default_sample_spec = conf->default_sample_spec;
732
865
    c->default_n_fragments = conf->default_n_fragments;
733
866
    c->default_fragment_size_msec = conf->default_fragment_size_msec;
738
871
    c->realtime_priority = conf->realtime_priority;
739
872
    c->realtime_scheduling = !!conf->realtime_scheduling;
740
873
    c->disable_remixing = !!conf->disable_remixing;
 
874
    c->disable_lfe_remixing = !!conf->disable_lfe_remixing;
 
875
    c->running_as_daemon = !!conf->daemonize;
 
876
    c->disallow_exit = conf->disallow_exit;
741
877
 
742
878
    pa_assert_se(pa_signal_init(pa_mainloop_get_api(mainloop)) == 0);
743
879
    pa_signal_new(SIGINT, signal_callback, c);
744
880
    pa_signal_new(SIGTERM, signal_callback, c);
745
 
 
746
881
#ifdef SIGUSR1
747
882
    pa_signal_new(SIGUSR1, signal_callback, c);
748
883
#endif
754
889
#endif
755
890
 
756
891
#ifdef OS_IS_WIN32
757
 
    pa_assert_se(timer = pa_mainloop_get_api(mainloop)->time_new(pa_mainloop_get_api(mainloop), pa_gettimeofday(&tv), message_cb, NULL));
 
892
    win32_timer = pa_mainloop_get_api(mainloop)->time_new(pa_mainloop_get_api(mainloop), pa_gettimeofday(&win32_tv), message_cb, NULL);
758
893
#endif
759
894
 
760
 
    if (conf->daemonize)
761
 
        c->running_as_daemon = TRUE;
762
 
 
763
895
    oil_init();
764
896
 
765
897
    if (!conf->no_cpu_limit)
766
898
        pa_assert_se(pa_cpu_limit_init(pa_mainloop_get_api(mainloop)) == 0);
767
899
 
768
900
    buf = pa_strbuf_new();
769
 
    if (conf->default_script_file)
770
 
        r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail);
 
901
    if (conf->load_default_script_file) {
 
902
        FILE *f;
 
903
 
 
904
        if ((f = pa_daemon_conf_open_default_script_file(conf))) {
 
905
            r = pa_cli_command_execute_file_stream(c, f, buf, &conf->fail);
 
906
            fclose(f);
 
907
        }
 
908
    }
771
909
 
772
910
    if (r >= 0)
773
911
        r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
 
912
 
774
913
    pa_log_error("%s", s = pa_strbuf_tostring_free(buf));
775
914
    pa_xfree(s);
776
915
 
779
918
    c->disallow_module_loading = !!conf->disallow_module_loading;
780
919
 
781
920
    if (r < 0 && conf->fail) {
782
 
        pa_log("Failed to initialize daemon.");
783
 
#ifdef HAVE_FORK
784
 
        if (conf->daemonize)
785
 
            pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
786
 
#endif
787
 
    } else if (!c->modules || pa_idxset_size(c->modules) == 0) {
788
 
        pa_log("daemon startup without any loaded modules, refusing to work.");
789
 
#ifdef HAVE_FORK
790
 
        if (conf->daemonize)
791
 
            pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
792
 
#endif
793
 
    } else {
794
 
 
795
 
        retval = 0;
796
 
 
797
 
        if (c->default_sink_name &&
798
 
            pa_namereg_get(c, c->default_sink_name, PA_NAMEREG_SINK, 1) == NULL) {
799
 
            pa_log_error("%s : Default sink name (%s) does not exist in name register.", __FILE__, c->default_sink_name);
800
 
            retval = !!conf->fail;
801
 
        }
802
 
 
803
 
#ifdef HAVE_FORK
804
 
        if (conf->daemonize)
805
 
            pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
806
 
#endif
807
 
 
808
 
        if (!retval) {
809
 
            pa_log_info("Daemon startup complete.");
810
 
            if (pa_mainloop_run(mainloop, &retval) < 0)
811
 
                retval = 1;
812
 
            pa_log_info("Daemon shutdown initiated.");
813
 
        }
814
 
    }
 
921
        pa_log(_("Failed to initialize daemon."));
 
922
        goto finish;
 
923
    }
 
924
 
 
925
    if (!c->modules || pa_idxset_size(c->modules) == 0) {
 
926
        pa_log(_("Daemon startup without any loaded modules, refusing to work."));
 
927
        goto finish;
 
928
    }
 
929
 
 
930
    if (c->default_sink_name && !pa_namereg_get(c, c->default_sink_name, PA_NAMEREG_SINK, TRUE) && conf->fail) {
 
931
        pa_log_error(_("Default sink name (%s) does not exist in name register."), c->default_sink_name);
 
932
        goto finish;
 
933
    }
 
934
 
 
935
#ifdef HAVE_FORK
 
936
    if (daemon_pipe[1] >= 0) {
 
937
        int ok = 0;
 
938
        pa_loop_write(daemon_pipe[1], &ok, sizeof(ok), NULL);
 
939
        pa_close(daemon_pipe[1]);
 
940
        daemon_pipe[1] = -1;
 
941
    }
 
942
#endif
 
943
 
 
944
    pa_log_info(_("Daemon startup complete."));
 
945
 
 
946
    retval = 0;
 
947
    if (pa_mainloop_run(mainloop, &retval) < 0)
 
948
        goto finish;
 
949
 
 
950
    pa_log_info(_("Daemon shutdown initiated."));
 
951
 
 
952
finish:
 
953
 
 
954
    if (autospawn_fd >= 0) {
 
955
        if (autospawn_locked)
 
956
            pa_autospawn_lock_release();
 
957
 
 
958
        pa_autospawn_lock_done(FALSE);
 
959
    }
 
960
 
 
961
    if (lf)
 
962
        pa_xfree(lf);
815
963
 
816
964
#ifdef OS_IS_WIN32
817
 
    pa_mainloop_get_api(mainloop)->time_free(timer);
 
965
    if (win32_timer)
 
966
        pa_mainloop_get_api(mainloop)->time_free(win32_timer);
818
967
#endif
819
968
 
820
 
    pa_core_unref(c);
 
969
    if (c) {
 
970
        pa_core_unref(c);
 
971
        pa_log_info(_("Daemon terminated."));
 
972
    }
821
973
 
822
974
    if (!conf->no_cpu_limit)
823
975
        pa_cpu_limit_done();
824
976
 
825
977
    pa_signal_done();
826
978
 
827
 
    pa_log_info("Daemon terminated.");
 
979
#ifdef HAVE_FORK
 
980
    if (daemon_pipe[1] >= 0)
 
981
        pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
828
982
 
829
 
finish:
 
983
    pa_close_pipe(daemon_pipe);
 
984
#endif
830
985
 
831
986
    if (mainloop)
832
987
        pa_mainloop_free(mainloop);
837
992
    if (valid_pid_file)
838
993
        pa_pid_file_remove();
839
994
 
840
 
    pa_close_pipe(daemon_pipe);
841
 
 
842
995
#ifdef OS_IS_WIN32
843
996
    WSACleanup();
844
997
#endif