2 * Copyright (C) 2005-2006 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.
20 * \file stat.c functions used for sending/receiving the status of para_server
24 #include "close_on_fork.h"
29 /** the maximal number of simultaneous connections */
30 #define MAX_STAT_CLIENTS 50
33 * The structure associated with a connected client that sent the 'stat' command
36 * A status client is identified by its file descriptor. para_audiod
37 * keeps a list of connected status clients.
43 * the stat client's file descriptor
48 * its entry in the list of stat clients
50 struct list_head node;
53 static struct list_head client_list;
54 static int initialized;
55 static int num_clients;
58 * the list of all status items sent by para_server/para_audiod
60 const char *status_item_list[NUM_STAT_ITEMS] = {
61 [SI_STATUS_BAR] = "status_bar",
62 [SI_STATUS] = "status",
63 [SI_NUM_PLAYED] = "num_played",
66 [SI_LENGTH_MIN] = "length_min",
67 [SI_LENGTH_SEC] = "length_sec",
69 [SI_FILE_SIZE] = "file_size",
70 [SI_STATUS_FLAGS] = "status_flags",
71 [SI_FORMAT] = "format",
74 [SI_AUDIO_INFO1] = "audio_file_info1",
75 [SI_AUDIO_INFO2] = "audio_file_info2",
77 [SI_AUDIO_INFO3] = "audio_file_info3",
78 [SI_DBINFO1] = "dbinfo1",
79 [SI_DBINFO2] = "dbinfo2",
81 [SI_DBINFO3] = "dbinfo3",
82 [SI_DECODER_FLAGS] = "decoder_flags",
83 [SI_AUDIOD_STATUS] = "audiod_status",
85 [SI_PLAY_TIME] = "play_time",
86 [SI_UPTIME] = "uptime",
87 [SI_OFFSET] = "offset",
89 [SI_LENGTH] = "length",
90 [SI_STREAM_START] = "stream_start",
91 [SI_CURRENT_TIME] = "current_time",
93 [SI_AUDIOD_UPTIME] = "audiod_uptime",
94 [SI_SELECTOR] = "dbtool"
96 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
98 static void dump_stat_client_list(void)
100 struct stat_client *sc;
104 list_for_each_entry(sc, &client_list, node)
105 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
108 * add a status client to the list
110 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
111 * the number of connected clients exceeds #MAX_STAT_CLIENTS
113 int stat_client_add(int fd)
115 struct stat_client *new_client;
117 if (num_clients >= MAX_STAT_CLIENTS) {
118 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
120 return -E_TOO_MANY_CLIENTS;
123 INIT_LIST_HEAD(&client_list);
126 PARA_INFO_LOG("adding client on fd %d\n", fd);
127 new_client = para_malloc(sizeof(struct stat_client));
129 add_close_on_fork_list(fd);
130 list_add(&new_client->node, &client_list);
131 dump_stat_client_list();
136 * write a message to all connected stat clients
138 * \param msg a \p NULL terminated buffer
140 void stat_client_write(char *msg)
142 struct stat_client *sc, *tmp;
145 struct timeval tv = {0 , 0};
149 buf = make_message("%s\n", msg);
151 list_for_each_entry_safe(sc, tmp, &client_list, node) {
152 int fd = sc->fd, ret;
156 // PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
158 ret = select(fd + 1, NULL, &wfds, NULL, &tv);
159 while (ret < 0 && errno == EINTR);
161 ret = write(fd, buf, len);
162 PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
166 /* write error or fd not ready for writing */
168 del_close_on_fork_list(fd);
170 PARA_INFO_LOG("deleting client on fd %d\n", fd);
173 dump_stat_client_list();
176 PARA_DEBUG_LOG("%d client(s)\n", num_clients);
180 * send empty status list
182 * Send to each connected client the full status item list
185 void dump_empty_status(void)
188 char *empty_items = NULL;
192 FOR_EACH_STAT_ITEM(i) {
193 char *tmp = make_message("%s%s:\n", empty_items? empty_items : "",
194 status_item_list[i]);
198 // PARA_DEBUG_LOG("%s: empty items: %s\n", __func__, empty_items);
199 stat_client_write(empty_items);
204 * check if line starts with known status item.
206 * \param line buffer containing the line
208 * \return If the beginning of \a line matches any paraslash status item and is
209 * followed by a colon, the number of that status item is returned. Otherwise,
210 * this function returns \p -E_UNKNOWN_STAT_ITEM.
212 int stat_line_valid(const char *line)
218 return -E_UNKNOWN_STAT_ITEM;
219 line_len = strlen(line);
220 for (i = 0; i < NUM_STAT_ITEMS; i++) {
221 const char *s = status_item_list[i];
222 size_t item_len = strlen(s);
224 if (line_len > item_len && line[item_len] == ':' &&
225 !strncmp(line, s, item_len))
228 return -E_UNKNOWN_STAT_ITEM;
232 * call a custom function for each complete line
234 * \param buf the buffer containing data seperated by newlines
235 * \param n the number of bytes in \a buf
236 * \param line_handler the custom function
238 * If \a line_handler is \p NULL, return number of complete lines in buf.
239 * Otherwise, call \a line_handler for each complete line. The rest of the
240 * buffer (last chunk containing incomplete line is moved to the beginning of
243 * \return If line_handler is not NULL, this function returns the number
244 * of bytes not handled to \a line_handler.
246 unsigned for_each_line(char *buf, int n, void (*line_handler)(char *))
248 char *start = buf, *end;
249 int i, num_lines = 0;
251 while (start < buf + n) {
255 next_cr = memchr(start, '\n', buf + n - start);
256 next_null = memchr(start, '\0', buf + n - start);
257 if (!next_cr && !next_null)
259 if (next_cr && next_null) {
260 end = next_cr < next_null? next_cr : next_null;
261 } else if (next_null) {
277 memmove(buf, start, i);