4 #include <dirent.h> /* readdir() */
14 /** \file afs.c Paraslash's audio file selector. */
16 static uint32_t socket_cookie
;
19 * Compare two osl objects of string type.
21 * \param obj1 Pointer to the first object.
22 * \param obj2 Pointer to the second object.
24 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
25 * are taken into account.
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
31 * \sa strcmp(3), strncmp(3), osl_compare_func.
33 int string_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
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
));
40 /** The osl tables used by afs. \sa blob.c */
42 /** Contains audio file information. See aft.c. */
44 /** The table for the paraslash attributes. See attribute.c. */
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
54 * A standard blob table containing the mood definitions. For details
58 /** A blob table containing lyrics on a per-song basis. */
60 /** Another blob table for images (for example album cover art). */
62 /** Yet another blob table for storing standard playlists. */
64 /** How many tables are in use? */
68 static struct table_info afs_tables
[NUM_AFS_TABLES
];
72 * A wrapper for strtol(3).
74 * \param str The string to be converted to a long integer.
75 * \param result The converted value is stored here.
77 * \return Positive on success, -E_ATOL on errors.
79 * \sa strtol(3), atoi(3).
81 int para_atol(const char *str
, long *result
)
87 errno
= 0; /* To distinguish success/failure after call */
88 val
= strtol(str
, &endptr
, base
);
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 */
95 goto out
; /* No digits were found */
97 goto out
; /* Further characters after number */
105 * Struct to let para_server call a function specified from child context.
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).
114 * \sa fork(2), ipc.c.
116 struct callback_data
{
117 /** The function to be called. */
118 callback_function
*handler
;
119 /** The sma for the parameters of the callback function. */
121 /** The size of the query sma. */
123 /** If the callback produced a result, it is stored in this sma. */
125 /** The size of the result sma. */
127 /** The return value of the callback function. */
129 /** The return value of the callback() procedure. */
133 static struct callback_data
*shm_callback_data
;
134 static int callback_mutex
;
135 static int child_mutex
;
136 static int result_mutex
;
139 * Ask the parent process to call a given function.
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.
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.
151 * \return Negative, if the shared memory area could not be set up. The return
152 * value of the callback function otherwise.
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().
158 int send_callback_request(callback_function
*f
, struct osl_object
*query
,
159 struct osl_object
*result
)
161 struct callback_data cbd
= {.handler
= f
};
165 assert(query
->data
&& query
->size
);
166 ret
= shm_new(query
->size
);
169 cbd
.query_shmid
= ret
;
170 cbd
.query_size
= query
->size
;
171 ret
= shm_attach(cbd
.query_shmid
, ATTACH_RW
, &query_sma
);
174 memcpy(query_sma
, query
->data
, query
->size
);
175 ret
= shm_detach(query_sma
);
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
;
185 mutex_unlock(callback_mutex
);
186 kill(getppid(), SIGUSR1
); /* wake up parent */
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.
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 */
199 ret
= shm_attach(shm_callback_data
->result_shmid
, ATTACH_RO
,
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
);
208 PARA_ERROR_LOG("can not detach result\n");
210 PARA_WARNING_LOG("no result pointer\n");
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 */
217 PARA_WARNING_LOG("callback has no result\n");
222 ret
= shm_callback_data
->callback_ret
;
224 /* give other children a chance */
225 mutex_unlock(child_mutex
);
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
);
234 * Send a callback request passing an options structure and an argument vector.
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.
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.
247 * \sa send_standard_callback_request(), send_callback_request().
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
)
255 struct osl_object query
= {.size
= options
? options
->size
: 0};
257 for (i
= 0; i
< argc
; i
++)
258 query
.size
+= strlen(argv
[i
]) + 1;
259 query
.data
= para_malloc(query
.size
);
262 memcpy(query
.data
, options
->data
, options
->size
);
265 for (i
= 0; i
< argc
; i
++) {
266 strcpy(p
, argv
[i
]); /* OK */
267 p
+= strlen(argv
[i
]) + 1;
269 ret
= send_callback_request(f
, &query
, result
);
275 * Send a callback request with an argument vector only.
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().
282 * This is similar to send_option_arg_callback_request(), but no options buffer
283 * is passed to the parent process.
285 * \return The return value of the underlying call to
286 * send_option_arg_callback_request().
288 int send_standard_callback_request(int argc
, const char **argv
,
289 callback_function
*f
, struct osl_object
*result
)
291 return send_option_arg_callback_request(NULL
, argc
, argv
, f
, result
);
295 * write input from fd to dynamically allocated char array,
296 * but maximal max_size byte. Return size.
298 static int fd2buf(int fd
, char **buf
, int max_size
)
300 const size_t chunk_size
= 1024;
305 *buf
= para_malloc(size
* sizeof(char));
307 while ((ret
= read(fd
, p
, chunk_size
)) > 0) {
309 if ((p
- *buf
) + chunk_size
>= size
) {
313 if (size
> max_size
) {
314 ret
= -E_INPUT_TOO_LARGE
;
317 tmp
= para_realloc(*buf
, size
);
318 p
= (p
- *buf
) + tmp
;
334 * Read from stdin, and send the result to the parent process.
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.
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.
347 * \return Negative on errors, the return value of the underlying call to
348 * send_callback_request() otherwise.
350 int stdin_command(struct osl_object
*arg_obj
, callback_function
*f
,
351 unsigned max_len
, struct osl_object
*result
)
355 struct osl_object query
;
356 int ret
= fd2buf(STDIN_FILENO
, &stdin_buf
, max_len
);
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
);
366 ret
= send_callback_request(f
, &query
, result
);
372 * Open the audio file with highest score.
374 * \param afd Audio file data is returned here.
376 * This stores all information for streaming the "best" audio file
377 * in the \a afd structure.
379 * \return Positive on success, negative on errors.
381 * \sa close_audio_file(), open_and_update_audio_file().
383 int open_next_audio_file(struct audio_file_data
*afd
)
385 struct osl_row
*aft_row
;
388 ret
= score_get_best(&aft_row
, &afd
->score
);
391 ret
= open_and_update_audio_file(aft_row
, afd
);
398 * Free all resources which were allocated by open_next_audio_file().
400 * \param afd The structure previously filled in by open_next_audio_file().
402 * \return The return value of the underlying call to para_munmap().
404 * \sa open_next_audio_file().
406 int close_audio_file(struct audio_file_data
*afd
)
408 free(afd
->afhi
.chunk_table
);
409 return para_munmap(afd
->map
.data
, afd
->map
.size
);
413 static void play_loop(enum play_mode current_play_mode
)
416 struct audio_file_data afd
;
418 afd
.current_play_mode
= current_play_mode
;
419 for (i
= 0; i
< 0; i
++) {
420 ret
= open_next_audio_file(&afd
);
422 PARA_ERROR_LOG("failed to open next audio file: %d\n", ret
);
425 PARA_NOTICE_LOG("next audio file: %s, score: %li\n", afd
.path
, afd
.score
);
427 close_audio_file(&afd
);
433 static enum play_mode
init_admissible_files(void)
436 char *given_mood
, *given_playlist
;
438 given_mood
= "mood_that_was_given_at_the_command_line";
439 given_playlist
= "given_playlist";
442 ret
= mood_open(given_mood
);
445 PARA_WARNING_LOG("ignoring playlist %s\n",
447 return PLAY_MODE_MOOD
;
450 if (given_playlist
) {
451 ret
= playlist_open(given_playlist
);
453 return PLAY_MODE_PLAYLIST
;
455 ret
= mood_open(NULL
); /* open first available mood */
457 return PLAY_MODE_MOOD
;
458 mood_open(""); /* open dummy mood, always successful */
459 return PLAY_MODE_MOOD
;
464 static void setup_command_socket(void)
467 char *socket_name
= "/tmp/afs_command_socket";
468 struct sockaddr_un unix_addr
;
471 ret
= create_local_socket(socket_name
, &unix_addr
,
472 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
475 command_socket
= ret
;
476 if (listen(command_socket
, 5) < 0) {
477 PARA_EMERG_LOG("%s", "can not listen on socket\n");
480 PARA_INFO_LOG("listening on command socket %s (fd %d)\n", socket_name
,
492 __noreturn
int afs_init(uint32_t cookie
, int socket_fd
)
496 enum play_mode current_play_mode
;
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();
504 ret
= attribute_init(&afs_tables
[TBLNUM_ATTRIBUTES
]);
506 goto attribute_init_error
;
507 ret
= moods_init(&afs_tables
[TBLNUM_MOODS
]);
509 goto moods_init_error
;
510 ret
= playlists_init(&afs_tables
[TBLNUM_PLAYLIST
]);
512 goto playlists_init_error
;
513 ret
= lyrics_init(&afs_tables
[TBLNUM_LYRICS
]);
515 goto lyrics_init_error
;
516 ret
= images_init(&afs_tables
[TBLNUM_IMAGES
]);
518 goto images_init_error
;
519 ret
= score_init(&afs_tables
[TBLNUM_SCORES
]);
521 goto score_init_error
;
522 ret
= aft_init(&afs_tables
[TBLNUM_AUDIO_FILES
]);
526 current_play_mode
= init_admissible_files();
530 ret
= shm_new(sizeof(struct callback_data
));
534 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_area
);
537 shm_callback_data
= shm_area
;
541 callback_mutex
= ret
;
550 mutex_lock(result_mutex
);
553 score_shutdown(OSL_MARK_CLEAN
);
555 images_shutdown(OSL_MARK_CLEAN
);
557 lyrics_shutdown(OSL_MARK_CLEAN
);
559 playlists_shutdown(OSL_MARK_CLEAN
);
560 playlists_init_error
:
561 moods_shutdown(OSL_MARK_CLEAN
);
563 attribute_shutdown(OSL_MARK_CLEAN
);
564 attribute_init_error
:
568 static int create_all_tables(void)
572 for (i
= 0; i
< NUM_AFS_TABLES
; i
++) {
573 struct table_info
*ti
= afs_tables
+ i
;
575 if (ti
->flags
& TBLFLAG_SKIP_CREATE
)
577 ret
= osl_create_table(ti
->desc
);
584 /* TODO load tables after init */
585 static int com_init(__a_unused
int fd
, int argc
, const char **argv
)
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
;
594 if (ti
->flags
& TBLFLAG_SKIP_CREATE
)
596 if (strcmp(argv
[i
], ti
->desc
->name
))
598 PARA_NOTICE_LOG("creating table %s\n", argv
[i
]);
599 ret
= osl_create_table(ti
->desc
);
604 if (j
== NUM_AFS_TABLES
)
605 return -E_BAD_TABLE_NAME
;
609 /** Describes a command of para_server. */
611 /** The name of the command. */
613 /** The handler function. */
614 int (*handler
)(int fd
, int argc
, const char **argv
);
617 static struct command cmd
[] = {
624 .handler
= com_addlyr
,
628 .handler
= com_addimg
,
632 .handler
= com_addmood
,
636 .handler
= com_addpl
,
640 .handler
= com_catlyr
,
644 .handler
= com_catimg
,
648 .handler
= com_mvimg
,
652 .handler
= com_mvlyr
,
656 .handler
= com_mvmood
,
664 .handler
= com_catmood
,
668 .handler
= com_catpl
,
672 .handler
= com_rmatt
,
680 .handler
= com_lsatt
,
684 .handler
= com_afs_ls
,
688 .handler
= com_lslyr
,
692 .handler
= com_lsimg
,
696 .handler
= com_lsmood
,
704 .handler
= com_setatt
,
708 .handler
= com_addatt
,
712 .handler
= com_afs_rm
,
716 .handler
= com_rmlyr
,
720 .handler
= com_rmimg
,
724 .handler
= com_rmmood
,
732 .handler
= com_touch
,
739 static void call_callback(void)
741 struct osl_object query
, result
= {.data
= NULL
};
744 shm_callback_data
->result_shmid
= -1; /* no result */
745 ret
= shm_attach(shm_callback_data
->query_shmid
, ATTACH_RW
,
749 query
.size
= shm_callback_data
->query_size
;
750 shm_callback_data
->callback_ret
= shm_callback_data
->handler(&query
,
752 if (result
.data
&& result
.size
) {
754 ret
= shm_new(result
.size
);
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
);
762 memcpy(sma
, result
.data
, result
.size
);
763 ret
= shm_detach(sma
);
765 PARA_ERROR_LOG("detach result failed\n");
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;
777 ret2
= shm_detach(query
.data
);
779 PARA_ERROR_LOG("detach query failed\n");
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 */
791 static void afs_shutdown(enum osl_close_flags flags
)
793 score_shutdown(flags
);
794 attribute_shutdown(flags
);
797 moods_shutdown(flags
);
798 playlists_shutdown(flags
);
799 lyrics_shutdown(flags
);
800 images_shutdown(flags
);
805 static int got_sigchld
;
806 static void server_loop(int child_pid
)
810 PARA_DEBUG_LOG("server pid: %d, child pid: %d\n",
811 getpid(), child_pid
);
813 mutex_lock(callback_mutex
);
814 if (shm_callback_data
->handler
)
816 mutex_unlock(callback_mutex
);
820 mutex_destroy(result_mutex
);
821 mutex_destroy(callback_mutex
);
822 mutex_destroy(child_mutex
);
823 afs_shutdown(OSL_MARK_CLEAN
);
828 int main(int argc
, const char **argv
)
830 int i
, ret
= -E_AFS_SYNTAX
;
832 signal(SIGUSR1
, dummy
);
833 signal(SIGCHLD
, sigchld_handler
);
839 PARA_EMERG_LOG("afs_init returned %d\n", ret
);
849 for (i
= 0; cmd
[i
].name
; i
++) {
850 if (strcmp(cmd
[i
].name
, argv
[1]))
852 ret
= cmd
[i
].handler(1, argc
- 1 , argv
+ 1);
856 PARA_ERROR_LOG("unknown command: %s\n", argv
[1]);
860 PARA_ERROR_LOG("error %d\n", ret
);
862 PARA_DEBUG_LOG("%s", "success\n");
864 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;