2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file audiod_command.c commands for para_audiod */
23 #include "audiod.cmdline.h"
25 #include "close_on_fork.h"
28 #include "grab_client.cmdline.h"
29 #include "grab_client.h"
37 #include "audiod_command_list.h"
39 /** iterate over the array of all audiod commands */
40 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
42 static int client_write(int fd
, const char *buf
)
44 size_t len
= strlen(buf
);
45 return write(fd
, buf
, len
) != len
? -E_CLIENT_WRITE
: 1;
48 static char *get_time_string(struct timeval
*newest_stime
)
50 struct timeval diff
, adj_stream_start
, tmp
;
51 int total
= 0, use_server_time
= 1,
52 length_seconds
= stat_task
->length_seconds
;
54 if (!stat_task
->playing
) {
57 return make_message("%s:\n", status_item_list
[SI_PLAY_TIME
]);
59 if (audiod_status
== AUDIOD_OFF
)
61 if (stat_task
->sa_time_diff_sign
> 0)
62 tv_diff(&stat_task
->server_stream_start
, &stat_task
->sa_time_diff
,
65 tv_add(&stat_task
->server_stream_start
, &stat_task
->sa_time_diff
,
67 tmp
= adj_stream_start
;
68 if (newest_stime
&& audiod_status
== AUDIOD_ON
) {
69 tv_diff(newest_stime
, &adj_stream_start
, &diff
);
70 if (tv2ms(&diff
) < 5000) {
75 tv_diff(now
, &tmp
, &diff
);
76 total
= diff
.tv_sec
+ stat_task
->offset_seconds
;
77 if (total
> length_seconds
)
78 total
= length_seconds
;
83 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
84 status_item_list
[SI_PLAY_TIME
],
85 use_server_time
? "~" : "",
88 (length_seconds
- total
) / 60,
89 (length_seconds
- total
) % 60,
90 length_seconds
? (total
* 100 + length_seconds
/ 2) /
97 __malloc
static char *audiod_status_string(void)
99 const char *status
= (audiod_status
== AUDIOD_ON
)?
100 "on" : (audiod_status
== AUDIOD_OFF
)? "off": "sb";
101 return make_message("%s:%s\n", status_item_list
[SI_AUDIOD_STATUS
], status
);
104 static struct timeval
*wstime(void)
107 struct timeval
*max
= NULL
;
109 struct slot_info
*s
= &slot
[i
];
112 if (max
&& tv_diff(&s
->wstime
, max
, NULL
) <= 0)
118 __malloc
static char *decoder_flags(void)
121 char flags
[MAX_STREAM_SLOTS
+ 1];
124 struct slot_info
*s
= &slot
[i
];
126 if (s
->receiver_node
)
132 flags
[MAX_STREAM_SLOTS
] = '\0';
133 return make_message("%s:%s\n", status_item_list
[SI_DECODER_FLAGS
],
137 static int dump_commands(int fd
)
139 char *buf
= para_strdup(""), *tmp
= NULL
;
143 FOR_EACH_COMMAND(i
) {
144 tmp
= make_message("%s%s\t%s\n", buf
, audiod_cmds
[i
].name
,
145 audiod_cmds
[i
].description
);
149 ret
= client_write(fd
, buf
);
155 * command handlers don't close their fd on errors (ret < 0) so that
156 * its caller can send an error message. Otherwise (ret >= 0) it's up
157 * to each individual command to close the fd if necessary.
160 int com_help(int fd
, int argc
, char **argv
)
164 const char *dflt
= "No such command. Available commands:\n";
167 ret
= dump_commands(fd
);
170 FOR_EACH_COMMAND(i
) {
171 if (strcmp(audiod_cmds
[i
].name
, argv
[1]))
175 "SYNOPSIS\n\tpara_audioc %s\n"
178 audiod_cmds
[i
].description
,
179 audiod_cmds
[i
].usage
,
182 ret
= client_write(fd
, buf
);
186 ret
= client_write(fd
, dflt
);
188 ret
= dump_commands(fd
);
195 int com_tasks(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
197 char *tl
= get_task_list();
200 ret
= client_write(fd
, tl
);
207 int com_kill(int fd
, int argc
, char **argv
)
211 return -E_AUDIOD_SYNTAX
;
212 for (i
= 1; i
< argc
; i
++) {
213 ret
= kill_task(argv
[i
]);
222 int com_stat(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
226 long unsigned mask
= ~0LU;
230 for (i
= 1; i
< argc
; i
++) {
231 ret
= stat_item_valid(argv
[i
]);
237 PARA_INFO_LOG("mask: 0x%lx\n", mask
);
238 if (mask
& (1 << SI_PLAY_TIME
)) {
239 struct timeval
*t
= wstime();
240 char *ts
= get_time_string(t
);
242 ret
= client_write(fd
, ts
);
248 if (mask
& (1 << SI_AUDIOD_UPTIME
)) {
249 char *tmp
, *us
= uptime_str();
250 tmp
= make_message("%s:%s\n",
251 status_item_list
[SI_AUDIOD_UPTIME
], us
);
253 ret
= client_write(fd
, tmp
);
258 if (mask
& (1 << SI_AUDIOD_STATUS
)) {
259 char *s
= audiod_status_string();
260 ret
= client_write(fd
, s
);
265 if (mask
& (1 << SI_DECODER_FLAGS
)) {
266 char *df
=decoder_flags();
267 ret
= client_write(fd
, df
);
273 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
275 if (!((1 << i
) & mask
))
277 v
= stat_task
->stat_item_values
[i
];
278 tmp
= make_message("%s%s%s", buf
? buf
: "",
279 v
? v
: "", v
? "\n" : "");
283 ret
= client_write(fd
, buf
);
286 ret
= stat_client_add(fd
, mask
);
291 static struct filter_node
*find_filter_node(int slot_num
, int format
, int filternum
)
293 struct filter_node
*fn
;
297 struct slot_info
*s
= &slot
[i
];
298 if (s
->format
< 0 || !s
->fc
)
300 if (slot_num
>= 0 && slot_num
!= i
)
302 if (format
>= 0 && s
->format
!= format
)
304 if (num_filters(i
) < filternum
)
308 list_for_each_entry(fn
, &s
->fc
->filters
, node
)
309 if (filternum
<= 0 || j
++ == filternum
)
316 int com_grab(int fd
, char *cmdline
)
318 struct grab_client
*gc
;
319 struct filter_node
*fn
;
323 gc
= grab_client_new(fd
, cmdline
, &err
);
326 fn
= find_filter_node(gc
->conf
->slot_arg
, gc
->audio_format_num
, gc
->conf
->filter_num_arg
);
328 activate_grab_client(gc
, fn
);
331 if (err
!= -E_GC_HELP_GIVEN
&& err
!= -E_GC_VERSION_GIVEN
)
333 if (err
== -E_GC_HELP_GIVEN
) {
334 msg
= make_message("%s\n\n", grab_client_args_info_usage
);
335 for (i
= 0; grab_client_args_info_help
[i
]; i
++) {
336 char *tmp
= make_message("%s%s\n", msg
,
337 grab_client_args_info_help
[i
]);
342 msg
= make_message("%s %s\n",
343 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE
,
344 GRAB_CLIENT_CMDLINE_PARSER_VERSION
);
345 err
= client_write(fd
, msg
);
353 int __noreturn
com_term(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
356 clean_exit(EXIT_SUCCESS
, "terminating on user request");
359 int com_on(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
361 audiod_status
= AUDIOD_ON
;
366 int com_off(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
368 audiod_status
= AUDIOD_OFF
;
373 int com_sb(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
375 audiod_status
= AUDIOD_STANDBY
;
380 int com_cycle(int fd
, int argc
, char **argv
)
382 switch (audiod_status
) {
384 return com_sb(fd
, argc
, argv
);
387 return com_on(fd
, argc
, argv
);
390 return com_off(fd
, argc
, argv
);
397 static int check_perms(uid_t uid
)
401 if (!conf
.user_allow_given
)
403 for (i
= 0; i
< conf
.user_allow_given
; i
++)
404 if (uid
== conf
.user_allow_arg
[i
])
406 return -E_UCRED_PERM
;
410 * handle arriving connections on the local socket
412 * \param accept_fd the fd to call accept() on
414 * This is called whenever para_audiod's main task detects an incoming
415 * connection by the readability of \a accept_fd. This function reads the
416 * command sent by the peer, checks the connecting user's permissions by using
417 * unix socket credentials (if supported by the OS) and calls the corresponding
418 * command handler if permissions are OK.
420 * \return positive on success, negative on errors
422 * \sa para_accept(), recv_cred_buffer()
424 int handle_connect(int accept_fd
)
426 int i
, argc
, ret
, clifd
= -1;
427 char *cmd
= NULL
, *p
, *buf
= para_calloc(MAXLINE
), **argv
= NULL
;
428 struct sockaddr_un unix_addr
;
430 ret
= para_accept(accept_fd
, &unix_addr
, sizeof(struct sockaddr_un
));
434 ret
= recv_cred_buffer(clifd
, buf
, MAXLINE
- 1);
437 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret
, buf
);
438 ret
= check_perms(ret
);
441 ret
= -E_INVALID_AUDIOD_CMD
;
442 cmd
= para_strdup(buf
);
443 p
= strchr(cmd
, '\n');
450 for (i
= 0; audiod_cmds
[i
].name
; i
++) {
452 if (strcmp(audiod_cmds
[i
].name
, cmd
))
454 if (audiod_cmds
[i
].handler
) {
455 argc
= split_args(buf
, &argv
, "\n");
456 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv
[0], argc
);
457 ret
= audiod_cmds
[i
].handler(clifd
, argc
, argv
);
460 for (j
= 0; p
[j
]; j
++)
463 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd
, p
);
464 ret
= audiod_cmds
[i
].line_handler(clifd
, p
);
467 ret
= -E_INVALID_AUDIOD_CMD
;
472 if (clifd
> 0 && ret
< 0 && ret
!= -E_CLIENT_WRITE
) {
473 char *tmp
= make_message("%s\n", PARA_STRERROR(-ret
));
474 client_write(clifd
, tmp
);
481 void audiod_status_dump(void)
483 struct timeval
*t
= wstime();
484 char *old
, *new, *tmp
;
486 old
= stat_task
->stat_item_values
[SI_PLAY_TIME
];
487 new = get_time_string(t
);
489 if (!old
|| strcmp(old
, new)) {
491 stat_client_write(new, SI_PLAY_TIME
);
492 stat_task
->stat_item_values
[SI_PLAY_TIME
] = new;
498 old
= stat_task
->stat_item_values
[SI_AUDIOD_UPTIME
];
499 if (!old
|| strcmp(old
, new)) {
501 tmp
= make_message("%s:%s\n",
502 status_item_list
[SI_AUDIOD_UPTIME
], new);
503 stat_client_write(tmp
, SI_AUDIOD_UPTIME
);
505 stat_task
->stat_item_values
[SI_AUDIOD_UPTIME
] = new;
509 old
= stat_task
->stat_item_values
[SI_AUDIOD_STATUS
];
510 new = audiod_status_string();
511 if (!old
|| strcmp(old
, new)) {
513 stat_client_write(new, SI_AUDIOD_STATUS
);
514 stat_task
->stat_item_values
[SI_AUDIOD_STATUS
] = new;
518 old
= stat_task
->stat_item_values
[SI_DECODER_FLAGS
];
519 new = decoder_flags();
520 if (!old
|| strcmp(old
, new)) {
521 stat_client_write(new, SI_DECODER_FLAGS
);
522 stat_task
->stat_item_values
[SI_DECODER_FLAGS
] = new;
528 * send empty status list
530 * Send to each connected client the full status item list
533 void dump_empty_status(void)
537 FOR_EACH_STAT_ITEM(i
) {
538 char *tmp
= make_message("%s:\n", status_item_list
[i
]);
539 stat_client_write(tmp
, i
);
541 free(stat_task
->stat_item_values
[i
]);
542 stat_task
->stat_item_values
[i
] = NULL
;