Remove users of the old audio file selector code.
[paraslash.git] / server.c
1 /*
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file server.c Paraslash's main server */
8
9
10 /** \mainpage Paraslash API Reference
11 *
12 * Good starting points for reading are probably \ref audio_file_selector,
13 * \ref sender, \ref receiver, \ref receiver_node, \ref filter, \ref
14 * filter_node.
15 *
16 */
17
18 #include <signal.h>
19 #include <sys/types.h>
20 #include <dirent.h>
21
22 #include "para.h"
23 #include "error.h"
24 #include "server.cmdline.h"
25 #include "afs_common.h"
26 #include "afh.h"
27 #include "string.h"
28 #include "afs.h"
29 #include "server.h"
30 #include "vss.h"
31 #include "config.h"
32 #include "close_on_fork.h"
33 #include "send.h"
34 #include "net.h"
35 #include "daemon.h"
36 #include "ipc.h"
37 #include "fd.h"
38 #include "list.h"
39 #include "sched.h"
40 #include "signal.h"
41 #include "user_list.h"
42
43 /** define the array of error lists needed by para_server */
44 INIT_SERVER_ERRLISTS;
45
46 /** shut down non-authorized connections after that many seconds */
47 #define ALARM_TIMEOUT 10
48
49 /**
50 * pointer to shared memory area for communication between para_server
51 * and its children. exported to vss.c. command.c and to all selectors.
52 */
53 struct misc_meta_data *mmd;
54
55 /**
56 * the configuration of para_server
57 *
58 * It also contains the options for all audio file selectors, audio format handler
59 * and all supported senders.
60 */
61 struct server_args_info conf;
62
63 /** the file containing user information (public key, permissions) */
64 char *user_list_file = NULL;
65
66 extern void dccp_send_init(struct sender *);
67 extern void http_send_init(struct sender *);
68 extern void ortp_send_init(struct sender *);
69
70 /** the list of supported senders */
71 struct sender senders[] = {
72 {
73 .name = "http",
74 .init = http_send_init,
75 },
76 {
77 .name = "dccp",
78 .init = dccp_send_init,
79 },
80 #ifdef HAVE_ORTP
81 {
82 .name = "ortp",
83 .init = ortp_send_init,
84 },
85 #endif
86 {
87 .name = NULL,
88 }
89 };
90
91
92 /* global variables for server-internal use */
93 static FILE *logfile;
94 static int mmd_mutex, mmd_shm_id;
95 static int signal_pipe;
96
97 /**
98 * para_server's log function
99 *
100 * \param ll the log level
101 * \param fmt the format string describing the log message
102 */
103 void para_log(int ll, const char* fmt,...)
104 {
105 va_list argp;
106 FILE *outfd;
107 struct tm *tm;
108 time_t t1;
109 char str[MAXLINE] = "";
110 pid_t mypid;
111
112 if (ll < conf.loglevel_arg)
113 return;
114 outfd = logfile? logfile : stderr;
115 time(&t1);
116 tm = localtime(&t1);
117 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
118 fprintf(outfd, "%s ", str);
119 if (conf.loglevel_arg <= INFO)
120 fprintf(outfd, "%i: ", ll);
121 mypid = getpid();
122 if (conf.loglevel_arg <= INFO)
123 fprintf(outfd, "(%d) ", mypid);
124 va_start(argp, fmt);
125 vfprintf(outfd, fmt, argp);
126 va_end(argp);
127 }
128
129 /*
130 * setup shared memory area and get mutex for locking
131 */
132 static void shm_init(void)
133 {
134 void *shm;
135 int ret = shm_new(sizeof(struct misc_meta_data));
136
137 if (ret < 0)
138 goto err_out;
139
140 ret = shm_attach(ret, ATTACH_RW, &shm);
141 if (ret < 0)
142 goto err_out;
143 mmd = shm;
144 mmd_shm_id = ret;
145
146 ret = mutex_new();
147 if (ret < 0)
148 goto err_out;
149 mmd_mutex = ret;
150
151 mmd->selector_num = 0;
152 mmd->num_played = 0;
153 mmd->num_commands = 0;
154 mmd->events = 0;
155 mmd->num_connects = 0;
156 mmd->active_connections = 0;
157 strcpy(mmd->filename, "(none)");
158 mmd->audio_format = -1;
159 mmd->vss_status_flags = VSS_NEXT;
160 mmd->new_vss_status_flags = VSS_NEXT;
161 mmd->sender_cmd_data.cmd_num = -1;
162 return;
163 err_out:
164 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret));
165 exit(EXIT_FAILURE);
166 }
167
168 /**
169 * lock the shared memory area containing the mmd struct
170 *
171 * \sa semop(2), struct misc_meta_data
172 */
173 void mmd_lock(void)
174 {
175 mutex_lock(mmd_mutex);
176 }
177
178 /**
179 * unlock the shared memory area containing the mmd struct
180 *
181 * \sa semop(2), struct misc_meta_data
182 */
183
184 void mmd_unlock(void)
185 {
186 mutex_unlock(mmd_mutex);
187 }
188
189 static void parse_config(int override)
190 {
191 char *home = para_homedir();
192 struct stat statbuf;
193 int ret;
194 char *cf;
195
196 if (conf.config_file_given)
197 cf = conf.config_file_arg;
198 else
199 cf = make_message("%s/.paraslash/server.conf", home);
200 free(user_list_file);
201 if (!conf.user_list_given)
202 user_list_file = make_message("%s/.paraslash/server.users", home);
203 else
204 user_list_file = para_strdup(conf.user_list_arg);
205 ret = stat(cf, &statbuf);
206 if (ret && conf.config_file_given) {
207 ret = -1;
208 PARA_EMERG_LOG("can not stat config file %s\n", cf);
209 goto out;
210 }
211 if (!ret) {
212 int tmp = conf.daemon_given;
213 struct server_cmdline_parser_params params = {
214 .override = override,
215 .initialize = 0,
216 .check_required = 0,
217 .check_ambiguity = 0
218 };
219 server_cmdline_parser_config_file(cf, &conf, &params);
220 conf.daemon_given = tmp;
221 }
222 /* logfile */
223 if (!conf.logfile_given && conf.daemon_given) {
224 ret = -1;
225 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
226 goto out;
227 }
228 if (conf.logfile_given)
229 logfile = open_log(conf.logfile_arg);
230 ret = 1;
231 out:
232 free(cf);
233 free(home);
234 if (ret > 0)
235 return;
236 free(user_list_file);
237 user_list_file = NULL;
238 exit(EXIT_FAILURE);
239 }
240
241 static void setup_signal_handling(void)
242 {
243 int ret = 0;
244
245 signal_pipe = para_signal_init();
246 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
247 ret += para_install_sighandler(SIGINT);
248 ret += para_install_sighandler(SIGTERM);
249 ret += para_install_sighandler(SIGHUP);
250 ret += para_install_sighandler(SIGCHLD);
251 ret += para_install_sighandler(SIGUSR1);
252 signal(SIGPIPE, SIG_IGN);
253 if (ret != 5) {
254 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
255 exit(EXIT_FAILURE);
256 }
257 }
258
259 static unsigned init_network(void)
260 {
261 int fd, ret = init_tcp_socket(conf.port_arg);
262
263 if (ret < 0)
264 goto err;
265 fd = ret;
266 ret = mark_fd_nonblock(fd);
267 if (ret < 0)
268 goto err;
269 return fd;
270 err:
271 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
272 exit(EXIT_FAILURE);
273 }
274
275 static void init_random_seed(void)
276 {
277 int fd, ret = -1;
278 unsigned int seed;
279 size_t len = sizeof(unsigned int);
280
281 fd = open("/dev/urandom", O_RDONLY);
282 if (fd < 0)
283 goto out;
284 ret = -2;
285 if (read(fd, &seed, len) != len)
286 goto out;
287 srandom(seed);
288 ret = 1;
289 out:
290 if (fd >= 0)
291 close(fd);
292 if (ret > 0)
293 return;
294 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
295 ret);
296 exit(EXIT_FAILURE);
297 }
298
299 uint32_t afs_socket_cookie;
300 int afs_socket;
301 static pid_t afs_pid;
302
303 static void init_afs(void)
304 {
305 int ret, afs_server_socket[2];
306
307 ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
308 if (ret < 0)
309 exit(EXIT_FAILURE);
310 afs_socket_cookie = para_random((uint32_t)-1);
311 afs_pid = fork();
312 if (afs_pid < 0)
313 exit(EXIT_FAILURE);
314 if (!afs_pid) { /* child (afs) */
315 close(afs_server_socket[0]);
316 afs_init(afs_socket_cookie, afs_server_socket[1]);
317 }
318 close(afs_server_socket[1]);
319 afs_socket = afs_server_socket[0];
320 ret = mark_fd_nonblock(afs_socket);
321 if (ret < 0)
322 exit(EXIT_FAILURE);
323 add_close_on_fork_list(afs_socket);
324 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n", afs_socket,
325 (unsigned) afs_socket_cookie);
326 }
327
328
329 static unsigned do_inits(int argc, char **argv)
330 {
331 /* connector's address information */
332 int sockfd;
333
334 init_random_seed();
335 /* parse command line options */
336 server_cmdline_parser(argc, argv, &conf);
337 HANDLE_VERSION_FLAG("server", conf);
338 para_drop_privileges(conf.user_arg, conf.group_arg);
339 /* parse config file, open log and set defaults */
340 parse_config(0);
341 log_welcome("para_server", conf.loglevel_arg);
342 shm_init(); /* init mmd struct */
343 server_uptime(UPTIME_SET); /* reset server uptime */
344 init_user_list(user_list_file);
345 /* become daemon */
346 if (conf.daemon_given)
347 daemon_init();
348 // init_selector();
349 // PARA_ERROR_LOG("num: %d\n", mmd->selector_num);
350 PARA_NOTICE_LOG("%s", "initializing virtual streaming system\n");
351 afh_init();
352 vss_init();
353 mmd->server_pid = getpid();
354 setup_signal_handling();
355 init_afs();
356 mmd_lock();
357 /* init network socket */
358 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
359 sockfd = init_network();
360 PARA_NOTICE_LOG("%s", "init complete\n");
361 return sockfd;
362 }
363
364 /*
365 * called when server gets SIGHUP or when client invokes hup command.
366 */
367 static void handle_sighup(void)
368 {
369 PARA_NOTICE_LOG("%s", "SIGHUP\n");
370 close_log(logfile); /* gets reopened if necessary by parse_config */
371 logfile = NULL;
372 parse_config(1); /* reopens log */
373 init_user_list(user_list_file); /* reload user list */
374 if (afs_pid)
375 kill(afs_pid, SIGHUP);
376 }
377
378 static void status_refresh(void)
379 {
380 static int prev_uptime = -1, prev_events = -1;
381 int uptime = server_uptime(UPTIME_GET), ret = 1;
382
383 if (prev_events != mmd->events)
384 goto out;
385 if (mmd->new_vss_status_flags != mmd->vss_status_flags)
386 goto out;
387 if (uptime / 60 != prev_uptime / 60)
388 goto out;
389 ret = 0;
390 out:
391 prev_uptime = uptime;
392 prev_events = mmd->events;
393 mmd->vss_status_flags = mmd->new_vss_status_flags;
394 if (ret) {
395 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
396 mmd->events, mmd->audio_format);
397 killpg(0, SIGUSR1);
398 }
399 }
400
401 /**
402 * the main function of para_server
403 *
404 * \param argc usual argument count
405 * \param argv usual argument vector
406 *
407 * \return EXIT_SUCCESS or EXIT_FAILURE
408 *
409 */
410 int main(int argc, char *argv[])
411 {
412 /* listen on sock_fd, new connection on new_fd */
413 int sockfd, new_fd;
414 struct sockaddr_in their_addr;
415 int i, max_fileno, ret;
416 pid_t chld_pid;
417 fd_set rfds, wfds;
418 struct timeval *timeout;
419
420 valid_fd_012();
421 sockfd = do_inits(argc, argv);
422 repeat:
423 FD_ZERO(&rfds);
424 FD_ZERO(&wfds);
425 max_fileno = -1;
426 /* check socket and signal pipe in any case */
427 para_fd_set(sockfd, &rfds, &max_fileno);
428 para_fd_set(signal_pipe, &rfds, &max_fileno);
429 timeout = vss_preselect(&rfds, &wfds, &max_fileno);
430 status_refresh();
431 for (i = 0; senders[i].name; i++) {
432 if (senders[i].status != SENDER_ON)
433 continue;
434 if (!senders[i].pre_select)
435 continue;
436 senders[i].pre_select(&max_fileno, &rfds, &wfds);
437 }
438 mmd_unlock();
439 ret = para_select(max_fileno + 1, &rfds, &wfds, timeout);
440 mmd_lock();
441 vss_post_select(&rfds, &wfds);
442 if (ret < 0)
443 goto repeat;
444 for (i = 0; senders[i].name; i++) {
445 if (senders[i].status != SENDER_ON)
446 continue;
447 if (!senders[i].post_select)
448 continue;
449 senders[i].post_select(&rfds, &wfds);
450 }
451 vss_send_chunk();
452 status_refresh();
453 if (FD_ISSET(signal_pipe, &rfds)) {
454 int sig;
455 pid_t pid;
456 sig = para_next_signal();
457 switch (sig) {
458 case SIGHUP:
459 handle_sighup();
460 break;
461 case SIGCHLD:
462 for (;;) {
463 ret = para_reap_child(&pid);
464 if (ret <= 0)
465 break;
466 if (pid != afs_pid)
467 continue;
468 PARA_EMERG_LOG("fatal: afs died\n");
469 goto genocide;
470 }
471 break;
472 /* die on sigint/sigterm. Kill all children too. */
473 case SIGINT:
474 case SIGTERM:
475 PARA_EMERG_LOG("terminating on signal %d\n", sig);
476 genocide:
477 kill(0, SIGTERM);
478 mutex_destroy(mmd_mutex);
479 shm_detach(mmd);
480 shm_destroy(mmd_shm_id);
481
482 exit(EXIT_FAILURE);
483 }
484 }
485 if (mmd->sender_cmd_data.cmd_num >= 0) {
486 int num = mmd->sender_cmd_data.cmd_num,
487 s = mmd->sender_cmd_data.sender_num;
488
489 if (senders[s].client_cmds[num])
490 senders[s].client_cmds[num](&mmd->sender_cmd_data);
491 mmd->sender_cmd_data.cmd_num = -1;
492 }
493 if (!FD_ISSET(sockfd, &rfds))
494 goto repeat;
495
496 new_fd = para_accept(sockfd, &their_addr, sizeof(struct sockaddr_in));
497 if (new_fd < 0)
498 goto repeat;
499 PARA_INFO_LOG("got connection from %s, forking\n",
500 inet_ntoa(their_addr.sin_addr));
501 mmd->num_connects++;
502 mmd->active_connections++;
503 random();
504 chld_pid = fork();
505 if (chld_pid < 0) {
506 PARA_CRIT_LOG("%s", "fork failed\n");
507 goto repeat;
508 }
509 if (chld_pid) {
510 close(new_fd);
511 /* parent keeps accepting connections */
512 goto repeat;
513 }
514 alarm(ALARM_TIMEOUT);
515 close_listed_fds();
516 close(sockfd); /* child doesn't need the listener */
517 /*
518 * put info on who we are serving into argv[0] to make
519 * client ip visible in top/ps
520 */
521 for (i = argc - 1; i >= 0; i--)
522 memset(argv[i], 0, strlen(argv[i]));
523 sprintf(argv[0], "para_server (serving %s)",
524 inet_ntoa(their_addr.sin_addr));
525 return handle_connect(new_fd, &their_addr);
526 }