2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file audiod_command.c commands for para_audiod */
13 #include "audiod.cmdline.h"
15 #include "close_on_fork.h"
18 #include "grab_client.cmdline.h"
19 #include "grab_client.h"
27 #include "audiod_command_list.h"
29 /** iterate over the array of all audiod commands */
30 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
32 static int client_write(int fd
, const char *buf
)
34 size_t len
= strlen(buf
);
35 return write(fd
, buf
, len
) != len
? -E_CLIENT_WRITE
: 1;
38 static char *get_time_string(struct timeval
*newest_stime
)
40 struct timeval diff
, adj_stream_start
, tmp
;
41 int total
= 0, use_server_time
= 1,
42 length_seconds
= stat_task
->length_seconds
;
44 if (!stat_task
->playing
) {
47 return make_message("%s:\n", status_item_list
[SI_PLAY_TIME
]);
49 if (audiod_status
== AUDIOD_OFF
)
51 if (stat_task
->sa_time_diff_sign
> 0)
52 tv_diff(&stat_task
->server_stream_start
, &stat_task
->sa_time_diff
,
55 tv_add(&stat_task
->server_stream_start
, &stat_task
->sa_time_diff
,
57 tmp
= adj_stream_start
;
58 if (newest_stime
&& audiod_status
== AUDIOD_ON
) {
59 tv_diff(newest_stime
, &adj_stream_start
, &diff
);
60 if (tv2ms(&diff
) < 5000) {
65 tv_diff(now
, &tmp
, &diff
);
66 total
= diff
.tv_sec
+ stat_task
->offset_seconds
;
67 if (total
> length_seconds
)
68 total
= length_seconds
;
73 "%s: %s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
74 status_item_list
[SI_PLAY_TIME
],
75 use_server_time
? "~" : "",
78 (length_seconds
- total
) / 60,
79 (length_seconds
- total
) % 60,
80 length_seconds
? (total
* 100 + length_seconds
/ 2) /
87 __malloc
static char *audiod_status_string(void)
89 const char *status
= (audiod_status
== AUDIOD_ON
)?
90 "on" : (audiod_status
== AUDIOD_OFF
)? "off": "sb";
91 return make_message("%s: %s\n", status_item_list
[SI_AUDIOD_STATUS
], status
);
94 static struct timeval
*wstime(void)
97 struct timeval
*max
= NULL
;
99 struct slot_info
*s
= &slot
[i
];
102 if (max
&& tv_diff(&s
->wstime
, max
, NULL
) <= 0)
108 __malloc
static char *decoder_flags(void)
111 char flags
[MAX_STREAM_SLOTS
+ 1];
114 struct slot_info
*s
= &slot
[i
];
116 if (s
->receiver_node
)
122 flags
[MAX_STREAM_SLOTS
] = '\0';
123 return make_message("%s: %s\n", status_item_list
[SI_DECODER_FLAGS
],
127 static int dump_commands(int fd
)
129 char *buf
= para_strdup(""), *tmp
= NULL
;
133 FOR_EACH_COMMAND(i
) {
134 tmp
= make_message("%s%s\t%s\n", buf
, audiod_cmds
[i
].name
,
135 audiod_cmds
[i
].description
);
139 ret
= client_write(fd
, buf
);
145 * command handlers don't close their fd on errors (ret < 0) so that
146 * its caller can send an error message. Otherwise (ret >= 0) it's up
147 * to each individual command to close the fd if necessary.
150 int com_help(int fd
, int argc
, char **argv
)
154 const char *dflt
= "No such command. Available commands:\n";
157 ret
= dump_commands(fd
);
160 FOR_EACH_COMMAND(i
) {
161 if (strcmp(audiod_cmds
[i
].name
, argv
[1]))
165 "SYNOPSIS\n\tpara_audioc %s\n"
168 audiod_cmds
[i
].description
,
169 audiod_cmds
[i
].usage
,
172 ret
= client_write(fd
, buf
);
176 ret
= client_write(fd
, dflt
);
178 ret
= dump_commands(fd
);
185 int com_tasks(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
187 char *tl
= get_task_list();
190 ret
= client_write(fd
, tl
);
197 int com_kill(int fd
, int argc
, char **argv
)
201 return -E_AUDIOD_SYNTAX
;
202 for (i
= 1; i
< argc
; i
++) {
203 ret
= kill_task(argv
[i
]);
212 int com_stat(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
216 long unsigned mask
= ~0LU;
220 for (i
= 1; i
< argc
; i
++) {
221 ret
= stat_item_valid(argv
[i
]);
227 PARA_INFO_LOG("mask: 0x%lx\n", mask
);
228 if (mask
& (1 << SI_PLAY_TIME
)) {
229 struct timeval
*t
= wstime();
230 char *ts
= get_time_string(t
);
232 ret
= client_write(fd
, ts
);
238 if (mask
& (1 << SI_AUDIOD_UPTIME
)) {
239 char *tmp
, *us
= uptime_str();
240 tmp
= make_message("%s: %s\n",
241 status_item_list
[SI_AUDIOD_UPTIME
], us
);
243 ret
= client_write(fd
, tmp
);
248 if (mask
& (1 << SI_AUDIOD_STATUS
)) {
249 char *s
= audiod_status_string();
250 ret
= client_write(fd
, s
);
255 if (mask
& (1 << SI_DECODER_FLAGS
)) {
256 char *df
= decoder_flags();
257 ret
= client_write(fd
, df
);
262 FOR_EACH_STATUS_ITEM(i
) {
264 if (!((1 << i
) & mask
))
266 v
= stat_task
->stat_item_values
[i
];
267 tmp
= make_message("%s%s%s", buf
? buf
: "",
268 v
? v
: "", v
? "\n" : "");
272 ret
= client_write(fd
, buf
);
275 ret
= stat_client_add(fd
, mask
);
280 static struct filter_node
*find_filter_node(int slot_num
, int format
, int filternum
)
282 struct filter_node
*fn
;
286 struct slot_info
*s
= &slot
[i
];
287 if (s
->format
< 0 || !s
->fc
)
289 if (slot_num
>= 0 && slot_num
!= i
)
291 if (format
>= 0 && s
->format
!= format
)
293 if (num_filters(i
) < filternum
)
297 list_for_each_entry(fn
, &s
->fc
->filters
, node
)
298 if (filternum
<= 0 || j
++ == filternum
)
305 int com_grab(int fd
, char *cmdline
)
307 struct grab_client
*gc
;
308 struct filter_node
*fn
;
312 gc
= grab_client_new(fd
, cmdline
, &err
);
315 fn
= find_filter_node(gc
->conf
->slot_arg
, gc
->audio_format_num
, gc
->conf
->filter_num_arg
);
317 activate_grab_client(gc
, fn
);
320 if (err
!= -E_GC_HELP_GIVEN
&& err
!= -E_GC_VERSION_GIVEN
)
322 if (err
== -E_GC_HELP_GIVEN
) {
323 msg
= make_message("%s\n\n", grab_client_args_info_usage
);
324 for (i
= 0; grab_client_args_info_help
[i
]; i
++) {
325 char *tmp
= make_message("%s%s\n", msg
,
326 grab_client_args_info_help
[i
]);
331 msg
= make_message("%s %s\n",
332 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE
,
333 GRAB_CLIENT_CMDLINE_PARSER_VERSION
);
334 err
= client_write(fd
, msg
);
342 __noreturn
int com_term(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
345 clean_exit(EXIT_SUCCESS
, "terminating on user request");
348 int com_on(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
350 audiod_status
= AUDIOD_ON
;
355 int com_off(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
357 audiod_status
= AUDIOD_OFF
;
362 int com_sb(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
364 audiod_status
= AUDIOD_STANDBY
;
369 int com_cycle(int fd
, int argc
, char **argv
)
371 switch (audiod_status
) {
373 return com_sb(fd
, argc
, argv
);
376 return com_on(fd
, argc
, argv
);
379 return com_off(fd
, argc
, argv
);
386 static int check_perms(uid_t uid
)
390 if (!conf
.user_allow_given
)
392 for (i
= 0; i
< conf
.user_allow_given
; i
++)
393 if (uid
== conf
.user_allow_arg
[i
])
395 return -E_UCRED_PERM
;
399 * handle arriving connections on the local socket
401 * \param accept_fd the fd to call accept() on
403 * This is called whenever para_audiod's main task detects an incoming
404 * connection by the readability of \a accept_fd. This function reads the
405 * command sent by the peer, checks the connecting user's permissions by using
406 * unix socket credentials (if supported by the OS) and calls the corresponding
407 * command handler if permissions are OK.
409 * \return positive on success, negative on errors
411 * \sa para_accept(), recv_cred_buffer()
413 int handle_connect(int accept_fd
)
415 int i
, argc
, ret
, clifd
= -1;
416 char *cmd
= NULL
, *p
, *buf
= para_calloc(MAXLINE
), **argv
= NULL
;
417 struct sockaddr_un unix_addr
;
420 ret
= para_accept(accept_fd
, &unix_addr
, sizeof(struct sockaddr_un
));
424 ret
= recv_cred_buffer(clifd
, buf
, MAXLINE
- 1);
428 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret
, buf
);
429 ret
= check_perms(uid
);
432 ret
= -E_INVALID_AUDIOD_CMD
;
433 cmd
= para_strdup(buf
);
434 p
= strchr(cmd
, '\n');
441 for (i
= 0; audiod_cmds
[i
].name
; i
++) {
443 if (strcmp(audiod_cmds
[i
].name
, cmd
))
445 if (audiod_cmds
[i
].handler
) {
446 argc
= split_args(buf
, &argv
, "\n");
447 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv
[0], argc
);
448 ret
= audiod_cmds
[i
].handler(clifd
, argc
, argv
);
451 for (j
= 0; p
[j
]; j
++)
454 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd
, p
);
455 ret
= audiod_cmds
[i
].line_handler(clifd
, p
);
458 ret
= -E_INVALID_AUDIOD_CMD
;
463 if (clifd
> 0 && ret
< 0 && ret
!= -E_CLIENT_WRITE
) {
464 char *tmp
= make_message("%s\n", PARA_STRERROR(-ret
));
465 client_write(clifd
, tmp
);
472 * send the current audiod status to all connected stat clients
474 void audiod_status_dump(void)
476 struct timeval
*t
= wstime();
477 char *old
, *new, *tmp
;
479 old
= stat_task
->stat_item_values
[SI_PLAY_TIME
];
480 new = get_time_string(t
);
482 if (!old
|| strcmp(old
, new)) {
484 stat_client_write(new, SI_PLAY_TIME
);
485 stat_task
->stat_item_values
[SI_PLAY_TIME
] = new;
491 old
= stat_task
->stat_item_values
[SI_AUDIOD_UPTIME
];
492 if (!old
|| strcmp(old
, new)) {
494 tmp
= make_message("%s: %s\n",
495 status_item_list
[SI_AUDIOD_UPTIME
], new);
496 stat_client_write(tmp
, SI_AUDIOD_UPTIME
);
498 stat_task
->stat_item_values
[SI_AUDIOD_UPTIME
] = new;
502 old
= stat_task
->stat_item_values
[SI_AUDIOD_STATUS
];
503 new = audiod_status_string();
504 if (!old
|| strcmp(old
, new)) {
506 stat_client_write(new, SI_AUDIOD_STATUS
);
507 stat_task
->stat_item_values
[SI_AUDIOD_STATUS
] = new;
511 old
= stat_task
->stat_item_values
[SI_DECODER_FLAGS
];
512 new = decoder_flags();
513 if (!old
|| strcmp(old
, new)) {
515 stat_client_write(new, SI_DECODER_FLAGS
);
516 stat_task
->stat_item_values
[SI_DECODER_FLAGS
] = new;
522 * send empty status list
524 * Send to each connected client the full status item list
527 void dump_empty_status(void)
531 FOR_EACH_STATUS_ITEM(i
) {
532 char *tmp
= make_message("%s:\n", status_item_list
[i
]);
533 stat_client_write(tmp
, i
);
535 free(stat_task
->stat_item_values
[i
]);
536 stat_task
->stat_item_values
[i
] = NULL
;