2 * Copyright (C) 2005-2009 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"
19 #include "grab_client.cmdline.h"
20 #include "grab_client.h"
28 #include "audiod_command_list.h"
30 extern char *stat_item_values
[NUM_STAT_ITEMS
];
33 /** iterate over the array of all audiod commands */
34 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
36 static int client_write(int fd
, const char *buf
)
38 size_t len
= strlen(buf
);
39 return write(fd
, buf
, len
) != len
? -E_CLIENT_WRITE
: 1;
42 __malloc
static char *audiod_status_string(void)
44 const char *status
= (audiod_status
== AUDIOD_ON
)?
45 "on" : (audiod_status
== AUDIOD_OFF
)? "off": "sb";
46 return make_message("%s: %s\n", status_item_list
[SI_AUDIOD_STATUS
], status
);
49 static int get_play_time_slot_num(void)
54 struct slot_info
*s
= &slot
[i
];
57 if (oldest
>= 0 && tv_diff(&s
->wstime
, &slot
[oldest
].wstime
,
65 __malloc
static char *decoder_flags(void)
68 char flags
[MAX_STREAM_SLOTS
+ 1];
71 struct slot_info
*s
= &slot
[i
];
79 flags
[MAX_STREAM_SLOTS
] = '\0';
80 return make_message("%s: %s\n", status_item_list
[SI_DECODER_FLAGS
],
84 static int dump_commands(int fd
)
86 char *buf
= para_strdup(""), *tmp
= NULL
;
91 tmp
= make_message("%s%s\t%s\n", buf
, audiod_cmds
[i
].name
,
92 audiod_cmds
[i
].description
);
96 ret
= client_write(fd
, buf
);
102 * command handlers don't close their fd on errors (ret < 0) so that
103 * its caller can send an error message. Otherwise (ret >= 0) it's up
104 * to each individual command to close the fd if necessary.
107 int com_help(int fd
, int argc
, char **argv
)
111 const char *dflt
= "No such command. Available commands:\n";
114 ret
= dump_commands(fd
);
117 FOR_EACH_COMMAND(i
) {
118 if (strcmp(audiod_cmds
[i
].name
, argv
[1]))
122 "SYNOPSIS\n\tpara_audioc %s\n"
125 audiod_cmds
[i
].description
,
126 audiod_cmds
[i
].usage
,
129 ret
= client_write(fd
, buf
);
133 ret
= client_write(fd
, dflt
);
135 ret
= dump_commands(fd
);
142 int com_tasks(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
144 char *tl
= get_task_list();
147 ret
= client_write(fd
, tl
);
154 int com_kill(int fd
, int argc
, char **argv
)
158 return -E_AUDIOD_SYNTAX
;
159 for (i
= 1; i
< argc
; i
++) {
160 ret
= kill_task(argv
[i
]);
169 int com_stat(int fd
, int argc
, char **argv
)
174 const uint64_t one
= 1;
177 for (i
= 1; i
< argc
; i
++) {
178 ret
= stat_item_valid(argv
[i
]);
181 mask
|= (one
<< ret
);
184 mask
--; /* set all bits */
185 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask
);
186 if (mask
& (one
<< SI_PLAY_TIME
)) {
187 int slot_num
= get_play_time_slot_num();
188 char *ts
= get_time_string(slot_num
);
190 PARA_ERROR_LOG("play time: %s\n", ts
);
191 ret
= client_write(fd
, ts
);
197 if (mask
& (one
<< SI_AUDIOD_UPTIME
)) {
198 char *tmp
, *us
= uptime_str();
199 tmp
= make_message("%s: %s\n",
200 status_item_list
[SI_AUDIOD_UPTIME
], us
);
202 ret
= client_write(fd
, tmp
);
207 if (mask
& (one
<< SI_AUDIOD_STATUS
)) {
208 char *s
= audiod_status_string();
209 ret
= client_write(fd
, s
);
214 if (mask
& (one
<< SI_DECODER_FLAGS
)) {
215 char *df
= decoder_flags();
216 ret
= client_write(fd
, df
);
221 FOR_EACH_STATUS_ITEM(i
) {
223 if (!((one
<< i
) & mask
))
225 v
= stat_item_values
[i
];
226 tmp
= make_message("%s%s%s", buf
? buf
: "",
227 v
? v
: "", v
? "\n" : "");
231 ret
= client_write(fd
, buf
);
234 ret
= stat_client_add(fd
, mask
);
239 static struct filter_node
*find_filter_node(int slot_num
, int format
, int filternum
)
244 struct slot_info
*s
= &slot
[i
];
245 if (s
->format
< 0 || !s
->fc
)
247 if (slot_num
>= 0 && slot_num
!= i
)
249 if (format
>= 0 && s
->format
!= format
)
251 if (num_filters(i
) <= filternum
)
254 return s
->fc
->filter_nodes
+ filternum
;
259 int com_grab(int fd
, char *cmdline
)
261 struct grab_client
*gc
;
262 struct filter_node
*fn
;
266 gc
= grab_client_new(fd
, cmdline
, &err
);
269 fn
= find_filter_node(gc
->conf
->slot_arg
, gc
->audio_format_num
, gc
->conf
->filter_num_arg
);
271 activate_grab_client(gc
, fn
);
274 if (err
!= -E_GC_HELP_GIVEN
&& err
!= -E_GC_VERSION_GIVEN
)
276 if (err
== -E_GC_HELP_GIVEN
) {
277 msg
= make_message("%s\n\n", grab_client_args_info_usage
);
278 for (i
= 0; grab_client_args_info_help
[i
]; i
++) {
279 char *tmp
= make_message("%s%s\n", msg
,
280 grab_client_args_info_help
[i
]);
285 msg
= make_message("%s %s\n",
286 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE
,
287 GRAB_CLIENT_CMDLINE_PARSER_VERSION
);
288 err
= client_write(fd
, msg
);
296 __noreturn
int com_term(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
299 clean_exit(EXIT_SUCCESS
, "terminating on user request");
302 int com_on(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
304 audiod_status
= AUDIOD_ON
;
309 int com_off(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
311 audiod_status
= AUDIOD_OFF
;
316 int com_sb(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
318 audiod_status
= AUDIOD_STANDBY
;
323 int com_cycle(int fd
, int argc
, char **argv
)
325 switch (audiod_status
) {
327 return com_sb(fd
, argc
, argv
);
330 return com_on(fd
, argc
, argv
);
333 return com_off(fd
, argc
, argv
);
340 static int check_perms(uid_t uid
)
344 if (!conf
.user_allow_given
)
346 for (i
= 0; i
< conf
.user_allow_given
; i
++)
347 if (uid
== conf
.user_allow_arg
[i
])
349 return -E_UCRED_PERM
;
353 * handle arriving connections on the local socket
355 * \param accept_fd the fd to call accept() on
357 * This is called whenever para_audiod's main task detects an incoming
358 * connection by the readability of \a accept_fd. This function reads the
359 * command sent by the peer, checks the connecting user's permissions by using
360 * unix socket credentials (if supported by the OS) and calls the corresponding
361 * command handler if permissions are OK.
363 * \return positive on success, negative on errors
365 * \sa para_accept(), recv_cred_buffer()
367 int handle_connect(int accept_fd
)
369 int i
, argc
, ret
, clifd
= -1;
370 char *cmd
= NULL
, *p
, *buf
= para_calloc(MAXLINE
), **argv
= NULL
;
371 struct sockaddr_un unix_addr
;
374 ret
= para_accept(accept_fd
, &unix_addr
, sizeof(struct sockaddr_un
));
378 ret
= recv_cred_buffer(clifd
, buf
, MAXLINE
- 1);
382 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret
, buf
);
383 ret
= check_perms(uid
);
386 cmd
= para_strdup(buf
);
387 p
= strchr(cmd
, '\n');
394 for (i
= 0; audiod_cmds
[i
].name
; i
++) {
396 if (strcmp(audiod_cmds
[i
].name
, cmd
))
398 if (audiod_cmds
[i
].handler
) {
399 argc
= split_args(buf
, &argv
, "\n");
400 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv
[0], argc
);
401 ret
= audiod_cmds
[i
].handler(clifd
, argc
, argv
);
404 for (j
= 0; p
[j
]; j
++)
407 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd
, p
);
408 ret
= audiod_cmds
[i
].line_handler(clifd
, p
);
411 ret
= -E_INVALID_AUDIOD_CMD
;
416 if (clifd
> 0 && ret
< 0 && ret
!= -E_CLIENT_WRITE
) {
417 char *tmp
= make_message("%s\n", para_strerror(-ret
));
418 client_write(clifd
, tmp
);
425 * send the current audiod status to all connected stat clients
427 void audiod_status_dump(void)
429 int slot_num
= get_play_time_slot_num();
430 char *old
, *new, *tmp
;
432 old
= stat_item_values
[SI_PLAY_TIME
];
433 new = get_time_string(slot_num
);
435 if (!old
|| strcmp(old
, new)) {
437 stat_client_write(new, SI_PLAY_TIME
);
438 stat_item_values
[SI_PLAY_TIME
] = new;
444 new = make_message("%s: %s\n", status_item_list
[SI_AUDIOD_UPTIME
],
447 old
= stat_item_values
[SI_AUDIOD_UPTIME
];
448 if (!old
|| strcmp(old
, new)) {
450 stat_client_write(new, SI_AUDIOD_UPTIME
);
451 stat_item_values
[SI_AUDIOD_UPTIME
] = new;
455 old
= stat_item_values
[SI_AUDIOD_STATUS
];
456 new = audiod_status_string();
457 if (!old
|| strcmp(old
, new)) {
459 stat_client_write(new, SI_AUDIOD_STATUS
);
460 stat_item_values
[SI_AUDIOD_STATUS
] = new;
464 old
= stat_item_values
[SI_DECODER_FLAGS
];
465 new = decoder_flags();
466 if (!old
|| strcmp(old
, new)) {
468 stat_client_write(new, SI_DECODER_FLAGS
);
469 stat_item_values
[SI_DECODER_FLAGS
] = new;
475 * send empty status list
477 * Send to each connected client the full status item list
480 void dump_empty_status(void)
484 FOR_EACH_STATUS_ITEM(i
) {
485 char *tmp
= make_message("%s:\n", status_item_list
[i
]);
486 stat_client_write(tmp
, i
);
488 free(stat_item_values
[i
]);
489 stat_item_values
[i
] = NULL
;