- gettimeofday(&now, NULL);
- tv_add(&af->eof_tv, &now, &eof_barrier);
- af->close_audio_file();
- audio_file = NULL;
- mmd->audio_format = -1;
- af = NULL;
- mmd->chunks_sent = 0;
- mmd->offset = 0;
- mmd->seconds_total = 0;
- tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
- status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
- strcpy(mmd->audio_file_info, tmp);
- free(tmp);
- tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
- status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
- strcpy(mmd->dbinfo, tmp);
- free(tmp);
- mmd->filename[0] = '\0';
- mmd->size = 0;
- mmd->events++;
+ PARA_ERROR_LOG("unknown command\n");
+
+}
+
+static void execute_afs_command(int fd, uint32_t expected_cookie)
+{
+ uint32_t cookie;
+ int query_shmid;
+ char buf[sizeof(cookie) + sizeof(query_shmid)];
+ int ret = recv_bin_buffer(fd, buf, sizeof(buf));
+
+ if (ret < 0) {
+ PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
+ return;
+ }
+ if (ret != sizeof(buf)) {
+ PARA_NOTICE_LOG("short read (%d bytes, expected %lu)\n",
+ ret, (long unsigned) sizeof(buf));
+ return;
+ }
+ cookie = *(uint32_t *)buf;
+ if (cookie != expected_cookie) {
+ PARA_NOTICE_LOG("received invalid cookie(got %u, expected %u)\n",
+ (unsigned)cookie, (unsigned)expected_cookie);
+ return;
+ }
+ query_shmid = *(int *)(buf + sizeof(cookie));
+ if (query_shmid < 0) {
+ PARA_WARNING_LOG("received invalid query shmid %d)\n",
+ query_shmid);
+ return;
+ }
+ /* Ignore return value: Errors might be OK here. */
+ call_callback(fd, query_shmid);
+}
+
+/** Shutdown connection if query has not arrived until this many seconds. */
+#define AFS_CLIENT_TIMEOUT 3
+
+static void command_post_select(struct sched *s, struct task *t)
+{
+ struct command_task *ct = t->private_data;
+ struct sockaddr_un unix_addr;
+ struct afs_client *client, *tmp;
+
+ if (FD_ISSET(server_socket, &s->rfds))
+ execute_server_command();
+
+ /* Check the list of connected clients. */
+ list_for_each_entry_safe(client, tmp, &afs_client_list, node) {
+ if (FD_ISSET(client->fd, &s->rfds))
+ execute_afs_command(client->fd, ct->cookie);
+ else { /* prevent bogus connection flooding */
+ struct timeval diff;
+ tv_diff(now, &client->connect_time, &diff);
+ if (diff.tv_sec < AFS_CLIENT_TIMEOUT)
+ continue;
+ PARA_WARNING_LOG("connection timeout\n");
+ }
+ close(client->fd);
+ list_del(&client->node);
+ free(client);
+ }
+ /* Accept connections on the local socket. */
+ if (!FD_ISSET(ct->fd, &s->rfds))
+ goto out;
+ t->ret = para_accept(ct->fd, &unix_addr, sizeof(unix_addr));
+ if (t->ret < 0) {
+ PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
+ goto out;
+ }
+ client = para_malloc(sizeof(*client));
+ client->fd = t->ret;
+ client->connect_time = *now;
+ para_list_add(&client->node, &afs_client_list);
+out:
+ t->ret = 1;
+}
+
+static void register_command_task(uint32_t cookie)
+{
+ struct command_task *ct = &command_task_struct;
+ ct->fd = setup_command_socket_or_die();
+ ct->cookie = cookie;
+
+ ct->task.pre_select = command_pre_select;
+ ct->task.post_select = command_post_select;
+ ct->task.private_data = ct;
+ sprintf(ct->task.status, "command task");
+ register_task(&ct->task);
+}
+
+static void register_tasks(uint32_t cookie)
+{
+ register_signal_task();
+ register_command_task(cookie);