2 * This file is part of buxton.
4 * Copyright (C) 2013 Intel Corporation
6 * buxton is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
13 * \file daemon.h Internal header
14 * This file is used internally by buxton to provide functionality
15 * used for the buxtond
24 #include <sys/socket.h>
31 #include "serialize.h"
34 * List for daemon's clients
36 typedef struct client_list_item {
37 LIST_FIELDS(struct client_list_item, item); /**<List type */
38 int fd; /**<File descriptor of connected client */
39 struct ucred cred; /**<Credentials of connected client */
40 BuxtonString *smack_label; /**<Smack label of connected client */
41 uint8_t *data; /**<Data buffer for the client */
42 size_t offset; /**<Current position to write to data buffer */
43 size_t size; /**<Size of the data buffer */
47 * Notification registration
49 typedef struct BuxtonNotification {
50 client_list_item *client; /**<Client */
51 BuxtonData *old_data; /**<Old value of a particular key*/
52 uint32_t msgid; /**<Message id from the client */
56 * Global store of buxtond state
58 typedef struct BuxtonDaemon {
60 size_t accepting_alloc;
63 struct pollfd *pollfds;
64 client_list_item *client_list;
65 Hashmap *notify_mapping;
70 * Take a BuxtonData array and set key, layer and value items
72 * @param msg Control message specifying how to parse the array
73 * @param count Number of elements in the array
74 * @param list BuxtonData array to parse
75 * @param key Pointer to pointer to set if key is used
76 * @param value Pointer to pointer to set if value is used
77 * @returns bool indicating success of parsing
79 bool parse_list(BuxtonControlMessage msg, size_t count, BuxtonData *list,
80 _BuxtonKey *key, BuxtonData **value)
81 __attribute__((warn_unused_result));
84 * Handle a message within buxtond
85 * @param self Reference to BuxtonDaemon
86 * @param client Current client
87 * @param size Size of the data being handled
88 * @returns bool True if message was successfully handled
90 bool buxtond_handle_message(BuxtonDaemon *self,
91 client_list_item *client,
93 __attribute__((warn_unused_result));
96 * Notify clients a value changes in buxtond
97 * @param self Refernece to BuxtonDaemon
98 * @param client Current client
99 * @param key Modified key
100 * @param value Modified value
102 void buxtond_notify_clients(BuxtonDaemon *self, client_list_item *client,
103 _BuxtonKey* key, BuxtonData *value);
106 * Buxton daemon function for setting a value
107 * @param self buxtond instance being run
108 * @param client Used to validate smack access
109 * @param key Key for the value being set
110 * @param value Value being set
111 * @param status Will be set with the int32_t result of the operation
113 void set_value(BuxtonDaemon *self, client_list_item *client,
114 _BuxtonKey *key, BuxtonData *value, int32_t *status);
117 * Buxton daemon function for setting a label
118 * @param self buxtond instance being run
119 * @param client Used to validate smack access
120 * @param key Key or group for the label being set
121 * @param value Label being set
122 * @param status Will be set with the int32_t result of the operation
124 void set_label(BuxtonDaemon *self, client_list_item *client,
125 _BuxtonKey *key, BuxtonData *value, int32_t *status);
128 * Buxton daemon function for creating a group
129 * @param self buxtond instance being run
130 * @param client Used to validate smack access
131 * @param key Key with layer and group members initialized
132 * @param status Will be set with the int32_t result of the operation
134 void create_group(BuxtonDaemon *self, client_list_item *client,
135 _BuxtonKey *key, int32_t *status);
138 * Buxton daemon function for removing a group
139 * @param self buxtond instance being run
140 * @param client Used to validate smack access
141 * @param key Key with layer and group members initialized
142 * @param status Will be set with the int32_t result of the operation
144 void remove_group(BuxtonDaemon *self, client_list_item *client,
145 _BuxtonKey *key, int32_t *status);
148 * Buxton daemon function for getting a value
149 * @param self buxtond instance being run
150 * @param client Used to validate smack access
151 * @param key Key for the value being sought
152 * @param status Will be set with the int32_t result of the operation
153 * @returns BuxtonData Value stored for key if successful otherwise NULL
155 BuxtonData *get_value(BuxtonDaemon *self, client_list_item *client,
156 _BuxtonKey *key, int32_t *status)
157 __attribute__((warn_unused_result));
160 * Buxton daemon function for unsetting a value
161 * @param self buxtond instance being run
162 * @param client Used to validate smack access
163 * @param key Key for the value being dunset
164 * @param status Will be set with the int32_t result of the operation
166 void unset_value(BuxtonDaemon *self, client_list_item *client,
167 _BuxtonKey *key, int32_t *status);
170 * Buxton daemon function for listing keys in a given layer
171 * @param self buxtond instance being run
172 * @param client Used to validate smack access
173 * @param layer Layer to query
174 * @param status Will be set with the int32_t result of the operation
176 BuxtonArray *list_keys(BuxtonDaemon *self, client_list_item *client,
177 BuxtonString *layer, int32_t *status)
178 __attribute__((warn_unused_result));
181 * Buxton daemon function for registering notifications on a given key
182 * @param self buxtond instance being run
183 * @param client Used to validate smack access
184 * @param key Key to notify for changes on
185 * @param msgid Message ID from the client
186 * @param status Will be set with the int32_t result of the operation
188 void register_notification(BuxtonDaemon *self, client_list_item *client,
189 _BuxtonKey *key, uint32_t msgid,
193 * Buxton daemon function for unregistering notifications from the given key
194 * @param self buxtond instance being run
195 * @param client Used to validate smack access
196 * @param key Key to no longer recieve notifications for
197 * @param status Will be set with the int32_t result of the operation
198 * @return Message ID used to send key's notifications to the client
200 uint32_t unregister_notification(BuxtonDaemon *self, client_list_item *client,
201 _BuxtonKey *key, int32_t *status)
202 __attribute__((warn_unused_result));
205 * Verify credentials for the client socket
206 * @param cl Client to check the credentials of
207 * @return bool indicating credentials where found or not
209 bool identify_client(client_list_item *cl)
210 __attribute__((warn_unused_result));
213 * Add a fd to daemon's poll list
214 * @param self buxtond instance being run
215 * @param fd File descriptor to add to the poll list
216 * @param events Priority mask for events
217 * @param a Accepting status of the fd
220 void add_pollfd(BuxtonDaemon *self, int fd, short events, bool a);
223 * Add a fd to daemon's poll list
224 * @param self buxtond instance being run
225 * @param i File descriptor to remove from poll list
228 void del_pollfd(BuxtonDaemon *self, nfds_t i);
231 * Setup a client's smack label
232 * @param cl Client to set smack label on
235 void handle_smack_label(client_list_item *cl);
238 * Handle a client connection
239 * @param self buxtond instance being run
240 * @param cl The currently activate client
241 * @param i The currently active file descriptor
242 * @return bool indicating more data to process
244 bool handle_client(BuxtonDaemon *self, client_list_item *cl, nfds_t i)
245 __attribute__((warn_unused_result));
248 * Terminate client connectoin
249 * @param self buxtond instance being run
250 * @param cl The client to terminate
251 * @param i File descriptor to remove from poll list
253 void terminate_client(BuxtonDaemon *self, client_list_item *cl, nfds_t i);
256 * Editor modelines - http://www.wireshark.org/tools/modelines.html
261 * indent-tabs-mode: t
264 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
265 * :indentSize=8:tabSize=8:noTabs=false: