2 * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file server.c Paraslash's main server */
22 /** \mainpage Paraslash API Reference
24 * Good starting points for reading are probably \ref audio_file_selector,
25 * \ref sender, \ref receiver, \ref receiver_node, \ref filter, \ref
32 #include "server.cmdline.h"
37 #include "close_on_fork.h"
46 #include "user_list.h"
48 /** define the array of error lists needed by para_server */
51 /** shut down non-authorized connections after that many seconds */
52 #define ALARM_TIMEOUT 10
54 /* these are exported to afs.c. command.c and to all selectors */
55 struct misc_meta_data
*mmd
;
56 /** the configuration of para_server
58 * It also contains the options for all audio file selectors and all supported
61 struct server_args_info conf
;
62 char *user_list_file
= NULL
;
63 extern void dccp_send_init(struct sender
*);
64 extern void http_send_init(struct sender
*);
65 extern void ortp_send_init(struct sender
*);
67 /* TODO: This is better handled by autoconf */
68 /** the list of supported audio file selectors */
69 struct audio_file_selector selectors
[] = {
72 .init
= random_selector_init
,
73 .update_audio_file
= NULL
,
77 .init
= playlist_selector_init
,
78 .update_audio_file
= NULL
,
85 .init
= mysql_selector_init
,
86 .update_audio_file
= NULL
,
96 /** the list of supported senders */
97 struct sender senders
[] = {
100 .init
= http_send_init
,
104 .init
= dccp_send_init
,
109 .init
= ortp_send_init
,
118 /* global variables for server-internal use */
119 static FILE *logfile
;
120 static int mmd_mutex
, mmd_shm_id
;
121 static int signal_pipe
;
124 * para_server's log function
126 * \param ll the log level
127 * \param fmt the format string describing the log message
129 void para_log(int ll
, const char* fmt
,...)
135 char str
[MAXLINE
] = "";
138 if (ll
< conf
.loglevel_arg
)
140 outfd
= logfile
? logfile
: stderr
;
143 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
144 fprintf(outfd
, "%s ", str
);
145 if (conf
.loglevel_arg
<= INFO
)
146 fprintf(outfd
, "%i: ", ll
);
148 if (conf
.loglevel_arg
<= INFO
)
149 fprintf(outfd
, "(%d) ", mypid
);
151 vfprintf(outfd
, fmt
, argp
);
156 * setup shared memory area and get mutex for locking
158 static void shm_init(void)
161 int ret
= shm_new(sizeof(struct misc_meta_data
));
166 ret
= shm_attach(ret
, ATTACH_RW
, &shm
);
177 mmd
->selector_num
= 0;
179 mmd
->num_commands
= 0;
181 mmd
->num_connects
= 0;
182 mmd
->active_connections
= 0;
183 strcpy(mmd
->filename
, "(none)");
184 mmd
->audio_format
= -1;
185 mmd
->afs_status_flags
= AFS_NEXT
;
186 mmd
->new_afs_status_flags
= AFS_NEXT
;
187 mmd
->sender_cmd_data
.cmd_num
= -1;
190 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret
));
195 * lock the shared memory area containing the mmd struct
197 * \sa semop(2), struct misc_meta_data
201 mutex_lock(mmd_mutex
);
205 * unlock the shared memory area containing the mmd struct
207 * \sa semop(2), struct misc_meta_data
210 void mmd_unlock(void)
212 mutex_unlock(mmd_mutex
);
215 static void parse_config(int override
)
217 char *home
= para_homedir();
222 if (conf
.config_file_given
)
223 cf
= conf
.config_file_arg
;
225 cf
= make_message("%s/.paraslash/server.conf", home
);
226 free(user_list_file
);
227 if (!conf
.user_list_given
)
228 user_list_file
= make_message("%s/.paraslash/server.users", home
);
230 user_list_file
= para_strdup(conf
.user_list_arg
);
231 ret
= stat(cf
, &statbuf
);
232 if (ret
&& conf
.config_file_given
) {
234 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
238 int tmp
= conf
.daemon_given
;
239 server_cmdline_parser_configfile(cf
, &conf
, override
, 0, 0);
240 conf
.daemon_given
= tmp
;
243 if (!conf
.logfile_given
&& conf
.daemon_given
) {
245 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
248 if (conf
.logfile_given
)
249 logfile
= open_log(conf
.logfile_arg
);
256 free(user_list_file
);
257 user_list_file
= NULL
;
261 static void setup_signal_handling(void)
265 signal_pipe
= para_signal_init();
266 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
267 ret
+= para_install_sighandler(SIGINT
);
268 ret
+= para_install_sighandler(SIGTERM
);
269 ret
+= para_install_sighandler(SIGHUP
);
270 ret
+= para_install_sighandler(SIGCHLD
);
271 ret
+= para_install_sighandler(SIGUSR1
);
272 signal(SIGPIPE
, SIG_IGN
);
274 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
279 static void init_selector(void)
283 mmd
->selector_change
= -1; /* no change nec., set to new num by com_chs */
284 if (!conf
.selector_given
)
286 for (i
= 0; selectors
[i
].name
; i
++) {
287 if (strcmp(selectors
[i
].name
, conf
.selector_arg
))
289 PARA_NOTICE_LOG("initializing %s audio file selector\n",
291 ret
= selectors
[i
].init(&selectors
[i
]);
293 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret
));
296 mmd
->selector_num
= i
;
299 PARA_WARNING_LOG("%s", "falling back to the random selector\n");
301 mmd
->selector_num
= 0;
302 selectors
[0].init(&selectors
[0]); /* always successful */
305 static unsigned init_network(void)
307 int fd
, ret
= init_tcp_socket(conf
.port_arg
);
312 ret
= mark_fd_nonblock(fd
);
317 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
321 static void init_random_seed(void)
323 int fd
, ret
= -1, len
= sizeof(unsigned int);
326 fd
= open("/dev/urandom", O_RDONLY
);
330 if (read(fd
, &seed
, len
) != len
)
339 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
344 static unsigned do_inits(int argc
, char **argv
)
346 /* connector's address information */
350 /* parse command line options */
351 server_cmdline_parser(argc
, argv
, &conf
);
352 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
353 /* parse config file, open log and set defaults */
355 log_welcome("para_server", conf
.loglevel_arg
);
356 shm_init(); /* init mmd struct */
357 server_uptime(UPTIME_SET
); /* reset server uptime */
358 init_user_list(user_list_file
);
360 if (conf
.daemon_given
)
363 PARA_NOTICE_LOG("%s", "initializing audio file sender\n");
364 /* audio file sender */
366 mmd
->server_pid
= getpid();
367 setup_signal_handling();
369 /* init network socket */
370 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
371 sockfd
= init_network();
372 PARA_NOTICE_LOG("%s", "init complete\n");
376 static void change_selector(void)
378 int ret
, old
= mmd
->selector_num
, new = mmd
->selector_change
;
380 selectors
[old
].shutdown();
381 ret
= selectors
[new].init(&selectors
[new]);
382 mmd
->selector_change
= -1; /* reset */
384 mmd
->selector_num
= new;
388 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret
));
389 selectors
[0].init(&selectors
[0]);
390 mmd
->selector_num
= 0;
394 * called when server gets SIGHUP or when client invokes hup command.
396 static void handle_sighup(void)
398 PARA_NOTICE_LOG("%s", "SIGHUP\n");
399 close_log(logfile
); /* gets reopened if necessary by parse_config */
401 parse_config(1); /* reopens log */
402 mmd
->selector_change
= mmd
->selector_num
; /* do not change selector.. */
403 change_selector(); /* .. just reload */
404 init_user_list(user_list_file
); /* reload user list */
407 static void status_refresh(void)
409 static int prev_uptime
= -1, prev_events
= -1;
410 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
412 if (prev_events
!= mmd
->events
)
414 if (mmd
->new_afs_status_flags
!= mmd
->afs_status_flags
)
416 if (uptime
/ 60 != prev_uptime
/ 60)
420 prev_uptime
= uptime
;
421 prev_events
= mmd
->events
;
422 mmd
->afs_status_flags
= mmd
->new_afs_status_flags
;
424 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
425 mmd
->events
, mmd
->audio_format
);
433 int main(int argc
, char *argv
[])
435 /* listen on sock_fd, new connection on new_fd */
437 struct sockaddr_in their_addr
;
438 int i
, max_fileno
, ret
;
441 struct timeval
*timeout
;
444 sockfd
= do_inits(argc
, argv
);
449 /* check socket and signal pipe in any case */
450 para_fd_set(sockfd
, &rfds
, &max_fileno
);
451 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
452 timeout
= afs_preselect();
454 for (i
= 0; senders
[i
].name
; i
++) {
455 if (senders
[i
].status
!= SENDER_ON
)
457 if (!senders
[i
].pre_select
)
459 senders
[i
].pre_select( &max_fileno
, &rfds
, &wfds
);
461 if (selectors
[mmd
->selector_num
].pre_select
) {
462 ret
= selectors
[mmd
->selector_num
].pre_select(&rfds
, &wfds
);
463 max_fileno
= PARA_MAX(max_fileno
, ret
);
466 ret
= para_select(max_fileno
+ 1, &rfds
, &wfds
, timeout
);
468 if (mmd
->selector_change
>= 0)
470 if (selectors
[mmd
->selector_num
].post_select
)
471 selectors
[mmd
->selector_num
].post_select(&rfds
, &wfds
);
474 for (i
= 0; senders
[i
].name
; i
++) {
475 if (senders
[i
].status
!= SENDER_ON
)
477 if (!senders
[i
].post_select
)
479 senders
[i
].post_select(&rfds
, &wfds
);
483 if (FD_ISSET(signal_pipe
, &rfds
)) {
485 sig
= para_next_signal();
491 para_reap_children();
493 /* die on sigint/sigterm. Kill all children too. */
496 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
498 selectors
[mmd
->selector_num
].shutdown();
499 mutex_destroy(mmd_mutex
);
501 shm_destroy(mmd_shm_id
);
505 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
506 int num
= mmd
->sender_cmd_data
.cmd_num
,
507 s
= mmd
->sender_cmd_data
.sender_num
;
509 if (senders
[s
].client_cmds
[num
])
510 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
511 mmd
->sender_cmd_data
.cmd_num
= -1;
513 if (!FD_ISSET(sockfd
, &rfds
))
516 new_fd
= para_accept(sockfd
, &their_addr
, sizeof(struct sockaddr_in
));
519 PARA_INFO_LOG("got connection from %s, forking\n",
520 inet_ntoa(their_addr
.sin_addr
));
522 mmd
->active_connections
++;
526 PARA_CRIT_LOG("%s", "fork failed\n");
531 /* parent keeps accepting connections */
534 alarm(ALARM_TIMEOUT
);
536 close(sockfd
); /* child doesn't need the listener */
538 * put info on who we are serving into argv[0] to make
539 * client ip visible in top/ps
541 for (i
= argc
- 1; i
>= 0; i
--)
542 memset(argv
[i
], 0, strlen(argv
[i
]));
543 sprintf(argv
[0], "para_server (serving %s)",
544 inet_ntoa(their_addr
.sin_addr
));
545 return handle_connect(new_fd
, &their_addr
);