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