1 | //roard.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | #ifdef ROAR_SUPPORT_LISTEN |
---|
28 | char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address |
---|
29 | #endif |
---|
30 | |
---|
31 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX) |
---|
32 | #define SUPPORT_PIDFILE |
---|
33 | char * pidfile = NULL; |
---|
34 | #endif |
---|
35 | |
---|
36 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
37 | void usage (void) { |
---|
38 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
39 | |
---|
40 | printf("Misc Options:\n\n"); |
---|
41 | printf( |
---|
42 | " --daemon - Bring the server into background after init\n" |
---|
43 | " --terminate - Terminate after last client quited\n" |
---|
44 | " --start - No op parameter (starting roard is default operation)\n" |
---|
45 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
46 | " --stop - Stops a running roard (provide --pidfile!)\n" |
---|
47 | " --shutdown - Terminates a running roard (provide --pidfile!)\n" |
---|
48 | " --realtime - Trys to get realtime priority,\n" |
---|
49 | " give multible times for being more realtime\n" |
---|
50 | " --chroot DIR - chroots to the given dir\n" |
---|
51 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
52 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
53 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
54 | " --location LOC - Set lion readable location of server\n" |
---|
55 | #ifdef SUPPORT_PIDFILE |
---|
56 | " --pidfile PIDFILE - Write a pidfile at PIDFILE\n" |
---|
57 | #endif |
---|
58 | ); |
---|
59 | |
---|
60 | printf("\nAudio Options:\n\n"); |
---|
61 | printf( |
---|
62 | " -R --rate RATE - Set server rate\n" |
---|
63 | " -B --bits BITS - Set server bits\n" |
---|
64 | " -C --chans CHANNELS - Set server channels\n" |
---|
65 | ); |
---|
66 | |
---|
67 | printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n"); |
---|
68 | printf(" -d --driver DRV - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
69 | printf(" -D --device DEV - Set the device\n"); |
---|
70 | printf(" -dO OPTS - Set output options\n"); |
---|
71 | printf(" --list-driver - List all drivers\n"); |
---|
72 | |
---|
73 | printf("\nOutput Options:\n\n"); |
---|
74 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
75 | printf(" -O --odevice DEV - Set the device\n"); |
---|
76 | printf(" -oO OPTS - Set output options\n"); |
---|
77 | printf(" -oN - Adds another output\n"); |
---|
78 | printf(" -oP - Mark output as primary\n"); |
---|
79 | |
---|
80 | printf("\nSource Options:\n\n"); |
---|
81 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
82 | " -S DEV - Use DEV as input device\n" |
---|
83 | " -sO OPTS - Use OPTS as input options\n" |
---|
84 | " -sN - Adds another source\n" |
---|
85 | " -sP - Make souce as primary\n" |
---|
86 | ); |
---|
87 | printf(" --list-sources - List all sources\n"); |
---|
88 | |
---|
89 | printf("\nCodec Filter Options:\n\n"); |
---|
90 | printf(" --list-cf - List all codec filter\n" |
---|
91 | ); |
---|
92 | |
---|
93 | printf("\nMIDI Options:\n\n"); |
---|
94 | printf(" --midi-no-console - Disable console based MIDI synth\n" |
---|
95 | " --midi-console DEV - Set device for MIDI console\n" |
---|
96 | ); |
---|
97 | |
---|
98 | printf("\nLight Control Options:\n\n"); |
---|
99 | printf(" --light-channels NUM - Sets the number of channels for Light control (default: %i)\n", |
---|
100 | LIGHT_CHANNELS_DEFAULT |
---|
101 | ); |
---|
102 | |
---|
103 | printf("\nServer Options:\n\n"); |
---|
104 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
105 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
106 | #ifdef ROAR_HAVE_LIBDNET |
---|
107 | " -n --decnet - use DECnet listen socket\n" |
---|
108 | #endif |
---|
109 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
110 | #ifdef PF_INET6 |
---|
111 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
112 | #endif |
---|
113 | #ifdef IPV6_ADDRFORM |
---|
114 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
115 | " this is normaly not usefull.\n" |
---|
116 | #endif |
---|
117 | " -p --port - TCP Port to bind to\n" |
---|
118 | " -b --bind - IP/Hostname to bind to\n" |
---|
119 | " --sock - Filename for UNIX Domain Socket\n" |
---|
120 | #ifdef ROAR_HAVE_LIBSLP |
---|
121 | " --slp - Enable OpenSLP support\n" |
---|
122 | #endif |
---|
123 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
124 | " You need the permittions to change the GID\n" |
---|
125 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
126 | " You need the permittions to change the UID (normaly only root has)\n" |
---|
127 | " --no-listen - Do not listen for new clients\n" |
---|
128 | " (only usefull for relaing, impleys --terminate)\n" |
---|
129 | " --client-fh - Comunicate with a client over this handle\n" |
---|
130 | " (only usefull for relaing)\n" |
---|
131 | " --close-fh - Closes the given fh\n" |
---|
132 | " --standby - Start in standby state\n" |
---|
133 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
134 | ROAR_DEFAULT_SOCKGRP |
---|
135 | ); |
---|
136 | // printf("\n Options:\n\n"); |
---|
137 | printf("\n"); |
---|
138 | } |
---|
139 | #endif |
---|
140 | |
---|
141 | int restart_server (char * server, int terminate) { |
---|
142 | struct roar_connection con; |
---|
143 | #ifdef ROAR_HAVE_KILL |
---|
144 | char buf[80]; |
---|
145 | ssize_t l; |
---|
146 | struct roar_vio_calls fh; |
---|
147 | pid_t pid; |
---|
148 | int ok; |
---|
149 | |
---|
150 | if ( pidfile != NULL ) { |
---|
151 | if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) { |
---|
152 | ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile); |
---|
153 | } else { |
---|
154 | l = roar_vio_read(&fh, buf, 80); |
---|
155 | roar_vio_close(&fh); |
---|
156 | if ( l > 0 ) { |
---|
157 | buf[l-1] = 0; |
---|
158 | buf[79] = 0; |
---|
159 | pid = atoi(buf); |
---|
160 | if ( terminate ) { |
---|
161 | ok = kill(pid, SIGUSR1); |
---|
162 | } else { |
---|
163 | ok = kill(pid, SIGINT); |
---|
164 | } |
---|
165 | if ( ok == 0 ) { |
---|
166 | return 0; |
---|
167 | } else { |
---|
168 | ROAR_WARN("restart_server(*): Can not kill roard by pidfile"); |
---|
169 | } |
---|
170 | } else { |
---|
171 | ROAR_WARN("restart_server(*): Can not find a PID in the pidfile"); |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | #endif |
---|
176 | |
---|
177 | if ( roar_connect(&con, server) == -1 ) { |
---|
178 | return -1; |
---|
179 | } |
---|
180 | |
---|
181 | if ( roar_terminate(&con, terminate) == -1 ) { |
---|
182 | return -1; |
---|
183 | } |
---|
184 | |
---|
185 | return roar_disconnect(&con); |
---|
186 | } |
---|
187 | |
---|
188 | #define R_SETUID 1 |
---|
189 | #define R_SETGID 2 |
---|
190 | |
---|
191 | int init_config (void) { |
---|
192 | int i; |
---|
193 | |
---|
194 | memset(g_config, 0, sizeof(struct roard_config)); |
---|
195 | |
---|
196 | for (i = 0; i < ROAR_DIR_DIRIDS; i++) { |
---|
197 | g_config->streams[i].mixer_channels = 1; |
---|
198 | g_config->streams[i].mixer.rpg_mul = 1; |
---|
199 | g_config->streams[i].mixer.rpg_div = 1; |
---|
200 | g_config->streams[i].mixer.scale = 65535; |
---|
201 | g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale; |
---|
202 | } |
---|
203 | |
---|
204 | g_config->streams[ROAR_DIR_FILTER ].flags = ROAR_FLAG_SYNC; |
---|
205 | g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC; |
---|
206 | g_config->streams[ROAR_DIR_BIDIR ].flags = ROAR_FLAG_ANTIECHO; |
---|
207 | |
---|
208 | g_config->location = "***default***"; |
---|
209 | |
---|
210 | return 0; |
---|
211 | } |
---|
212 | |
---|
213 | int add_output (char * drv, char * dev, char * opts, int prim, int count) { |
---|
214 | int stream; |
---|
215 | struct roar_stream * s; |
---|
216 | struct roar_stream_server * ss; |
---|
217 | char * k, * v; |
---|
218 | #ifdef ROAR_DRIVER_CODEC |
---|
219 | char * to_free = NULL; |
---|
220 | #endif |
---|
221 | int codec; |
---|
222 | int sync = 0, f_mmap = 0; |
---|
223 | int32_t blocks = -1, blocksize = -1; |
---|
224 | int dir = ROAR_DIR_OUTPUT; |
---|
225 | int error = 0; |
---|
226 | // DMX: |
---|
227 | int32_t channel = -1; |
---|
228 | int32_t universe = -1; |
---|
229 | uint16_t tu16; |
---|
230 | float q = -32e6; |
---|
231 | |
---|
232 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
233 | |
---|
234 | if ( drv == NULL && count == 0 ) { |
---|
235 | drv = ROAR_DRIVER_DEFAULT; |
---|
236 | prim = 1; |
---|
237 | sync = 1; |
---|
238 | |
---|
239 | #ifdef ROAR_DRIVER_CODEC |
---|
240 | if ( opts == NULL ) { |
---|
241 | opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC); |
---|
242 | } |
---|
243 | #endif |
---|
244 | } |
---|
245 | |
---|
246 | if ( opts == NULL && count == 0 ) { |
---|
247 | sync = 1; |
---|
248 | prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1 |
---|
249 | } |
---|
250 | |
---|
251 | if ( (stream = streams_new()) == -1 ) { |
---|
252 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
253 | if ( prim ) alive = 0; |
---|
254 | return -1; |
---|
255 | } |
---|
256 | |
---|
257 | streams_get(stream, &ss); |
---|
258 | s = ROAR_STREAM(ss); |
---|
259 | |
---|
260 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
261 | |
---|
262 | s->pos_rel_id = -1; |
---|
263 | // s->info.codec = codec; |
---|
264 | |
---|
265 | codec = s->info.codec; |
---|
266 | |
---|
267 | k = strtok(opts, ","); |
---|
268 | while (k != NULL) { |
---|
269 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
270 | |
---|
271 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
272 | *v++ = 0; |
---|
273 | } |
---|
274 | |
---|
275 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
276 | if ( strcmp(k, "rate") == 0 ) { |
---|
277 | s->info.rate = atoi(v); |
---|
278 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
279 | s->info.channels = atoi(v); |
---|
280 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
281 | s->info.bits = atoi(v); |
---|
282 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
283 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
284 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
285 | error++; |
---|
286 | } |
---|
287 | } else if ( strcmp(k, "q") == 0 ) { |
---|
288 | q = atof(v); |
---|
289 | } else if ( strcmp(k, "blocks") == 0 ) { |
---|
290 | blocks = atoi(v); |
---|
291 | } else if ( strcmp(k, "blocksize") == 0 ) { |
---|
292 | blocksize = atoi(v); |
---|
293 | } else if ( strcmp(k, "mmap") == 0 ) { |
---|
294 | f_mmap = 1; |
---|
295 | } else if ( strcmp(k, "subsystem") == 0 ) { |
---|
296 | if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) { |
---|
297 | dir = ROAR_DIR_OUTPUT; |
---|
298 | } else if ( !strcasecmp(v, "midi") ) { |
---|
299 | dir = ROAR_DIR_MIDI_OUT; |
---|
300 | } else if ( !strcasecmp(v, "light") ) { |
---|
301 | dir = ROAR_DIR_LIGHT_OUT; |
---|
302 | } else if ( !strcasecmp(v, "raw") ) { |
---|
303 | dir = ROAR_DIR_RAW_OUT; |
---|
304 | } else { |
---|
305 | ROAR_ERR("add_output(*): unknown subsystem '%s'", k); |
---|
306 | error++; |
---|
307 | } |
---|
308 | // DMX: |
---|
309 | } else if ( strcmp(k, "channel") == 0 ) { |
---|
310 | channel = atoi(v); |
---|
311 | if ( channel < 0 || channel > 65535 ) { |
---|
312 | ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel); |
---|
313 | channel = -1; |
---|
314 | error++; |
---|
315 | } |
---|
316 | } else if ( strcmp(k, "universe") == 0 ) { |
---|
317 | universe = atoi(v); |
---|
318 | if ( universe < 0 || universe > 255 ) { |
---|
319 | ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe); |
---|
320 | universe = -1; |
---|
321 | error++; |
---|
322 | } |
---|
323 | |
---|
324 | } else if ( strcmp(k, "name") == 0 ) { |
---|
325 | if ( streams_set_name(stream, v) == -1 ) { |
---|
326 | ROAR_ERR("add_output(*): Can not set Stream name"); |
---|
327 | error++; |
---|
328 | } |
---|
329 | |
---|
330 | } else if ( strcmp(k, "meta") == 0 ) { |
---|
331 | streams_set_flag(stream, ROAR_FLAG_META); |
---|
332 | } else if ( strcmp(k, "sync") == 0 ) { |
---|
333 | sync = 1; |
---|
334 | } else if ( strcmp(k, "primary") == 0 ) { |
---|
335 | prim = 1; |
---|
336 | |
---|
337 | } else if ( strcmp(k, "cleanmeta") == 0 ) { |
---|
338 | streams_set_flag(stream, ROAR_FLAG_CLEANMETA); |
---|
339 | } else if ( strcmp(k, "autoconf") == 0 ) { |
---|
340 | streams_set_flag(stream, ROAR_FLAG_AUTOCONF); |
---|
341 | } else { |
---|
342 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
343 | error++; |
---|
344 | } |
---|
345 | |
---|
346 | if ( error ) { |
---|
347 | streams_delete(stream); |
---|
348 | if ( prim ) alive = 0; |
---|
349 | #ifdef ROAR_DRIVER_CODEC |
---|
350 | if ( to_free != NULL ) |
---|
351 | free(to_free); |
---|
352 | #endif |
---|
353 | return -1; |
---|
354 | } |
---|
355 | |
---|
356 | k = strtok(NULL, ","); |
---|
357 | } |
---|
358 | |
---|
359 | if ( streams_set_dir(stream, dir, 1) == -1 ) { |
---|
360 | streams_delete(stream); |
---|
361 | return -1; |
---|
362 | } |
---|
363 | |
---|
364 | #ifdef ROAR_DRIVER_CODEC |
---|
365 | if ( to_free != NULL ) |
---|
366 | free(to_free); |
---|
367 | #endif |
---|
368 | |
---|
369 | if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW ) |
---|
370 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
371 | |
---|
372 | s->info.codec = codec; |
---|
373 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
374 | |
---|
375 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) { |
---|
376 | ss->driver_id = -1; // don't close a driver not opened... |
---|
377 | memset(&(ss->vio), 0, sizeof(struct roar_vio_calls)); |
---|
378 | streams_delete(stream); |
---|
379 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
380 | if ( prim ) alive = 0; |
---|
381 | return -1; |
---|
382 | } |
---|
383 | |
---|
384 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here |
---|
385 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM, s); // ignore errors here |
---|
386 | |
---|
387 | if ( blocks != -1 ) |
---|
388 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks); |
---|
389 | |
---|
390 | if ( blocksize != -1 ) |
---|
391 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize); |
---|
392 | |
---|
393 | // TODO: we shoudld *really* check for errors here... |
---|
394 | if ( channel != -1 ) { |
---|
395 | tu16 = channel; |
---|
396 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16); |
---|
397 | } |
---|
398 | if ( universe != -1 ) { |
---|
399 | tu16 = universe; |
---|
400 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16); |
---|
401 | } |
---|
402 | |
---|
403 | ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id); |
---|
404 | |
---|
405 | streams_set_fh(stream, -1); // update some internal structures |
---|
406 | |
---|
407 | if ( q > -1e6 ) { |
---|
408 | ROAR_DBG("add_output(*): setting q=%f", q); |
---|
409 | streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q); |
---|
410 | } |
---|
411 | |
---|
412 | client_stream_add(g_source_client, stream); |
---|
413 | |
---|
414 | if ( prim ) { |
---|
415 | streams_mark_primary(stream); |
---|
416 | s->pos_rel_id = stream; |
---|
417 | } |
---|
418 | |
---|
419 | if ( sync ) { |
---|
420 | streams_set_flag(stream, ROAR_FLAG_SYNC); |
---|
421 | } else { |
---|
422 | streams_reset_flag(stream, ROAR_FLAG_SYNC); |
---|
423 | } |
---|
424 | |
---|
425 | if ( f_mmap ) |
---|
426 | streams_set_flag(stream, ROAR_FLAG_MMAP); |
---|
427 | |
---|
428 | return 0; |
---|
429 | } |
---|
430 | |
---|
431 | |
---|
432 | // SLP: |
---|
433 | void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) { |
---|
434 | /* return the error code in the cookie */ |
---|
435 | *(SLPError*)cookie = errcode; |
---|
436 | } |
---|
437 | |
---|
438 | int register_slp (int unreg, char * sockname) { |
---|
439 | #ifdef ROAR_HAVE_LIBSLP |
---|
440 | static int regged = 0; |
---|
441 | static char * sn = NULL; |
---|
442 | SLPError err; |
---|
443 | SLPError callbackerr; |
---|
444 | SLPHandle hslp; |
---|
445 | char addr[1024]; |
---|
446 | char attr[1024] = ""; |
---|
447 | char * location; |
---|
448 | |
---|
449 | if ( sockname != NULL ) |
---|
450 | sn = sockname; |
---|
451 | |
---|
452 | snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn); |
---|
453 | |
---|
454 | err = SLPOpen("en", SLP_FALSE, &hslp); |
---|
455 | |
---|
456 | if (err != SLP_OK) { |
---|
457 | ROAR_ERR("Error opening slp handle: Error #%i", err); |
---|
458 | return -1; |
---|
459 | } |
---|
460 | |
---|
461 | if (!unreg) { |
---|
462 | |
---|
463 | if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) { |
---|
464 | ROAR_ERR("Error using SLPEscape() on server location, really bad!"); |
---|
465 | SLPClose(hslp); |
---|
466 | return -1; |
---|
467 | } |
---|
468 | |
---|
469 | snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i)," |
---|
470 | "(light-channels=%i),(location=%s)", |
---|
471 | g_sa->rate, g_sa->channels, g_sa->bits, |
---|
472 | g_light_state.channels, |
---|
473 | location |
---|
474 | ); |
---|
475 | |
---|
476 | /* Register a service with SLP */ |
---|
477 | err = SLPReg(hslp, |
---|
478 | addr, |
---|
479 | SLP_LIFETIME_MAXIMUM, |
---|
480 | 0, |
---|
481 | attr, |
---|
482 | SLP_TRUE, |
---|
483 | register_slp_callback, |
---|
484 | &callbackerr); |
---|
485 | regged = 1; |
---|
486 | } else if ( unreg && regged ) { |
---|
487 | err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr); |
---|
488 | regged = 0; |
---|
489 | } else { |
---|
490 | SLPClose(hslp); |
---|
491 | return -1; |
---|
492 | } |
---|
493 | |
---|
494 | /* err may contain an error code that occurred as the slp library */ |
---|
495 | /* _prepared_ to make the call. */ |
---|
496 | if ( err != SLP_OK ) { |
---|
497 | ROAR_ERR("Error (de)registering service with slp: Error #%i", err); |
---|
498 | return -1; |
---|
499 | } |
---|
500 | |
---|
501 | /* callbackerr may contain an error code (that was assigned through */ |
---|
502 | /* the callback cookie) that occurred as slp packets were sent on */ |
---|
503 | /* the wire */ |
---|
504 | if (callbackerr != SLP_OK) { |
---|
505 | ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr); |
---|
506 | return -1; |
---|
507 | } |
---|
508 | |
---|
509 | SLPClose(hslp); |
---|
510 | return 0; |
---|
511 | #else |
---|
512 | return -1; |
---|
513 | #endif |
---|
514 | } |
---|
515 | |
---|
516 | |
---|
517 | // MAIN: |
---|
518 | |
---|
519 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
520 | int main (int argc, char * argv[]) { |
---|
521 | #else |
---|
522 | int main (void) { |
---|
523 | #endif |
---|
524 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
525 | int i; |
---|
526 | char * k; |
---|
527 | #endif |
---|
528 | #ifdef ROAR_SUPPORT_LISTEN |
---|
529 | char user_sock[80] = {0}; |
---|
530 | #endif |
---|
531 | struct roar_audio_info sa, max_sa; |
---|
532 | struct roard_config config; |
---|
533 | #ifdef ROAR_HAVE_FORK |
---|
534 | int daemon = 0; |
---|
535 | #endif |
---|
536 | int realtime = 0; |
---|
537 | int sysclocksync = 0; |
---|
538 | char * driver = NULL; |
---|
539 | char * device = NULL; |
---|
540 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
541 | char * opts = NULL; |
---|
542 | #endif |
---|
543 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
544 | #ifdef ROAR_SUPPORT_LISTEN |
---|
545 | int port = ROAR_DEFAULT_PORT; |
---|
546 | #endif |
---|
547 | int drvid; |
---|
548 | char * s_drv = "cf"; |
---|
549 | char * s_dev = NULL; |
---|
550 | char * s_con = NULL; |
---|
551 | char * s_opt = NULL; |
---|
552 | int s_prim = 0; |
---|
553 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
554 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
555 | char * o_opts = NULL; |
---|
556 | int o_prim = 0; |
---|
557 | int o_count = 0; |
---|
558 | int light_channels = LIGHT_CHANNELS_DEFAULT; |
---|
559 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
560 | char * sock_user = NULL; |
---|
561 | #ifdef ROAR_SUPPORT_LISTEN |
---|
562 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
563 | #endif |
---|
564 | #ifdef ROAR_HAVE_LIBSLP |
---|
565 | int reg_slp = 0; |
---|
566 | #endif |
---|
567 | #ifdef ROAR_HAVE_CHROOT |
---|
568 | char * chrootdir = NULL; |
---|
569 | #endif |
---|
570 | #if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID) |
---|
571 | int setids = 0; |
---|
572 | #endif |
---|
573 | #ifdef ROAR_HAVE_UNIX |
---|
574 | char * env_roar_proxy_backup; |
---|
575 | #endif |
---|
576 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
577 | struct group * grp = NULL; |
---|
578 | #endif |
---|
579 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
580 | struct passwd * pwd = NULL; |
---|
581 | #endif |
---|
582 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
583 | struct servent * serv = NULL; |
---|
584 | #endif |
---|
585 | DRIVER_USERDATA_T drvinst; |
---|
586 | struct roar_client * self = NULL; |
---|
587 | #ifdef ROAR_HAVE_LIBDNET |
---|
588 | char decnethost[80]; |
---|
589 | #endif |
---|
590 | #ifdef SUPPORT_PIDFILE |
---|
591 | struct roar_vio_calls pidfile_vio; |
---|
592 | #endif |
---|
593 | |
---|
594 | g_standby = 0; |
---|
595 | g_autostandby = 0; |
---|
596 | alive = 1; |
---|
597 | #ifdef ROAR_SUPPORT_LISTEN |
---|
598 | g_no_listen = 0; |
---|
599 | g_listen_socket = -1; |
---|
600 | #else |
---|
601 | g_terminate = 1; |
---|
602 | #endif |
---|
603 | |
---|
604 | sa.bits = ROAR_BITS_DEFAULT; |
---|
605 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
606 | sa.rate = ROAR_RATE_DEFAULT; |
---|
607 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
608 | |
---|
609 | g_sa = &sa; |
---|
610 | g_max_sa = &max_sa; |
---|
611 | |
---|
612 | memcpy(g_max_sa, g_sa, sizeof(max_sa)); |
---|
613 | |
---|
614 | g_config = &config; |
---|
615 | |
---|
616 | if ( init_config() == -1 ) { |
---|
617 | ROAR_ERR("Can not init default config!"); |
---|
618 | return 1; |
---|
619 | } |
---|
620 | |
---|
621 | if ( midi_init_config() == -1 ) { |
---|
622 | ROAR_ERR("Can not init MIDI config!"); |
---|
623 | return 1; |
---|
624 | } |
---|
625 | |
---|
626 | #ifdef ROAR_SUPPORT_LISTEN |
---|
627 | #ifdef ROAR_HAVE_GETUID |
---|
628 | if ( getuid() != 0 && getenv("HOME") != NULL ) { |
---|
629 | snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
630 | server = user_sock; |
---|
631 | } |
---|
632 | #endif |
---|
633 | |
---|
634 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
635 | server = getenv("ROAR_SERVER"); |
---|
636 | #endif |
---|
637 | |
---|
638 | if ( clients_init() == -1 ) { |
---|
639 | ROAR_ERR("Can not init clients!"); |
---|
640 | return 1; |
---|
641 | } |
---|
642 | |
---|
643 | if ( streams_init() == -1 ) { |
---|
644 | ROAR_ERR("Can not init streams!"); |
---|
645 | return 1; |
---|
646 | } |
---|
647 | |
---|
648 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
649 | ROAR_ERR("Can not create self client!"); |
---|
650 | return 1; |
---|
651 | } |
---|
652 | |
---|
653 | if ( sources_init() == -1 ) { |
---|
654 | ROAR_ERR("Can not init sources!"); |
---|
655 | return 1; |
---|
656 | } |
---|
657 | |
---|
658 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
659 | ROAR_ERR("Can not init set source client!"); |
---|
660 | return 1; |
---|
661 | } |
---|
662 | |
---|
663 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
664 | for (i = 1; i < argc; i++) { |
---|
665 | k = argv[i]; |
---|
666 | |
---|
667 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
668 | usage(); |
---|
669 | return 0; |
---|
670 | |
---|
671 | } else if ( strcmp(k, "--start") == 0 ) { |
---|
672 | // this is a no op |
---|
673 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
674 | #ifdef ROAR_SUPPORT_LISTEN |
---|
675 | if ( restart_server(server, 1) == -1 ) { |
---|
676 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
677 | } |
---|
678 | #else |
---|
679 | ROAR_ERR("--restart not supported"); |
---|
680 | #endif |
---|
681 | } else if ( strcmp(k, "--shutdown") == 0 ) { |
---|
682 | #ifdef ROAR_SUPPORT_LISTEN |
---|
683 | if ( restart_server(server, 1) == -1 ) { |
---|
684 | ROAR_WARN("Can not terminate old server (not running at %s?)", server); |
---|
685 | return 1; |
---|
686 | } |
---|
687 | return 0; |
---|
688 | #else |
---|
689 | ROAR_ERR("--shutdown not supported"); |
---|
690 | return 1; |
---|
691 | #endif |
---|
692 | } else if ( strcmp(k, "--stop") == 0 ) { |
---|
693 | #ifdef ROAR_SUPPORT_LISTEN |
---|
694 | if ( restart_server(server, 0) == -1 ) { |
---|
695 | ROAR_WARN("Can not stop old server (not running at %s?)", server); |
---|
696 | return 1; |
---|
697 | } |
---|
698 | return 0; |
---|
699 | #else |
---|
700 | ROAR_ERR("--stop not supported"); |
---|
701 | return 1; |
---|
702 | #endif |
---|
703 | |
---|
704 | |
---|
705 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
706 | #ifdef ROAR_HAVE_FORK |
---|
707 | daemon = 1; |
---|
708 | #else |
---|
709 | ROAR_ERR("--daemon not supported"); |
---|
710 | #endif |
---|
711 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
712 | g_terminate = 1; |
---|
713 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
714 | sysclocksync = 1000; |
---|
715 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
716 | realtime++; |
---|
717 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
718 | #ifdef ROAR_HAVE_CHROOT |
---|
719 | chrootdir = argv[++i]; |
---|
720 | #else |
---|
721 | ROAR_ERR("--chroot not supported"); |
---|
722 | i++; |
---|
723 | #endif |
---|
724 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
725 | #ifdef ROAR_HAVE_SETGID |
---|
726 | setids |= R_SETGID; |
---|
727 | #else |
---|
728 | ROAR_ERR("--setgid not supported"); |
---|
729 | #endif |
---|
730 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
731 | #ifdef ROAR_HAVE_SETUID |
---|
732 | setids |= R_SETUID; |
---|
733 | #else |
---|
734 | ROAR_ERR("--setuid not supported"); |
---|
735 | #endif |
---|
736 | } else if ( strcmp(k, "--location") == 0 ) { |
---|
737 | g_config->location = argv[++i]; |
---|
738 | } else if ( strcmp(k, "--pidfile") == 0 ) { |
---|
739 | #ifdef SUPPORT_PIDFILE |
---|
740 | pidfile = argv[++i]; |
---|
741 | #else |
---|
742 | ROAR_ERR("--pidfile not supported"); |
---|
743 | i++; |
---|
744 | #endif |
---|
745 | |
---|
746 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
747 | print_codecfilterlist(); |
---|
748 | return 0; |
---|
749 | |
---|
750 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
751 | sa.rate = atoi(argv[++i]); |
---|
752 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
753 | sa.bits = atoi(argv[++i]); |
---|
754 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
755 | sa.channels = atoi(argv[++i]); |
---|
756 | |
---|
757 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
758 | driver = argv[++i]; |
---|
759 | if ( strcmp(driver, "list") == 0 ) { |
---|
760 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
761 | print_driverlist(); |
---|
762 | return 0; |
---|
763 | } |
---|
764 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
765 | device = argv[++i]; |
---|
766 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
767 | opts = argv[++i]; |
---|
768 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
769 | print_driverlist(); |
---|
770 | return 0; |
---|
771 | |
---|
772 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
773 | o_drv = argv[++i]; |
---|
774 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
775 | o_dev = argv[++i]; |
---|
776 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
777 | o_opts = argv[++i]; |
---|
778 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
779 | o_prim = 1; |
---|
780 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
781 | if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
782 | o_count++; |
---|
783 | |
---|
784 | o_drv = o_dev = o_opts = NULL; |
---|
785 | o_prim = 0; |
---|
786 | |
---|
787 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
788 | s_drv = argv[++i]; |
---|
789 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
790 | s_dev = argv[++i]; |
---|
791 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
792 | s_opt = argv[++i]; |
---|
793 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
794 | s_con = argv[++i]; |
---|
795 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
796 | s_prim = 1; |
---|
797 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
798 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
799 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
800 | } |
---|
801 | s_opt = s_dev = s_con = NULL; |
---|
802 | s_drv = "cf"; |
---|
803 | s_prim = 0; |
---|
804 | } else if ( strcmp(k, "--list-sources") == 0 ) { |
---|
805 | print_sourcelist(); |
---|
806 | return 0; |
---|
807 | |
---|
808 | } else if ( strcmp(k, "--light-channels") == 0 ) { |
---|
809 | light_channels = atoi(argv[++i]); |
---|
810 | |
---|
811 | } else if ( strcmp(k, "--midi-no-console") == 0 ) { |
---|
812 | midi_config.init_cb = 0; |
---|
813 | } else if ( strcmp(k, "--midi-console") == 0 ) { |
---|
814 | midi_config.console_dev = argv[++i]; |
---|
815 | |
---|
816 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
817 | // This is only usefull in INET not UNIX mode. |
---|
818 | #ifdef ROAR_SUPPORT_LISTEN |
---|
819 | if ( *server == '/' ) |
---|
820 | server = ROAR_DEFAULT_HOST; |
---|
821 | |
---|
822 | errno = 0; |
---|
823 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
824 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
825 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
826 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
827 | return 1; |
---|
828 | } |
---|
829 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
830 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
831 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
832 | port = ROAR_NET2HOST16(serv->s_port); |
---|
833 | #else |
---|
834 | ROAR_ERR("invalite port number: %s", argv[i]); |
---|
835 | return 1; |
---|
836 | #endif |
---|
837 | } |
---|
838 | #endif |
---|
839 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
840 | #ifdef ROAR_SUPPORT_LISTEN |
---|
841 | server = argv[++i]; |
---|
842 | #endif |
---|
843 | |
---|
844 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
845 | #ifdef ROAR_SUPPORT_LISTEN |
---|
846 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
847 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
848 | |
---|
849 | if ( *server == '/' ) |
---|
850 | server = ROAR_DEFAULT_HOST; |
---|
851 | #endif |
---|
852 | |
---|
853 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
854 | #ifdef ROAR_SUPPORT_LISTEN |
---|
855 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
856 | if ( *server == '/' ) |
---|
857 | server = ROAR_DEFAULT_HOST; |
---|
858 | #endif |
---|
859 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
860 | #ifdef ROAR_SUPPORT_LISTEN |
---|
861 | #ifdef PF_INET6 |
---|
862 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
863 | if ( *server == '/' ) |
---|
864 | server = ROAR_DEFAULT_HOST; |
---|
865 | #else |
---|
866 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
867 | return 1; |
---|
868 | #endif |
---|
869 | #endif |
---|
870 | |
---|
871 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
872 | #ifdef ROAR_SUPPORT_LISTEN |
---|
873 | // ignore this case as it is the default behavor. |
---|
874 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
875 | #endif |
---|
876 | |
---|
877 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
878 | #ifdef ROAR_SUPPORT_LISTEN |
---|
879 | #ifdef ROAR_HAVE_LIBDNET |
---|
880 | port = ROAR_DEFAULT_NUM; |
---|
881 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
882 | server = decnethost; |
---|
883 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
884 | #else |
---|
885 | ROAR_ERR("No DECnet support compiled in!"); |
---|
886 | return 1; |
---|
887 | #endif |
---|
888 | #endif |
---|
889 | |
---|
890 | } else if ( strcmp(k, "--slp") == 0 ) { |
---|
891 | #ifdef ROAR_HAVE_LIBSLP |
---|
892 | reg_slp = 1; |
---|
893 | #else |
---|
894 | ROAR_ERR("No OpenSLP support compiled in!"); |
---|
895 | return 1; |
---|
896 | #endif |
---|
897 | |
---|
898 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
899 | sock_grp = argv[++i]; |
---|
900 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
901 | sock_user = argv[++i]; |
---|
902 | |
---|
903 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
904 | #ifdef ROAR_SUPPORT_LISTEN |
---|
905 | server = ""; |
---|
906 | g_terminate = 1; |
---|
907 | g_no_listen = 1; |
---|
908 | #endif |
---|
909 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
910 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
911 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
912 | return 1; |
---|
913 | } |
---|
914 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
915 | #ifdef ROAR_HAVE_IO_POSIX |
---|
916 | close(atoi(argv[++i])); |
---|
917 | #else |
---|
918 | i++; |
---|
919 | ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]); |
---|
920 | #endif |
---|
921 | |
---|
922 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
923 | g_standby = 1; |
---|
924 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
925 | g_autostandby = 1; |
---|
926 | } else { |
---|
927 | usage(); |
---|
928 | return 1; |
---|
929 | } |
---|
930 | |
---|
931 | } |
---|
932 | #endif |
---|
933 | |
---|
934 | if ( s_dev != NULL ) { |
---|
935 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
936 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
937 | } |
---|
938 | } |
---|
939 | |
---|
940 | add_output(o_drv, o_dev, o_opts, o_prim, o_count); |
---|
941 | |
---|
942 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
943 | |
---|
944 | if ( midi_init() == -1 ) { |
---|
945 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
946 | } |
---|
947 | |
---|
948 | if ( light_init(light_channels) == -1 ) { |
---|
949 | ROAR_ERR("Can not initialize light control subsystem"); |
---|
950 | } |
---|
951 | |
---|
952 | #ifdef ROAR_SUPPORT_LISTEN |
---|
953 | if ( *server != 0 ) { |
---|
954 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
955 | #ifdef ROAR_HAVE_UNIX |
---|
956 | if ( *server == '/' ) { |
---|
957 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
958 | env_roar_proxy_backup = strdup(env_roar_proxy_backup); |
---|
959 | unsetenv("ROAR_PROXY"); |
---|
960 | } |
---|
961 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
962 | close(i); |
---|
963 | ROAR_ERR("Can not open listen socket!"); |
---|
964 | return 1; |
---|
965 | } else { |
---|
966 | unlink(server); |
---|
967 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
968 | ROAR_ERR("Can not open listen socket!"); |
---|
969 | return 1; |
---|
970 | } |
---|
971 | } |
---|
972 | if ( env_roar_proxy_backup != NULL ) { |
---|
973 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
974 | free(env_roar_proxy_backup); |
---|
975 | } |
---|
976 | #else |
---|
977 | if (0) { // noop |
---|
978 | #endif |
---|
979 | } else { |
---|
980 | ROAR_ERR("Can not open listen socket!"); |
---|
981 | return 1; |
---|
982 | } |
---|
983 | } |
---|
984 | |
---|
985 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
986 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
987 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
988 | } |
---|
989 | #endif |
---|
990 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
991 | if ( sock_user || (setids & R_SETUID) ) { |
---|
992 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
993 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
994 | } |
---|
995 | } |
---|
996 | #endif |
---|
997 | |
---|
998 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX) |
---|
999 | if ( *server == '/' ) { |
---|
1000 | if ( grp ) { |
---|
1001 | if ( pwd ) { |
---|
1002 | if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 ) |
---|
1003 | return 1; |
---|
1004 | } else { |
---|
1005 | if ( chown(server, -1, grp->gr_gid) == -1 ) |
---|
1006 | return 1; |
---|
1007 | } |
---|
1008 | #ifdef ROAR_HAVE_GETUID |
---|
1009 | if ( getuid() == 0 ) |
---|
1010 | if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 ) |
---|
1011 | return 1; |
---|
1012 | #endif |
---|
1013 | } |
---|
1014 | } |
---|
1015 | #endif |
---|
1016 | } |
---|
1017 | #endif |
---|
1018 | |
---|
1019 | if ( output_buffer_init(&sa) == -1 ) { |
---|
1020 | ROAR_ERR("Can not init output buffer!"); |
---|
1021 | return 1; |
---|
1022 | } |
---|
1023 | |
---|
1024 | if ( driver == NULL ) { |
---|
1025 | driver = "null"; |
---|
1026 | } else { |
---|
1027 | ROAR_WARN("Usage of old driver interface. use -o not -d!"); |
---|
1028 | } |
---|
1029 | |
---|
1030 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
1031 | ROAR_ERR("Can not open output driver!"); |
---|
1032 | return 1; |
---|
1033 | } |
---|
1034 | |
---|
1035 | if ( samples_init() == -1 ) { |
---|
1036 | ROAR_ERR("Can not init samples!"); |
---|
1037 | return 1; |
---|
1038 | } |
---|
1039 | |
---|
1040 | |
---|
1041 | // we should handle this on microcontrollers, too. |
---|
1042 | #if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32) |
---|
1043 | signal(SIGINT, on_sig_int); |
---|
1044 | signal(SIGCHLD, on_sig_chld); |
---|
1045 | signal(SIGUSR1, on_sig_usr1); |
---|
1046 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
1047 | #endif |
---|
1048 | |
---|
1049 | if ( realtime ) { |
---|
1050 | #ifdef DEBUG |
---|
1051 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
1052 | #endif |
---|
1053 | |
---|
1054 | #ifdef ROAR_HAVE_NICE |
---|
1055 | errno = 0; |
---|
1056 | nice(-5*realtime); // -5 for each --realtime |
---|
1057 | if ( errno ) { |
---|
1058 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
1059 | } |
---|
1060 | #else |
---|
1061 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
1062 | #endif |
---|
1063 | /* |
---|
1064 | #ifdef __linux__ |
---|
1065 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
1066 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
1067 | #endif |
---|
1068 | */ |
---|
1069 | } |
---|
1070 | |
---|
1071 | #ifdef ROAR_HAVE_SETGID |
---|
1072 | if ( setids & R_SETGID ) { |
---|
1073 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
1074 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
1075 | } |
---|
1076 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
1077 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
1078 | } |
---|
1079 | } |
---|
1080 | #endif |
---|
1081 | |
---|
1082 | |
---|
1083 | clients_set_pid(g_self_client, getpid()); |
---|
1084 | #ifdef ROAR_HAVE_GETUID |
---|
1085 | clients_set_uid(g_self_client, getuid()); |
---|
1086 | #endif |
---|
1087 | #ifdef ROAR_HAVE_GETGID |
---|
1088 | clients_set_gid(g_self_client, getgid()); |
---|
1089 | #endif |
---|
1090 | clients_get(g_self_client, &self); |
---|
1091 | |
---|
1092 | if ( self == NULL ) { |
---|
1093 | ROAR_ERR("Can not get self client!"); |
---|
1094 | return 1; |
---|
1095 | } |
---|
1096 | |
---|
1097 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
1098 | |
---|
1099 | #ifdef ROAR_HAVE_FORK |
---|
1100 | if ( daemon ) { |
---|
1101 | close(ROAR_STDIN ); |
---|
1102 | close(ROAR_STDOUT); |
---|
1103 | close(ROAR_STDERR); |
---|
1104 | |
---|
1105 | if ( fork() ) |
---|
1106 | ROAR_U_EXIT(0); |
---|
1107 | |
---|
1108 | #ifdef ROAR_HAVE_SETSID |
---|
1109 | setsid(); |
---|
1110 | #endif |
---|
1111 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
1112 | } |
---|
1113 | #endif |
---|
1114 | |
---|
1115 | #ifdef SUPPORT_PIDFILE |
---|
1116 | if ( pidfile != NULL ) { |
---|
1117 | if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) { |
---|
1118 | ROAR_ERR("Can not write pidfile: %s", pidfile); |
---|
1119 | } else { |
---|
1120 | roar_vio_printf(&pidfile_vio, "%i\n", getpid()); |
---|
1121 | roar_vio_close(&pidfile_vio); |
---|
1122 | } |
---|
1123 | } |
---|
1124 | #endif |
---|
1125 | |
---|
1126 | #ifdef ROAR_HAVE_CHROOT |
---|
1127 | if (chrootdir) { |
---|
1128 | if ( chroot(chrootdir) == -1 ) { |
---|
1129 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
1130 | return 2; |
---|
1131 | } |
---|
1132 | if ( chdir("/") == -1 ) { |
---|
1133 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
1134 | return 2; |
---|
1135 | } |
---|
1136 | } |
---|
1137 | #endif |
---|
1138 | |
---|
1139 | #ifdef ROAR_HAVE_SETUID |
---|
1140 | if ( setids & R_SETUID ) { |
---|
1141 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
1142 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
1143 | return 3; |
---|
1144 | } |
---|
1145 | #ifdef ROAR_HAVE_GETUID |
---|
1146 | clients_set_uid(g_self_client, getuid()); |
---|
1147 | #endif |
---|
1148 | } |
---|
1149 | #endif |
---|
1150 | |
---|
1151 | // Register with OpenSLP: |
---|
1152 | #ifdef ROAR_HAVE_LIBSLP |
---|
1153 | if ( reg_slp ) { |
---|
1154 | register_slp(0, server); |
---|
1155 | } |
---|
1156 | #endif |
---|
1157 | |
---|
1158 | // start main loop... |
---|
1159 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
1160 | |
---|
1161 | // clean up. |
---|
1162 | clean_quit_prep(); |
---|
1163 | driver_close(drvinst, drvid); |
---|
1164 | output_buffer_free(); |
---|
1165 | |
---|
1166 | return 0; |
---|
1167 | } |
---|
1168 | |
---|
1169 | void cleanup_listen_socket (int terminate) { |
---|
1170 | // Deregister from SLP: |
---|
1171 | #ifdef ROAR_HAVE_LIBSLP |
---|
1172 | register_slp(1, NULL); |
---|
1173 | #endif |
---|
1174 | |
---|
1175 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1176 | if ( g_listen_socket != -1 ) { |
---|
1177 | #ifdef ROAR_HAVE_IO_POSIX |
---|
1178 | close(g_listen_socket); |
---|
1179 | #endif // #else is useless because we are in void context. |
---|
1180 | |
---|
1181 | g_listen_socket = -1; |
---|
1182 | |
---|
1183 | #ifdef ROAR_HAVE_UNIX |
---|
1184 | if ( *server == '/' ) |
---|
1185 | unlink(server); |
---|
1186 | #endif |
---|
1187 | } |
---|
1188 | |
---|
1189 | #endif |
---|
1190 | |
---|
1191 | if ( terminate ) |
---|
1192 | g_terminate = 1; |
---|
1193 | } |
---|
1194 | |
---|
1195 | void clean_quit_prep (void) { |
---|
1196 | cleanup_listen_socket(0); |
---|
1197 | |
---|
1198 | sources_free(); |
---|
1199 | streams_free(); |
---|
1200 | clients_free(); |
---|
1201 | midi_cb_stop(); // stop console beep |
---|
1202 | midi_free(); |
---|
1203 | light_free(); |
---|
1204 | |
---|
1205 | #ifdef SUPPORT_PIDFILE |
---|
1206 | if ( pidfile != NULL ) |
---|
1207 | unlink(pidfile); |
---|
1208 | #endif |
---|
1209 | } |
---|
1210 | |
---|
1211 | void clean_quit (void) { |
---|
1212 | clean_quit_prep(); |
---|
1213 | // driver_close(drvinst, drvid); |
---|
1214 | // output_buffer_free(); |
---|
1215 | exit(0); |
---|
1216 | } |
---|
1217 | |
---|
1218 | //ll |
---|