Commit | Line | Data |
---|---|---|
5bac5b78 RK |
1 | /* |
2 | * This file is part of DisOrder. | |
8a886602 | 3 | * Copyright (C) 2004-2009, 2011 Richard Kettlewell |
5bac5b78 RK |
4 | * |
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. | |
9 | * | |
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. | |
14 | * | |
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/>. | |
17 | */ | |
18 | /** @file server/background.c | |
19 | * @brief Background process support for playing tracks | |
20 | */ | |
21 | ||
22 | #include "disorder-server.h" | |
23 | ||
c0f52b2c RK |
24 | /** @brief Fork the player or decoder for @p q |
25 | * @param ev Event loop | |
5bac5b78 RK |
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() | |
30 | * | |
31 | * @c q->pl had better already be set. | |
32 | */ | |
33 | int play_background(ev_source *ev, | |
34 | const struct stringlist *player, | |
35 | struct queue_entry *q, | |
36 | play_background_child_fn *child, | |
37 | void *bgdata) { | |
38 | int lfd; | |
39 | struct pbgc_params params[1]; | |
40 | ||
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))) { | |
2e9ba080 | 53 | disorder_error(0, "prefork function for %s failed", q->track); |
5bac5b78 RK |
54 | return START_HARDFAIL; |
55 | } | |
56 | /* Capture the player/decoder's stderr and feed it into our logs. | |
57 | * | |
58 | * Use the second arg as the tag if available (it's probably a command name), | |
59 | * otherwise the module name. */ | |
60 | if(!isatty(2)) | |
61 | lfd = logfd(ev, player->s[2] ? player->s[2] : player->s[1]); | |
62 | else | |
63 | lfd = -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], "--")) { | |
69 | ++optv; | |
70 | --optc; | |
71 | break; | |
72 | } | |
657fdb79 RK |
73 | /* Currently no options supported */ |
74 | disorder_error(0, "unknown option %s", optv[0]); | |
75 | return START_HARDFAIL; | |
5bac5b78 RK |
76 | } |
77 | params->argc = optc; | |
78 | params->argv = optv; | |
79 | /* Create the child process */ | |
80 | switch(q->pid = fork()) { | |
81 | case 0: | |
82 | /* Child of disorderd */ | |
83 | exitfn = _exit; | |
84 | progname = "disorderd-fork"; | |
85 | ev_signal_atfork(ev); | |
86 | signal(SIGPIPE, SIG_DFL); | |
87 | /* Send our log output to DisOrder's logs */ | |
88 | if(lfd != -1) { | |
89 | xdup2(lfd, 1); | |
90 | xdup2(lfd, 2); | |
91 | xclose(lfd); /* tidy up */ | |
92 | } | |
93 | /* Create a new process group, ID = child PID */ | |
94 | setpgid(0, 0); | |
95 | _exit(child(q, params, bgdata)); | |
96 | case -1: | |
97 | /* Back in disorderd (child could not be created) */ | |
2e9ba080 | 98 | disorder_error(errno, "error calling fork"); |
5bac5b78 RK |
99 | if(q->type & DISORDER_PLAYER_PREFORK) |
100 | play_cleanup(q->pl, q->data); /* else would leak */ | |
101 | if(lfd != -1) | |
102 | xclose(lfd); | |
103 | return START_SOFTFAIL; | |
104 | } | |
105 | /* We don't need the child's end of the log pipe */ | |
106 | if(lfd != -1) | |
107 | xclose(lfd); | |
108 | /* Set the child's process group ID. | |
109 | * | |
110 | * But wait, didn't we already set it in the child? Yes, but it's possible | |
111 | * that we'll need to address it by process group ID before it gets that far, | |
112 | * so we set it here too. One or the other may fail but as long as one | |
113 | * succeeds that's fine. | |
114 | */ | |
115 | setpgid(q->pid, q->pid); | |
116 | /* Ask the event loop to tell us when the child terminates */ | |
117 | D(("player subprocess ID %lu", (unsigned long)q->pid)); | |
118 | return START_OK; | |
119 | } | |
120 | ||
121 | /* | |
122 | Local Variables: | |
123 | c-basic-offset:2 | |
124 | comment-column:40 | |
125 | fill-column:79 | |
126 | indent-tabs-mode:nil | |
127 | End: | |
128 | */ |