Fix afs_shutdown().
[paraslash.git] / afs.c
1 /*
2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file afs.c Paraslash's audio file selector. */
8
9 #include "server.cmdline.h"
10 #include "para.h"
11 #include "afh.h"
12 #include "server.h"
13 #include "error.h"
14 #include <dirent.h> /* readdir() */
15 #include <sys/mman.h>
16 #include <sys/time.h>
17 #include "net.h"
18 #include "afs.h"
19 #include "ipc.h"
20 #include "string.h"
21 #include "list.h"
22 #include "sched.h"
23 #include "signal.h"
24 #include "fd.h"
25
26 /** The osl tables used by afs. \sa blob.c. */
27 enum afs_table_num {
28 /** Contains audio file information. See aft.c. */
29 TBLNUM_AUDIO_FILES,
30 /** The table for the paraslash attributes. See attribute.c. */
31 TBLNUM_ATTRIBUTES,
32 /**
33 * Paraslash's scoring system is based on Gaussian normal
34 * distributions, and the relevant data is stored in the rbtrees of an
35 * osl table containing only volatile columns. See score.c for
36 * details.
37 */
38 TBLNUM_SCORES,
39 /**
40 * A standard blob table containing the mood definitions. For details
41 * see mood.c.
42 */
43 TBLNUM_MOODS,
44 /** A blob table containing lyrics on a per-song basis. */
45 TBLNUM_LYRICS,
46 /** Another blob table for images (for example album cover art). */
47 TBLNUM_IMAGES,
48 /** Yet another blob table for storing standard playlists. */
49 TBLNUM_PLAYLIST,
50 /** How many tables are in use? */
51 NUM_AFS_TABLES
52 };
53
54 static struct table_info afs_tables[NUM_AFS_TABLES];
55
56 struct command_task {
57 /** The file descriptor for the local socket. */
58 int fd;
59 /**
60 * Value sent by the command handlers to identify themselves as
61 * children of the running para_server.
62 */
63 uint32_t cookie;
64 /** The associated task structure. */
65 struct task task;
66 };
67
68 /**
69 * A random number used to "authenticate" the connection.
70 *
71 * para_server picks this number by random before forking the afs process. The
72 * command handlers write this number together with the id of the shared memory
73 * area containing the query. This way, a malicious local user has to know this
74 * number to be able to cause the afs process to crash by sending fake queries.
75 */
76 extern uint32_t afs_socket_cookie;
77
78 /**
79 * Struct to let command handlers execute a callback in afs context.
80 *
81 * Commands that need to change the state of afs can't change the relevant data
82 * structures directly because commands are executed in a child process, i.e.
83 * they get their own virtual address space.
84 *
85 * This structure is used by \p send_callback_request() (executed from handler
86 * context) in order to let the afs process call the specified function. An
87 * instance of that structure is written to a shared memory area together with
88 * the arguments to the callback function. The identifier of the shared memory
89 * area is written to the command socket.
90 *
91 * The afs process accepts connections on the command socket and reads the
92 * shared memory id, attaches the corresponing area, calls the given handler to
93 * perform the desired action and to optionally compute a result.
94 *
95 * The result and a \p callback_result structure is then written to another
96 * shared memory area. The identifier for that area is written to the handler's
97 * command socket, so that the handler process can read the id, attach the
98 * shared memory area and use the result.
99 *
100 * \sa struct callback_result.
101 */
102 struct callback_query {
103 /** The function to be called. */
104 callback_function *handler;
105 /** The number of bytes of the query */
106 size_t query_size;
107 };
108
109 /**
110 * Structure embedded in the result of a callback.
111 *
112 * If the callback produced a result, an instance of that structure is embeeded
113 * into the shared memory area holding the result, mainly to let the command
114 * handler know the size of the result.
115 *
116 * \sa struct callback_query.
117 */
118 struct callback_result {
119 /** The number of bytes of the result. */
120 size_t result_size;
121 };
122
123 /**
124 * Ask the parent process to call a given function.
125 *
126 * \param f The function to be called.
127 * \param query Pointer to arbitrary data for the callback.
128 * \param result Callback result will be stored here.
129 *
130 * This function creates a shared memory area, copies the buffer pointed to by
131 * \a buf to that area and notifies the afs process that \a f should be
132 * called ASAP.
133 *
134 * \return Negative, on errors, the return value of the callback function
135 * otherwise.
136 *
137 * \sa send_option_arg_callback_request(), send_standard_callback_request().
138 */
139 int send_callback_request(callback_function *f, struct osl_object *query,
140 struct osl_object *result)
141 {
142 struct callback_query *cq;
143 struct callback_result *cr;
144 int ret, fd = -1, query_shmid, result_shmid;
145 void *query_shm, *result_shm;
146 char buf[sizeof(afs_socket_cookie) + sizeof(int)];
147 // char *tmpsocket_name;
148 struct sockaddr_un unix_addr;
149
150 assert(query->data && query->size);
151 ret = shm_new(query->size + sizeof(*cq));
152 if (ret < 0)
153 return ret;
154 query_shmid = ret;
155 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
156 if (ret < 0)
157 goto out;
158 cq = query_shm;
159 cq->handler = f;
160 cq->query_size = query->size;
161
162 memcpy(query_shm + sizeof(*cq), query->data, query->size);
163 ret = shm_detach(query_shm);
164 if (ret < 0)
165 goto out;
166
167 *(uint32_t *) buf = afs_socket_cookie;
168 *(int *) (buf + sizeof(afs_socket_cookie)) = query_shmid;
169
170 ret = get_stream_socket(PF_UNIX);
171 if (ret < 0)
172 goto out;
173 fd = ret;
174 ret = init_unix_addr(&unix_addr, conf.afs_socket_arg);
175 if (ret < 0)
176 goto out;
177 ret = -E_CONNECT;
178 if (connect(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) < 0) /* FIXME: Use para_connect() */
179 goto out;
180 ret = send_bin_buffer(fd, buf, sizeof(buf));
181 PARA_NOTICE_LOG("bin buffer ret: %d\n", ret);
182 if (ret < 0)
183 goto out;
184 ret = recv_bin_buffer(fd, buf, sizeof(buf));
185 PARA_NOTICE_LOG("ret: %d\n", ret);
186 if (ret < 0)
187 goto out;
188 if (ret != sizeof(int)) {
189 ret = -E_RECV;
190 goto out;
191 }
192 ret = *(int *) buf;
193 PARA_NOTICE_LOG("result_shmid: %d\n", ret);
194 if (ret <= 0)
195 goto out;
196 result_shmid = ret;
197 ret = shm_attach(result_shmid, ATTACH_RO, &result_shm);
198 if (ret >= 0) {
199 assert(result);
200 cr = result_shm;
201 result->size = cr->result_size;
202 result->data = para_malloc(result->size);
203 memcpy(result->data, result_shm + sizeof(*cr), result->size);
204 ret = shm_detach(result_shm);
205 if (ret < 0)
206 PARA_ERROR_LOG("can not detach result\n");
207 } else
208 PARA_ERROR_LOG("attach result failed: %d\n", ret);
209 if (shm_destroy(result_shmid) < 0)
210 PARA_ERROR_LOG("destroy result failed\n");
211 ret = 1;
212 out:
213 if (shm_destroy(query_shmid) < 0)
214 PARA_ERROR_LOG("%s\n", "shm destroy error");
215 if (fd >= 0)
216 close(fd);
217 PARA_DEBUG_LOG("callback_ret: %d\n", ret);
218 return ret;
219 }
220
221 /**
222 * Send a callback request passing an options structure and an argument vector.
223 *
224 * \param options pointer to an arbitrary data structure.
225 * \param argc Argument count.
226 * \param argv Standard argument vector.
227 * \param f The callback function.
228 * \param result The result of the query is stored here.
229 *
230 * Some commands have a couple of options that are parsed in child context for
231 * syntactic correctness and are stored in a special options structure for that
232 * command. This function allows to pass such a structure together with a list
233 * of further arguments (often a list of audio files) to the parent process.
234 *
235 * \sa send_standard_callback_request(), send_callback_request().
236 */
237 int send_option_arg_callback_request(struct osl_object *options,
238 int argc, char * const * const argv, callback_function *f,
239 struct osl_object *result)
240 {
241 char *p;
242 int i, ret;
243 struct osl_object query = {.size = options? options->size : 0};
244
245 for (i = 0; i < argc; i++)
246 query.size += strlen(argv[i]) + 1;
247 query.data = para_malloc(query.size);
248 p = query.data;
249 if (options) {
250 memcpy(query.data, options->data, options->size);
251 p += options->size;
252 }
253 for (i = 0; i < argc; i++) {
254 strcpy(p, argv[i]); /* OK */
255 p += strlen(argv[i]) + 1;
256 }
257 ret = send_callback_request(f, &query, result);
258 free(query.data);
259 return ret;
260 }
261
262 /**
263 * Send a callback request with an argument vector only.
264 *
265 * \param argc The same meaning as in send_option_arg_callback_request().
266 * \param argv The same meaning as in send_option_arg_callback_request().
267 * \param f The same meaning as in send_option_arg_callback_request().
268 * \param result The same meaning as in send_option_arg_callback_request().
269 *
270 * This is similar to send_option_arg_callback_request(), but no options buffer
271 * is passed to the parent process.
272 *
273 * \return The return value of the underlying call to
274 * send_option_arg_callback_request().
275 */
276 int send_standard_callback_request(int argc, char * const * const argv,
277 callback_function *f, struct osl_object *result)
278 {
279 return send_option_arg_callback_request(NULL, argc, argv, f, result);
280 }
281
282 /**
283 * Compare two osl objects of string type.
284 *
285 * \param obj1 Pointer to the first object.
286 * \param obj2 Pointer to the second object.
287 *
288 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
289 * are taken into account.
290 *
291 * \return It returns an integer less than, equal to, or greater than zero if
292 * \a obj1 is found, respectively, to be less than, to match, or be greater than
293 * obj2.
294 *
295 * \sa strcmp(3), strncmp(3), osl_compare_func.
296 */
297 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
298 {
299 const char *str1 = (const char *)obj1->data;
300 const char *str2 = (const char *)obj2->data;
301 return strncmp(str1, str2, PARA_MIN(obj1->size, obj2->size));
302 }
303
304 /**
305 * A wrapper for strtol(3).
306 *
307 * \param str The string to be converted to a long integer.
308 * \param result The converted value is stored here.
309 *
310 * \return Positive on success, -E_ATOL on errors.
311 *
312 * \sa strtol(3), atoi(3).
313 */
314 int para_atol(const char *str, long *result)
315 {
316 char *endptr;
317 long val;
318 int ret, base = 10;
319
320 errno = 0; /* To distinguish success/failure after call */
321 val = strtol(str, &endptr, base);
322 ret = -E_ATOL;
323 if (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
324 goto out; /* overflow */
325 if (errno != 0 && val == 0)
326 goto out; /* other error */
327 if (endptr == str)
328 goto out; /* No digits were found */
329 if (*endptr != '\0')
330 goto out; /* Further characters after number */
331 *result = val;
332 ret = 1;
333 out:
334 return ret;
335 }
336
337
338 /*
339 * write input from fd to dynamically allocated char array,
340 * but maximal max_size byte. Return size.
341 */
342 static int fd2buf(int fd, char **buf, int max_size)
343 {
344 const size_t chunk_size = 1024;
345 size_t size = 2048;
346 char *p;
347 int ret;
348
349 *buf = para_malloc(size * sizeof(char));
350 p = *buf;
351 while ((ret = read(fd, p, chunk_size)) > 0) {
352 p += ret;
353 if ((p - *buf) + chunk_size >= size) {
354 char *tmp;
355
356 size *= 2;
357 if (size > max_size) {
358 ret = -E_INPUT_TOO_LARGE;
359 goto out;
360 }
361 tmp = para_realloc(*buf, size);
362 p = (p - *buf) + tmp;
363 *buf = tmp;
364 }
365 }
366 if (ret < 0) {
367 ret = -E_READ;
368 goto out;
369 }
370 ret = p - *buf;
371 out:
372 if (ret < 0 && *buf)
373 free(*buf);
374 return ret;
375 }
376
377 /**
378 * Read from stdin, and send the result to the parent process.
379 *
380 * \param arg_obj Pointer to the arguments to \a f.
381 * \param f The callback function.
382 * \param max_len Don't read more than that many bytes from stdin.
383 * \param result The result of the query is stored here.
384 *
385 * This function is used by commands that wish to let para_server store
386 * arbitrary data specified by the user (for instance the add_blob family of
387 * commands). First, at most \a max_len bytes are read from stdin, the result
388 * is concatenated with the buffer given by \a arg_obj, and the combined buffer
389 * is made available to the parent process via shared memory.
390 *
391 * \return Negative on errors, the return value of the underlying call to
392 * send_callback_request() otherwise.
393 */
394 int stdin_command(struct osl_object *arg_obj, callback_function *f,
395 unsigned max_len, struct osl_object *result)
396 {
397 char *stdin_buf;
398 size_t stdin_len;
399 struct osl_object query;
400 int ret = fd2buf(STDIN_FILENO, &stdin_buf, max_len);
401
402 if (ret < 0)
403 return ret;
404 stdin_len = ret;
405 query.size = arg_obj->size + stdin_len;
406 query.data = para_malloc(query.size);
407 memcpy(query.data, arg_obj->data, arg_obj->size);
408 memcpy((char *)query.data + arg_obj->size, stdin_buf, stdin_len);
409 free(stdin_buf);
410 ret = send_callback_request(f, &query, result);
411 free(query.data);
412 return ret;
413 }
414
415 /**
416 * Open the audio file with highest score.
417 *
418 * \param afd Audio file data is returned here.
419 *
420 * This stores all information for streaming the "best" audio file
421 * in the \a afd structure.
422 *
423 * \return Positive on success, negative on errors.
424 *
425 * \sa close_audio_file(), open_and_update_audio_file().
426 */
427 int open_next_audio_file(struct audio_file_data *afd)
428 {
429 struct osl_row *aft_row;
430 int ret;
431 for (;;) {
432 ret = score_get_best(&aft_row, &afd->score);
433 if (ret < 0)
434 return ret;
435 ret = open_and_update_audio_file(aft_row, afd);
436 if (ret >= 0)
437 return ret;
438 }
439 }
440
441 /**
442 * Free all resources which were allocated by open_next_audio_file().
443 *
444 * \param afd The structure previously filled in by open_next_audio_file().
445 *
446 * \return The return value of the underlying call to para_munmap().
447 *
448 * \sa open_next_audio_file().
449 */
450 int close_audio_file(struct audio_file_data *afd)
451 {
452 free(afd->afhi.chunk_table);
453 return para_munmap(afd->map.data, afd->map.size);
454 }
455
456 #if 0
457 static void play_loop(enum play_mode current_play_mode)
458 {
459 int i, ret;
460 struct audio_file_data afd;
461
462 afd.current_play_mode = current_play_mode;
463 for (i = 0; i < 0; i++) {
464 ret = open_next_audio_file(&afd);
465 if (ret < 0) {
466 PARA_ERROR_LOG("failed to open next audio file: %d\n", ret);
467 return;
468 }
469 PARA_NOTICE_LOG("next audio file: %s, score: %li\n", afd.path, afd.score);
470 sleep(1);
471 close_audio_file(&afd);
472 }
473 }
474 #endif
475
476
477 static enum play_mode init_admissible_files(void)
478 {
479 int ret;
480 char *given_mood, *given_playlist;
481
482 given_mood = "mood_that_was_given_at_the_command_line";
483 given_playlist = "given_playlist";
484
485 if (given_mood) {
486 ret = mood_open(given_mood);
487 if (ret >= 0) {
488 if (given_playlist)
489 PARA_WARNING_LOG("ignoring playlist %s\n",
490 given_playlist);
491 return PLAY_MODE_MOOD;
492 }
493 }
494 if (given_playlist) {
495 ret = playlist_open(given_playlist);
496 if (ret >= 0)
497 return PLAY_MODE_PLAYLIST;
498 }
499 ret = mood_open(NULL); /* open first available mood */
500 if (ret >= 0)
501 return PLAY_MODE_MOOD;
502 mood_open(""); /* open dummy mood, always successful */
503 return PLAY_MODE_MOOD;
504 }
505
506 static int setup_command_socket_or_die(void)
507 {
508 int ret;
509 char *socket_name = conf.afs_socket_arg;
510 struct sockaddr_un unix_addr;
511
512 unlink(socket_name);
513 ret = create_local_socket(socket_name, &unix_addr,
514 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
515 if (ret < 0)
516 exit(EXIT_FAILURE);
517 if (listen(ret , 5) < 0) {
518 PARA_EMERG_LOG("%s", "can not listen on socket\n");
519 exit(EXIT_FAILURE);
520 }
521 PARA_INFO_LOG("listening on command socket %s (fd %d)\n", socket_name,
522 ret);
523 return ret;
524 }
525
526 static int server_socket;
527 static struct command_task command_task_struct;
528 static struct signal_task signal_task_struct;
529
530 static void unregister_tasks(void)
531 {
532 unregister_task(&command_task_struct.task);
533 unregister_task(&signal_task_struct.task);
534 }
535
536 static void afs_shutdown(enum osl_close_flags flags)
537 {
538 PARA_NOTICE_LOG("cleaning up\n");
539 score_shutdown(flags);
540 attribute_shutdown(flags);
541 mood_close();
542 playlist_close();
543 moods_shutdown(flags);
544 playlists_shutdown(flags);
545 lyrics_shutdown(flags);
546 images_shutdown(flags);
547 aft_shutdown(flags);
548 }
549
550 static void signal_pre_select(struct sched *s, struct task *t)
551 {
552 struct signal_task *st = t->private_data;
553 t->ret = 1;
554 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
555 }
556
557 static void signal_post_select(struct sched *s, struct task *t)
558 {
559 struct signal_task *st = t->private_data;
560 t->ret = 1;
561 if (!FD_ISSET(st->fd, &s->rfds))
562 return;
563 st->signum = para_next_signal();
564 PARA_NOTICE_LOG("caught signal %d\n", st->signum);
565 t->ret = 1;
566 if (st->signum == SIGUSR1)
567 return; /* ignore SIGUSR1 */
568 t->ret = -E_SIGNAL_CAUGHT;
569 unregister_tasks();
570 }
571
572 static void register_signal_task(void)
573 {
574 struct signal_task *st = &signal_task_struct;
575 st->fd = para_signal_init();
576 PARA_INFO_LOG("signal pipe: fd %d\n", st->fd);
577 para_install_sighandler(SIGINT);
578 para_install_sighandler(SIGTERM);
579 para_install_sighandler(SIGPIPE);
580
581 st->task.pre_select = signal_pre_select;
582 st->task.post_select = signal_post_select;
583 st->task.private_data = st;
584 sprintf(st->task.status, "signal task");
585 register_task(&st->task);
586 }
587
588 static void command_pre_select(struct sched *s, struct task *t)
589 {
590 struct command_task *ct = t->private_data;
591 t->ret = 1;
592 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
593 }
594
595 /*
596 * On errors, negative value is written to fd.
597 * On success: If query produced a result, the result_shmid is written to fd.
598 * Otherwise, zero is written.
599 */
600 static int call_callback(int fd, int query_shmid)
601 {
602 void *query_shm, *result_shm;
603 struct callback_query *cq;
604 struct callback_result *cr;
605 struct osl_object query, result = {.data = NULL};
606 int result_shmid = -1, ret, ret2;
607
608 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
609 if (ret < 0)
610 goto out;
611 cq = query_shm;
612 query.data = (char *)query_shm + sizeof(*cq);
613 query.size = cq->query_size;
614 ret = cq->handler(&query, &result);
615 ret2 = shm_detach(query_shm);
616 if (ret2 < 0 && ret >= 0)
617 ret = ret2;
618 if (ret < 0)
619 goto out;
620 ret = 0;
621 if (!result.data || !result.size)
622 goto out;
623 ret = shm_new(result.size + sizeof(struct callback_result));
624 if (ret < 0)
625 goto out;
626 result_shmid = ret;
627 ret = shm_attach(result_shmid, ATTACH_RW, &result_shm);
628 if (ret < 0)
629 goto out;
630 cr = result_shm;
631 cr->result_size = result.size;
632 memcpy(result_shm + sizeof(*cr), result.data, result.size);
633 ret = shm_detach(result_shm);
634 if (ret < 0)
635 goto out;
636 ret = result_shmid;
637 out:
638 free(result.data);
639 ret2 = send_bin_buffer(fd, (char *)&ret, sizeof(int));
640 if (ret < 0 || ret2 < 0) {
641 if (result_shmid >= 0)
642 if (shm_destroy(result_shmid) < 0)
643 PARA_ERROR_LOG("destroy result failed\n");
644 if (ret >= 0)
645 ret = ret2;
646 }
647 return ret;
648 }
649
650 static void command_post_select(struct sched *s, struct task *t)
651 {
652 struct command_task *ct = t->private_data;
653 struct sockaddr_un unix_addr;
654 char buf[sizeof(uint32_t) + sizeof(int)];
655 uint32_t cookie;
656 int query_shmid, fd;
657
658 t->ret = 1;
659 if (!FD_ISSET(ct->fd, &s->rfds))
660 return;
661 t->ret = para_accept(ct->fd, &unix_addr, sizeof(unix_addr));
662 if (t->ret < 0)
663 return;
664 /*
665 * The following errors may be caused by a malicious local user. So do
666 * not return an error in this case as this would terminate para_afs
667 * and para_server.
668 */
669 fd = t->ret;
670 /* FIXME: This is easily dosable (peer doesn't send data) */
671 t->ret = recv_bin_buffer(fd, buf, sizeof(buf));
672 if (t->ret < 0) {
673 PARA_NOTICE_LOG("%s (%d)\n", PARA_STRERROR(-t->ret), t->ret);
674 t->ret = 1;
675 goto out;
676 }
677 if (t->ret != sizeof(buf)) {
678 PARA_NOTICE_LOG("short read (%d bytes, expected %u)\n",
679 t->ret, sizeof(buf));
680 t->ret = 1;
681 goto out;
682 }
683 cookie = *(uint32_t *)buf;
684 if (cookie != ct->cookie) {
685 PARA_NOTICE_LOG("received invalid cookie(got %u, expected %u)\n",
686 (unsigned)cookie, (unsigned)ct->cookie);
687 t->ret = 1;
688 goto out;
689 }
690 query_shmid = *(int *)(buf + sizeof(cookie));
691 if (query_shmid < 0) {
692 PARA_WARNING_LOG("received invalid query shmid %d)\n",
693 query_shmid);
694 t->ret = 1;
695 goto out;
696 }
697 t->ret = call_callback(fd, query_shmid);
698 if (t->ret < 0) {
699 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
700 t->ret = 1;
701 goto out;
702 }
703 out:
704 close(fd);
705 }
706
707 static void register_command_task(uint32_t cookie)
708 {
709 struct command_task *ct = &command_task_struct;
710 ct->fd = setup_command_socket_or_die();
711 ct->cookie = cookie;
712
713 ct->task.pre_select = command_pre_select;
714 ct->task.post_select = command_post_select;
715 ct->task.private_data = ct;
716 sprintf(ct->task.status, "command task");
717 register_task(&ct->task);
718 }
719
720 void register_tasks(uint32_t cookie)
721 {
722 register_signal_task();
723 register_command_task(cookie);
724 }
725
726 static int open_afs_tables(void)
727 {
728 int ret;
729 char *db;
730
731 if (conf.afs_database_dir_given)
732 db = conf.afs_database_dir_arg;
733 else {
734 char *home = para_homedir();
735 db = make_message("%s/.paraslash/afs_database", home);
736 free(home);
737 }
738 PARA_INFO_LOG("afs_database dir %s\n", db);
739 ret = para_mkdir(db, 0777);
740 if (ret < 0 && ret != -E_EXIST)
741 goto err;
742 ret = attribute_init(&afs_tables[TBLNUM_ATTRIBUTES], db);
743 if (ret < 0)
744 goto err;
745 ret = moods_init(&afs_tables[TBLNUM_MOODS], db);
746 if (ret < 0)
747 goto moods_init_error;
748 ret = playlists_init(&afs_tables[TBLNUM_PLAYLIST], db);
749 if (ret < 0)
750 goto playlists_init_error;
751 ret = lyrics_init(&afs_tables[TBLNUM_LYRICS], db);
752 if (ret < 0)
753 goto lyrics_init_error;
754 ret = images_init(&afs_tables[TBLNUM_IMAGES], db);
755 if (ret < 0)
756 goto images_init_error;
757 ret = score_init(&afs_tables[TBLNUM_SCORES], db);
758 if (ret < 0)
759 goto score_init_error;
760 ret = aft_init(&afs_tables[TBLNUM_AUDIO_FILES], db);
761 if (ret < 0)
762 goto aft_init_error;
763 if (!conf.afs_database_dir_given)
764 free(db);
765 return 1;
766
767 aft_init_error:
768 score_shutdown(OSL_MARK_CLEAN);
769 score_init_error:
770 images_shutdown(OSL_MARK_CLEAN);
771 images_init_error:
772 lyrics_shutdown(OSL_MARK_CLEAN);
773 lyrics_init_error:
774 playlists_shutdown(OSL_MARK_CLEAN);
775 playlists_init_error:
776 moods_shutdown(OSL_MARK_CLEAN);
777 moods_init_error:
778 attribute_shutdown(OSL_MARK_CLEAN);
779 err:
780 if (!conf.afs_database_dir_given)
781 free(db);
782 return ret;
783 }
784
785 __noreturn int afs_init(uint32_t cookie, int socket_fd)
786 {
787 enum play_mode current_play_mode;
788 struct sched s;
789 int ret = open_afs_tables();
790
791 if (ret < 0) {
792 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
793 exit(EXIT_FAILURE);
794 }
795 server_socket = socket_fd;
796 PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n",
797 server_socket, (unsigned) cookie);
798 current_play_mode = init_admissible_files();
799 register_tasks(cookie);
800 s.default_timeout.tv_sec = 0;
801 s.default_timeout.tv_usec = 99 * 1000;
802 ret = sched(&s);
803 if (ret < 0)
804 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
805 afs_shutdown(OSL_MARK_CLEAN);
806 exit(EXIT_FAILURE);
807 }
808
809 static int create_all_tables(void)
810 {
811 int i, ret;
812
813 for (i = 0; i < NUM_AFS_TABLES; i++) {
814 struct table_info *ti = afs_tables + i;
815
816 if (ti->flags & TBLFLAG_SKIP_CREATE)
817 continue;
818 ret = osl_create_table(ti->desc);
819 if (ret < 0)
820 return ret;
821 }
822 return 1;
823 }
824
825 int com_init(int fd, int argc, char * const * const argv)
826 {
827 int i, j, ret;
828
829 if (argc == 1) {
830 ret = create_all_tables();
831 if (ret < 0)
832 return ret;
833 return open_afs_tables();
834 }
835 for (i = 1; i < argc; i++) {
836 for (j = 0; j < NUM_AFS_TABLES; j++) {
837 struct table_info *ti = afs_tables + j;
838
839 if (ti->flags & TBLFLAG_SKIP_CREATE)
840 continue;
841 if (strcmp(argv[i], ti->desc->name))
842 continue;
843 ret = send_va_buffer(fd, "creating table %s\n", argv[i]);
844 if (ret < 0)
845 return ret;
846 ret = osl_create_table(ti->desc);
847 if (ret < 0)
848 return ret;
849 break;
850 }
851 if (j == NUM_AFS_TABLES)
852 return -E_BAD_TABLE_NAME;
853 }
854 return open_afs_tables();
855 }