]> code.delx.au - pulseaudio/blob - polyp/main.c
make polypaudio run when installed
[pulseaudio] / polyp / main.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <signal.h>
32 #include <stddef.h>
33 #include <assert.h>
34 #include <ltdl.h>
35 #include <memblock.h>
36
37 #include "core.h"
38 #include "mainloop.h"
39 #include "module.h"
40 #include "mainloop-signal.h"
41 #include "cmdline.h"
42 #include "cli-command.h"
43 #include "util.h"
44 #include "sioman.h"
45
46 static struct pa_mainloop *mainloop;
47
48 static void exit_signal_callback(void *id, int sig, void *userdata) {
49 struct pa_mainloop_api* m = pa_mainloop_get_api(mainloop);
50 m->quit(m, 1);
51 fprintf(stderr, __FILE__": got signal.\n");
52 }
53
54 static void aux_signal_callback(void *id, int sig, void *userdata) {
55 struct pa_core *c = userdata;
56 assert(c);
57 pa_module_load(c, sig == SIGUSR1 ? "module-cli" : "module-cli-protocol-unix", NULL);
58 }
59
60 static void close_pipe(int p[2]) {
61 if (p[0] != -1)
62 close(p[0]);
63 if (p[1] != -1)
64 close(p[1]);
65 p[0] = p[1] = -1;
66 }
67
68 int main(int argc, char *argv[]) {
69 struct pa_core *c;
70 struct pa_cmdline *cmdline = NULL;
71 struct pa_strbuf *buf = NULL;
72 char *s;
73 int r, retval = 1;
74 int daemon_pipe[2] = { -1, -1 };
75
76 if (!(cmdline = pa_cmdline_parse(argc, argv))) {
77 fprintf(stderr, __FILE__": failed to parse command line.\n");
78 goto finish;
79 }
80
81 if (cmdline->help) {
82 pa_cmdline_help(argv[0]);
83 retval = 0;
84 goto finish;
85 }
86
87 if (cmdline->daemonize) {
88 pid_t child;
89
90 if (pa_stdio_acquire() < 0) {
91 fprintf(stderr, __FILE__": failed to acquire stdio.\n");
92 goto finish;
93 }
94
95 if (pipe(daemon_pipe) < 0) {
96 fprintf(stderr, __FILE__": failed to create pipe.\n");
97 goto finish;
98 }
99
100 if ((child = fork()) < 0) {
101 fprintf(stderr, __FILE__": fork() failed: %s\n", strerror(errno));
102 goto finish;
103 }
104
105 if (child != 0) {
106 /* Father */
107
108 close(daemon_pipe[1]);
109 daemon_pipe[1] = -1;
110
111 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
112 fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
113 retval = 1;
114 }
115
116 goto finish;
117 }
118
119 close(daemon_pipe[0]);
120 daemon_pipe[0] = -1;
121
122 setsid();
123 setpgrp();
124 }
125
126 r = lt_dlinit();
127 assert(r == 0);
128 #ifdef DLSEARCHDIR
129 lt_dladdsearchdir(DLSEARCHDIR);
130 #endif
131
132 mainloop = pa_mainloop_new();
133 assert(mainloop);
134
135 r = pa_signal_init(pa_mainloop_get_api(mainloop));
136 assert(r == 0);
137 pa_signal_register(SIGINT, exit_signal_callback, NULL);
138 signal(SIGPIPE, SIG_IGN);
139
140 c = pa_core_new(pa_mainloop_get_api(mainloop));
141 assert(c);
142
143 pa_signal_register(SIGUSR1, aux_signal_callback, c);
144 pa_signal_register(SIGUSR2, aux_signal_callback, c);
145
146 buf = pa_strbuf_new();
147 assert(buf);
148 r = pa_cli_command_execute(c, cmdline->cli_commands, buf, &cmdline->fail, &cmdline->verbose);
149 fprintf(stderr, s = pa_strbuf_tostring_free(buf));
150 free(s);
151
152 if (r < 0 && cmdline->fail) {
153 fprintf(stderr, __FILE__": failed to initialize daemon.\n");
154 if (cmdline->daemonize)
155 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
156 } else if (!c->modules || pa_idxset_ncontents(c->modules) == 0) {
157 fprintf(stderr, __FILE__": daemon startup without any loaded modules, refusing to work.\n");
158 if (cmdline->daemonize)
159 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
160 } else {
161 retval = 0;
162 if (cmdline->daemonize)
163 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
164 fprintf(stderr, __FILE__": mainloop entry.\n");
165 if (pa_mainloop_run(mainloop, &retval) < 0)
166 retval = 1;
167 fprintf(stderr, __FILE__": mainloop exit.\n");
168 }
169
170 pa_core_free(c);
171
172 pa_signal_done();
173 pa_mainloop_free(mainloop);
174
175 lt_dlexit();
176
177 finish:
178
179 if (cmdline)
180 pa_cmdline_free(cmdline);
181
182 close_pipe(daemon_pipe);
183
184 return retval;
185 }