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