diff --git a/common.h b/common.h index f4ec5fbf..31f8c5ca 100644 --- a/common.h +++ b/common.h @@ -24,6 +24,7 @@ typedef struct { char *output_name; audio_output *output; int buffer_start_fill; + int daemonise; } shairport_cfg; extern int debuglev; diff --git a/rtsp.c b/rtsp.c index 0169e347..3625ba65 100644 --- a/rtsp.c +++ b/rtsp.c @@ -758,6 +758,14 @@ void rtsp_listen_loop(void) { mdns_register(); + if (config.daemonise) { + pid_t pid = fork(); + if (pid) { + printf("%d\n", pid); + exit(0); + } + } + printf("Listening for connections.\n"); int acceptfd; diff --git a/shairport.c b/shairport.c index 1138405b..4ad37b4e 100644 --- a/shairport.c +++ b/shairport.c @@ -59,6 +59,8 @@ void usage(char *progname) { " -o output set audio output\n" " -b fill set how full the buffer must be before audio output starts\n" " This value is in frames; default %d\n" + " -d fork (daemonise)\n" + " The PID of the child process is written to stdout\n" "Run %s -o -h to find the available options for a specific output\n" "\n", config.buffer_start_fill, progname); @@ -74,13 +76,16 @@ void usage(char *progname) { int parse_options(int argc, char **argv) { int opt; - while ((opt = getopt(argc, argv, "+hvp:a:o:b:")) > 0) { + while ((opt = getopt(argc, argv, "+hdvp:a:o:b:")) > 0) { switch (opt) { default: printf("Unknown argument -%c\n", optopt); case 'h': usage(argv[0]); exit(1); + case 'd': + config.daemonise = 1; + break; case 'v': debuglev++; break;