2 * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file server.c Paraslash's main server. */
11 * \mainpage Paraslash API Reference
13 * Starting points for getting an overview:
15 * - The main programs: \ref server.c, \ref audiod.c, \ref client.c,
16 * \ref audioc.c, \ref fsck.c,
17 * - Server: \ref server_command, \ref sender,
18 * - Audio file selector: \ref audio_format_handler, \ref mood, \ref afs_table,
19 * - Client: \ref receiver, \ref receiver_node, \ref filter, \ref filter_node.
22 * The gory details, listed by topic:
24 * - Audio format handlers: \ref mp3_afh.c, \ref ogg_afh.c, \ref aac_afh.c,
25 * - Decoders: \ref mp3dec.c, \ref oggdec.c, \ref aacdec.c,
26 * - Volume normalizer: \ref compress.c,
27 * - Output: \ref alsa_write.c, \ref osx_write.c,
28 * - http: \ref http_recv.c, \ref http_send.c,
29 * - ortp: \ref ortp_recv.c, \ref ortp_send.c,
30 * - dccp: \ref dccp_recv.c, \ref dccp_send.c,
31 * - Audio file selector: \ref afs.c, \ref aft.c, \ref mood.c,
32 * - Afs structures: \ref afs_table, \ref audio_file_data,
33 * \ref afs_info \ref afh_info,
34 * - Afs tables: \ref aft.c, \ref mood.c, \ref playlist.c,
35 * \ref attribute.c, \ref score.c,
36 * - The virtual streaming system: \ref vss.c, \ref chunk_queue.c.
40 * - Scheduling: \ref sched.c, \ref sched.h,
41 * - Networking: \ref net.c,
42 * - File descriptors: \ref fd.c,
43 * - Signals: \ref signal.c,
44 * - Daemons: \ref daemon.c,
45 * - Strings: \ref string.c, \ref string.h,
46 * - Time: \ref time.c,
47 * - Spawning processes: \ref exec.c,
48 * - Inter process communication: \ref ipc.c,
49 * - The object storage layer: \ref osl.c,
50 * - Blob tables: \ref blob.c,
51 * - The error subssystem: \ref error.h.
52 * - Access control for paraslash senders: \ref acl.c, \ref acl.h.
54 * Low-level data structures:
56 * - Doubly linked lists: \ref list.h,
57 * - Red-black trees: \ref rbtree.h, \ref rbtree.c,
58 * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h,
59 * - Hashing: \ref hash.h, \ref sha1.h, \ref sha1.c,
60 * - Crypto: \ref crypt.c.
69 #include "server.cmdline.h"
76 #include "close_on_fork.h"
85 #include "user_list.h"
87 /** Define the array of error lists needed by para_server. */
90 /** Shut down non-authorized connections after that many seconds. */
91 #define ALARM_TIMEOUT 10
94 * Pointer to shared memory area for communication between para_server
95 * and its children. Exported to vss.c. command.c and to afs.
97 struct misc_meta_data
*mmd
;
100 * The configuration of para_server
102 * It also contains the options for the audio file selector, audio format
103 * handler and all supported senders.
105 struct server_args_info conf
;
107 /** A random value used in child context for authentication. */
108 uint32_t afs_socket_cookie
;
110 /* global variables for server-internal use */
111 static FILE *logfile
;
112 /** The file containing user information (public key, permissions). */
113 static char *user_list_file
= NULL
;
114 static int mmd_mutex
, mmd_shm_id
;
115 static pid_t afs_pid
;
118 /** The task resposible for server command handling. */
119 struct server_command_task
{
120 /** TCP port on which para_server listens for connections. */
122 /** Copied from para_server's main function. */
124 /** Argument vector passed to para_server's main function. */
126 /** The command task structure for scheduling. */
131 * Para_server's log function.
133 * \param ll The log level.
134 * \param fmt The format string describing the log message.
136 void para_log(int ll
, const char* fmt
,...)
142 char str
[MAXLINE
] = "";
145 if (ll
< conf
.loglevel_arg
)
147 outfd
= logfile
? logfile
: stderr
;
150 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
151 fprintf(outfd
, "%s ", str
);
152 if (conf
.loglevel_arg
<= INFO
)
153 fprintf(outfd
, "%i: ", ll
);
155 if (conf
.loglevel_arg
<= INFO
)
156 fprintf(outfd
, "(%d) ", (int)mypid
);
158 vfprintf(outfd
, fmt
, argp
);
163 * setup shared memory area and get mutex for locking
165 static void shm_init(void)
168 int ret
= shm_new(sizeof(struct misc_meta_data
));
173 ret
= shm_attach(ret
, ATTACH_RW
, &shm
);
185 mmd
->num_commands
= 0;
187 mmd
->num_connects
= 0;
188 mmd
->active_connections
= 0;
189 mmd
->vss_status_flags
= VSS_NEXT
;
190 mmd
->new_vss_status_flags
= VSS_NEXT
;
193 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
198 * Lock the shared memory area containing the mmd struct.
200 * \sa semop(2), struct misc_meta_data.
204 mutex_lock(mmd_mutex
);
208 * Unlock the shared memory area containing the mmd struct.
210 * \sa semop(2), struct misc_meta_data.
213 void mmd_unlock(void)
215 mutex_unlock(mmd_mutex
);
218 static void parse_config(int override
)
220 char *home
= para_homedir();
225 if (conf
.config_file_given
)
226 cf
= para_strdup(conf
.config_file_arg
);
228 cf
= make_message("%s/.paraslash/server.conf", home
);
229 free(user_list_file
);
230 if (!conf
.user_list_given
)
231 user_list_file
= make_message("%s/.paraslash/server.users", home
);
233 user_list_file
= para_strdup(conf
.user_list_arg
);
234 ret
= stat(cf
, &statbuf
);
235 if (ret
&& conf
.config_file_given
) {
237 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
241 int tmp
= conf
.daemon_given
;
242 struct server_cmdline_parser_params params
= {
243 .override
= override
,
246 .check_ambiguity
= 0,
249 server_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
250 conf
.daemon_given
= tmp
;
252 if (conf
.logfile_given
)
253 logfile
= open_log(conf
.logfile_arg
);
260 free(user_list_file
);
261 user_list_file
= NULL
;
265 static void signal_pre_select(struct sched
*s
, struct task
*t
)
267 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
268 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
272 * called when server gets SIGHUP or when client invokes hup command.
274 static void handle_sighup(void)
276 PARA_NOTICE_LOG("SIGHUP\n");
277 close_log(logfile
); /* gets reopened if necessary by parse_config */
279 parse_config(1); /* reopens log */
280 init_user_list(user_list_file
); /* reload user list */
282 kill(afs_pid
, SIGHUP
);
285 static void signal_post_select(struct sched
*s
, struct task
*t
)
287 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
289 if (!FD_ISSET(st
->fd
, &s
->rfds
))
292 st
->signum
= para_next_signal();
293 switch (st
->signum
) {
300 int ret
= para_reap_child(&pid
);
305 PARA_EMERG_LOG("fatal: afs died\n");
309 /* die on sigint/sigterm. Kill all children too. */
312 PARA_EMERG_LOG("terminating on signal %d\n", st
->signum
);
315 mutex_destroy(mmd_mutex
);
317 shm_destroy(mmd_shm_id
);
323 static void init_signal_task(void)
325 static struct signal_task signal_task_struct
,
326 *st
= &signal_task_struct
;
328 st
->task
.pre_select
= signal_pre_select
;
329 st
->task
.post_select
= signal_post_select
;
330 sprintf(st
->task
.status
, "signal task");
332 st
->fd
= para_signal_init(); /* always successful */
334 PARA_NOTICE_LOG("setting up signal handlers\n");
335 if (para_install_sighandler(SIGINT
) < 0)
337 if (para_install_sighandler(SIGTERM
) < 0)
339 if (para_install_sighandler(SIGHUP
) < 0)
341 if (para_install_sighandler(SIGCHLD
) < 0)
343 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
345 if (signal(SIGUSR1
, SIG_IGN
) == SIG_ERR
)
347 add_close_on_fork_list(st
->fd
);
348 register_task(&st
->task
);
351 PARA_EMERG_LOG("could not install signal handlers\n");
355 static void command_pre_select(struct sched
*s
, struct task
*t
)
357 struct server_command_task
*sct
= container_of(t
, struct server_command_task
, task
);
358 para_fd_set(sct
->listen_fd
, &s
->rfds
, &s
->max_fileno
);
361 static void command_post_select(struct sched
*s
, struct task
*t
)
363 struct server_command_task
*sct
= container_of(t
, struct server_command_task
, task
);
369 if (!FD_ISSET(sct
->listen_fd
, &s
->rfds
))
371 ret
= para_accept(sct
->listen_fd
, NULL
, 0);
375 peer_name
= remote_name(new_fd
);
376 PARA_INFO_LOG("got connection from %s, forking\n", peer_name
);
378 mmd
->active_connections
++;
382 ret
= -ERRNO_TO_PARA_ERROR(errno
);
387 /* parent keeps accepting connections */
390 alarm(ALARM_TIMEOUT
);
392 para_signal_shutdown();
394 * put info on who we are serving into argv[0] to make
395 * client ip visible in top/ps
397 for (i
= sct
->argc
- 1; i
>= 0; i
--)
398 memset(sct
->argv
[i
], 0, strlen(sct
->argv
[i
]));
399 sprintf(sct
->argv
[0], "para_server (serving %s)", peer_name
);
400 return handle_connect(new_fd
, peer_name
);
403 PARA_CRIT_LOG("%s\n", para_strerror(-ret
));
406 static void init_server_command_task(int argc
, char **argv
)
409 static struct server_command_task server_command_task_struct
,
410 *sct
= &server_command_task_struct
;
412 PARA_NOTICE_LOG("initializing tcp command socket\n");
413 sct
->task
.pre_select
= command_pre_select
;
414 sct
->task
.post_select
= command_post_select
;
417 ret
= para_listen(AF_UNSPEC
, IPPROTO_TCP
, conf
.port_arg
);
420 sct
->listen_fd
= ret
;
421 ret
= mark_fd_nonblocking(sct
->listen_fd
);
424 add_close_on_fork_list(sct
->listen_fd
); /* child doesn't need the listener */
425 register_task(&sct
->task
);
428 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
432 static void init_random_seed(void)
435 int fd
, ret
= para_open("/dev/urandom", O_RDONLY
, 0);
440 ret
= read(fd
, &seed
, sizeof(seed
));
442 ret
= -ERRNO_TO_PARA_ERROR(errno
);
445 if (ret
!= sizeof(seed
)) {
446 ret
= -ERRNO_TO_PARA_ERROR(EIO
);
456 PARA_EMERG_LOG("can not seed pseudo random number generator: %s\n",
457 para_strerror(-ret
));
461 static int init_afs(void)
463 int ret
, afs_server_socket
[2];
465 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
468 afs_socket_cookie
= para_random((uint32_t)-1);
472 if (!afs_pid
) { /* child (afs) */
473 close(afs_server_socket
[0]);
474 afs_init(afs_socket_cookie
, afs_server_socket
[1]);
476 close(afs_server_socket
[1]);
477 ret
= mark_fd_nonblocking(afs_server_socket
[0]);
480 add_close_on_fork_list(afs_server_socket
[0]);
481 PARA_INFO_LOG("afs_socket: %d, afs_socket_cookie: %u\n",
482 afs_server_socket
[0], (unsigned) afs_socket_cookie
);
483 return afs_server_socket
[0];
486 static void server_init(int argc
, char **argv
)
488 struct server_cmdline_parser_params params
= {
492 .check_ambiguity
= 0,
499 /* parse command line options */
500 server_cmdline_parser_ext(argc
, argv
, &conf
, ¶ms
);
501 HANDLE_VERSION_FLAG("server", conf
);
502 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
503 /* parse config file, open log and set defaults */
505 log_welcome("para_server", conf
.loglevel_arg
);
506 shm_init(); /* init mmd struct */
507 server_uptime(UPTIME_SET
); /* reset server uptime */
508 init_user_list(user_list_file
);
510 if (conf
.daemon_given
)
512 PARA_NOTICE_LOG("initializing audio format handlers\n");
514 mmd
->server_pid
= getpid();
516 PARA_NOTICE_LOG("initializing the audio file selector\n");
517 afs_socket
= init_afs();
518 PARA_NOTICE_LOG("initializing virtual streaming system\n");
519 init_vss_task(afs_socket
);
520 init_server_command_task(argc
, argv
);
521 PARA_NOTICE_LOG("server init complete\n");
524 static void status_refresh(void)
526 static int prev_uptime
= -1, prev_events
= -1;
527 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
529 if (prev_events
!= mmd
->events
)
531 if (mmd
->new_vss_status_flags
!= mmd
->vss_status_flags
)
533 if (uptime
/ 60 != prev_uptime
/ 60)
539 prev_uptime
= uptime
;
540 prev_events
= mmd
->events
;
541 mmd
->vss_status_flags
= mmd
->new_vss_status_flags
;
543 PARA_DEBUG_LOG("%d events, forcing status update\n",
549 static int server_select(int max_fileno
, fd_set
*readfds
, fd_set
*writefds
,
550 struct timeval
*timeout_tv
)
556 ret
= para_select(max_fileno
+ 1, readfds
, writefds
, timeout_tv
);
562 * The main function of para_server.
564 * \param argc Usual argument count.
565 * \param argv Usual argument vector.
567 * \return EXIT_SUCCESS or EXIT_FAILURE.
569 int main(int argc
, char *argv
[])
572 static struct sched s
= {
577 .select_function
= server_select
579 server_init(argc
, argv
);
583 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));