89a1f49b9f823120c36e8c3f29922d45497a9225
[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,audiod,gui}. */
48 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
49
50 static void dump_stat_client_list(void)
51 {
52 struct stat_client *sc;
53
54 if (!initialized)
55 return;
56 list_for_each_entry(sc, &client_list, node)
57 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
58 }
59 /**
60 * Add a status client to the list.
61 *
62 * \param fd The file descriptor of the client.
63 * \param mask Bitfield of status items for this client.
64 *
65 * Only those status items having the bit set in \a mask will be
66 * sent to the client.
67 *
68 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
69 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
70 */
71 int stat_client_add(int fd, long unsigned mask)
72 {
73 struct stat_client *new_client;
74
75 if (num_clients >= MAX_STAT_CLIENTS) {
76 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
77 MAX_STAT_CLIENTS);
78 return -E_TOO_MANY_CLIENTS;
79 }
80 if (!initialized) {
81 INIT_LIST_HEAD(&client_list);
82 initialized = 1;
83 }
84 PARA_INFO_LOG("adding client on fd %d\n", fd);
85 new_client = para_malloc(sizeof(struct stat_client));
86 new_client->fd = fd;
87 new_client->item_mask = mask;
88 para_list_add(&new_client->node, &client_list);
89 dump_stat_client_list();
90 num_clients++;
91 return 1;
92 }
93 /**
94 * Write a message to all connected status clients.
95 *
96 * \param msg A \p NULL terminated buffer.
97 * \param itemnum The number of the status item of \a msg.
98 *
99 * On write errors, remove the status client from the client list and close its
100 * file descriptor.
101 */
102 void stat_client_write(const char *msg, int itemnum)
103 {
104 struct stat_client *sc, *tmp;
105 size_t len = strlen(msg);
106 struct timeval tv = {0 , 0};
107
108 if (!initialized || !len)
109 return;
110 list_for_each_entry_safe(sc, tmp, &client_list, node) {
111 int fd = sc->fd, ret;
112 fd_set wfds;
113
114 if (!((1 << itemnum) & sc->item_mask))
115 continue;
116 FD_ZERO(&wfds);
117 FD_SET(fd, &wfds);
118 ret = para_select(fd + 1, NULL, &wfds, &tv);
119 if (ret > 0) {
120 ret = write(fd, msg, len);
121 PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg, fd, ret);
122 if (ret == len)
123 continue;
124 }
125 /* write error or fd not ready for writing */
126 close(fd);
127 num_clients--;
128 PARA_INFO_LOG("deleting client on fd %d\n", fd);
129 list_del(&sc->node);
130 free(sc);
131 dump_stat_client_list();
132 }
133 PARA_DEBUG_LOG("%d client(s)\n", num_clients);
134 }
135
136 /**
137 * Check if string is a known status item.
138 *
139 * \param item Buffer containing the text to check.
140 *
141 * \return If \a item is a valid status item, the number of that status item is
142 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
143 */
144 int stat_item_valid(const char *item)
145 {
146 int i;
147 if (!item || !*item) {
148 PARA_ERROR_LOG("%s\n", "no item");
149 return -E_UNKNOWN_STAT_ITEM;
150 }
151 FOR_EACH_STATUS_ITEM(i)
152 if (!strcmp(status_item_list[i], item))
153 return i;
154 PARA_ERROR_LOG("invalid stat item: %s\n", item);
155 return -E_UNKNOWN_STAT_ITEM;
156 }
157
158 /**
159 * Check if line starts with known status item.
160 *
161 * \param line Buffer containing the line.
162 *
163 * \return If the beginning of \a line matches any paraslash status item and is
164 * followed by a colon, the number of that status item is returned. Otherwise,
165 * this function returns \p -E_UNKNOWN_STAT_ITEM.
166 */
167 int stat_line_valid(const char *line)
168 {
169 int i;
170 size_t line_len;
171
172 if (!line || !*line)
173 return -E_UNKNOWN_STAT_ITEM;
174 line_len = strlen(line);
175 FOR_EACH_STATUS_ITEM(i) {
176 const char *s = status_item_list[i];
177 size_t item_len = strlen(s);
178
179 if (line_len > item_len && line[item_len] == ':' &&
180 !strncmp(line, s, item_len))
181 return i;
182 }
183 return -E_UNKNOWN_STAT_ITEM;
184 }
185