0457a0f3dc689139179f9a5495e110b6a260f8f3
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 * \file stat.c Functions used for sending/receiving the status of para_server
13 #include <sys/types.h>
17 #include "close_on_fork.h"
23 /** The maximal number of simultaneous connections. */
24 #define MAX_STAT_CLIENTS 50
27 * Describes a status client of para_audiod.
29 * There's one such structure per audiod client that sent the 'stat' command.
31 * A status client is identified by its file descriptor. para_audiod
32 * keeps a list of connected status clients.
35 /** The stat client's file descriptor. */
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
;
43 static struct list_head client_list
;
44 static int initialized
;
45 static int num_clients
;
47 /** The list of all status items used by para_{server,audiod,gui}. */
48 const char *status_item_list
[] = {STATUS_ITEM_ARRAY
};
50 static void dump_stat_client_list(void)
52 struct stat_client
*sc
;
56 list_for_each_entry(sc
, &client_list
, node
)
57 PARA_INFO_LOG("stat client on fd %d\n", sc
->fd
);
60 * Add a status client to the list.
62 * \param fd The file descriptor of the client.
63 * \param mask Bitfield of status items for this client.
65 * Only those status items having the bit set in \a mask will be
68 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
69 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
71 int stat_client_add(int fd
, long unsigned mask
)
73 struct stat_client
*new_client
;
75 if (num_clients
>= MAX_STAT_CLIENTS
) {
76 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
78 return -E_TOO_MANY_CLIENTS
;
81 INIT_LIST_HEAD(&client_list
);
84 PARA_INFO_LOG("adding client on fd %d\n", fd
);
85 new_client
= para_malloc(sizeof(struct stat_client
));
87 new_client
->item_mask
= mask
;
88 para_list_add(&new_client
->node
, &client_list
);
89 dump_stat_client_list();
94 * Write a message to all connected status clients.
96 * \param msg A \p NULL terminated buffer.
97 * \param itemnum The number of the status item of \a msg.
99 * On write errors, remove the status client from the client list and close its
102 void stat_client_write(const char *msg
, int itemnum
)
104 struct stat_client
*sc
, *tmp
;
105 size_t len
= strlen(msg
);
107 if (!initialized
|| !len
)
109 list_for_each_entry_safe(sc
, tmp
, &client_list
, node
) {
110 int fd
= sc
->fd
, ret
;
112 if (!((1 << itemnum
) & sc
->item_mask
))
114 if (write_ok(fd
) > 0) {
115 ret
= write(fd
, msg
, len
);
116 PARA_DEBUG_LOG("dumped %s to fd %d, ret = %d\n", msg
, fd
, ret
);
120 /* write error or fd not ready for writing */
123 PARA_INFO_LOG("deleting client on fd %d\n", fd
);
126 dump_stat_client_list();
128 PARA_DEBUG_LOG("%d client(s)\n", num_clients
);
132 * Check if string is a known status item.
134 * \param item Buffer containing the text to check.
136 * \return If \a item is a valid status item, the number of that status item is
137 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
139 int stat_item_valid(const char *item
)
142 if (!item
|| !*item
) {
143 PARA_ERROR_LOG("%s\n", "no item");
144 return -E_UNKNOWN_STAT_ITEM
;
146 FOR_EACH_STATUS_ITEM(i
)
147 if (!strcmp(status_item_list
[i
], item
))
149 PARA_ERROR_LOG("invalid stat item: %s\n", item
);
150 return -E_UNKNOWN_STAT_ITEM
;
154 * Check if line starts with known status item.
156 * \param line Buffer containing the line.
158 * \return If the beginning of \a line matches any paraslash status item and is
159 * followed by a colon, the number of that status item is returned. Otherwise,
160 * this function returns \p -E_UNKNOWN_STAT_ITEM.
162 int stat_line_valid(const char *line
)
168 return -E_UNKNOWN_STAT_ITEM
;
169 line_len
= strlen(line
);
170 FOR_EACH_STATUS_ITEM(i
) {
171 const char *s
= status_item_list
[i
];
172 size_t item_len
= strlen(s
);
174 if (line_len
> item_len
&& line
[item_len
] == ':' &&
175 !strncmp(line
, s
, item_len
))
178 return -E_UNKNOWN_STAT_ITEM
;