2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 * \file stat.c functions used for sending/receiving the status of para_server
12 #include "close_on_fork.h"
18 /** the maximal number of simultaneous connections */
19 #define MAX_STAT_CLIENTS 50
22 * describes a status client of para_audiod
24 * There's one such structure per audiod client that sent the 'stat' command.
26 * A status client is identified by its file descriptor. para_audiod
27 * keeps a list of connected status clients.
30 /** the stat client's file descriptor */
32 /** bitmask of those status items the client is interested in */
33 long unsigned item_mask;
34 /** its entry in the list of stat clients */
35 struct list_head node;
38 static struct list_head client_list;
39 static int initialized;
40 static int num_clients;
43 * the list of all status items sent by para_server/para_audiod
45 const char *status_item_list[NUM_STAT_ITEMS] = {
46 [SI_STATUS_BAR] = "status_bar",
47 [SI_STATUS] = "status",
48 [SI_NUM_PLAYED] = "num_played",
51 [SI_LENGTH_MIN] = "length_min",
52 [SI_LENGTH_SEC] = "length_sec",
54 [SI_FILE_SIZE] = "file_size",
55 [SI_STATUS_FLAGS] = "status_flags",
56 [SI_FORMAT] = "format",
59 [SI_AUDIO_INFO1] = "audio_file_info1",
60 [SI_AUDIO_INFO2] = "audio_file_info2",
62 [SI_AUDIO_INFO3] = "audio_file_info3",
63 [SI_DBINFO1] = "dbinfo1",
64 [SI_DBINFO2] = "dbinfo2",
66 [SI_DBINFO3] = "dbinfo3",
67 [SI_DECODER_FLAGS] = "decoder_flags",
68 [SI_AUDIOD_STATUS] = "audiod_status",
70 [SI_PLAY_TIME] = "play_time",
71 [SI_UPTIME] = "uptime",
72 [SI_OFFSET] = "offset",
74 [SI_LENGTH] = "length",
75 [SI_STREAM_START] = "stream_start",
76 [SI_CURRENT_TIME] = "current_time",
78 [SI_AUDIOD_UPTIME] = "audiod_uptime",
79 [SI_SELECTOR] = "dbtool"
82 static void dump_stat_client_list(void)
84 struct stat_client *sc;
88 list_for_each_entry(sc, &client_list, node)
89 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
92 * add a status client to the list
94 * \param fd the file descriptor of the client
95 * \param mask bitfield of status items for this client
97 * Only those status items having the bit set in \a mask will be
100 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
101 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
103 int stat_client_add(int fd, long unsigned mask)
105 struct stat_client *new_client;
107 if (num_clients >= MAX_STAT_CLIENTS) {
108 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
110 return -E_TOO_MANY_CLIENTS;
113 INIT_LIST_HEAD(&client_list);
116 PARA_INFO_LOG("adding client on fd %d\n", fd);
117 new_client = para_malloc(sizeof(struct stat_client));
119 new_client->item_mask = mask;
120 para_list_add(&new_client->node, &client_list);
121 dump_stat_client_list();
126 * write a message to all connected status clients
128 * \param msg a \p NULL terminated buffer
129 * \param itemnum The number of the status item of \a msg
131 * On write errors, remove the status client from the client list and close its
134 void stat_client_write(const char *msg, int itemnum)
136 struct stat_client *sc, *tmp;
137 size_t len = strlen(msg);
138 struct timeval tv = {0 , 0};
140 if (!initialized || !len)
142 list_for_each_entry_safe(sc, tmp, &client_list, node) {
143 int fd = sc->fd, ret;
146 if (!((1 << itemnum) & sc->item_mask))
150 // PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
151 ret = para_select(fd + 1, NULL, &wfds, &tv);
153 ret = write(fd, msg, len);
154 PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
158 /* write error or fd not ready for writing */
161 PARA_INFO_LOG("deleting client on fd %d\n", fd);
164 dump_stat_client_list();
166 PARA_DEBUG_LOG("%d client(s)\n", num_clients);
171 * check if string is a known status item.
173 * \param item buffer containing the text to check
175 * \return If \a item is a valid status item, the number of that status item is
176 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
178 int stat_item_valid(const char *item)
181 if (!item || !*item) {
182 PARA_ERROR_LOG("%s\n", "no item");
183 return -E_UNKNOWN_STAT_ITEM;
185 FOR_EACH_STAT_ITEM(i)
186 if (!strcmp(status_item_list[i], item))
188 return -E_UNKNOWN_STAT_ITEM;
192 * check if line starts with known status item.
194 * \param line buffer containing the line
196 * \return If the beginning of \a line matches any paraslash status item and is
197 * followed by a colon, the number of that status item is returned. Otherwise,
198 * this function returns \p -E_UNKNOWN_STAT_ITEM.
200 int stat_line_valid(const char *line)
206 return -E_UNKNOWN_STAT_ITEM;
207 line_len = strlen(line);
208 FOR_EACH_STAT_ITEM(i) {
209 const char *s = status_item_list[i];
210 size_t item_len = strlen(s);
212 if (line_len > item_len && line[item_len] == ':' &&
213 !strncmp(line, s, item_len))
216 return -E_UNKNOWN_STAT_ITEM;
220 * call a custom function for each complete line
222 * \param buf the buffer containing data seperated by newlines
223 * \param n the number of bytes in \a buf
224 * \param line_handler the custom function
226 * If \a line_handler is \p NULL, return number of complete lines in buf.
227 * Otherwise, call \a line_handler for each complete line. The rest of the
228 * buffer (last chunk containing incomplete line is moved to the beginning of
231 * \return If line_handler is not NULL, this function returns the number
232 * of bytes not handled to \a line_handler.
234 size_t for_each_line(char *buf, size_t n, void (*line_handler)(char *))
236 char *start = buf, *end;
237 size_t num_lines = 0, bytes_left = n;
239 // PARA_INFO_LOG("buf: %s", buf);
244 next_cr = memchr(start, '\n', bytes_left);
245 next_null = memchr(start, '\0', bytes_left);
246 if (!next_cr && !next_null)
248 if (next_cr && next_null) {
249 end = next_cr < next_null? next_cr : next_null;
250 } else if (next_null) {
257 // PARA_INFO_LOG("calling line handler: %s\n", start);
261 bytes_left = buf + n - start;
265 if (bytes_left && bytes_left != n)
266 memmove(buf, start, bytes_left);