fix the plm database tool
[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 /**
47 *
48 * its entry in the list of stat clients
49 */
50 struct list_head node;
51 };
52
53 static struct list_head client_list;
54 static int initialized;
55 static int num_clients;
56
57 /**
58 * the list of all status items sent by para_server/para_audiod
59 */
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",
64
65 [SI_MTIME] = "mtime",
66 [SI_LENGTH_MIN] = "length_min",
67 [SI_LENGTH_SEC] = "length_sec",
68
69 [SI_FILE_SIZE] = "file_size",
70 [SI_STATUS_FLAGS] = "status_flags",
71 [SI_FORMAT] = "format",
72
73 [SI_SCORE] = "score",
74 [SI_AUDIO_INFO1] = "audio_file_info1",
75 [SI_AUDIO_INFO2] = "audio_file_info2",
76
77 [SI_AUDIO_INFO3] = "audio_file_info3",
78 [SI_DBINFO1] = "dbinfo1",
79 [SI_DBINFO2] = "dbinfo2",
80
81 [SI_DBINFO3] = "dbinfo3",
82 [SI_DECODER_FLAGS] = "decoder_flags",
83 [SI_AUDIOD_STATUS] = "audiod_status",
84
85 [SI_PLAY_TIME] = "play_time",
86 [SI_UPTIME] = "uptime",
87 [SI_OFFSET] = "offset",
88
89 [SI_LENGTH] = "length",
90 [SI_STREAM_START] = "stream_start",
91 [SI_CURRENT_TIME] = "current_time",
92
93 [SI_AUDIOD_UPTIME] = "audiod_uptime",
94 [SI_DBTOOL] = "dbtool"
95 };
96 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
97
98 static void dump_stat_client_list(void)
99 {
100 struct stat_client *sc;
101
102 if (!initialized)
103 return;
104 list_for_each_entry(sc, &client_list, node)
105 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
106 }
107 /**
108 * add a status client to the list
109 *
110 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
111 * the number of connected clients exceeds #MAX_STAT_CLIENTS
112 */
113 int stat_client_add(int fd)
114 {
115 struct stat_client *new_client;
116
117 if (num_clients >= MAX_STAT_CLIENTS) {
118 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
119 MAX_STAT_CLIENTS);
120 return -E_TOO_MANY_CLIENTS;
121 }
122 if (!initialized) {
123 INIT_LIST_HEAD(&client_list);
124 initialized = 1;
125 }
126 PARA_INFO_LOG("adding client on fd %d\n", fd);
127 new_client = para_malloc(sizeof(struct stat_client));
128 new_client->fd = fd;
129 add_close_on_fork_list(fd);
130 list_add(&new_client->node, &client_list);
131 dump_stat_client_list();
132 num_clients++;
133 return 1;
134 }
135 /**
136 * write a message to all connected stat clients
137 *
138 * \param msg a \p NULL terminated buffer
139 */
140 void stat_client_write(char *msg)
141 {
142 struct stat_client *sc, *tmp;
143 char *buf;
144 ssize_t len;
145 struct timeval tv = {0 , 0};
146
147 if (!initialized)
148 return;
149 buf = make_message("%s\n", msg);
150 len = strlen(buf);
151 list_for_each_entry_safe(sc, tmp, &client_list, node) {
152 int fd = sc->fd, ret;
153 fd_set wfds;
154 FD_ZERO(&wfds);
155 FD_SET(fd, &wfds);
156 // PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
157 do
158 ret = select(fd + 1, NULL, &wfds, NULL, &tv);
159 while (ret < 0 && errno == EINTR);
160 if (ret) {
161 ret = write(fd, buf, len);
162 PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
163 if (ret == len )
164 continue;
165 }
166 /* write error or fd not ready for writing */
167 close(fd);
168 del_close_on_fork_list(fd);
169 num_clients--;
170 PARA_INFO_LOG("deleting client on fd %d\n", fd);
171 list_del(&sc->node);
172 free(sc);
173 dump_stat_client_list();
174 }
175 free(buf);
176 PARA_DEBUG_LOG("%d client(s)\n", num_clients);
177 }
178
179 /**
180 * send empty status list
181 *
182 * Send to each connected client the full status item list
183 * with empty values.
184 */
185 void dump_empty_status(void)
186 {
187 int i;
188 char *empty_items = NULL;
189
190 if (!initialized)
191 return;
192 FOR_EACH_STAT_ITEM(i) {
193 char *tmp = make_message("%s%s:\n", empty_items? empty_items : "",
194 status_item_list[i]);
195 free(empty_items);
196 empty_items = tmp;
197 }
198 // PARA_DEBUG_LOG("%s: empty items: %s\n", __func__, empty_items);
199 stat_client_write(empty_items);
200 free(empty_items);
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 (i = 0; i < NUM_STAT_ITEMS; 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 * \param num upper bound on calls to \a line_handler
238 *
239 * If \a line_handler is \p NULL, return number of complete lines in buf.
240 * Otherwise, call \a line_handler for each complete line, but no more than \a num
241 * times. If \a num is zero, there is no restriction on how often \a line_handler
242 * may be called. The rest of the buffer (last chunk containing incomplete line
243 * if \a num is zero) is moved to the beginning of the buffer.
244 *
245 * \return If line_handler is not NULL, this function returns the number
246 * of bytes not handled to \a line_handler.
247 */
248 unsigned for_each_line(char *buf, int n, void (*line_handler)(char *), int num)
249 {
250 char *start = buf, *end;
251 int i, num_lines = 0;
252
253 while (start < buf + n) {
254 char *next_null;
255 char *next_cr;
256
257 next_cr = memchr(start, '\n', buf + n - start);
258 next_null = memchr(start, '\0', buf + n - start);
259 if (!next_cr && !next_null)
260 break;
261 if (next_cr && next_null) {
262 end = next_cr < next_null? next_cr : next_null;
263 } else if (next_null) {
264 end = next_null;
265 } else
266 end = next_cr;
267 num_lines++;
268 if (line_handler) {
269 *end = '\0';
270 line_handler(start);
271 start = ++end;
272 if (num && num_lines >= num)
273 break;
274 } else
275 start = ++end;
276 }
277 if (!line_handler)
278 return num_lines;
279 i = buf + n - start;
280 if (i && i != n)
281 memmove(buf, start, i);
282 return i;
283 }
284