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 dbtool, \ref sender,
25 * \ref receiver, \ref receiver_node, \ref filter, \ref filter_node.
31 #include "server.cmdline.h"
36 #include "close_on_fork.h"
44 /** define the array of error lists needed by para_server */
47 /** shut down non-authorized connections after that many seconds */
48 #define ALARM_TIMEOUT 10
50 /* these are exported to afs/command/dbtool */
51 struct misc_meta_data
*mmd
;
52 /** the configuration of para_server
54 * It also contains the options for all database tools and all supported
57 struct gengetopt_args_info conf
;
58 char *user_list
= NULL
;
59 extern void http_send_init(struct sender
*);
60 extern void ortp_send_init(struct sender
*);
61 extern struct audio_format afl
[];
63 /** the list of supported database tools */
64 struct dbtool dblist
[] = {
67 .init
= random_dbtool_init
,
68 .update_audio_file
= NULL
,
72 .init
= plm_dbtool_init
,
73 .update_audio_file
= NULL
,
80 .init
= mysql_dbtool_init
,
81 .update_audio_file
= NULL
,
91 /** the list of supported senders */
92 struct sender senders
[] = {
95 .init
= http_send_init
,
100 .init
= ortp_send_init
,
109 /* global variables for server-internal use */
110 static FILE *logfile
;
111 static int mmd_mutex
, mmd_shm_id
;
112 static int signal_pipe
;
115 * para_server's log function
117 * \param ll the log level
118 * \param fmt the format string describing the log message
120 void para_log(int ll
, char* fmt
,...)
126 char str
[MAXLINE
] = "";
129 if (ll
< conf
.loglevel_arg
)
138 if (conf
.daemon_given
&& !logfile
)
142 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
143 fprintf(outfd
, "%s ", str
);
144 if (conf
.loglevel_arg
<= INFO
)
145 fprintf(outfd
, "%i: ", ll
);
147 if (conf
.loglevel_arg
<= INFO
)
148 fprintf(outfd
, "(%d) ", mypid
);
150 vfprintf(outfd
, fmt
, argp
);
155 * setup shared memory area and get mutex for locking
157 static void shm_init(void)
160 int ret
= shm_new(sizeof(struct misc_meta_data
));
165 ret
= shm_attach(ret
, ATTACH_RW
, &shm
);
178 mmd
->num_commands
= 0;
180 mmd
->num_connects
= 0;
181 mmd
->active_connections
= 0;
182 strcpy(mmd
->filename
, "(none)");
183 mmd
->audio_format
= -1;
184 mmd
->afs_status_flags
= AFS_NEXT
;
185 mmd
->new_afs_status_flags
= AFS_NEXT
;
186 mmd
->sender_cmd_data
.cmd_num
= -1;
189 PARA_EMERG_LOG("%s", PARA_STRERROR(-ret
));
194 * lock the shared memory area containing the mmd struct
196 * \sa semop(2), struct misc_meta_data
200 mutex_lock(mmd_mutex
);
204 * unlock the shared memory area containing the mmd struct
206 * \sa semop(2), struct misc_meta_data
209 void mmd_unlock(void)
211 mutex_unlock(mmd_mutex
);
214 static void parse_config(int override
)
216 char *home
= para_homedir();
221 if (conf
.config_file_given
)
222 cf
= conf
.config_file_arg
;
224 cf
= make_message("%s/.paraslash/server.conf", home
);
226 if (!conf
.user_list_given
)
227 user_list
= make_message("%s/.paraslash/server.users", home
);
229 user_list
= para_strdup(conf
.user_list_arg
);
230 ret
= stat(cf
, &statbuf
);
231 if (ret
&& conf
.config_file_given
) {
233 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
237 int tmp
= conf
.daemon_given
;
238 cmdline_parser_configfile(cf
, &conf
, override
, 0, 0);
239 conf
.daemon_given
= tmp
;
242 if (!conf
.logfile_given
&& conf
.daemon_given
) {
244 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
247 if (conf
.logfile_given
)
248 logfile
= open_log(conf
.logfile_arg
);
260 static void setup_signal_handling(void)
264 signal_pipe
= para_signal_init();
265 // fcntl(signal_pipe, F_SETFL, O_NONBLOCK);
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_dbtool(void)
283 mmd
->dbt_change
= -1; /* no change nec., set to new dbt num by com_cdt */
286 if (conf
.dbtool_given
) {
287 for (i
= 0; dblist
[i
].name
; i
++) {
288 if (strcmp(dblist
[i
].name
, conf
.dbtool_arg
))
290 PARA_NOTICE_LOG("initializing %s database tool\n",
292 if (dblist
[i
].init(&dblist
[i
]) < 0) {
293 PARA_WARNING_LOG("init %s failed",
300 PARA_WARNING_LOG("%s", "no such dbtool, switching to random\n");
303 /* use the first dbtool that works
304 * (assuming that random always works)
306 for (i
= 1; dblist
[i
].name
; i
++) {
307 int ret
= dblist
[i
].init(&dblist
[i
]);
309 PARA_INFO_LOG("initialized %s\n", dblist
[i
].name
);
313 PARA_CRIT_LOG("%s init failed: %s\n", dblist
[i
].name
,
314 PARA_STRERROR(-ret
));
318 dblist
[0].init(&dblist
[0]); /* always successful */
321 static unsigned init_network(void)
323 int sockfd
= init_tcp_socket(conf
.port_arg
);
330 static void init_random_seed(void)
332 int fd
, ret
= -1, len
= sizeof(unsigned int);
335 fd
= open("/dev/random", O_RDONLY
);
339 if (read(fd
, &seed
, len
) != len
)
348 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
353 static unsigned do_inits(int argc
, char **argv
)
355 /* connector's address information */
359 /* parse command line options */
360 cmdline_parser(argc
, argv
, &conf
);
361 para_drop_privileges(conf
.user_arg
);
362 /* parse config file, open log and set defaults */
364 log_welcome("para_server", conf
.loglevel_arg
);
365 shm_init(); /* init mmd struct */
366 server_uptime(UPTIME_SET
); /* reset server uptime */
368 if (conf
.daemon_given
)
371 PARA_NOTICE_LOG("%s", "initializing audio file sender\n");
372 /* audio file sender */
374 mmd
->server_pid
= getpid();
375 setup_signal_handling();
377 /* init network socket */
378 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
379 sockfd
= init_network();
380 if (conf
.autoplay_given
) {
381 mmd
->afs_status_flags
|= AFS_PLAYING
;
382 mmd
->new_afs_status_flags
|= AFS_PLAYING
;
384 PARA_NOTICE_LOG("%s", "init complete\n");
388 static void handle_dbt_change(void)
390 int ret
, old
= mmd
->dbt_num
, new = mmd
->dbt_change
;
392 dblist
[old
].shutdown();
393 ret
= dblist
[new].init(&dblist
[new]);
394 mmd
->dbt_change
= -1; /* reset */
400 PARA_ERROR_LOG("%s -- switching to the random dbtool\n", PARA_STRERROR(-ret
));
401 dblist
[0].init(&dblist
[0]);
406 * called when server gets SIGHUP or when client invokes hup command.
408 static void handle_sighup(void)
410 PARA_NOTICE_LOG("%s", "SIGHUP\n");
411 close_log(logfile
); /* gets reopened if necessary by parse_config */
413 parse_config(1); /* reopens log */
414 mmd
->dbt_change
= mmd
->dbt_num
; /* do not change dbtool */
415 handle_dbt_change(); /* force reloading dbtool */
418 static void status_refresh(void)
420 static int prev_uptime
= -1, prev_events
= -1;
421 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
423 if (prev_events
!= mmd
->events
)
425 if (mmd
->new_afs_status_flags
!= mmd
->afs_status_flags
)
427 if (uptime
/ 60 != prev_uptime
/ 60)
431 prev_uptime
= uptime
;
432 prev_events
= mmd
->events
;
433 mmd
->afs_status_flags
= mmd
->new_afs_status_flags
;
435 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
436 mmd
->events
, mmd
->audio_format
);
444 int main(int argc
, char *argv
[])
446 /* listen on sock_fd, new connection on new_fd */
448 struct sockaddr_in their_addr
;
449 int err
, i
, max_fileno
, ret
;
452 struct timeval
*timeout
;
455 sockfd
= do_inits(argc
, argv
);
457 /* check socket and signal pipe in any case */
460 FD_SET(sockfd
, &rfds
);
462 FD_SET(signal_pipe
, &rfds
);
463 max_fileno
= MAX(max_fileno
, signal_pipe
);
465 timeout
= afs_preselect();
467 for (i
= 0; senders
[i
].name
; i
++) {
468 if (senders
[i
].status
!= SENDER_ON
)
470 if (!senders
[i
].pre_select
)
472 senders
[i
].pre_select(mmd
->audio_format
>= 0?
473 &afl
[mmd
->audio_format
] : NULL
,
477 if (dblist
[mmd
->dbt_num
].pre_select
) {
478 ret
= dblist
[mmd
->dbt_num
].pre_select(&rfds
, &wfds
);
479 max_fileno
= MAX(max_fileno
, ret
);
482 // PARA_DEBUG_LOG("%s: select (max = %i)\n", __func__, max_fileno);
483 ret
= select(max_fileno
+ 1, &rfds
, &wfds
, NULL
, timeout
);
485 //PARA_DEBUG_LOG("%s: select returned %i\n", __func__, ret);
487 if (mmd
->dbt_change
>= 0)
489 if (dblist
[mmd
->dbt_num
].post_select
)
490 dblist
[mmd
->dbt_num
].post_select(&rfds
, &wfds
);
491 if (ret
< 0 && err
== EINTR
)
494 PARA_CRIT_LOG("select error (%s)\n", strerror(err
));
497 for (i
= 0; senders
[i
].name
; i
++) {
498 if (senders
[i
].status
!= SENDER_ON
)
500 if (!senders
[i
].post_select
)
502 senders
[i
].post_select(mmd
->audio_format
>= 0?
503 &afl
[mmd
->audio_format
] : NULL
,
510 if (FD_ISSET(signal_pipe
, &rfds
)) {
512 sig
= para_next_signal();
518 para_reap_children();
520 /* die on sigint/sigterm. Kill all children too. */
523 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
525 dblist
[mmd
->dbt_num
].shutdown();
526 mutex_destroy(mmd_mutex
);
528 shm_destroy(mmd_shm_id
);
532 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
533 int num
= mmd
->sender_cmd_data
.cmd_num
,
534 s
= mmd
->sender_cmd_data
.sender_num
;
536 if (senders
[s
].client_cmds
[num
])
537 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
538 mmd
->sender_cmd_data
.cmd_num
= -1;
540 if (!FD_ISSET(sockfd
, &rfds
))
543 new_fd
= para_accept(sockfd
, &their_addr
, sizeof(struct sockaddr_in
));
546 PARA_INFO_LOG("got connection from %s, forking\n",
547 inet_ntoa(their_addr
.sin_addr
));
549 mmd
->active_connections
++;
553 PARA_CRIT_LOG("%s", "fork failed\n");
558 /* parent keeps accepting connections */
561 alarm(ALARM_TIMEOUT
);
563 close(sockfd
); /* child doesn't need the listener */
565 * put info on who we are serving into argv[0] to make
566 * client ip visible in top/ps
568 for (i
= argc
- 1; i
>= 0; i
--)
569 memset(argv
[i
], 0, strlen(argv
[i
]));
570 sprintf(argv
[0], "para_server (serving %s)",
571 inet_ntoa(their_addr
.sin_addr
));
572 return handle_connect(new_fd
, &their_addr
);