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