3 #include <dirent.h> /* readdir() */
13 /** \file afs.c Paraslash's audio file selector. */
16 * Compare two osl objects of string type.
18 * \param obj1 Pointer to the first object.
19 * \param obj2 Pointer to the second object.
21 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
22 * are taken into account.
24 * \return It returns an integer less than, equal to, or greater than zero if
25 * \a obj1 is found, respectively, to be less than, to match, or be greater than
28 * \sa strcmp(3), strncmp(3), osl_compare_func.
30 int string_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
32 const char *str1
= (const char *)obj1
->data
;
33 const char *str2
= (const char *)obj2
->data
;
34 return strncmp(str1
, str2
, PARA_MIN(obj1
->size
, obj2
->size
));
37 /** The osl tables used by afs. \sa blob.c */
39 /** Contains audio file information. See aft.c. */
41 /** The table for the paraslash attributes. See attribute.c. */
44 * Paraslash's scoring system is based on Gaussian normal
45 * distributions, and the relevant data is stored in the rbtrees of an
46 * osl table containing only volatile columns. See score.c for
51 * A standard blob table containing the mood definitions. For details
55 /** A blob table containing lyrics on a per-song basis. */
57 /** Another blob table for images (for example album cover art). */
59 /** Yet another blob table for storing standard playlists. */
61 /** How many tables are in use? */
65 static struct table_info afs_tables
[NUM_AFS_TABLES
];
69 * A wrapper for strtol(3).
71 * \param str The string to be converted to a long integer.
72 * \param result The converted value is stored here.
74 * \return Positive on success, -E_ATOL on errors.
76 * \sa strtol(3), atoi(3).
78 int para_atol(const char *str
, long *result
)
84 errno
= 0; /* To distinguish success/failure after call */
85 val
= strtol(str
, &endptr
, base
);
87 if (errno
== ERANGE
&& (val
== LONG_MAX
|| val
== LONG_MIN
))
88 goto out
; /* overflow */
89 if (errno
!= 0 && val
== 0)
90 goto out
; /* other error */
92 goto out
; /* No digits were found */
94 goto out
; /* Further characters after number */
102 * Struct to let para_server call a function specified from child context.
104 * Commands that need to change the state of para_server can't
105 * change the relevant data structures directly because commands
106 * are executed in a child process, i.e. they get their own
107 * virtual address space. This structure must be used to let
108 * para_server (i.e. the parent process) call a function specified
109 * by the child (the command handler).
111 * \sa fork(2), ipc.c.
113 struct callback_data
{
114 /** The function to be called. */
115 callback_function
*handler
;
116 /** The sma for the parameters of the callback function. */
118 /** The size of the query sma. */
120 /** If the callback produced a result, it is stored in this sma. */
122 /** The size of the result sma. */
124 /** The return value of the callback function. */
126 /** The return value of the callback() procedure. */
130 static struct callback_data
*shm_callback_data
;
131 static int callback_mutex
;
132 static int child_mutex
;
133 static int result_mutex
;
136 * Ask the parent process to call a given function.
138 * \param f The function to be called.
139 * \param query Pointer to arbitrary data for the callback.
140 * \param result Callback result will be stored here.
142 * This function creates a shared memory area, copies the buffer pointed to by
143 * \a buf to that area and notifies the parent process that \a f should be
144 * called ASAP. It provides proper locking via semaphores to protect against
145 * concurent access to the shared memory area and against concurrent access by
146 * another child process that asks to call the same function.
148 * \return Negative, if the shared memory area could not be set up. The return
149 * value of the callback function otherwise.
151 * \sa shm_new(), shm_attach(), shm_detach(), mutex_lock(), mutex_unlock(),
152 * shm_destroy(), struct callback_data, send_option_arg_callback_request(),
153 * send_standard_callback_request().
155 int send_callback_request(callback_function
*f
, struct osl_object
*query
,
156 struct osl_object
*result
)
158 struct callback_data cbd
= {.handler
= f
};
162 assert(query
->data
&& query
->size
);
163 ret
= shm_new(query
->size
);
166 cbd
.query_shmid
= ret
;
167 cbd
.query_size
= query
->size
;
168 ret
= shm_attach(cbd
.query_shmid
, ATTACH_RW
, &query_sma
);
171 memcpy(query_sma
, query
->data
, query
->size
);
172 ret
= shm_detach(query_sma
);
175 /* prevent other children from interacting */
176 mutex_lock(child_mutex
);
177 /* prevent parent from messing with shm_callback_data. */
178 mutex_lock(callback_mutex
);
179 /* all three mutexes are locked, set parameters for callback */
180 *shm_callback_data
= cbd
;
182 mutex_unlock(callback_mutex
);
183 kill(getppid(), SIGUSR1
); /* wake up parent */
185 * At this time only the parent can run. It will execute our callback
186 * and unlock the result_mutex when ready to indicate that the child
187 * may use the result. So let's sleep on this mutex.
189 mutex_lock(result_mutex
);
190 /* No need to aquire the callback mutex again */
191 ret
= shm_callback_data
->sma_ret
;
192 if (ret
< 0) /* sma problem, callback might not have been executed */
193 goto unlock_child_mutex
;
194 if (shm_callback_data
->result_shmid
>= 0) { /* parent provided a result */
196 ret
= shm_attach(shm_callback_data
->result_shmid
, ATTACH_RO
,
199 if (result
) { /* copy result */
200 result
->size
= shm_callback_data
->result_size
;
201 result
->data
= para_malloc(result
->size
);
202 memcpy(result
->data
, sma
, result
->size
);
203 ret
= shm_detach(sma
);
205 PARA_ERROR_LOG("can not detach result\n");
207 PARA_WARNING_LOG("no result pointer\n");
209 PARA_ERROR_LOG("attach result failed: %d\n", ret
);
210 if (shm_destroy(shm_callback_data
->result_shmid
) < 0)
211 PARA_ERROR_LOG("destroy result failed\n");
212 } else { /* no result from callback */
214 PARA_WARNING_LOG("callback has no result\n");
219 ret
= shm_callback_data
->callback_ret
;
221 /* give other children a chance */
222 mutex_unlock(child_mutex
);
224 if (shm_destroy(cbd
.query_shmid
) < 0)
225 PARA_ERROR_LOG("%s\n", "shm destroy error");
226 PARA_DEBUG_LOG("callback_ret: %d\n", ret
);
231 * Send a callback request passing an options structure and an argument vector.
233 * \param options pointer to an arbitrary data structure.
234 * \param argc Argument count.
235 * \param argv Standard argument vector.
236 * \param f The callback function.
237 * \param result The result of the query is stored here.
239 * Some commands have a couple of options that are parsed in child context for
240 * syntactic correctness and are stored in a special options structure for that
241 * command. This function allows to pass such a structure together with a list
242 * of further arguments (often a list of audio files) to the parent process.
244 * \sa send_standard_callback_request(), send_callback_request().
246 int send_option_arg_callback_request(struct osl_object
*options
,
247 int argc
, const char **argv
, callback_function
*f
,
248 struct osl_object
*result
)
252 struct osl_object query
= {.size
= options
? options
->size
: 0};
254 for (i
= 0; i
< argc
; i
++)
255 query
.size
+= strlen(argv
[i
]) + 1;
256 query
.data
= para_malloc(query
.size
);
259 memcpy(query
.data
, options
->data
, options
->size
);
262 for (i
= 0; i
< argc
; i
++) {
263 strcpy(p
, argv
[i
]); /* OK */
264 p
+= strlen(argv
[i
]) + 1;
266 ret
= send_callback_request(f
, &query
, result
);
272 * Send a callback request with an argument vector only.
274 * \param argc The same meaning as in send_option_arg_callback_request().
275 * \param argv The same meaning as in send_option_arg_callback_request().
276 * \param f The same meaning as in send_option_arg_callback_request().
277 * \param result The same meaning as in send_option_arg_callback_request().
279 * This is similar to send_option_arg_callback_request(), but no options buffer
280 * is passed to the parent process.
282 * \return The return value of the underlying call to
283 * send_option_arg_callback_request().
285 int send_standard_callback_request(int argc
, const char **argv
,
286 callback_function
*f
, struct osl_object
*result
)
288 return send_option_arg_callback_request(NULL
, argc
, argv
, f
, result
);
292 * write input from fd to dynamically allocated char array,
293 * but maximal max_size byte. Return size.
295 static int fd2buf(int fd
, char **buf
, int max_size
)
297 const size_t chunk_size
= 1024;
302 *buf
= para_malloc(size
* sizeof(char));
304 while ((ret
= read(fd
, p
, chunk_size
)) > 0) {
306 if ((p
- *buf
) + chunk_size
>= size
) {
310 if (size
> max_size
) {
311 ret
= -E_INPUT_TOO_LARGE
;
314 tmp
= para_realloc(*buf
, size
);
315 p
= (p
- *buf
) + tmp
;
331 * Read from stdin, and send the result to the parent process.
333 * \param arg_obj Pointer to the arguments to \a f.
334 * \param f The callback function.
335 * \param max_len Don't read more than that many bytes from stdin.
336 * \param result The result of the query is stored here.
338 * This function is used by commands that wish to let para_server store
339 * arbitrary data specified by the user (for instance the add_blob family of
340 * commands). First, at most \a max_len bytes are read from stdin, the result
341 * is concatenated with the buffer given by \a arg_obj, and the combined buffer
342 * is made available to the parent process via shared memory.
344 * \return Negative on errors, the return value of the underlying call to
345 * send_callback_request() otherwise.
347 int stdin_command(struct osl_object
*arg_obj
, callback_function
*f
,
348 unsigned max_len
, struct osl_object
*result
)
352 struct osl_object query
;
353 int ret
= fd2buf(STDIN_FILENO
, &stdin_buf
, max_len
);
358 query
.size
= arg_obj
->size
+ stdin_len
;
359 query
.data
= para_malloc(query
.size
);
360 memcpy(query
.data
, arg_obj
->data
, arg_obj
->size
);
361 memcpy((char *)query
.data
+ arg_obj
->size
, stdin_buf
, stdin_len
);
363 ret
= send_callback_request(f
, &query
, result
);
368 static void para_init_random_seed(void)
373 gettimeofday(&now
, NULL
);
379 * Open the audio file with highest score.
381 * \param afd Audio file data is returned here.
383 * This stores all information for streaming the "best" audio file
384 * in the \a afd structure.
386 * \return Positive on success, negative on errors.
388 * \sa close_audio_file(), open_and_update_audio_file().
390 int open_next_audio_file(struct audio_file_data
*afd
)
392 struct osl_row
*aft_row
;
395 ret
= score_get_best(&aft_row
, &afd
->score
);
398 ret
= open_and_update_audio_file(aft_row
, afd
);
405 * Free all resources which were allocated by open_next_audio_file().
407 * \param afd The structure previously filled in by open_next_audio_file().
409 * \return The return value of the underlying call to para_munmap().
411 * \sa open_next_audio_file().
413 int close_audio_file(struct audio_file_data
*afd
)
415 free(afd
->afhi
.chunk_table
);
416 return para_munmap(afd
->map
.data
, afd
->map
.size
);
419 static void play_loop(enum play_mode current_play_mode
)
422 struct audio_file_data afd
;
424 afd
.current_play_mode
= current_play_mode
;
425 for (i
= 0; i
< 0; i
++) {
426 ret
= open_next_audio_file(&afd
);
428 PARA_ERROR_LOG("failed to open next audio file: %d\n", ret
);
431 PARA_NOTICE_LOG("next audio file: %s, score: %li\n", afd
.path
, afd
.score
);
433 close_audio_file(&afd
);
437 static enum play_mode
init_admissible_files(void)
440 char *given_mood
, *given_playlist
;
442 given_mood
= "mood_that_was_given_at_the_command_line";
443 given_playlist
= "given_playlist";
446 ret
= mood_open(given_mood
);
449 PARA_WARNING_LOG("ignoring playlist %s\n",
451 return PLAY_MODE_MOOD
;
454 if (given_playlist
) {
455 ret
= playlist_open(given_playlist
);
457 return PLAY_MODE_PLAYLIST
;
459 ret
= mood_open(NULL
); /* open first available mood */
461 return PLAY_MODE_MOOD
;
462 mood_open(""); /* open dummy mood, always successful */
463 return PLAY_MODE_MOOD
;
466 static int afs_init(void)
470 enum play_mode current_play_mode
;
472 para_init_random_seed();
474 ret
= attribute_init(&afs_tables
[TBLNUM_ATTRIBUTES
]);
475 PARA_DEBUG_LOG("ret %d\n", ret
);
478 ret
= moods_init(&afs_tables
[TBLNUM_MOODS
]);
480 goto moods_init_error
;
481 ret
= playlists_init(&afs_tables
[TBLNUM_PLAYLIST
]);
483 goto playlists_init_error
;
484 ret
= lyrics_init(&afs_tables
[TBLNUM_LYRICS
]);
486 goto lyrics_init_error
;
487 ret
= images_init(&afs_tables
[TBLNUM_IMAGES
]);
489 goto images_init_error
;
490 ret
= score_init(&afs_tables
[TBLNUM_SCORES
]);
492 goto score_init_error
;
493 ret
= aft_init(&afs_tables
[TBLNUM_AUDIO_FILES
]);
497 current_play_mode
= init_admissible_files();
498 play_loop(current_play_mode
);
500 ret
= shm_new(sizeof(struct callback_data
));
504 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_area
);
507 shm_callback_data
= shm_area
;
511 callback_mutex
= ret
;
520 mutex_lock(result_mutex
);
523 score_shutdown(OSL_MARK_CLEAN
);
525 images_shutdown(OSL_MARK_CLEAN
);
527 lyrics_shutdown(OSL_MARK_CLEAN
);
529 playlists_shutdown(OSL_MARK_CLEAN
);
530 playlists_init_error
:
531 moods_shutdown(OSL_MARK_CLEAN
);
533 attribute_shutdown(OSL_MARK_CLEAN
);
537 static uint32_t afs_socket_cookie
;
538 static int para_random(unsigned max
)
540 return ((max
+ 0.0) * (rand() / (RAND_MAX
+ 1.0)));
545 int ret
, afs_server_socket
[2];
547 para_init_random_seed();
548 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, afs_server_socket
);
551 afs_socket_cookie
= para_random((uint32_t)-1);
555 if (!ret
) { /* child (afs) */
556 char *socket_name
= "/tmp/afs_command_socket";
557 struct sockaddr_un unix_addr
;
561 ret
= create_local_socket(socket_name
, &unix_addr
,
562 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
566 if (listen(fd
, 5) < 0) {
567 PARA_EMERG_LOG("%s", "can not listen on socket\n");
573 PARA_NOTICE_LOG("accepting\n");
574 ret
= para_accept(fd
, &unix_addr
, sizeof(struct sockaddr_un
));
580 if (!ret
) { /* child (handler) */
581 PARA_NOTICE_LOG("reading stdin\n");
585 PARA_NOTICE_LOG("read: %s\n", buf
);
591 PARA_NOTICE_LOG("sending next requerst\n");
596 static int create_all_tables(void)
600 for (i
= 0; i
< NUM_AFS_TABLES
; i
++) {
601 struct table_info
*ti
= afs_tables
+ i
;
603 if (ti
->flags
& TBLFLAG_SKIP_CREATE
)
605 ret
= osl_create_table(ti
->desc
);
612 /* TODO load tables after init */
613 static int com_init(__a_unused
int fd
, int argc
, const char **argv
)
617 return create_all_tables();
618 for (i
= 1; i
< argc
; i
++) {
619 for (j
= 0; j
< NUM_AFS_TABLES
; j
++) {
620 struct table_info
*ti
= afs_tables
+ j
;
622 if (ti
->flags
& TBLFLAG_SKIP_CREATE
)
624 if (strcmp(argv
[i
], ti
->desc
->name
))
626 PARA_NOTICE_LOG("creating table %s\n", argv
[i
]);
627 ret
= osl_create_table(ti
->desc
);
632 if (j
== NUM_AFS_TABLES
)
633 return -E_BAD_TABLE_NAME
;
637 /** Describes a command of para_server. */
639 /** The name of the command. */
641 /** The handler function. */
642 int (*handler
)(int fd
, int argc
, const char **argv
);
645 static struct command cmd
[] = {
652 .handler
= com_addlyr
,
656 .handler
= com_addimg
,
660 .handler
= com_addmood
,
664 .handler
= com_addpl
,
668 .handler
= com_catlyr
,
672 .handler
= com_catimg
,
676 .handler
= com_mvimg
,
680 .handler
= com_mvlyr
,
684 .handler
= com_mvmood
,
692 .handler
= com_catmood
,
696 .handler
= com_catpl
,
700 .handler
= com_rmatt
,
708 .handler
= com_lsatt
,
712 .handler
= com_afs_ls
,
716 .handler
= com_lslyr
,
720 .handler
= com_lsimg
,
724 .handler
= com_lsmood
,
732 .handler
= com_setatt
,
736 .handler
= com_addatt
,
740 .handler
= com_afs_rm
,
744 .handler
= com_rmlyr
,
748 .handler
= com_rmimg
,
752 .handler
= com_rmmood
,
760 .handler
= com_touch
,
767 static void call_callback(void)
769 struct osl_object query
, result
= {.data
= NULL
};
772 shm_callback_data
->result_shmid
= -1; /* no result */
773 ret
= shm_attach(shm_callback_data
->query_shmid
, ATTACH_RW
,
777 query
.size
= shm_callback_data
->query_size
;
778 shm_callback_data
->callback_ret
= shm_callback_data
->handler(&query
,
780 if (result
.data
&& result
.size
) {
782 ret
= shm_new(result
.size
);
785 shm_callback_data
->result_shmid
= ret
;
786 shm_callback_data
->result_size
= result
.size
;
787 ret
= shm_attach(shm_callback_data
->result_shmid
, ATTACH_RW
, &sma
);
790 memcpy(sma
, result
.data
, result
.size
);
791 ret
= shm_detach(sma
);
793 PARA_ERROR_LOG("detach result failed\n");
800 if (shm_destroy(shm_callback_data
->result_shmid
) < 0)
801 PARA_ERROR_LOG("destroy result failed\n");
802 shm_callback_data
->result_shmid
= -1;
805 ret2
= shm_detach(query
.data
);
807 PARA_ERROR_LOG("detach query failed\n");
813 PARA_ERROR_LOG("sma error %d\n", ret
);
814 shm_callback_data
->sma_ret
= ret
;
815 shm_callback_data
->handler
= NULL
;
816 mutex_unlock(result_mutex
); /* wake up child */
819 static void dummy(__a_unused
int s
)
822 static void afs_shutdown(enum osl_close_flags flags
)
824 score_shutdown(flags
);
825 attribute_shutdown(flags
);
828 moods_shutdown(flags
);
829 playlists_shutdown(flags
);
830 lyrics_shutdown(flags
);
831 images_shutdown(flags
);
835 static int got_sigchld
;
836 static void sigchld_handler(__a_unused
int s
)
841 static void server_loop(int child_pid
)
845 PARA_DEBUG_LOG("server pid: %d, child pid: %d\n",
846 getpid(), child_pid
);
848 mutex_lock(callback_mutex
);
849 if (shm_callback_data
->handler
)
851 mutex_unlock(callback_mutex
);
855 mutex_destroy(result_mutex
);
856 mutex_destroy(callback_mutex
);
857 mutex_destroy(child_mutex
);
858 afs_shutdown(OSL_MARK_CLEAN
);
864 int main(int argc
, const char **argv
)
866 int i
, ret
= -E_AFS_SYNTAX
;
868 signal(SIGUSR1
, dummy
);
869 signal(SIGCHLD
, sigchld_handler
);
875 PARA_EMERG_LOG("afs_init returned %d\n", ret
);
885 for (i
= 0; cmd
[i
].name
; i
++) {
886 if (strcmp(cmd
[i
].name
, argv
[1]))
888 ret
= cmd
[i
].handler(1, argc
- 1 , argv
+ 1);
892 PARA_ERROR_LOG("unknown command: %s\n", argv
[1]);
896 PARA_ERROR_LOG("error %d\n", ret
);
898 PARA_DEBUG_LOG("%s", "success\n");
900 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;