]> code.delx.au - pulseaudio/blob - polyp/main.c
introduce pa_xmalloc() and friends
[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 #include "xmalloc.h"
46
47 static struct pa_mainloop *mainloop;
48
49 static void exit_signal_callback(void *id, int sig, void *userdata) {
50 struct pa_mainloop_api* m = pa_mainloop_get_api(mainloop);
51 m->quit(m, 1);
52 fprintf(stderr, __FILE__": got signal.\n");
53 }
54
55 static void aux_signal_callback(void *id, int sig, void *userdata) {
56 struct pa_core *c = userdata;
57 assert(c);
58 pa_module_load(c, sig == SIGUSR1 ? "module-cli" : "module-cli-protocol-unix", NULL);
59 }
60
61 static void close_pipe(int p[2]) {
62 if (p[0] != -1)
63 close(p[0]);
64 if (p[1] != -1)
65 close(p[1]);
66 p[0] = p[1] = -1;
67 }
68
69 int main(int argc, char *argv[]) {
70 struct pa_core *c;
71 struct pa_cmdline *cmdline = NULL;
72 struct pa_strbuf *buf = NULL;
73 char *s;
74 int r, retval = 1;
75 int daemon_pipe[2] = { -1, -1 };
76
77 if (!(cmdline = pa_cmdline_parse(argc, argv))) {
78 fprintf(stderr, __FILE__": failed to parse command line.\n");
79 goto finish;
80 }
81
82 if (cmdline->help) {
83 pa_cmdline_help(argv[0]);
84 retval = 0;
85 goto finish;
86 }
87
88 if (cmdline->daemonize) {
89 pid_t child;
90
91 if (pa_stdio_acquire() < 0) {
92 fprintf(stderr, __FILE__": failed to acquire stdio.\n");
93 goto finish;
94 }
95
96 if (pipe(daemon_pipe) < 0) {
97 fprintf(stderr, __FILE__": failed to create pipe.\n");
98 goto finish;
99 }
100
101 if ((child = fork()) < 0) {
102 fprintf(stderr, __FILE__": fork() failed: %s\n", strerror(errno));
103 goto finish;
104 }
105
106 if (child != 0) {
107 /* Father */
108
109 close(daemon_pipe[1]);
110 daemon_pipe[1] = -1;
111
112 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
113 fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
114 retval = 1;
115 }
116
117 goto finish;
118 }
119
120 close(daemon_pipe[0]);
121 daemon_pipe[0] = -1;
122
123 setsid();
124 setpgrp();
125 }
126
127 r = lt_dlinit();
128 assert(r == 0);
129 #ifdef DLSEARCHDIR
130 /* lt_dladdsearchdir(DLSEARCHDIR);*/
131 #endif
132
133 mainloop = pa_mainloop_new();
134 assert(mainloop);
135
136 r = pa_signal_init(pa_mainloop_get_api(mainloop));
137 assert(r == 0);
138 pa_signal_register(SIGINT, exit_signal_callback, NULL);
139 signal(SIGPIPE, SIG_IGN);
140
141 c = pa_core_new(pa_mainloop_get_api(mainloop));
142 assert(c);
143
144 pa_signal_register(SIGUSR1, aux_signal_callback, c);
145 pa_signal_register(SIGUSR2, aux_signal_callback, c);
146
147 buf = pa_strbuf_new();
148 assert(buf);
149 r = pa_cli_command_execute(c, cmdline->cli_commands, buf, &cmdline->fail, &cmdline->verbose);
150 fprintf(stderr, s = pa_strbuf_tostring_free(buf));
151 pa_xfree(s);
152
153 if (r < 0 && cmdline->fail) {
154 fprintf(stderr, __FILE__": failed to initialize daemon.\n");
155 if (cmdline->daemonize)
156 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
157 } else if (!c->modules || pa_idxset_ncontents(c->modules) == 0) {
158 fprintf(stderr, __FILE__": daemon startup without any loaded modules, refusing to work.\n");
159 if (cmdline->daemonize)
160 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
161 } else {
162 retval = 0;
163 if (cmdline->daemonize)
164 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
165 fprintf(stderr, __FILE__": mainloop entry.\n");
166 if (pa_mainloop_run(mainloop, &retval) < 0)
167 retval = 1;
168 fprintf(stderr, __FILE__": mainloop exit.\n");
169 }
170
171 pa_core_free(c);
172
173 pa_signal_done();
174 pa_mainloop_free(mainloop);
175
176 lt_dlexit();
177
178 finish:
179
180 if (cmdline)
181 pa_cmdline_free(cmdline);
182
183 close_pipe(daemon_pipe);
184
185 return retval;
186 }