stat_client_write(): add help text for new 'itemnum' parameter
[paraslash.git] / stat.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 /**
20 * \file stat.c functions used for sending/receiving the status of para_server
21 * and para_audiod
22 */
23 #include "para.h"
24 #include "close_on_fork.h"
25 #include "list.h"
26 #include "error.h"
27 #include "string.h"
28
29 /** the maximal number of simultaneous connections */
30 #define MAX_STAT_CLIENTS 50
31
32 /**
33 * The structure associated with a connected client that sent the 'stat' command
34 *
35 *
36 * A status client is identified by its file descriptor. para_audiod
37 * keeps a list of connected status clients.
38 */
39 struct stat_client {
40 /**
41 *
42 *
43 * the stat client's file descriptor
44 */
45 int fd;
46 long unsigned item_mask;
47 /**
48 *
49 * its entry in the list of stat clients
50 */
51 struct list_head node;
52 };
53
54 static struct list_head client_list;
55 static int initialized;
56 static int num_clients;
57
58 /**
59 * the list of all status items sent by para_server/para_audiod
60 */
61 const char *status_item_list[NUM_STAT_ITEMS] = {
62 [SI_STATUS_BAR] = "status_bar",
63 [SI_STATUS] = "status",
64 [SI_NUM_PLAYED] = "num_played",
65
66 [SI_MTIME] = "mtime",
67 [SI_LENGTH_MIN] = "length_min",
68 [SI_LENGTH_SEC] = "length_sec",
69
70 [SI_FILE_SIZE] = "file_size",
71 [SI_STATUS_FLAGS] = "status_flags",
72 [SI_FORMAT] = "format",
73
74 [SI_SCORE] = "score",
75 [SI_AUDIO_INFO1] = "audio_file_info1",
76 [SI_AUDIO_INFO2] = "audio_file_info2",
77
78 [SI_AUDIO_INFO3] = "audio_file_info3",
79 [SI_DBINFO1] = "dbinfo1",
80 [SI_DBINFO2] = "dbinfo2",
81
82 [SI_DBINFO3] = "dbinfo3",
83 [SI_DECODER_FLAGS] = "decoder_flags",
84 [SI_AUDIOD_STATUS] = "audiod_status",
85
86 [SI_PLAY_TIME] = "play_time",
87 [SI_UPTIME] = "uptime",
88 [SI_OFFSET] = "offset",
89
90 [SI_LENGTH] = "length",
91 [SI_STREAM_START] = "stream_start",
92 [SI_CURRENT_TIME] = "current_time",
93
94 [SI_AUDIOD_UPTIME] = "audiod_uptime",
95 [SI_SELECTOR] = "dbtool"
96 };
97 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
98
99 static void dump_stat_client_list(void)
100 {
101 struct stat_client *sc;
102
103 if (!initialized)
104 return;
105 list_for_each_entry(sc, &client_list, node)
106 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
107 }
108 /**
109 * add a status client to the list
110 *
111 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
112 * the number of connected clients exceeds #MAX_STAT_CLIENTS
113 */
114 int stat_client_add(int fd, long unsigned mask)
115 {
116 struct stat_client *new_client;
117
118 if (num_clients >= MAX_STAT_CLIENTS) {
119 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
120 MAX_STAT_CLIENTS);
121 return -E_TOO_MANY_CLIENTS;
122 }
123 if (!initialized) {
124 INIT_LIST_HEAD(&client_list);
125 initialized = 1;
126 }
127 PARA_INFO_LOG("adding client on fd %d\n", fd);
128 new_client = para_malloc(sizeof(struct stat_client));
129 new_client->fd = fd;
130 new_client->item_mask = mask;
131 add_close_on_fork_list(fd);
132 list_add(&new_client->node, &client_list);
133 dump_stat_client_list();
134 num_clients++;
135 return 1;
136 }
137 /**
138 * write a message to all connected status clients
139 *
140 * \param msg a \p NULL terminated buffer
141 * \param itemnum The number of the status item of \a msg
142 *
143 * On write errors, remove the status client from the client list and close its
144 * file descriptor.
145 */
146 void stat_client_write(char *msg, int itemnum)
147 {
148 struct stat_client *sc, *tmp;
149 ssize_t len;
150 struct timeval tv = {0 , 0};
151
152 if (!initialized)
153 return;
154 len = strlen(msg);
155 list_for_each_entry_safe(sc, tmp, &client_list, node) {
156 int fd = sc->fd, ret;
157 fd_set wfds;
158
159 if (!((1 << itemnum) & sc->item_mask))
160 continue;
161 FD_ZERO(&wfds);
162 FD_SET(fd, &wfds);
163 // PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
164 do
165 ret = select(fd + 1, NULL, &wfds, NULL, &tv);
166 while (ret < 0 && errno == EINTR);
167 if (ret) {
168 ret = write(fd, msg, len);
169 PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
170 if (ret == len )
171 continue;
172 }
173 /* write error or fd not ready for writing */
174 close(fd);
175 del_close_on_fork_list(fd);
176 num_clients--;
177 PARA_INFO_LOG("deleting client on fd %d\n", fd);
178 list_del(&sc->node);
179 free(sc);
180 dump_stat_client_list();
181 }
182 PARA_DEBUG_LOG("%d client(s)\n", num_clients);
183 }
184
185 /**
186 * send empty status list
187 *
188 * Send to each connected client the full status item list
189 * with empty values.
190 */
191 void dump_empty_status(void)
192 {
193 int i;
194
195 if (!initialized)
196 return;
197 FOR_EACH_STAT_ITEM(i) {
198 char *tmp = make_message("%s:\n", status_item_list[i]);
199 stat_client_write(tmp, i);
200 free(tmp);
201 }
202 }
203
204 /**
205 * check if string is a known status item.
206 *
207 * \param item buffer containing the text to check
208 *
209 * \return If \a item is a valid status item, the number of that status item is
210 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
211 */
212 int stat_item_valid(const char *item)
213 {
214 int i;
215 if (!item || !*item)
216 return -E_UNKNOWN_STAT_ITEM;
217 FOR_EACH_STAT_ITEM(i)
218 if (!strcmp(status_item_list[i], item))
219 return i;
220 return -E_UNKNOWN_STAT_ITEM;
221 }
222
223 /**
224 * check if line starts with known status item.
225 *
226 * \param line buffer containing the line
227 *
228 * \return If the beginning of \a line matches any paraslash status item and is
229 * followed by a colon, the number of that status item is returned. Otherwise,
230 * this function returns \p -E_UNKNOWN_STAT_ITEM.
231 */
232 int stat_line_valid(const char *line)
233 {
234 int i;
235 size_t line_len;
236
237 if (!line || !*line)
238 return -E_UNKNOWN_STAT_ITEM;
239 line_len = strlen(line);
240 FOR_EACH_STAT_ITEM(i) {
241 const char *s = status_item_list[i];
242 size_t item_len = strlen(s);
243
244 if (line_len > item_len && line[item_len] == ':' &&
245 !strncmp(line, s, item_len))
246 return i;
247 }
248 return -E_UNKNOWN_STAT_ITEM;
249 }
250
251 /**
252 * call a custom function for each complete line
253 *
254 * \param buf the buffer containing data seperated by newlines
255 * \param n the number of bytes in \a buf
256 * \param line_handler the custom function
257 *
258 * If \a line_handler is \p NULL, return number of complete lines in buf.
259 * Otherwise, call \a line_handler for each complete line. The rest of the
260 * buffer (last chunk containing incomplete line is moved to the beginning of
261 * the buffer.
262 *
263 * \return If line_handler is not NULL, this function returns the number
264 * of bytes not handled to \a line_handler.
265 */
266 unsigned for_each_line(char *buf, int n, void (*line_handler)(char *))
267 {
268 char *start = buf, *end;
269 int i, num_lines = 0;
270
271 while (start < buf + n) {
272 char *next_null;
273 char *next_cr;
274
275 next_cr = memchr(start, '\n', buf + n - start);
276 next_null = memchr(start, '\0', buf + n - start);
277 if (!next_cr && !next_null)
278 break;
279 if (next_cr && next_null) {
280 end = next_cr < next_null? next_cr : next_null;
281 } else if (next_null) {
282 end = next_null;
283 } else
284 end = next_cr;
285 num_lines++;
286 if (line_handler) {
287 *end = '\0';
288 line_handler(start);
289 start = ++end;
290 } else
291 start = ++end;
292 }
293 if (!line_handler)
294 return num_lines;
295 i = buf + n - start;
296 if (i && i != n)
297 memmove(buf, start, i);
298 return i;
299 }
300