struct task task;
};
+extern struct misc_meta_data *mmd;
+
static int server_socket;
static struct command_task command_task_struct;
static struct signal_task signal_task_struct;
static enum play_mode current_play_mode;
+static char *current_mop; /* mode or playlist specifier. NULL means dummy mooe */
+
/**
* A random number used to "authenticate" the connection.
return ret;
}
-int pass_afd(int fd, char *buf, size_t size)
+static int pass_afd(int fd, char *buf, size_t size)
{
struct msghdr msg = {.msg_iov = NULL};
struct cmsghdr *cmsg;
/* Sum of the length of all control messages in the buffer */
msg.msg_controllen = cmsg->cmsg_len;
- PARA_NOTICE_LOG("passing %zu bytes and fd %d\n", size, fd);
+ PARA_DEBUG_LOG("passing %zu bytes and fd %d\n", size, fd);
ret = sendmsg(server_socket, &msg, 0);
if (ret < 0) {
ret = -ERRNO_TO_PARA_ERROR(errno);
struct audio_file_data afd;
int ret, shmid;
char buf[8];
+ long score;
- PARA_NOTICE_LOG("getting next af\n");
- ret = score_get_best(&aft_row, &afd.score);
+ PARA_NOTICE_LOG("getting next audio file\n");
+ ret = score_get_best(&aft_row, &score);
if (ret < 0)
return ret;
- ret = open_and_update_audio_file(aft_row, &afd);
+ ret = open_and_update_audio_file(aft_row, &afd, score);
if (ret < 0)
return ret;
shmid = ret;
- PARA_NOTICE_LOG("shmid: %u\n", shmid);
if (!write_ok(server_socket)) {
PARA_EMERG_LOG("afs_socket not writable\n");
goto destroy;
return ret;
}
-static char *current_mop; /* mode or playlist specifier. NULL means dummy mooe */
-
/* Never fails if arg == NULL */
static int activate_mood_or_playlist(char *arg, int *num_admissible)
{
current_play_mode = mode;
if (arg != current_mop) {
free(current_mop);
- if (arg)
+ if (arg) {
current_mop = para_strdup(arg);
- else
+ mmd_lock();
+ strncpy(mmd->afs_mode_string, arg,
+ sizeof(mmd->afs_mode_string));
+ mmd->afs_mode_string[sizeof(mmd->afs_mode_string) - 1] = '\0';
+ mmd_unlock();
+ } else {
+ mmd_lock();
+ strcpy(mmd->afs_mode_string, "dummy");
+ mmd_unlock();
current_mop = NULL;
+ }
}
return 1;
}
ret = activate_mood_or_playlist(current_mop, &num_admissible);
if (ret < 0) {
para_printf(&pb, "failed, switching to dummy\n");
- change_current_mood(NULL); /* always successful */
+ activate_mood_or_playlist(NULL, &num_admissible);
}
}
para_printf(&pb, "activated %s (%d admissible files)\n", current_mop?
return ret;
}
-static void init_admissible_files(void)
+static void init_admissible_files(char *arg)
{
- int ret = 0;
- char *arg = conf.afs_initial_mode_arg;
- ret = activate_mood_or_playlist(arg, NULL);
- if (ret >= 0)
- return;
- PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
- PARA_NOTICE_LOG("defaulting to dummy mood\n");
- activate_mood_or_playlist(NULL, NULL); /* always successful */
+ if (activate_mood_or_playlist(arg, NULL) < 0)
+ activate_mood_or_playlist(NULL, NULL); /* always successful */
}
static int setup_command_socket_or_die(void)
{
- int ret;
+ int ret, socket_fd;
char *socket_name = conf.afs_socket_arg;
struct sockaddr_un unix_addr;
PARA_EMERG_LOG("%s: %s\n", PARA_STRERROR(-ret), socket_name);
exit(EXIT_FAILURE);
}
- if (listen(ret , 5) < 0) {
- PARA_EMERG_LOG("%s", "can not listen on socket\n");
+ socket_fd = ret;
+ if (listen(socket_fd , 5) < 0) {
+ PARA_EMERG_LOG("can not listen on socket\n");
exit(EXIT_FAILURE);
}
- PARA_INFO_LOG("listening on command socket %s (fd %d)\n", socket_name,
- ret);
- return ret;
+ ret = mark_fd_nonblock(socket_fd);
+ if (ret < 0) {
+ close(socket_fd);
+ return ret;
+ }
+ PARA_INFO_LOG("listening on socket %s (fd %d)\n", socket_name, ret);
+ return socket_fd;
}
static void close_afs_tables(void)
int i, ret;
get_database_dir();
+ PARA_NOTICE_LOG("opening %u osl tables in %s\n", NUM_AFS_TABLES,
+ database_dir);
for (i = 0; i < NUM_AFS_TABLES; i++) {
ret = afs_tables[i].open(database_dir);
if (ret >= 0)
continue;
PARA_ERROR_LOG("%s init: %s\n", afs_tables[i].name,
PARA_STRERROR(-ret));
+ break;
}
if (ret >= 0)
return ret;
- do
- afs_tables[i].close();
- while (i--);
+ while (i)
+ afs_tables[--i].close();
return ret;
}
static void signal_post_select(struct sched *s, struct task *t)
{
struct signal_task *st = t->private_data;
+ t->ret = -E_AFS_PARENT_DIED;
+ if (getppid() == 1)
+ goto err;
t->ret = 1;
if (!FD_ISSET(st->fd, &s->rfds))
return;
if (st->signum == SIGHUP) {
close_afs_tables();
t->ret = open_afs_tables();
+ if (t->ret < 0)
+ goto err;
+ init_admissible_files(current_mop);
return;
}
- PARA_NOTICE_LOG("caught signal %d\n", st->signum);
t->ret = -E_AFS_SIGNAL;
+err:
+ PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
unregister_tasks();
}
static struct list_head afs_client_list;
+/** Describes on connected afs client. */
struct afs_client {
+ /** Position in the afs client list. */
struct list_head node;
+ /** The socket file descriptor for this client. */
int fd;
+ /** The time the client connected. */
struct timeval connect_time;
};
return;
}
buf[ret] = '\0';
- PARA_NOTICE_LOG("received: %s\n", buf);
+ PARA_DEBUG_LOG("received: %s\n", buf);
if (!strcmp(buf, "new")) {
ret = open_next_audio_file();
- PARA_NOTICE_LOG("ret: %d\n", ret);
+ if (ret < 0)
+ PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
return;
}
PARA_ERROR_LOG("unknown command\n");
struct command_task *ct = t->private_data;
struct sockaddr_un unix_addr;
struct afs_client *client, *tmp;
-
+ int fd;
if (FD_ISSET(server_socket, &s->rfds))
execute_server_command();
PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
goto out;
}
+ fd = t->ret;
+ t->ret = mark_fd_nonblock(fd);
+ if (t->ret < 0) {
+ PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
+ close(fd);
+ goto out;
+ }
client = para_malloc(sizeof(*client));
- client->fd = t->ret;
+ client->fd = fd;
client->connect_time = *now;
para_list_add(&client->node, &afs_client_list);
out:
exit(EXIT_FAILURE);
PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n",
server_socket, (unsigned) cookie);
- init_admissible_files();
+ init_admissible_files(conf.afs_initial_mode_arg);
register_tasks(cookie);
s.default_timeout.tv_sec = 0;
- s.default_timeout.tv_usec = 99 * 1000;
+ s.default_timeout.tv_usec = 999 * 1000;
ret = sched(&s);
if (ret < 0)
PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));