X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=afs.c;h=018fc5dcd23f51da96291504b75e16f80fd46aff;hp=c2ac0e4110e15ceac8f677947894e2a1afd057b4;hb=9ae0041ae0dd073657862c4abdf3886a66035ee9;hpb=b3ee0fa979a74e9d7a1497ec5a3ac8fa7cc7472b diff --git a/afs.c b/afs.c index c2ac0e41..018fc5dc 100644 --- a/afs.c +++ b/afs.c @@ -164,7 +164,6 @@ int send_callback_request(callback_function *f, struct osl_object *query, int ret, fd = -1, query_shmid, result_shmid; void *query_shm, *result_shm; char buf[sizeof(afs_socket_cookie) + sizeof(int)]; - struct sockaddr_un unix_addr; size_t query_shm_size = sizeof(*cq); if (query) @@ -189,16 +188,10 @@ int send_callback_request(callback_function *f, struct osl_object *query, *(uint32_t *) buf = afs_socket_cookie; *(int *) (buf + sizeof(afs_socket_cookie)) = query_shmid; - ret = get_stream_socket(PF_UNIX); + ret = create_remote_socket(conf.afs_socket_arg); if (ret < 0) goto out; fd = ret; - ret = init_unix_addr(&unix_addr, conf.afs_socket_arg); - if (ret < 0) - goto out; - ret = PARA_CONNECT(fd, &unix_addr); - if (ret < 0) - goto out; ret = send_bin_buffer(fd, buf, sizeof(buf)); if (ret < 0) goto out; @@ -206,7 +199,7 @@ int send_callback_request(callback_function *f, struct osl_object *query, if (ret < 0) goto out; if (ret != sizeof(int)) { - ret = -E_RECV; + ret = -E_AFS_SHORT_READ; goto out; } ret = *(int *) buf; @@ -486,17 +479,27 @@ int open_next_audio_file(void) struct audio_file_data afd; int ret, shmid; char buf[8]; - + long score; +again: PARA_NOTICE_LOG("getting next audio file\n"); - ret = score_get_best(&aft_row, &afd.score); - if (ret < 0) - return ret; - ret = open_and_update_audio_file(aft_row, &afd); - if (ret < 0) - return ret; + ret = score_get_best(&aft_row, &score); + if (ret < 0) { + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); + goto no_admissible_files; + } + ret = open_and_update_audio_file(aft_row, score, &afd); + if (ret < 0) { + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); + ret = score_delete(aft_row); + if (ret < 0) { + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); + goto no_admissible_files; + } + goto again; + } shmid = ret; if (!write_ok(server_socket)) { - PARA_EMERG_LOG("afs_socket not writable\n"); + ret = -E_AFS_SOCKET; goto destroy; } *(uint32_t *)buf = NEXT_AUDIO_FILE; @@ -505,10 +508,13 @@ int open_next_audio_file(void) close(afd.fd); if (ret >= 0) return ret; - PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); destroy: shm_destroy(shmid); return ret; +no_admissible_files: + *(uint32_t *)buf = NO_ADMISSIBLE_FILES; + *(uint32_t *)(buf + 4) = (uint32_t)0; + return send_bin_buffer(server_socket, buf, 8); } /* Never fails if arg == NULL */ @@ -521,14 +527,14 @@ static int activate_mood_or_playlist(char *arg, int *num_admissible) ret = change_current_mood(NULL); /* always successful */ mode = PLAY_MODE_MOOD; } else { - if (!strncmp(arg, "p:", 2)) { + if (!strncmp(arg, "p/", 2)) { ret = playlist_open(arg + 2); mode = PLAY_MODE_PLAYLIST; - } else if (!strncmp(arg, "m:", 2)) { + } else if (!strncmp(arg, "m/", 2)) { ret = change_current_mood(arg + 2); mode = PLAY_MODE_MOOD; } else - ret = -E_AFS_SYNTAX; + return -E_AFS_SYNTAX; if (ret < 0) return ret; } @@ -540,7 +546,9 @@ static int activate_mood_or_playlist(char *arg, int *num_admissible) if (arg) { current_mop = para_strdup(arg); mmd_lock(); - strcpy(mmd->afs_mode_string, arg); /* FIXME: check length */ + 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(); @@ -568,7 +576,7 @@ static int com_select_callback(const struct osl_object *query, playlist_close(); ret = activate_mood_or_playlist(arg, &num_admissible); if (ret < 0) { - para_printf(&pb, "%s\n", PARA_STRERROR(-ret)); + para_printf(&pb, "%s\n", para_strerror(-ret)); para_printf(&pb, "switching back to %s\n", current_mop? current_mop : "dummy"); ret = activate_mood_or_playlist(current_mop, &num_admissible); @@ -610,7 +618,7 @@ static void init_admissible_files(char *arg) 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; @@ -618,16 +626,21 @@ static int setup_command_socket_or_die(void) ret = create_local_socket(socket_name, &unix_addr, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH); if (ret < 0) { - PARA_EMERG_LOG("%s: %s\n", PARA_STRERROR(-ret), socket_name); + 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_nonblocking(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) @@ -678,7 +691,7 @@ static int open_afs_tables(void) if (ret >= 0) continue; PARA_ERROR_LOG("%s init: %s\n", afs_tables[i].name, - PARA_STRERROR(-ret)); + para_strerror(-ret)); break; } if (ret >= 0) @@ -724,7 +737,7 @@ static void signal_post_select(struct sched *s, struct task *t) } t->ret = -E_AFS_SIGNAL; err: - PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret)); + PARA_NOTICE_LOG("%s\n", para_strerror(-t->ret)); unregister_tasks(); } @@ -747,9 +760,13 @@ static void register_signal_task(void) 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; }; @@ -827,15 +844,17 @@ static void execute_server_command(void) if (ret <= 0) { if (ret < 0) - PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); return; } buf[ret] = '\0'; PARA_DEBUG_LOG("received: %s\n", buf); if (!strcmp(buf, "new")) { ret = open_next_audio_file(); - if (ret < 0) - PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); + if (ret < 0) { + PARA_EMERG_LOG("%s\n", para_strerror(-ret)); + unregister_tasks(); + } return; } PARA_ERROR_LOG("unknown command\n"); @@ -850,7 +869,7 @@ static void execute_afs_command(int fd, uint32_t expected_cookie) int ret = recv_bin_buffer(fd, buf, sizeof(buf)); if (ret < 0) { - PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret)); + PARA_NOTICE_LOG("%s\n", para_strerror(-ret)); return; } if (ret != sizeof(buf)) { @@ -882,7 +901,7 @@ 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; - + int fd; if (FD_ISSET(server_socket, &s->rfds)) execute_server_command(); @@ -906,11 +925,18 @@ static void command_post_select(struct sched *s, struct task *t) 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)); + PARA_NOTICE_LOG("%s\n", para_strerror(-t->ret)); + goto out; + } + fd = t->ret; + t->ret = mark_fd_nonblocking(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: @@ -953,11 +979,11 @@ __noreturn void afs_init(uint32_t cookie, int socket_fd) ret = open_afs_tables(); if (ret < 0) { - PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret)); + PARA_EMERG_LOG("%s\n", para_strerror(-ret)); exit(EXIT_FAILURE); } server_socket = socket_fd; - ret = mark_fd_nonblock(server_socket); + ret = mark_fd_nonblocking(server_socket); if (ret < 0) exit(EXIT_FAILURE); PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n", @@ -966,9 +992,9 @@ __noreturn void afs_init(uint32_t cookie, int socket_fd) register_tasks(cookie); s.default_timeout.tv_sec = 0; s.default_timeout.tv_usec = 999 * 1000; - ret = sched(&s); + ret = schedule(&s); if (ret < 0) - PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret)); + PARA_EMERG_LOG("%s\n", para_strerror(-ret)); close_afs_tables(); exit(EXIT_FAILURE); } @@ -1119,7 +1145,7 @@ void afs_event(enum afs_events event, struct para_buffer *pb, continue; ret = t->event_handler(event, pb, data); if (ret < 0) - PARA_CRIT_LOG("%s\n", PARA_STRERROR(-ret)); + PARA_CRIT_LOG("%s\n", para_strerror(-ret)); } }