configure.ac: Add check for unix socket credentials
[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  * The structure associated with a connected client that sent the 'stat' command
35 *
36  *
37  * A status client is identified by its file descriptor.  para_audiod
38  * keeps a list of connected status clients.
39  */
40 struct stat_client {
41 /**
42  *
43  *
44  * the stat client's file descriptor
45  */
46 int fd;
47 long unsigned item_mask;
48 /**
49  *
50  * its entry in the list of stat clients
51 */
52 struct list_head node;
53 };
54
55 static struct list_head client_list;
56 static int initialized;
57 static int num_clients;
58
59 /**
60  * the list of all status items sent by para_server/para_audiod
61  */
62 const char *status_item_list[NUM_STAT_ITEMS] = {
63         [SI_STATUS_BAR] = "status_bar",
64         [SI_STATUS] = "status",
65         [SI_NUM_PLAYED] = "num_played",
66
67         [SI_MTIME] = "mtime",
68         [SI_LENGTH_MIN] = "length_min",
69         [SI_LENGTH_SEC] = "length_sec",
70
71         [SI_FILE_SIZE] = "file_size",
72         [SI_STATUS_FLAGS] = "status_flags",
73         [SI_FORMAT] = "format",
74
75         [SI_SCORE] = "score",
76         [SI_AUDIO_INFO1] = "audio_file_info1",
77         [SI_AUDIO_INFO2] = "audio_file_info2",
78
79         [SI_AUDIO_INFO3] = "audio_file_info3",
80         [SI_DBINFO1] = "dbinfo1",
81         [SI_DBINFO2] = "dbinfo2",
82
83         [SI_DBINFO3] = "dbinfo3",
84         [SI_DECODER_FLAGS] = "decoder_flags",
85         [SI_AUDIOD_STATUS] = "audiod_status",
86
87         [SI_PLAY_TIME] = "play_time",
88         [SI_UPTIME] = "uptime",
89         [SI_OFFSET] = "offset",
90
91         [SI_LENGTH] = "length",
92         [SI_STREAM_START] = "stream_start",
93         [SI_CURRENT_TIME] = "current_time",
94
95         [SI_AUDIOD_UPTIME] = "audiod_uptime",
96         [SI_SELECTOR] = "dbtool"
97 };
98 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
99
100 static void dump_stat_client_list(void)
101 {
102         struct stat_client *sc;
103
104         if (!initialized)
105                 return;
106         list_for_each_entry(sc, &client_list, node)
107                 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
108 }
109 /**
110  * add a status client to the list
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         add_close_on_fork_list(fd);
133         list_add(&new_client->node, &client_list);
134         dump_stat_client_list();
135         num_clients++;
136         return 1;
137 }
138 /**
139  * write a message to all connected status clients
140  *
141  * \param msg a \p NULL terminated buffer
142  * \param itemnum The number of the status item of \a msg
143  *
144  * On write errors, remove the status client from the client list and close its
145  * file descriptor.
146  */
147 void stat_client_write(char *msg, int itemnum)
148 {
149         struct stat_client *sc, *tmp;
150         ssize_t len;
151         struct timeval tv = {0 , 0};
152
153         if (!initialized)
154                 return;
155         len = strlen(msg);
156         list_for_each_entry_safe(sc, tmp, &client_list, node) {
157                 int fd = sc->fd, ret;
158                 fd_set wfds;
159
160                 if (!((1 << itemnum) & sc->item_mask))
161                         continue;
162                 FD_ZERO(&wfds);
163                 FD_SET(fd, &wfds);
164 //              PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
165                 ret = para_select(fd + 1, NULL, &wfds, &tv);
166                 if (ret > 0) {
167                         ret = write(fd, msg, len);
168                         PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
169                         if (ret == len )
170                                 continue;
171                 }
172                 /* write error or fd not ready for writing */
173                 close(fd);
174                 del_close_on_fork_list(fd);
175                 num_clients--;
176                 PARA_INFO_LOG("deleting client on fd %d\n", fd);
177                 list_del(&sc->node);
178                 free(sc);
179                 dump_stat_client_list();
180         }
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         PARA_ERROR_LOG("%s\n", "no item");
216                 return -E_UNKNOWN_STAT_ITEM;
217         }
218         FOR_EACH_STAT_ITEM(i)
219                 if (!strcmp(status_item_list[i], item))
220                         return i;
221         return -E_UNKNOWN_STAT_ITEM;
222 }
223
224 /**
225  * check if line starts with known status item.
226  *
227  * \param line buffer containing the line
228  *
229  * \return If the beginning of \a line matches any paraslash status item and is
230  * followed by a colon, the number of that status item is returned. Otherwise,
231  * this function returns \p -E_UNKNOWN_STAT_ITEM.
232  */
233 int stat_line_valid(const char *line)
234 {
235         int i;
236         size_t line_len;
237
238         if (!line || !*line)
239                 return -E_UNKNOWN_STAT_ITEM;
240         line_len = strlen(line);
241         FOR_EACH_STAT_ITEM(i) {
242                 const char *s = status_item_list[i];
243                 size_t item_len = strlen(s);
244
245                 if (line_len > item_len && line[item_len] == ':' &&
246                                 !strncmp(line, s, item_len))
247                         return i;
248         }
249         return -E_UNKNOWN_STAT_ITEM;
250 }
251
252 /**
253  * call a custom function for each complete line
254  *
255  * \param buf the buffer containing data seperated by newlines
256  * \param n the number of bytes in \a buf
257  * \param line_handler the custom function
258  *
259  * If \a line_handler is \p NULL, return number of complete lines in buf.
260  * Otherwise, call \a line_handler for each complete line.  The rest of the
261  * buffer (last chunk containing incomplete line is moved to the beginning of
262  * the buffer.
263  *
264  * \return If line_handler is not NULL, this function returns the number
265  * of bytes not handled to \a line_handler.
266  */
267 unsigned for_each_line(char *buf, int n, void (*line_handler)(char *))
268 {
269         char *start = buf, *end;
270         int i, num_lines = 0;
271
272         PARA_INFO_LOG("buf: %s", buf);
273         while (start < buf + n) {
274                 char *next_null;
275                 char *next_cr;
276
277                 next_cr = memchr(start, '\n', buf + n - start);
278                 next_null = memchr(start, '\0', buf + n - start);
279                 if (!next_cr && !next_null)
280                         break;
281                 if (next_cr && next_null) {
282                         end = next_cr < next_null? next_cr : next_null;
283                 } else if (next_null) {
284                         end = next_null;
285                 } else
286                         end = next_cr;
287                 num_lines++;
288                 if (line_handler) {
289                         *end = '\0';
290                         PARA_INFO_LOG("calling line handler: %s\n", start);
291                         line_handler(start);
292                         start = ++end;
293                 } else
294                         start = ++end;
295         }
296         if (!line_handler)
297                 return num_lines;
298         i = buf + n - start;
299         if (i && i != n)
300                 memmove(buf, start, i);
301         return i;
302 }
303