9b73fcd9c9ce6e2e4d8e4ef7c0319ab5906cd728
[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 stat clients
139  *
140  * \param msg a \p NULL terminated buffer
141  */
142 void stat_client_write(char *msg, int itemnum)
143 {
144         struct stat_client *sc, *tmp;
145 //      char *buf;
146         ssize_t len;
147         struct timeval tv = {0 , 0};
148
149         if (!initialized)
150                 return;
151 //      buf = make_message("%s\n", msg);
152         len = strlen(msg);
153         list_for_each_entry_safe(sc, tmp, &client_list, node) {
154                 int fd = sc->fd, ret;
155                 fd_set wfds;
156
157                 if (!((1 << itemnum) & sc->item_mask))
158                         continue;
159                 FD_ZERO(&wfds);
160                 FD_SET(fd, &wfds);
161 //              PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
162                 do
163                         ret = select(fd + 1, NULL, &wfds, NULL, &tv);
164                 while (ret < 0 && errno == EINTR);
165                 if (ret) {
166                         ret = write(fd, msg, len);
167                         PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
168                         if (ret == len )
169                                 continue;
170                 }
171                 /* write error or fd not ready for writing */
172                 close(fd);
173                 del_close_on_fork_list(fd);
174                 num_clients--;
175                 PARA_INFO_LOG("deleting client on fd %d\n", fd);
176                 list_del(&sc->node);
177                 free(sc);
178                 dump_stat_client_list();
179         }
180 //      free(buf);
181         PARA_DEBUG_LOG("%d client(s)\n", num_clients);
182 }
183
184 /**
185  * send empty status list
186  *
187  * Send to  each connected client the full status item list
188  * with empty values.
189  */
190 void dump_empty_status(void)
191 {
192         int i;
193
194         if (!initialized)
195                 return;
196         FOR_EACH_STAT_ITEM(i) {
197                 char *tmp = make_message("%s:\n", status_item_list[i]);
198                 stat_client_write(tmp, i);
199                 free(tmp);
200         }
201 }
202
203 /**
204  * check if string is a known status item.
205  *
206  * \param item buffer containing the text to check
207  *
208  * \return If \a item is a valid status item, the number of that status item is
209  * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
210  */
211 int stat_item_valid(const char *item)
212 {
213         int i;
214         if (!item || !*item)
215                 return -E_UNKNOWN_STAT_ITEM;
216         FOR_EACH_STAT_ITEM(i)
217                 if (!strcmp(status_item_list[i], item))
218                         return i;
219         return -E_UNKNOWN_STAT_ITEM;
220 }
221
222 /**
223  * check if line starts with known status item.
224  *
225  * \param line buffer containing the line
226  *
227  * \return If the beginning of \a line matches any paraslash status item and is
228  * followed by a colon, the number of that status item is returned. Otherwise,
229  * this function returns \p -E_UNKNOWN_STAT_ITEM.
230  */
231 int stat_line_valid(const char *line)
232 {
233         int i;
234         size_t line_len;
235
236         if (!line || !*line)
237                 return -E_UNKNOWN_STAT_ITEM;
238         line_len = strlen(line);
239         FOR_EACH_STAT_ITEM(i) {
240                 const char *s = status_item_list[i];
241                 size_t item_len = strlen(s);
242
243                 if (line_len > item_len && line[item_len] == ':' &&
244                                 !strncmp(line, s, item_len))
245                         return i;
246         }
247         return -E_UNKNOWN_STAT_ITEM;
248 }
249
250 /**
251  * call a custom function for each complete line
252  *
253  * \param buf the buffer containing data seperated by newlines
254  * \param n the number of bytes in \a buf
255  * \param line_handler the custom function
256  *
257  * If \a line_handler is \p NULL, return number of complete lines in buf.
258  * Otherwise, call \a line_handler for each complete line.  The rest of the
259  * buffer (last chunk containing incomplete line is moved to the beginning of
260  * the buffer.
261  *
262  * \return If line_handler is not NULL, this function returns the number
263  * of bytes not handled to \a line_handler.
264  */
265 unsigned for_each_line(char *buf, int n, void (*line_handler)(char *))
266 {
267         char *start = buf, *end;
268         int i, num_lines = 0;
269
270         while (start < buf + n) {
271                 char *next_null;
272                 char *next_cr;
273
274                 next_cr = memchr(start, '\n', buf + n - start);
275                 next_null = memchr(start, '\0', buf + n - start);
276                 if (!next_cr && !next_null)
277                         break;
278                 if (next_cr && next_null) {
279                         end = next_cr < next_null? next_cr : next_null;
280                 } else if (next_null) {
281                         end = next_null;
282                 } else
283                         end = next_cr;
284                 num_lines++;
285                 if (line_handler) {
286                         *end = '\0';
287                         line_handler(start);
288                         start = ++end;
289                 } else
290                         start = ++end;
291         }
292         if (!line_handler)
293                 return num_lines;
294         i = buf + n - start;
295         if (i && i != n)
296                 memmove(buf, start, i);
297         return i;
298 }
299