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