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