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 */
10 #include <sys/types.h>
14 #include "audiod.cmdline.h"
19 #include "grab_client.h"
26 #include "audiod_command_list.h"
28 extern char *stat_item_values
[NUM_STAT_ITEMS
];
30 /** Iterate over the array of all audiod commands. */
31 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
33 /** The maximal number of simultaneous connections. */
34 #define MAX_STAT_CLIENTS 50
36 /** Flags used for the stat command of para_audiod. */
37 enum stat_client_flags
{
38 /** Enable parser-friendly output. */
39 SCF_PARSER_FRIENDLY
= 1,
43 * Describes a status client of para_audiod.
45 * There's one such structure per audiod client that sent the 'stat' command.
47 * A status client is identified by its file descriptor. para_audiod
48 * keeps a list of connected status clients.
51 /** The stat client's file descriptor. */
53 /** Bitmask of those status items the client is interested in. */
55 /** See \ref stat_client flags. s*/
57 /** Its entry in the list of stat clients. */
58 struct list_head node
;
61 static INITIALIZED_LIST_HEAD(client_list
);
62 static int num_clients
;
64 /** The list of all status items used by para_{server,audiod,gui}. */
65 const char *status_item_list
[] = {STATUS_ITEM_ARRAY
};
67 static void dump_stat_client_list(void)
69 struct stat_client
*sc
;
71 list_for_each_entry(sc
, &client_list
, node
)
72 PARA_INFO_LOG("stat client on fd %d\n", sc
->fd
);
75 * Add a status client to the list.
77 * \param fd The file descriptor of the client.
78 * \param mask Bitfield of status items for this client.
79 * \param parser_friendly Enable parser-friendly output mode.
81 * Only those status items having the bit set in \a mask will be
84 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
85 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
87 static int stat_client_add(int fd
, uint64_t mask
, int parser_friendly
)
89 struct stat_client
*new_client
;
91 if (num_clients
>= MAX_STAT_CLIENTS
) {
92 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
94 return -E_TOO_MANY_CLIENTS
;
96 PARA_INFO_LOG("adding client on fd %d\n", fd
);
97 new_client
= para_calloc(sizeof(struct stat_client
));
99 new_client
->item_mask
= mask
;
101 new_client
->flags
= SCF_PARSER_FRIENDLY
;
102 para_list_add(&new_client
->node
, &client_list
);
103 dump_stat_client_list();
108 * Write a message to all connected status clients.
110 * \param item_num The number of the status item of \a msg.
112 * On write errors, remove the status client from the client list and close its
115 void stat_client_write_item(int item_num
)
117 struct stat_client
*sc
, *tmp
;
118 struct para_buffer pb
= {.flags
= 0};
119 struct para_buffer pfpb
= {.flags
= PBF_SIZE_PREFIX
};
120 const uint64_t one
= 1;
122 list_for_each_entry_safe(sc
, tmp
, &client_list
, node
) {
123 int fd
= sc
->fd
, ret
;
125 if (!((one
<< item_num
) & sc
->item_mask
))
127 if (write_ok(fd
) > 0) {
128 struct para_buffer
*b
=
129 (sc
->flags
& SCF_PARSER_FRIENDLY
)? &pfpb
: &pb
;
130 char *msg
= stat_item_values
[item_num
];
132 WRITE_STATUS_ITEM(b
, item_num
, "%s\n",
134 ret
= write(fd
, b
->buf
, b
->offset
);
135 if (ret
== b
->offset
)
138 /* write error or fd not ready for writing */
141 PARA_INFO_LOG("deleting client on fd %d\n", fd
);
144 dump_stat_client_list();
149 // PARA_DEBUG_LOG("%d client(s)\n", num_clients);
153 * Check if string is a known status item.
155 * \param item Buffer containing the text to check.
157 * \return If \a item is a valid status item, the number of that status item is
158 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
160 static int stat_item_valid(const char *item
)
163 if (!item
|| !*item
) {
164 PARA_ERROR_LOG("%s\n", "no item");
165 return -E_UNKNOWN_STAT_ITEM
;
167 FOR_EACH_STATUS_ITEM(i
)
168 if (!strcmp(status_item_list
[i
], item
))
170 PARA_ERROR_LOG("invalid stat item: %s\n", item
);
171 return -E_UNKNOWN_STAT_ITEM
;
174 static int client_write(int fd
, const char *buf
)
181 return write(fd
, buf
, len
) != len
? -E_CLIENT_WRITE
: 1;
184 __malloc
static char *audiod_status_string(void)
186 const char *status
= (audiod_status
== AUDIOD_ON
)?
187 "on" : (audiod_status
== AUDIOD_OFF
)? "off": "sb";
188 return para_strdup(status
);
191 static int get_play_time_slot_num(void)
196 struct slot_info
*s
= &slot
[i
];
199 if (oldest
>= 0 && tv_diff(&s
->wstime
, &slot
[oldest
].wstime
,
207 __malloc
static char *decoder_flags(void)
210 char flags
[MAX_STREAM_SLOTS
+ 1];
213 struct slot_info
*s
= &slot
[i
];
215 if (s
->receiver_node
)
223 flags
[MAX_STREAM_SLOTS
] = '\0';
224 return para_strdup(flags
);
227 static int dump_commands(int fd
)
229 char *buf
= para_strdup(""), *tmp
= NULL
;
233 FOR_EACH_COMMAND(i
) {
234 tmp
= make_message("%s%s\t%s\n", buf
, audiod_cmds
[i
].name
,
235 audiod_cmds
[i
].description
);
239 ret
= client_write(fd
, buf
);
245 * command handlers don't close their fd on errors (ret < 0) so that
246 * its caller can send an error message. Otherwise (ret >= 0) it's up
247 * to each individual command to close the fd if necessary.
250 int com_help(int fd
, int argc
, char **argv
)
254 const char *dflt
= "No such command. Available commands:\n";
257 ret
= dump_commands(fd
);
260 FOR_EACH_COMMAND(i
) {
261 if (strcmp(audiod_cmds
[i
].name
, argv
[1]))
265 "SYNOPSIS\n\tpara_audioc %s\n"
268 audiod_cmds
[i
].description
,
269 audiod_cmds
[i
].usage
,
272 ret
= client_write(fd
, buf
);
276 ret
= client_write(fd
, dflt
);
278 ret
= dump_commands(fd
);
285 int com_tasks(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
287 char *tl
= get_task_list();
290 ret
= client_write(fd
, tl
);
297 int com_kill(int fd
, int argc
, char **argv
)
301 return -E_AUDIOD_SYNTAX
;
302 for (i
= 1; i
< argc
; i
++) {
303 ret
= kill_task(argv
[i
]);
312 int com_stat(int fd
, int argc
, char **argv
)
314 int i
, ret
, parser_friendly
= 0;
316 const uint64_t one
= 1;
317 struct para_buffer b
= {.flags
= 0};
319 for (i
= 1; i
< argc
; i
++) {
320 const char *arg
= argv
[i
];
323 if (!strcmp(arg
, "--")) {
327 if (!strncmp(arg
, "-p", 2)) {
329 b
.flags
= PBF_SIZE_PREFIX
;
334 mask
--; /* set all bits */
335 for (; i
< argc
; i
++) {
336 ret
= stat_item_valid(argv
[i
]);
339 mask
|= (one
<< ret
);
341 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask
);
342 FOR_EACH_STATUS_ITEM(i
) {
343 char *item
= stat_item_values
[i
];
344 if (!((one
<< i
) & mask
))
346 WRITE_STATUS_ITEM(&b
, i
, "%s\n", item
? item
: "");
348 ret
= client_write(fd
, b
.buf
);
350 ret
= stat_client_add(fd
, mask
, parser_friendly
);
355 int com_grab(int fd
, int argc
, char **argv
)
357 return grab_client_new(fd
, argc
, argv
);
360 __noreturn
int com_term(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
363 clean_exit(EXIT_SUCCESS
, "terminating on user request");
366 int com_on(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
368 audiod_status
= AUDIOD_ON
;
373 int com_off(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
375 audiod_status
= AUDIOD_OFF
;
380 int com_sb(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
382 audiod_status
= AUDIOD_STANDBY
;
387 int com_cycle(int fd
, int argc
, char **argv
)
389 switch (audiod_status
) {
391 return com_sb(fd
, argc
, argv
);
394 return com_on(fd
, argc
, argv
);
397 return com_off(fd
, argc
, argv
);
404 static int check_perms(uid_t uid
)
408 if (!conf
.user_allow_given
)
410 for (i
= 0; i
< conf
.user_allow_given
; i
++)
411 if (uid
== conf
.user_allow_arg
[i
])
413 return -E_UCRED_PERM
;
417 * handle arriving connections on the local socket
419 * \param accept_fd the fd to call accept() on
421 * This is called whenever para_audiod's main task detects an incoming
422 * connection by the readability of \a accept_fd. This function reads the
423 * command sent by the peer, checks the connecting user's permissions by using
424 * unix socket credentials (if supported by the OS) and calls the corresponding
425 * command handler if permissions are OK.
427 * \return positive on success, negative on errors
429 * \sa para_accept(), recv_cred_buffer()
431 int handle_connect(int accept_fd
)
433 int i
, argc
, ret
, clifd
= -1;
434 char buf
[MAXLINE
], **argv
= NULL
;
435 struct sockaddr_un unix_addr
;
438 ret
= para_accept(accept_fd
, &unix_addr
, sizeof(struct sockaddr_un
));
442 ret
= recv_cred_buffer(clifd
, buf
, sizeof(buf
) - 1);
446 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret
, buf
);
447 ret
= check_perms(uid
);
450 ret
= create_argv(buf
, "\n", &argv
);
454 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
455 FOR_EACH_COMMAND(i
) {
456 if (strcmp(audiod_cmds
[i
].name
, argv
[0]))
458 ret
= audiod_cmds
[i
].handler(clifd
, argc
, argv
);
461 ret
= -E_INVALID_AUDIOD_CMD
;
464 if (clifd
> 0 && ret
< 0 && ret
!= -E_CLIENT_WRITE
) {
465 char *tmp
= make_message("%s\n", para_strerror(-ret
));
466 client_write(clifd
, tmp
);
474 * Send the current audiod status to all connected stat clients.
476 void audiod_status_dump(void)
478 int slot_num
= get_play_time_slot_num();
481 old
= stat_item_values
[SI_PLAY_TIME
];
482 new = get_time_string(slot_num
);
484 if (!old
|| strcmp(old
, new)) {
486 stat_item_values
[SI_PLAY_TIME
] = new;
487 stat_client_write_item(SI_PLAY_TIME
);
493 old
= stat_item_values
[SI_AUDIOD_UPTIME
];
494 if (!old
|| strcmp(old
, new)) {
496 stat_item_values
[SI_AUDIOD_UPTIME
] = new;
497 stat_client_write_item(SI_AUDIOD_UPTIME
);
501 old
= stat_item_values
[SI_AUDIOD_STATUS
];
502 new = audiod_status_string();
503 if (!old
|| strcmp(old
, new)) {
505 stat_item_values
[SI_AUDIOD_STATUS
] = new;
506 stat_client_write_item(SI_AUDIOD_STATUS
);
510 old
= stat_item_values
[SI_DECODER_FLAGS
];
511 new = decoder_flags();
512 if (!old
|| strcmp(old
, new)) {
514 stat_item_values
[SI_DECODER_FLAGS
] = new;
515 stat_client_write_item(SI_DECODER_FLAGS
);
521 * Flush and send all status items.
523 * Send to each connected client the full status item list
526 void clear_and_dump_items(void)
530 FOR_EACH_STATUS_ITEM(i
) {
531 free(stat_item_values
[i
]);
532 stat_item_values
[i
] = NULL
;
533 stat_client_write_item(i
);