2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2009 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file server/background.c
19 * @brief Background process support for playing tracks
22 #include "disorder-server.h"
24 /** @brief Fork the player or decoder for @p q
25 * @param ev Event loop
26 * @param player Pointer to player information
27 * @param q Track to play or decode
28 * @param child Function to run inside fork
29 * @param bgdata Passed to @c child()
31 * @c q->pl had better already be set.
33 int play_background(ev_source *ev,
34 const struct stringlist *player,
35 struct queue_entry *q,
36 play_background_child_fn *child,
39 struct pbgc_params params[1];
41 memset(params, 0, sizeof params);
42 /* Get the raw path. This needs to be done outside the fork. It's needed by
43 * the play-track callback which has to have the raw filename bytes we got
44 * from readdir() as well as the normalized unicode version of the track
45 * name. (Emphasize 'normalized'; even if you use UTF-8 for your filenames,
46 * they might not be normalized and if they are they might not be normalized
47 * to the same canonical form as DisOrder uses.) */
48 params->rawpath = trackdb_rawpath(q->track);
49 /* Call the prefork function in the player module. None of the built-in
50 * modules use this so it's not well tested, unfortunately. */
51 if(q->type & DISORDER_PLAYER_PREFORK)
52 if(!(q->data = play_prefork(q->pl, q->track))) {
53 error(0, "prefork function for %s failed", q->track);
54 return START_HARDFAIL;
56 /* Capture the player/decoder's stderr and feed it into our logs.
58 * Use the second arg as the tag if available (it's probably a command name),
59 * otherwise the module name. */
61 lfd = logfd(ev, player->s[2] ? player->s[2] : player->s[1]);
64 /* Parse player arguments */
65 int optc = player->n - 2;
66 const char **optv = (const char **)&player->s[2];
67 while(optc > 0 && optv[0][0] == '-') {
68 if(!strcmp(optv[0], "--")) {
73 if(!strcmp(optv[0], "--wait-for-device")
74 || !strncmp(optv[0], "--wait-for-device=", 18)) {
75 const char *waitdevice;
76 if((waitdevice = strchr(optv[0], '='))) {
77 params->waitdevice = waitdevice + 1;
79 params->waitdevice = ""; /* use default */
83 error(0, "unknown option %s", optv[0]);
84 return START_HARDFAIL;
89 /* Create the child process */
90 switch(q->pid = fork()) {
92 /* Child of disorderd */
94 progname = "disorderd-fork";
96 signal(SIGPIPE, SIG_DFL);
97 /* Send our log output to DisOrder's logs */
101 xclose(lfd); /* tidy up */
103 /* Create a new process group, ID = child PID */
105 _exit(child(q, params, bgdata));
107 /* Back in disorderd (child could not be created) */
108 error(errno, "error calling fork");
109 if(q->type & DISORDER_PLAYER_PREFORK)
110 play_cleanup(q->pl, q->data); /* else would leak */
113 return START_SOFTFAIL;
115 /* We don't need the child's end of the log pipe */
118 /* Set the child's process group ID.
120 * But wait, didn't we already set it in the child? Yes, but it's possible
121 * that we'll need to address it by process group ID before it gets that far,
122 * so we set it here too. One or the other may fail but as long as one
123 * succeeds that's fine.
125 setpgid(q->pid, q->pid);
126 /* Ask the event loop to tell us when the child terminates */
127 D(("player subprocess ID %lu", (unsigned long)q->pid));