1/***
2  This file is part of PulseAudio.
3
4  Copyright 2007 Lennart Poettering
5
6  PulseAudio is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as
8  published by the Free Software Foundation; either version 2.1 of the
9  License, or (at your option) any later version.
10
11  PulseAudio is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <errno.h>
25#include <stdio.h>
26#include <sys/types.h>
27#include <dirent.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <signal.h>
31
32#ifdef HAVE_SYS_PRCTL_H
33#include <sys/prctl.h>
34#endif
35#ifdef HAVE_SYS_RESOURCE_H
36#include <sys/resource.h>
37#endif
38
39#include <pulsecore/core-util.h>
40#include <pulsecore/core-error.h>
41#include <pulsecore/pipe.h>
42
43#include "start-child.h"
44
45int pa_start_child_for_read(const char *name, const char *argv1, pid_t *pid) {
46#ifdef HAVE_FORK
47    pid_t child;
48    int pipe_fds[2] = { -1, -1 };
49
50    if (pipe(pipe_fds) < 0) {
51        pa_log("pipe() failed: %s", pa_cstrerror(errno));
52        goto fail;
53    }
54
55    if ((child = fork()) == (pid_t) -1) {
56        pa_log("fork() failed: %s", pa_cstrerror(errno));
57        goto fail;
58
59    } else if (child != 0) {
60
61        /* Parent */
62        pa_assert_se(pa_close(pipe_fds[1]) == 0);
63
64        if (pid)
65            *pid = child;
66
67        return pipe_fds[0];
68    } else {
69        /* child */
70
71        pa_reset_personality();
72
73        pa_assert_se(pa_close(pipe_fds[0]) == 0);
74        pa_assert_se(dup2(pipe_fds[1], STDOUT_FILENO) == STDOUT_FILENO);
75
76        if (pipe_fds[1] != STDOUT_FILENO)
77            pa_assert_se(pa_close(pipe_fds[1]) == 0);
78
79        pa_close(STDIN_FILENO);
80        pa_assert_se(open("/dev/null", O_RDONLY) == STDIN_FILENO);
81
82        pa_close(STDERR_FILENO);
83        pa_assert_se(open("/dev/null", O_WRONLY) == STDERR_FILENO);
84
85        pa_close_all(-1);
86        pa_reset_sigs(-1);
87        pa_unblock_sigs(-1);
88        pa_reset_priority();
89        pa_unset_env_recorded();
90
91        /* Make sure our children are not influenced by the
92         * LD_BIND_NOW we set for ourselves. */
93        pa_unset_env("LD_BIND_NOW");
94
95#ifdef PR_SET_PDEATHSIG
96        /* On Linux we can use PR_SET_PDEATHSIG to have the helper
97        process killed when the daemon dies abnormally. On non-Linux
98        machines the client will die as soon as it writes data to
99        stdout again (SIGPIPE) */
100
101        prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
102#endif
103
104        execl(name, name, argv1, NULL);
105        _exit(1);
106    }
107
108fail:
109    pa_close_pipe(pipe_fds);
110#endif /* HAVE_FORK */
111
112    return -1;
113}
114