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"
37 #include <sys/mman.h> /* MAP_SHARED, PROT_READ, PROT_WRITE */
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
,
78 .init
= mysql_dbtool_init
,
79 .update_audio_file
= NULL
,
87 /** the list of supported senders */
88 struct sender senders
[] = {
91 .init
= http_send_init
,
96 .init
= ortp_send_init
,
105 /* global variables for server-internal use */
106 static FILE *logfile
;
107 static int mmd_semid
;
108 static int signal_pipe
;
111 * para_server's log function
113 * \param ll the log level
114 * \param fmt the format string describing the log message
116 void para_log(int ll
, char* fmt
,...)
122 char str
[MAXLINE
] = "";
125 if (ll
< conf
.loglevel_arg
)
134 if (conf
.daemon_given
&& !logfile
)
138 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
139 fprintf(outfd
, "%s ", str
);
140 if (conf
.loglevel_arg
<= INFO
)
141 fprintf(outfd
, "%i: ", ll
);
143 if (conf
.loglevel_arg
<= INFO
)
144 fprintf(outfd
, "(%d) ", mypid
);
146 vfprintf(outfd
, fmt
, argp
);
152 * setup shared memory area and get semaphore for locking
154 static void shm_init(void)
159 if ((fd
= open("/dev/zero", O_RDWR
)) < 0) {
160 PARA_EMERG_LOG("%s", "failed to open /dev/zero\n");
163 if ((area
= mmap(0, sizeof(struct misc_meta_data
),
164 PROT_READ
| PROT_WRITE
,
165 MAP_SHARED
, fd
, 0)) == (caddr_t
) - 1) {
166 PARA_EMERG_LOG("%s", "mmap error\n");
169 close(fd
); /* we dont need /dev/zero anymore */
170 mmd
= (struct misc_meta_data
*)area
;
174 mmd
->num_commands
= 0;
176 mmd
->num_connects
= 0;
177 mmd
->active_connections
= 0;
178 strcpy(mmd
->filename
, "(none)");
179 mmd
->audio_format
= -1;
180 mmd
->afs_status_flags
= AFS_NEXT
;
181 mmd
->new_afs_status_flags
= AFS_NEXT
;
182 mmd
->sender_cmd_data
.cmd_num
= -1;
184 mmd_semid
= semget(42, 1, IPC_CREAT
| 0666);
185 if (mmd_semid
== -1) {
186 PARA_EMERG_LOG("%s", "semget failed\n");
191 static void do_semop(struct sembuf
*sops
, int num
)
193 if (semop(mmd_semid
, sops
, num
) >= 0)
195 PARA_WARNING_LOG("semop failed (%s), retrying\n", strerror(errno
));
196 while (semop(mmd_semid
, sops
, num
) < 0)
201 * lock the shared memory area containing the mmd struct
203 * \sa semop(2), struct misc_meta_data
207 struct sembuf sops
[2] = {
223 * unlock the shared memory area containing the mmd struct
225 * \sa semop(2), struct misc_meta_data
227 void mmd_unlock(void)
229 struct sembuf sops
[1] = {
239 static void parse_config(int override
)
241 char *home
= para_homedir();
246 if (conf
.config_file_given
)
247 cf
= conf
.config_file_arg
;
249 cf
= make_message("%s/.paraslash/server.conf", home
);
251 if (!conf
.user_list_given
)
252 user_list
= make_message("%s/.paraslash/server.users", home
);
254 user_list
= para_strdup(conf
.user_list_arg
);
255 ret
= stat(cf
, &statbuf
);
256 if (ret
&& conf
.config_file_given
) {
258 PARA_EMERG_LOG("can not stat config file %s\n", cf
);
262 int tmp
= conf
.daemon_given
;
263 cmdline_parser_configfile(cf
, &conf
, override
, 0, 0);
264 conf
.daemon_given
= tmp
;
267 if (!conf
.logfile_given
&& conf
.daemon_given
) {
269 PARA_EMERG_LOG("%s", "daemon, but no log file\n");
272 if (conf
.logfile_given
)
273 logfile
= open_log(conf
.logfile_arg
);
285 static void setup_signal_handling(void)
289 signal_pipe
= para_signal_init();
290 // fcntl(signal_pipe, F_SETFL, O_NONBLOCK);
291 PARA_NOTICE_LOG("%s", "setting up signal handlers\n");
292 ret
+= para_install_sighandler(SIGINT
);
293 ret
+= para_install_sighandler(SIGTERM
);
294 ret
+= para_install_sighandler(SIGHUP
);
295 ret
+= para_install_sighandler(SIGCHLD
);
296 signal(SIGPIPE
, SIG_IGN
);
297 signal(SIGUSR1
, SIG_IGN
);
299 PARA_EMERG_LOG("%s", "could not install signal handlers\n");
304 static void init_dbtool(void)
308 mmd
->dbt_change
= -1; /* no change nec., set to new dbt num by com_cdt */
311 if (conf
.dbtool_given
) {
312 for (i
= 0; dblist
[i
].name
; i
++) {
313 if (strcmp(dblist
[i
].name
, conf
.dbtool_arg
))
315 PARA_NOTICE_LOG("initializing %s database tool\n",
317 if (dblist
[i
].init(&dblist
[i
]) < 0) {
318 PARA_WARNING_LOG("init %s failed",
325 PARA_WARNING_LOG("%s", "no such dbtool, switching to random\n");
328 /* use the first dbtool that works
329 * (assuming that random always works)
331 for (i
= 1; dblist
[i
].name
; i
++) {
332 int ret
= dblist
[i
].init(&dblist
[i
]);
334 PARA_INFO_LOG("initialized %s\n", dblist
[i
].name
);
338 PARA_CRIT_LOG("%s init failed: %s\n", dblist
[i
].name
,
339 PARA_STRERROR(-ret
));
343 dblist
[0].init(&dblist
[0]); /* always successful */
346 static unsigned init_network(void)
348 int sockfd
= init_tcp_socket(conf
.port_arg
);
355 static void init_random_seed(void)
357 int fd
, ret
= -1, len
= sizeof(unsigned int);
360 fd
= open("/dev/random", O_RDONLY
);
364 if (read(fd
, &seed
, len
) != len
)
373 PARA_EMERG_LOG("can not seed pseudo random generator (ret = %d)\n",
378 static unsigned do_inits(int argc
, char **argv
)
380 /* connector's address information */
384 /* parse command line options */
385 cmdline_parser(argc
, argv
, &conf
);
386 para_drop_privileges(conf
.user_arg
);
387 /* parse config file, open log and set defaults */
389 log_welcome("para_server", conf
.loglevel_arg
);
390 shm_init(); /* init mmd struct */
391 server_uptime(UPTIME_SET
); /* reset server uptime */
393 if (conf
.daemon_given
)
396 PARA_NOTICE_LOG("%s", "initializing audio file sender\n");
397 /* audio file sender */
399 mmd
->server_pid
= getpid();
400 setup_signal_handling();
402 /* init network socket */
403 PARA_NOTICE_LOG("%s", "initializing tcp command socket\n");
404 sockfd
= init_network();
405 if (conf
.autoplay_given
) {
406 mmd
->afs_status_flags
|= AFS_PLAYING
;
407 mmd
->new_afs_status_flags
|= AFS_PLAYING
;
409 PARA_NOTICE_LOG("%s", "init complete\n");
413 static void handle_dbt_change(void)
415 int ret
, old
= mmd
->dbt_num
, new = mmd
->dbt_change
;
417 dblist
[old
].shutdown();
418 ret
= dblist
[new].init(&dblist
[new]);
419 mmd
->dbt_change
= -1; /* reset */
425 PARA_ERROR_LOG("%s -- switching to the random dbtool\n", PARA_STRERROR(-ret
));
426 dblist
[0].init(&dblist
[0]);
431 * called when server gets SIGHUP or when client invokes hup command.
433 static void handle_sighup(void)
435 PARA_NOTICE_LOG("%s", "SIGHUP\n");
436 close_log(logfile
); /* gets reopened if necessary by parse_config */
438 parse_config(1); /* reopens log */
439 mmd
->dbt_change
= mmd
->dbt_num
; /* do not change dbtool */
440 handle_dbt_change(); /* force reloading dbtool */
443 static void status_refresh(void)
445 static int prev_uptime
= -1, prev_events
= -1;
446 int uptime
= server_uptime(UPTIME_GET
), ret
= 1;
448 if (prev_events
!= mmd
->events
)
450 if (mmd
->new_afs_status_flags
!= mmd
->afs_status_flags
)
452 if (uptime
/ 60 != prev_uptime
/ 60)
456 prev_uptime
= uptime
;
457 prev_events
= mmd
->events
;
458 mmd
->afs_status_flags
= mmd
->new_afs_status_flags
;
460 PARA_DEBUG_LOG("%d events, forcing status update, af = %d\n",
461 mmd
->events
, mmd
->audio_format
);
469 int main(int argc
, char *argv
[])
471 /* listen on sock_fd, new connection on new_fd */
473 struct sockaddr_in their_addr
;
474 int err
, i
, max_fileno
, ret
;
477 struct timeval
*timeout
;
480 sockfd
= do_inits(argc
, argv
);
482 /* check socket and signal pipe in any case */
485 FD_SET(sockfd
, &rfds
);
487 FD_SET(signal_pipe
, &rfds
);
488 max_fileno
= MAX(max_fileno
, signal_pipe
);
490 timeout
= afs_preselect();
492 for (i
= 0; senders
[i
].name
; i
++) {
493 if (senders
[i
].status
!= SENDER_ON
)
495 if (!senders
[i
].pre_select
)
497 senders
[i
].pre_select(mmd
->audio_format
>= 0?
498 &afl
[mmd
->audio_format
] : NULL
,
503 // PARA_DEBUG_LOG("%s: select (max = %i)\n", __func__, max_fileno);
504 ret
= select(max_fileno
+ 1, &rfds
, &wfds
, NULL
, timeout
);
506 //PARA_DEBUG_LOG("%s: select returned %i\n", __func__, ret);
508 if (mmd
->dbt_change
>= 0)
510 if (ret
< 0 && err
== EINTR
)
513 PARA_CRIT_LOG("select error (%s)\n", strerror(err
));
516 for (i
= 0; senders
[i
].name
; i
++) {
517 if (senders
[i
].status
!= SENDER_ON
)
519 if (!senders
[i
].post_select
)
521 senders
[i
].post_select(mmd
->audio_format
>= 0?
522 &afl
[mmd
->audio_format
] : NULL
,
529 if (FD_ISSET(signal_pipe
, &rfds
)) {
531 sig
= para_next_signal();
537 para_reap_children();
539 /* die on sigint/sigterm. Kill all children too. */
542 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
547 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
548 int num
= mmd
->sender_cmd_data
.cmd_num
,
549 s
= mmd
->sender_cmd_data
.sender_num
;
551 if (senders
[s
].client_cmds
[num
])
552 senders
[s
].client_cmds
[num
](&mmd
->sender_cmd_data
);
553 mmd
->sender_cmd_data
.cmd_num
= -1;
555 if (!FD_ISSET(sockfd
, &rfds
))
558 new_fd
= para_accept(sockfd
, &their_addr
, sizeof(struct sockaddr_in
));
561 PARA_INFO_LOG("got connection from %s, forking\n",
562 inet_ntoa(their_addr
.sin_addr
));
564 mmd
->active_connections
++;
568 PARA_CRIT_LOG("%s", "fork failed\n");
573 /* parent keeps accepting connections */
576 alarm(ALARM_TIMEOUT
);
578 close(sockfd
); /* child doesn't need the listener */
580 * put info on who we are serving into argv[0] to make
581 * client ip visible in top/ps
583 for (i
= argc
- 1; i
>= 0; i
--)
584 memset(argv
[i
], 0, strlen(argv
[i
]));
585 sprintf(argv
[0], "para_server (serving %s)",
586 inet_ntoa(their_addr
.sin_addr
));
587 return handle_connect(new_fd
, &their_addr
);