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 */
11 #include "audiod.cmdline.h"
13 #include "close_on_fork.h"
16 #include "grab_client.cmdline.h"
17 #include "grab_client.h"
25 #include "audiod_command_list.h"
27 /** iterate over the array of all audiod commands */
28 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
30 static int client_write(int fd
, const char *buf
)
32 size_t len
= strlen(buf
);
33 return write(fd
, buf
, len
) != len
? -E_CLIENT_WRITE
: 1;
36 static char *get_time_string(struct timeval
*newest_stime
)
38 struct timeval diff
, adj_stream_start
, tmp
;
39 int total
= 0, use_server_time
= 1,
40 length_seconds
= stat_task
->length_seconds
;
42 if (!stat_task
->playing
) {
45 return make_message("%s:\n", status_item_list
[SI_PLAY_TIME
]);
47 if (audiod_status
== AUDIOD_OFF
)
49 if (stat_task
->sa_time_diff_sign
> 0)
50 tv_diff(&stat_task
->server_stream_start
, &stat_task
->sa_time_diff
,
53 tv_add(&stat_task
->server_stream_start
, &stat_task
->sa_time_diff
,
55 tmp
= adj_stream_start
;
56 if (newest_stime
&& audiod_status
== AUDIOD_ON
) {
57 tv_diff(newest_stime
, &adj_stream_start
, &diff
);
58 if (tv2ms(&diff
) < 5000) {
63 tv_diff(now
, &tmp
, &diff
);
64 total
= diff
.tv_sec
+ stat_task
->offset_seconds
;
65 if (total
> length_seconds
)
66 total
= length_seconds
;
71 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
72 status_item_list
[SI_PLAY_TIME
],
73 use_server_time
? "~" : "",
76 (length_seconds
- total
) / 60,
77 (length_seconds
- total
) % 60,
78 length_seconds
? (total
* 100 + length_seconds
/ 2) /
85 __malloc
static char *audiod_status_string(void)
87 const char *status
= (audiod_status
== AUDIOD_ON
)?
88 "on" : (audiod_status
== AUDIOD_OFF
)? "off": "sb";
89 return make_message("%s:%s\n", status_item_list
[SI_AUDIOD_STATUS
], status
);
92 static struct timeval
*wstime(void)
95 struct timeval
*max
= NULL
;
97 struct slot_info
*s
= &slot
[i
];
100 if (max
&& tv_diff(&s
->wstime
, max
, NULL
) <= 0)
106 __malloc
static char *decoder_flags(void)
109 char flags
[MAX_STREAM_SLOTS
+ 1];
112 struct slot_info
*s
= &slot
[i
];
114 if (s
->receiver_node
)
120 flags
[MAX_STREAM_SLOTS
] = '\0';
121 return make_message("%s:%s\n", status_item_list
[SI_DECODER_FLAGS
],
125 static int dump_commands(int fd
)
127 char *buf
= para_strdup(""), *tmp
= NULL
;
131 FOR_EACH_COMMAND(i
) {
132 tmp
= make_message("%s%s\t%s\n", buf
, audiod_cmds
[i
].name
,
133 audiod_cmds
[i
].description
);
137 ret
= client_write(fd
, buf
);
143 * command handlers don't close their fd on errors (ret < 0) so that
144 * its caller can send an error message. Otherwise (ret >= 0) it's up
145 * to each individual command to close the fd if necessary.
148 int com_help(int fd
, int argc
, char **argv
)
152 const char *dflt
= "No such command. Available commands:\n";
155 ret
= dump_commands(fd
);
158 FOR_EACH_COMMAND(i
) {
159 if (strcmp(audiod_cmds
[i
].name
, argv
[1]))
163 "SYNOPSIS\n\tpara_audioc %s\n"
166 audiod_cmds
[i
].description
,
167 audiod_cmds
[i
].usage
,
170 ret
= client_write(fd
, buf
);
174 ret
= client_write(fd
, dflt
);
176 ret
= dump_commands(fd
);
183 int com_tasks(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
185 char *tl
= get_task_list();
188 ret
= client_write(fd
, tl
);
195 int com_kill(int fd
, int argc
, char **argv
)
199 return -E_AUDIOD_SYNTAX
;
200 for (i
= 1; i
< argc
; i
++) {
201 ret
= kill_task(argv
[i
]);
210 int com_stat(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
214 long unsigned mask
= ~0LU;
218 for (i
= 1; i
< argc
; i
++) {
219 ret
= stat_item_valid(argv
[i
]);
225 PARA_INFO_LOG("mask: 0x%lx\n", mask
);
226 if (mask
& (1 << SI_PLAY_TIME
)) {
227 struct timeval
*t
= wstime();
228 char *ts
= get_time_string(t
);
230 ret
= client_write(fd
, ts
);
236 if (mask
& (1 << SI_AUDIOD_UPTIME
)) {
237 char *tmp
, *us
= uptime_str();
238 tmp
= make_message("%s:%s\n",
239 status_item_list
[SI_AUDIOD_UPTIME
], us
);
241 ret
= client_write(fd
, tmp
);
246 if (mask
& (1 << SI_AUDIOD_STATUS
)) {
247 char *s
= audiod_status_string();
248 ret
= client_write(fd
, s
);
253 if (mask
& (1 << SI_DECODER_FLAGS
)) {
254 char *df
=decoder_flags();
255 ret
= client_write(fd
, df
);
261 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
263 if (!((1 << i
) & mask
))
265 v
= stat_task
->stat_item_values
[i
];
266 tmp
= make_message("%s%s%s", buf
? buf
: "",
267 v
? v
: "", v
? "\n" : "");
271 ret
= client_write(fd
, buf
);
274 ret
= stat_client_add(fd
, mask
);
279 static struct filter_node
*find_filter_node(int slot_num
, int format
, int filternum
)
281 struct filter_node
*fn
;
285 struct slot_info
*s
= &slot
[i
];
286 if (s
->format
< 0 || !s
->fc
)
288 if (slot_num
>= 0 && slot_num
!= i
)
290 if (format
>= 0 && s
->format
!= format
)
292 if (num_filters(i
) < filternum
)
296 list_for_each_entry(fn
, &s
->fc
->filters
, node
)
297 if (filternum
<= 0 || j
++ == filternum
)
304 int com_grab(int fd
, char *cmdline
)
306 struct grab_client
*gc
;
307 struct filter_node
*fn
;
311 gc
= grab_client_new(fd
, cmdline
, &err
);
314 fn
= find_filter_node(gc
->conf
->slot_arg
, gc
->audio_format_num
, gc
->conf
->filter_num_arg
);
316 activate_grab_client(gc
, fn
);
319 if (err
!= -E_GC_HELP_GIVEN
&& err
!= -E_GC_VERSION_GIVEN
)
321 if (err
== -E_GC_HELP_GIVEN
) {
322 msg
= make_message("%s\n\n", grab_client_args_info_usage
);
323 for (i
= 0; grab_client_args_info_help
[i
]; i
++) {
324 char *tmp
= make_message("%s%s\n", msg
,
325 grab_client_args_info_help
[i
]);
330 msg
= make_message("%s %s\n",
331 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE
,
332 GRAB_CLIENT_CMDLINE_PARSER_VERSION
);
333 err
= client_write(fd
, msg
);
341 __noreturn
int com_term(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
344 clean_exit(EXIT_SUCCESS
, "terminating on user request");
347 int com_on(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
349 audiod_status
= AUDIOD_ON
;
354 int com_off(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
356 audiod_status
= AUDIOD_OFF
;
361 int com_sb(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
363 audiod_status
= AUDIOD_STANDBY
;
368 int com_cycle(int fd
, int argc
, char **argv
)
370 switch (audiod_status
) {
372 return com_sb(fd
, argc
, argv
);
375 return com_on(fd
, argc
, argv
);
378 return com_off(fd
, argc
, argv
);
385 static int check_perms(uid_t uid
)
389 if (!conf
.user_allow_given
)
391 for (i
= 0; i
< conf
.user_allow_given
; i
++)
392 if (uid
== conf
.user_allow_arg
[i
])
394 return -E_UCRED_PERM
;
398 * handle arriving connections on the local socket
400 * \param accept_fd the fd to call accept() on
402 * This is called whenever para_audiod's main task detects an incoming
403 * connection by the readability of \a accept_fd. This function reads the
404 * command sent by the peer, checks the connecting user's permissions by using
405 * unix socket credentials (if supported by the OS) and calls the corresponding
406 * command handler if permissions are OK.
408 * \return positive on success, negative on errors
410 * \sa para_accept(), recv_cred_buffer()
412 int handle_connect(int accept_fd
)
414 int i
, argc
, ret
, clifd
= -1;
415 char *cmd
= NULL
, *p
, *buf
= para_calloc(MAXLINE
), **argv
= NULL
;
416 struct sockaddr_un unix_addr
;
419 ret
= para_accept(accept_fd
, &unix_addr
, sizeof(struct sockaddr_un
));
423 ret
= recv_cred_buffer(clifd
, buf
, MAXLINE
- 1);
427 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret
, buf
);
428 ret
= check_perms(uid
);
431 ret
= -E_INVALID_AUDIOD_CMD
;
432 cmd
= para_strdup(buf
);
433 p
= strchr(cmd
, '\n');
440 for (i
= 0; audiod_cmds
[i
].name
; i
++) {
442 if (strcmp(audiod_cmds
[i
].name
, cmd
))
444 if (audiod_cmds
[i
].handler
) {
445 argc
= split_args(buf
, &argv
, "\n");
446 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv
[0], argc
);
447 ret
= audiod_cmds
[i
].handler(clifd
, argc
, argv
);
450 for (j
= 0; p
[j
]; j
++)
453 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd
, p
);
454 ret
= audiod_cmds
[i
].line_handler(clifd
, p
);
457 ret
= -E_INVALID_AUDIOD_CMD
;
462 if (clifd
> 0 && ret
< 0 && ret
!= -E_CLIENT_WRITE
) {
463 char *tmp
= make_message("%s\n", PARA_STRERROR(-ret
));
464 client_write(clifd
, tmp
);
471 * send the current audiod status to all connected stat clients
473 void audiod_status_dump(void)
475 struct timeval
*t
= wstime();
476 char *old
, *new, *tmp
;
478 old
= stat_task
->stat_item_values
[SI_PLAY_TIME
];
479 new = get_time_string(t
);
481 if (!old
|| strcmp(old
, new)) {
483 stat_client_write(new, SI_PLAY_TIME
);
484 stat_task
->stat_item_values
[SI_PLAY_TIME
] = new;
490 old
= stat_task
->stat_item_values
[SI_AUDIOD_UPTIME
];
491 if (!old
|| strcmp(old
, new)) {
493 tmp
= make_message("%s:%s\n",
494 status_item_list
[SI_AUDIOD_UPTIME
], new);
495 stat_client_write(tmp
, SI_AUDIOD_UPTIME
);
497 stat_task
->stat_item_values
[SI_AUDIOD_UPTIME
] = new;
501 old
= stat_task
->stat_item_values
[SI_AUDIOD_STATUS
];
502 new = audiod_status_string();
503 if (!old
|| strcmp(old
, new)) {
505 stat_client_write(new, SI_AUDIOD_STATUS
);
506 stat_task
->stat_item_values
[SI_AUDIOD_STATUS
] = new;
510 old
= stat_task
->stat_item_values
[SI_DECODER_FLAGS
];
511 new = decoder_flags();
512 if (!old
|| strcmp(old
, new)) {
514 stat_client_write(new, SI_DECODER_FLAGS
);
515 stat_task
->stat_item_values
[SI_DECODER_FLAGS
] = new;
521 * send empty status list
523 * Send to each connected client the full status item list
526 void dump_empty_status(void)
530 FOR_EACH_STAT_ITEM(i
) {
531 char *tmp
= make_message("%s:\n", status_item_list
[i
]);
532 stat_client_write(tmp
, i
);
534 free(stat_task
->stat_item_values
[i
]);
535 stat_task
->stat_item_values
[i
] = NULL
;