e881ae09fed1f0d77983cb6eb6081598f3fc3f90
[paraslash.git] / stat.c
1 /*
2 * Copyright (C) 2005-2007 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 #include "fd.h"
29
30 /** the maximal number of simultaneous connections */
31 #define MAX_STAT_CLIENTS 50
32
33 /**
34 * describes a status client of para_audiod
35 *
36 * There's one such structure per audiod client that sent the 'stat' command.
37 *
38 * A status client is identified by its file descriptor. para_audiod
39 * keeps a list of connected status clients.
40 */
41 struct stat_client {
42 /** the stat client's file descriptor */
43 int fd;
44 /** bitmask of those status items the client is interested in */
45 long unsigned item_mask;
46 /** its entry in the list of stat clients */
47 struct list_head node;
48 };
49
50 static struct list_head client_list;
51 static int initialized;
52 static int num_clients;
53
54 /**
55 * the list of all status items sent by para_server/para_audiod
56 */
57 const char *status_item_list[NUM_STAT_ITEMS] = {
58 [SI_STATUS_BAR] = "status_bar",
59 [SI_STATUS] = "status",
60 [SI_NUM_PLAYED] = "num_played",
61
62 [SI_MTIME] = "mtime",
63 [SI_LENGTH_MIN] = "length_min",
64 [SI_LENGTH_SEC] = "length_sec",
65
66 [SI_FILE_SIZE] = "file_size",
67 [SI_STATUS_FLAGS] = "status_flags",
68 [SI_FORMAT] = "format",
69
70 [SI_SCORE] = "score",
71 [SI_AUDIO_INFO1] = "audio_file_info1",
72 [SI_AUDIO_INFO2] = "audio_file_info2",
73
74 [SI_AUDIO_INFO3] = "audio_file_info3",
75 [SI_DBINFO1] = "dbinfo1",
76 [SI_DBINFO2] = "dbinfo2",
77
78 [SI_DBINFO3] = "dbinfo3",
79 [SI_DECODER_FLAGS] = "decoder_flags",
80 [SI_AUDIOD_STATUS] = "audiod_status",
81
82 [SI_PLAY_TIME] = "play_time",
83 [SI_UPTIME] = "uptime",
84 [SI_OFFSET] = "offset",
85
86 [SI_LENGTH] = "length",
87 [SI_STREAM_START] = "stream_start",
88 [SI_CURRENT_TIME] = "current_time",
89
90 [SI_AUDIOD_UPTIME] = "audiod_uptime",
91 [SI_SELECTOR] = "dbtool"
92 };
93
94 static void dump_stat_client_list(void)
95 {
96 struct stat_client *sc;
97
98 if (!initialized)
99 return;
100 list_for_each_entry(sc, &client_list, node)
101 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
102 }
103 /**
104 * add a status client to the list
105 *
106 * \param fd the file descriptor of the client
107 * \param mask bitfield of status items for this client
108 *
109 * Only those status items having the bit set in \a mask will be
110 * sent to the client.
111 *
112 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
113 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
114 */
115 int stat_client_add(int fd, long unsigned mask)
116 {
117 struct stat_client *new_client;
118
119 if (num_clients >= MAX_STAT_CLIENTS) {
120 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
121 MAX_STAT_CLIENTS);
122 return -E_TOO_MANY_CLIENTS;
123 }
124 if (!initialized) {
125 INIT_LIST_HEAD(&client_list);
126 initialized = 1;
127 }
128 PARA_INFO_LOG("adding client on fd %d\n", fd);
129 new_client = para_malloc(sizeof(struct stat_client));
130 new_client->fd = fd;
131 new_client->item_mask = mask;
132 para_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(const char *msg, int itemnum)
147 {
148 struct stat_client *sc, *tmp;
149 size_t len = strlen(msg);
150 struct timeval tv = {0 , 0};
151
152 if (!initialized || !len)
153 return;
154 list_for_each_entry_safe(sc, tmp, &client_list, node) {
155 int fd = sc->fd, ret;
156 fd_set wfds;
157
158 if (!((1 << itemnum) & sc->item_mask))
159 continue;
160 FD_ZERO(&wfds);
161 FD_SET(fd, &wfds);
162 // PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
163 ret = para_select(fd + 1, NULL, &wfds, &tv);
164 if (ret > 0) {
165 ret = write(fd, msg, len);
166 PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
167 if (ret == len )
168 continue;
169 }
170 /* write error or fd not ready for writing */
171 close(fd);
172 num_clients--;
173 PARA_INFO_LOG("deleting client on fd %d\n", fd);
174 list_del(&sc->node);
175 free(sc);
176 dump_stat_client_list();
177 }
178 PARA_DEBUG_LOG("%d client(s)\n", num_clients);
179 }
180
181
182 /**
183 * check if string is a known status item.
184 *
185 * \param item buffer containing the text to check
186 *
187 * \return If \a item is a valid status item, the number of that status item is
188 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
189 */
190 int stat_item_valid(const char *item)
191 {
192 int i;
193 if (!item || !*item) {
194 PARA_ERROR_LOG("%s\n", "no item");
195 return -E_UNKNOWN_STAT_ITEM;
196 }
197 FOR_EACH_STAT_ITEM(i)
198 if (!strcmp(status_item_list[i], item))
199 return i;
200 return -E_UNKNOWN_STAT_ITEM;
201 }
202
203 /**
204 * check if line starts with known status item.
205 *
206 * \param line buffer containing the line
207 *
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.
211 */
212 int stat_line_valid(const char *line)
213 {
214 int i;
215 size_t line_len;
216
217 if (!line || !*line)
218 return -E_UNKNOWN_STAT_ITEM;
219 line_len = strlen(line);
220 FOR_EACH_STAT_ITEM(i) {
221 const char *s = status_item_list[i];
222 size_t item_len = strlen(s);
223
224 if (line_len > item_len && line[item_len] == ':' &&
225 !strncmp(line, s, item_len))
226 return i;
227 }
228 return -E_UNKNOWN_STAT_ITEM;
229 }
230
231 /**
232 * call a custom function for each complete line
233 *
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
237 *
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
241 * the buffer.
242 *
243 * \return If line_handler is not NULL, this function returns the number
244 * of bytes not handled to \a line_handler.
245 */
246 size_t for_each_line(char *buf, size_t n, void (*line_handler)(char *))
247 {
248 char *start = buf, *end;
249 size_t num_lines = 0, bytes_left = n;
250
251 // PARA_INFO_LOG("buf: %s", buf);
252 while (bytes_left) {
253 char *next_null;
254 char *next_cr;
255
256 next_cr = memchr(start, '\n', bytes_left);
257 next_null = memchr(start, '\0', bytes_left);
258 if (!next_cr && !next_null)
259 break;
260 if (next_cr && next_null) {
261 end = next_cr < next_null? next_cr : next_null;
262 } else if (next_null) {
263 end = next_null;
264 } else
265 end = next_cr;
266 num_lines++;
267 if (line_handler) {
268 *end = '\0';
269 // PARA_INFO_LOG("calling line handler: %s\n", start);
270 line_handler(start);
271 }
272 start = ++end;
273 bytes_left = buf + n - start;
274 }
275 if (!line_handler)
276 return num_lines;
277 if (bytes_left && bytes_left != n)
278 memmove(buf, start, bytes_left);
279 return bytes_left;
280 }
281