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