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;
66 Hashmap *client_key_mapping;
71 * Take a BuxtonData array and set key, layer and value items
73 * @param msg Control message specifying how to parse the array
74 * @param count Number of elements in the array
75 * @param list BuxtonData array to parse
76 * @param key Pointer to pointer to set if key is used
77 * @param value Pointer to pointer to set if value is used
78 * @returns bool indicating success of parsing
80 bool parse_list(BuxtonControlMessage msg, size_t count, BuxtonData *list,
81 _BuxtonKey *key, BuxtonData **value)
82 __attribute__((warn_unused_result));
85 * Handle a message within buxtond
86 * @param self Reference to BuxtonDaemon
87 * @param client Current client
88 * @param size Size of the data being handled
89 * @returns bool True if message was successfully handled
91 bool buxtond_handle_message(BuxtonDaemon *self,
92 client_list_item *client,
94 __attribute__((warn_unused_result));
97 * Notify clients a value changes in buxtond
98 * @param self Refernece to BuxtonDaemon
99 * @param client Current client
100 * @param key Modified key
101 * @param value Modified value
103 void buxtond_notify_clients(BuxtonDaemon *self, client_list_item *client,
104 _BuxtonKey* key, BuxtonData *value);
107 * Buxton daemon function for setting a value
108 * @param self buxtond instance being run
109 * @param client Used to validate smack access
110 * @param key Key for the value being set
111 * @param value Value being set
112 * @param status Will be set with the int32_t result of the operation
114 void set_value(BuxtonDaemon *self, client_list_item *client,
115 _BuxtonKey *key, BuxtonData *value, int32_t *status);
118 * Buxton daemon function for setting a label
119 * @param self buxtond instance being run
120 * @param client Used to validate smack access
121 * @param key Key or group for the label being set
122 * @param value Label being set
123 * @param status Will be set with the int32_t result of the operation
125 void set_label(BuxtonDaemon *self, client_list_item *client,
126 _BuxtonKey *key, BuxtonData *value, int32_t *status);
129 * Buxton daemon function for creating a group
130 * @param self buxtond instance being run
131 * @param client Used to validate smack access
132 * @param key Key with layer and group members initialized
133 * @param status Will be set with the int32_t result of the operation
135 void create_group(BuxtonDaemon *self, client_list_item *client,
136 _BuxtonKey *key, int32_t *status);
139 * Buxton daemon function for removing a group
140 * @param self buxtond instance being run
141 * @param client Used to validate smack access
142 * @param key Key with layer and group members initialized
143 * @param status Will be set with the int32_t result of the operation
145 void remove_group(BuxtonDaemon *self, client_list_item *client,
146 _BuxtonKey *key, int32_t *status);
149 * Buxton daemon function for getting a value
150 * @param self buxtond instance being run
151 * @param client Used to validate smack access
152 * @param key Key for the value being sought
153 * @param status Will be set with the int32_t result of the operation
154 * @returns BuxtonData Value stored for key if successful otherwise NULL
156 BuxtonData *get_value(BuxtonDaemon *self, client_list_item *client,
157 _BuxtonKey *key, int32_t *status)
158 __attribute__((warn_unused_result));
161 * Buxton daemon function for unsetting a value
162 * @param self buxtond instance being run
163 * @param client Used to validate smack access
164 * @param key Key for the value being dunset
165 * @param status Will be set with the int32_t result of the operation
167 void unset_value(BuxtonDaemon *self, client_list_item *client,
168 _BuxtonKey *key, int32_t *status);
171 * Buxton daemon function for listing keys in a given layer
172 * @param self buxtond instance being run
173 * @param client Used to validate smack access
174 * @param layer Layer to query
175 * @param status Will be set with the int32_t result of the operation
177 BuxtonArray *list_keys(BuxtonDaemon *self, client_list_item *client,
178 BuxtonString *layer, int32_t *status)
179 __attribute__((warn_unused_result));
182 * Buxton daemon function for registering notifications on a given key
183 * @param self buxtond instance being run
184 * @param client Used to validate smack access
185 * @param key Key to notify for changes on
186 * @param msgid Message ID from the client
187 * @param status Will be set with the int32_t result of the operation
189 void register_notification(BuxtonDaemon *self, client_list_item *client,
190 _BuxtonKey *key, uint32_t msgid,
194 * Buxton daemon function for unregistering notifications from the given key
195 * @param self buxtond instance being run
196 * @param client Used to validate smack access
197 * @param key Key to no longer recieve notifications for
198 * @param status Will be set with the int32_t result of the operation
199 * @return Message ID used to send key's notifications to the client
201 uint32_t unregister_notification(BuxtonDaemon *self, client_list_item *client,
202 _BuxtonKey *key, int32_t *status)
203 __attribute__((warn_unused_result));
206 * Verify credentials for the client socket
207 * @param cl Client to check the credentials of
208 * @return bool indicating credentials where found or not
210 bool identify_client(client_list_item *cl)
211 __attribute__((warn_unused_result));
214 * Add a fd to daemon's poll list
215 * @param self buxtond instance being run
216 * @param fd File descriptor to add to the poll list
217 * @param events Priority mask for events
218 * @param a Accepting status of the fd
221 void add_pollfd(BuxtonDaemon *self, int fd, short events, bool a);
224 * Add a fd to daemon's poll list
225 * @param self buxtond instance being run
226 * @param i File descriptor to remove from poll list
229 void del_pollfd(BuxtonDaemon *self, nfds_t i);
232 * Setup a client's smack label
233 * @param cl Client to set smack label on
236 void handle_smack_label(client_list_item *cl);
239 * Handle a client connection
240 * @param self buxtond instance being run
241 * @param cl The currently activate client
242 * @param i The currently active file descriptor
243 * @return bool indicating more data to process
245 bool handle_client(BuxtonDaemon *self, client_list_item *cl, nfds_t i)
246 __attribute__((warn_unused_result));
249 * Terminate client connectoin
250 * @param self buxtond instance being run
251 * @param cl The client to terminate
252 * @param i File descriptor to remove from poll list
254 void terminate_client(BuxtonDaemon *self, client_list_item *cl, nfds_t i);
257 * Editor modelines - http://www.wireshark.org/tools/modelines.html
262 * indent-tabs-mode: t
265 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
266 * :indentSize=8:tabSize=8:noTabs=false: