afs.cmd: Update help output.
[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
12
13 #include <sys/types.h>
14 #include <dirent.h>
15
16 #include "para.h"
17 #include "close_on_fork.h"
18 #include "list.h"
19 #include "error.h"
20 #include "string.h"
21 #include "fd.h"
22
23 /** the maximal number of simultaneous connections */
24 #define MAX_STAT_CLIENTS 50
25
26 /**
27  * describes a status client of para_audiod
28  *
29  * There's one such structure per audiod client that sent the 'stat' command.
30  *
31  * A status client is identified by its file descriptor.  para_audiod
32  * keeps a list of connected status clients.
33  */
34 struct stat_client {
35         /** the stat client's file descriptor */
36         int fd;
37         /** bitmask of those status items the client is interested in */
38         long unsigned item_mask;
39         /** its entry in the list of stat clients */
40         struct list_head node;
41 };
42
43 static struct list_head client_list;
44 static int initialized;
45 static int num_clients;
46
47 /**
48  * the list of all status items sent by para_server/para_audiod
49  */
50 const char *status_item_list[NUM_STAT_ITEMS] = {
51         [SI_STATUS_BAR] = "status_bar",
52         [SI_STATUS] = "status",
53         [SI_NUM_PLAYED] = "num_played",
54
55         [SI_MTIME] = "mtime",
56         [SI_LENGTH_MIN] = "length_min",
57         [SI_LENGTH_SEC] = "length_sec",
58
59         [SI_FILE_SIZE] = "file_size",
60         [SI_STATUS_FLAGS] = "status_flags",
61         [SI_FORMAT] = "format",
62
63         [SI_SCORE] = "score",
64         [SI_AUDIO_INFO1] = "audio_file_info1",
65         [SI_AUDIO_INFO2] = "audio_file_info2",
66
67         [SI_AUDIO_INFO3] = "audio_file_info3",
68         [SI_DBINFO1] = "dbinfo1",
69         [SI_DBINFO2] = "dbinfo2",
70
71         [SI_DBINFO3] = "dbinfo3",
72         [SI_DECODER_FLAGS] = "decoder_flags",
73         [SI_AUDIOD_STATUS] = "audiod_status",
74
75         [SI_PLAY_TIME] = "play_time",
76         [SI_UPTIME] = "uptime",
77         [SI_OFFSET] = "offset",
78
79         [SI_LENGTH] = "length",
80         [SI_STREAM_START] = "stream_start",
81         [SI_CURRENT_TIME] = "current_time",
82
83         [SI_AUDIOD_UPTIME] = "audiod_uptime",
84         [SI_SELECTOR] = "dbtool"
85 };
86
87 static void dump_stat_client_list(void)
88 {
89         struct stat_client *sc;
90
91         if (!initialized)
92                 return;
93         list_for_each_entry(sc, &client_list, node)
94                 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
95 }
96 /**
97  * add a status client to the list
98  *
99  * \param fd the file descriptor of the client
100  * \param mask bitfield of status items for this client
101  *
102  * Only those status items having the bit set in \a mask will be
103  * sent to the client.
104  *
105  * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
106  * the number of connected clients exceeds #MAX_STAT_CLIENTS.
107  */
108 int stat_client_add(int fd, long unsigned mask)
109 {
110         struct stat_client *new_client;
111
112         if (num_clients >= MAX_STAT_CLIENTS) {
113                 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
114                         MAX_STAT_CLIENTS);
115                 return -E_TOO_MANY_CLIENTS;
116         }
117         if (!initialized) {
118                 INIT_LIST_HEAD(&client_list);
119                 initialized = 1;
120         }
121         PARA_INFO_LOG("adding client on fd %d\n", fd);
122         new_client = para_malloc(sizeof(struct stat_client));
123         new_client->fd = fd;
124         new_client->item_mask = mask;
125         para_list_add(&new_client->node, &client_list);
126         dump_stat_client_list();
127         num_clients++;
128         return 1;
129 }
130 /**
131  * write a message to all connected status clients
132  *
133  * \param msg a \p NULL terminated buffer
134  * \param itemnum The number of the status item of \a msg
135  *
136  * On write errors, remove the status client from the client list and close its
137  * file descriptor.
138  */
139 void stat_client_write(const char *msg, int itemnum)
140 {
141         struct stat_client *sc, *tmp;
142         size_t len = strlen(msg);
143         struct timeval tv = {0 , 0};
144
145         if (!initialized || !len)
146                 return;
147         list_for_each_entry_safe(sc, tmp, &client_list, node) {
148                 int fd = sc->fd, ret;
149                 fd_set wfds;
150
151                 if (!((1 << itemnum) & sc->item_mask))
152                         continue;
153                 FD_ZERO(&wfds);
154                 FD_SET(fd, &wfds);
155 //              PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
156                 ret = para_select(fd + 1, NULL, &wfds, &tv);
157                 if (ret > 0) {
158                         ret = write(fd, msg, len);
159                         PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
160                         if (ret == len )
161                                 continue;
162                 }
163                 /* write error or fd not ready for writing */
164                 close(fd);
165                 num_clients--;
166                 PARA_INFO_LOG("deleting client on fd %d\n", fd);
167                 list_del(&sc->node);
168                 free(sc);
169                 dump_stat_client_list();
170         }
171         PARA_DEBUG_LOG("%d client(s)\n", num_clients);
172 }
173
174
175 /**
176  * check if string is a known status item.
177  *
178  * \param item buffer containing the text to check
179  *
180  * \return If \a item is a valid status item, the number of that status item is
181  * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
182  */
183 int stat_item_valid(const char *item)
184 {
185         int i;
186         if (!item || !*item) {
187         PARA_ERROR_LOG("%s\n", "no item");
188                 return -E_UNKNOWN_STAT_ITEM;
189         }
190         FOR_EACH_STAT_ITEM(i)
191                 if (!strcmp(status_item_list[i], item))
192                         return i;
193         return -E_UNKNOWN_STAT_ITEM;
194 }
195
196 /**
197  * check if line starts with known status item.
198  *
199  * \param line buffer containing the line
200  *
201  * \return If the beginning of \a line matches any paraslash status item and is
202  * followed by a colon, the number of that status item is returned. Otherwise,
203  * this function returns \p -E_UNKNOWN_STAT_ITEM.
204  */
205 int stat_line_valid(const char *line)
206 {
207         int i;
208         size_t line_len;
209
210         if (!line || !*line)
211                 return -E_UNKNOWN_STAT_ITEM;
212         line_len = strlen(line);
213         FOR_EACH_STAT_ITEM(i) {
214                 const char *s = status_item_list[i];
215                 size_t item_len = strlen(s);
216
217                 if (line_len > item_len && line[item_len] == ':' &&
218                                 !strncmp(line, s, item_len))
219                         return i;
220         }
221         return -E_UNKNOWN_STAT_ITEM;
222 }
223