2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
20 * \file stat.c functions used for sending/receiving the status of para_server
24 #include "close_on_fork.h"
30 /** the maximal number of simultaneous connections */
31 #define MAX_STAT_CLIENTS 50
34 * describes a status client of para_audiod
36 * There's one such structure per audiod client that sent the 'stat' command.
38 * A status client is identified by its file descriptor. para_audiod
39 * keeps a list of connected status clients.
42 /** the stat client's file descriptor */
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
;
50 static struct list_head client_list
;
51 static int initialized
;
52 static int num_clients
;
55 * the list of all status items sent by para_server/para_audiod
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",
63 [SI_LENGTH_MIN
] = "length_min",
64 [SI_LENGTH_SEC
] = "length_sec",
66 [SI_FILE_SIZE
] = "file_size",
67 [SI_STATUS_FLAGS
] = "status_flags",
68 [SI_FORMAT
] = "format",
71 [SI_AUDIO_INFO1
] = "audio_file_info1",
72 [SI_AUDIO_INFO2
] = "audio_file_info2",
74 [SI_AUDIO_INFO3
] = "audio_file_info3",
75 [SI_DBINFO1
] = "dbinfo1",
76 [SI_DBINFO2
] = "dbinfo2",
78 [SI_DBINFO3
] = "dbinfo3",
79 [SI_DECODER_FLAGS
] = "decoder_flags",
80 [SI_AUDIOD_STATUS
] = "audiod_status",
82 [SI_PLAY_TIME
] = "play_time",
83 [SI_UPTIME
] = "uptime",
84 [SI_OFFSET
] = "offset",
86 [SI_LENGTH
] = "length",
87 [SI_STREAM_START
] = "stream_start",
88 [SI_CURRENT_TIME
] = "current_time",
90 [SI_AUDIOD_UPTIME
] = "audiod_uptime",
91 [SI_SELECTOR
] = "dbtool"
94 static void dump_stat_client_list(void)
96 struct stat_client
*sc
;
100 list_for_each_entry(sc
, &client_list
, node
)
101 PARA_INFO_LOG("stat client on fd %d\n", sc
->fd
);
104 * add a status client to the list
106 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
107 * the number of connected clients exceeds #MAX_STAT_CLIENTS
109 int stat_client_add(int fd
, long unsigned mask
)
111 struct stat_client
*new_client
;
113 if (num_clients
>= MAX_STAT_CLIENTS
) {
114 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
116 return -E_TOO_MANY_CLIENTS
;
119 INIT_LIST_HEAD(&client_list
);
122 PARA_INFO_LOG("adding client on fd %d\n", fd
);
123 new_client
= para_malloc(sizeof(struct stat_client
));
125 new_client
->item_mask
= mask
;
126 list_add(&new_client
->node
, &client_list
);
127 dump_stat_client_list();
132 * write a message to all connected status clients
134 * \param msg a \p NULL terminated buffer
135 * \param itemnum The number of the status item of \a msg
137 * On write errors, remove the status client from the client list and close its
140 void stat_client_write(char *msg
, int itemnum
)
142 struct stat_client
*sc
, *tmp
;
144 struct timeval tv
= {0 , 0};
149 list_for_each_entry_safe(sc
, tmp
, &client_list
, node
) {
150 int fd
= sc
->fd
, ret
;
153 if (!((1 << itemnum
) & sc
->item_mask
))
157 // PARA_DEBUG_LOG("%s: p=%lx\n", __func__, (long)p);
158 ret
= para_select(fd
+ 1, NULL
, &wfds
, &tv
);
160 ret
= write(fd
, msg
, len
);
161 PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg
, fd
, ret
);
165 /* write error or fd not ready for writing */
168 PARA_INFO_LOG("deleting client on fd %d\n", fd
);
171 dump_stat_client_list();
173 PARA_DEBUG_LOG("%d client(s)\n", num_clients
);
178 * check if string is a known status item.
180 * \param item buffer containing the text to check
182 * \return If \a item is a valid status item, the number of that status item is
183 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
185 int stat_item_valid(const char *item
)
188 if (!item
|| !*item
) {
189 PARA_ERROR_LOG("%s\n", "no item");
190 return -E_UNKNOWN_STAT_ITEM
;
192 FOR_EACH_STAT_ITEM(i
)
193 if (!strcmp(status_item_list
[i
], item
))
195 return -E_UNKNOWN_STAT_ITEM
;
199 * check if line starts with known status item.
201 * \param line buffer containing the line
203 * \return If the beginning of \a line matches any paraslash status item and is
204 * followed by a colon, the number of that status item is returned. Otherwise,
205 * this function returns \p -E_UNKNOWN_STAT_ITEM.
207 int stat_line_valid(const char *line
)
213 return -E_UNKNOWN_STAT_ITEM
;
214 line_len
= strlen(line
);
215 FOR_EACH_STAT_ITEM(i
) {
216 const char *s
= status_item_list
[i
];
217 size_t item_len
= strlen(s
);
219 if (line_len
> item_len
&& line
[item_len
] == ':' &&
220 !strncmp(line
, s
, item_len
))
223 return -E_UNKNOWN_STAT_ITEM
;
227 * call a custom function for each complete line
229 * \param buf the buffer containing data seperated by newlines
230 * \param n the number of bytes in \a buf
231 * \param line_handler the custom function
233 * If \a line_handler is \p NULL, return number of complete lines in buf.
234 * Otherwise, call \a line_handler for each complete line. The rest of the
235 * buffer (last chunk containing incomplete line is moved to the beginning of
238 * \return If line_handler is not NULL, this function returns the number
239 * of bytes not handled to \a line_handler.
241 unsigned for_each_line(char *buf
, int n
, void (*line_handler
)(char *))
243 char *start
= buf
, *end
;
244 int i
, num_lines
= 0;
246 // PARA_INFO_LOG("buf: %s", buf);
247 while (start
< buf
+ n
) {
251 next_cr
= memchr(start
, '\n', buf
+ n
- start
);
252 next_null
= memchr(start
, '\0', buf
+ n
- start
);
253 if (!next_cr
&& !next_null
)
255 if (next_cr
&& next_null
) {
256 end
= next_cr
< next_null
? next_cr
: next_null
;
257 } else if (next_null
) {
264 // PARA_INFO_LOG("calling line handler: %s\n", start);
274 memmove(buf
, start
, i
);