]> git.tuebingen.mpg.de Git - paraslash.git/blob - stat.c
fix memset bug in enable_crypt()
[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 #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 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
94
95 static void dump_stat_client_list(void)
96 {
97         struct stat_client *sc;
98
99         if (!initialized)
100                 return;
101         list_for_each_entry(sc, &client_list, node)
102                 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
103 }
104 /**
105  * add a status client to the list
106  *
107  * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
108  * the number of connected clients exceeds #MAX_STAT_CLIENTS
109  */
110 int stat_client_add(int fd, long unsigned mask)
111 {
112         struct stat_client *new_client;
113
114         if (num_clients >= MAX_STAT_CLIENTS) {
115                 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
116                         MAX_STAT_CLIENTS);
117                 return -E_TOO_MANY_CLIENTS;
118         }
119         if (!initialized) {
120                 INIT_LIST_HEAD(&client_list);
121                 initialized = 1;
122         }
123         PARA_INFO_LOG("adding client on fd %d\n", fd);
124         new_client = para_malloc(sizeof(struct stat_client));
125         new_client->fd = fd;
126         new_client->item_mask = mask;
127         list_add(&new_client->node, &client_list);
128         dump_stat_client_list();
129         num_clients++;
130         return 1;
131 }
132 /**
133  * write a message to all connected status clients
134  *
135  * \param msg a \p NULL terminated buffer
136  * \param itemnum The number of the status item of \a msg
137  *
138  * On write errors, remove the status client from the client list and close its
139  * file descriptor.
140  */
141 void stat_client_write(char *msg, int itemnum)
142 {
143         struct stat_client *sc, *tmp;
144         ssize_t len;
145         struct timeval tv = {0 , 0};
146
147         if (!initialized)
148                 return;
149         len = strlen(msg);
150         list_for_each_entry_safe(sc, tmp, &client_list, node) {
151                 int fd = sc->fd, ret;
152                 fd_set wfds;
153
154                 if (!((1 << itemnum) & sc->item_mask))
155                         continue;
156                 FD_ZERO(&wfds);
157                 FD_SET(fd, &wfds);
158 //              PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
159                 ret = para_select(fd + 1, NULL, &wfds, &tv);
160                 if (ret > 0) {
161                         ret = write(fd, msg, 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                 num_clients--;
169                 PARA_INFO_LOG("deleting client on fd %d\n", fd);
170                 list_del(&sc->node);
171                 free(sc);
172                 dump_stat_client_list();
173         }
174         PARA_DEBUG_LOG("%d client(s)\n", num_clients);
175 }
176
177 /**
178  * send empty status list
179  *
180  * Send to  each connected client the full status item list
181  * with empty values.
182  */
183 void dump_empty_status(void)
184 {
185         int i;
186
187         if (!initialized)
188                 return;
189         FOR_EACH_STAT_ITEM(i) {
190                 char *tmp = make_message("%s:\n", status_item_list[i]);
191                 stat_client_write(tmp, i);
192                 free(tmp);
193         }
194 }
195
196 /**
197  * check if string is a known status item.
198  *
199  * \param item buffer containing the text to check
200  *
201  * \return If \a item is a valid status item, the number of that status item is
202  * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
203  */
204 int stat_item_valid(const char *item)
205 {
206         int i;
207         if (!item || !*item) {
208         PARA_ERROR_LOG("%s\n", "no item");
209                 return -E_UNKNOWN_STAT_ITEM;
210         }
211         FOR_EACH_STAT_ITEM(i)
212                 if (!strcmp(status_item_list[i], item))
213                         return i;
214         return -E_UNKNOWN_STAT_ITEM;
215 }
216
217 /**
218  * check if line starts with known status item.
219  *
220  * \param line buffer containing the line
221  *
222  * \return If the beginning of \a line matches any paraslash status item and is
223  * followed by a colon, the number of that status item is returned. Otherwise,
224  * this function returns \p -E_UNKNOWN_STAT_ITEM.
225  */
226 int stat_line_valid(const char *line)
227 {
228         int i;
229         size_t line_len;
230
231         if (!line || !*line)
232                 return -E_UNKNOWN_STAT_ITEM;
233         line_len = strlen(line);
234         FOR_EACH_STAT_ITEM(i) {
235                 const char *s = status_item_list[i];
236                 size_t item_len = strlen(s);
237
238                 if (line_len > item_len && line[item_len] == ':' &&
239                                 !strncmp(line, s, item_len))
240                         return i;
241         }
242         return -E_UNKNOWN_STAT_ITEM;
243 }
244
245 /**
246  * call a custom function for each complete line
247  *
248  * \param buf the buffer containing data seperated by newlines
249  * \param n the number of bytes in \a buf
250  * \param line_handler the custom function
251  *
252  * If \a line_handler is \p NULL, return number of complete lines in buf.
253  * Otherwise, call \a line_handler for each complete line.  The rest of the
254  * buffer (last chunk containing incomplete line is moved to the beginning of
255  * the buffer.
256  *
257  * \return If line_handler is not NULL, this function returns the number
258  * of bytes not handled to \a line_handler.
259  */
260 unsigned for_each_line(char *buf, int n, void (*line_handler)(char *))
261 {
262         char *start = buf, *end;
263         int i, num_lines = 0;
264
265 //      PARA_INFO_LOG("buf: %s", buf);
266         while (start < buf + n) {
267                 char *next_null;
268                 char *next_cr;
269
270                 next_cr = memchr(start, '\n', buf + n - start);
271                 next_null = memchr(start, '\0', buf + n - start);
272                 if (!next_cr && !next_null)
273                         break;
274                 if (next_cr && next_null) {
275                         end = next_cr < next_null? next_cr : next_null;
276                 } else if (next_null) {
277                         end = next_null;
278                 } else
279                         end = next_cr;
280                 num_lines++;
281                 if (line_handler) {
282                         *end = '\0';
283 //                      PARA_INFO_LOG("calling line handler: %s\n", start);
284                         line_handler(start);
285                         start = ++end;
286                 } else
287                         start = ++end;
288         }
289         if (!line_handler)
290                 return num_lines;
291         i = buf + n - start;
292         if (i && i != n)
293                 memmove(buf, start, i);
294         return i;
295 }
296