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"
45 /** define the array of error lists needed by para_server */
48 /** shut down non-authorized connections after that many seconds */
49 #define ALARM_TIMEOUT 10
51 /* these are exported to afs.c. command.c and to all selectors */
52 struct misc_meta_data
*mmd
;
53 /** the configuration of para_server
55 * It also contains the options for all audio file selectors and all supported
58 struct gengetopt_args_info conf
;
59 char *user_list
= NULL
;
60 extern void http_send_init(struct sender
*);
61 extern void ortp_send_init(struct sender
*);
62 extern struct audio_format afl
[];
64 /** the list of supported audio file selectors */
65 struct audio_file_selector dblist
[] = {
68 .init
= random_selector_init
,
69 .update_audio_file
= NULL
,
73 .init
= playlist_selector_init
,
74 .update_audio_file
= NULL
,
81 .init
= mysql_selector_init
,
82 .update_audio_file
= NULL
,
92 /** the list of supported senders */
93 struct sender senders
[] = {
96 .init
= http_send_init
,
101 .init
= ortp_send_init
,
110 /* global variables for server-internal use */
111 static FILE *logfile
;
112 static int mmd_mutex
, mmd_shm_id
;
113 static int signal_pipe
;
116 * para_server's log function
118 * \param ll the log level
119 * \param fmt the format string describing the log message
121 void para_log(int ll
, char* fmt
,...)
127 char str
[MAXLINE
] = "";
130 if (ll
< conf
.loglevel_arg
)
139 if (conf
.daemon_given
&& !logfile
)
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
);
227 if (!conf
.user_list_given
)
228 user_list
= make_message("%s/.paraslash/server.users", home
);
230 user_list
= 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 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
);
261 static void setup_signal_handling(void)
265 signal_pipe
= para_signal_init();
266 // fcntl(signal_pipe, F_SETFL, O_NONBLOCK);
267 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
268 ret
+= para_install_sighandler(SIGINT
);
269 ret
+= para_install_sighandler(SIGTERM
);
270 ret
+= para_install_sighandler(SIGHUP
);
271 ret
+= para_install_sighandler(SIGCHLD
);
272 ret
+= para_install_sighandler(SIGUSR1
);
273 signal(SIGPIPE
, SIG_IGN
);
275 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
280 static void init_selector(void)
284 mmd
->dbt_change
= -1; /* no change nec., set to new dbt num by com_cdt */
285 if (!conf
.selector_given
)
287 for (i
= 0; dblist
[i
].name
; i
++) {
288 if (strcmp(dblist
[i
].name
, conf
.selector_arg
))
290 PARA_NOTICE_LOG("initializing %s audio file selector\n",
292 ret
= dblist
[i
].init(&dblist
[i
]);
294 PARA_WARNING_LOG("%s", PARA_STRERROR(-ret
));
297 mmd
->selector_num
= i
;
300 PARA_WARNING_LOG("%s", "falling back to the random selector\n");
302 mmd
->selector_num
= 0;
303 dblist
[0].init(&dblist
[0]); /* always successful */
306 static unsigned init_network(void)
308 int sockfd
= init_tcp_socket(conf
.port_arg
);
315 static void init_random_seed(void)
317 int fd
, ret
= -1, len
= sizeof(unsigned int);
320 fd
= open("/dev/random", O_RDONLY
);
324 if (read(fd
, &seed
, len
) != len
)
333 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
338 static unsigned do_inits(int argc
, char **argv
)
340 /* connector's address information */
344 /* parse command line options */
345 cmdline_parser(argc
, argv
, &conf
);
346 para_drop_privileges(conf
.user_arg
);
347 /* parse config file, open log and set defaults */
349 log_welcome("para_server", conf
.loglevel_arg
);
350 shm_init(); /* init mmd struct */
351 server_uptime(UPTIME_SET
); /* reset server uptime */
353 if (conf
.daemon_given
)
356 PARA_NOTICE_LOG("%s", "initializing audio file sender\n");
357 /* audio file sender */
359 mmd
->server_pid
= getpid();
360 setup_signal_handling();
362 /* init network socket */
363 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
364 sockfd
= init_network();
365 if (conf
.autoplay_given
) {
366 mmd
->afs_status_flags
|= AFS_PLAYING
;
367 mmd
->new_afs_status_flags
|= AFS_PLAYING
;
369 PARA_NOTICE_LOG("%s", "init complete\n");
373 static void handle_dbt_change(void)
375 int ret
, old
= mmd
->selector_num
, new = mmd
->dbt_change
;
377 dblist
[old
].shutdown();
378 ret
= dblist
[new].init(&dblist
[new]);
379 mmd
->dbt_change
= -1; /* reset */
381 mmd
->selector_num
= new;
385 PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret
));
386 dblist
[0].init(&dblist
[0]);
387 mmd
->selector_num
= 0;
391 * called when server gets SIGHUP or when client invokes hup command.
393 static void handle_sighup(void)
395 PARA_NOTICE_LOG("%s", "SIGHUP\n");
396 close_log(logfile
); /* gets reopened if necessary by parse_config */
398 parse_config(1); /* reopens log */
399 mmd
->dbt_change
= mmd
->selector_num
; /* do not change selector */
400 handle_dbt_change(); /* reload selector */
403 static void status_refresh(void)
405 static int prev_uptime
= -1, prev_events
= -1;
406 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
408 if (prev_events
!= mmd
->events
)
410 if (mmd
->new_afs_status_flags
!= mmd
->afs_status_flags
)
412 if (uptime
/ 60 != prev_uptime
/ 60)
416 prev_uptime
= uptime
;
417 prev_events
= mmd
->events
;
418 mmd
->afs_status_flags
= mmd
->new_afs_status_flags
;
420 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
421 mmd
->events
, mmd
->audio_format
);
429 int main(int argc
, char *argv
[])
431 /* listen on sock_fd, new connection on new_fd */
433 struct sockaddr_in their_addr
;
434 int err
, i
, max_fileno
, ret
;
437 struct timeval
*timeout
;
440 sockfd
= do_inits(argc
, argv
);
442 /* check socket and signal pipe in any case */
445 FD_SET(sockfd
, &rfds
);
447 FD_SET(signal_pipe
, &rfds
);
448 max_fileno
= MAX(max_fileno
, signal_pipe
);
450 timeout
= afs_preselect();
452 for (i
= 0; senders
[i
].name
; i
++) {
453 if (senders
[i
].status
!= SENDER_ON
)
455 if (!senders
[i
].pre_select
)
457 senders
[i
].pre_select(mmd
->audio_format
>= 0?
458 &afl
[mmd
->audio_format
] : NULL
,
462 if (dblist
[mmd
->selector_num
].pre_select
) {
463 ret
= dblist
[mmd
->selector_num
].pre_select(&rfds
, &wfds
);
464 max_fileno
= MAX(max_fileno
, ret
);
467 // PARA_DEBUG_LOG("%s: select (max = %i)\n", __func__, max_fileno);
468 ret
= select(max_fileno
+ 1, &rfds
, &wfds
, NULL
, timeout
);
470 //PARA_DEBUG_LOG("%s: select returned %i\n", __func__, ret);
472 if (mmd
->dbt_change
>= 0)
474 if (dblist
[mmd
->selector_num
].post_select
)
475 dblist
[mmd
->selector_num
].post_select(&rfds
, &wfds
);
476 if (ret
< 0 && err
== EINTR
)
479 PARA_CRIT_LOG("select error (%s)\n", strerror(err
));
482 for (i
= 0; senders
[i
].name
; i
++) {
483 if (senders
[i
].status
!= SENDER_ON
)
485 if (!senders
[i
].post_select
)
487 senders
[i
].post_select(mmd
->audio_format
>= 0?
488 &afl
[mmd
->audio_format
] : NULL
,
495 if (FD_ISSET(signal_pipe
, &rfds
)) {
497 sig
= para_next_signal();
503 para_reap_children();
505 /* die on sigint/sigterm. Kill all children too. */
508 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
510 dblist
[mmd
->selector_num
].shutdown();
511 mutex_destroy(mmd_mutex
);
513 shm_destroy(mmd_shm_id
);
517 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
518 int num
= mmd
->sender_cmd_data
.cmd_num
,
519 s
= mmd
->sender_cmd_data
.sender_num
;
521 if (senders
[s
].client_cmds
[num
])
522 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
523 mmd
->sender_cmd_data
.cmd_num
= -1;
525 if (!FD_ISSET(sockfd
, &rfds
))
528 new_fd
= para_accept(sockfd
, &their_addr
, sizeof(struct sockaddr_in
));
531 PARA_INFO_LOG("got connection from %s, forking\n",
532 inet_ntoa(their_addr
.sin_addr
));
534 mmd
->active_connections
++;
538 PARA_CRIT_LOG("%s", "fork failed\n");
543 /* parent keeps accepting connections */
546 alarm(ALARM_TIMEOUT
);
548 close(sockfd
); /* child doesn't need the listener */
550 * put info on who we are serving into argv[0] to make
551 * client ip visible in top/ps
553 for (i
= argc
- 1; i
>= 0; i
--)
554 memset(argv
[i
], 0, strlen(argv
[i
]));
555 sprintf(argv
[0], "para_server (serving %s)",
556 inet_ntoa(their_addr
.sin_addr
));
557 return handle_connect(new_fd
, &their_addr
);