Imported Upstream version 3
[platform/upstream/buxton.git] / src / core / daemon.h
1 /*
2  * This file is part of buxton.
3  *
4  * Copyright (C) 2013 Intel Corporation
5  *
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.
10  */
11
12 /**
13  * \file daemon.h Internal header
14  * This file is used internally by buxton to provide functionality
15  * used for the buxtond
16  */
17 #pragma once
18
19 #ifdef HAVE_CONFIG_H
20         #include "config.h"
21 #endif
22
23 #include <sys/poll.h>
24 #include <sys/socket.h>
25
26 #include "buxton.h"
27 #include "backend.h"
28 #include "hashmap.h"
29 #include "list.h"
30 #include "protocol.h"
31 #include "serialize.h"
32
33 /**
34  * List for daemon's clients
35  */
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 */
44 } client_list_item;
45
46 /**
47  * Notification registration
48  */
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 */
53 } BuxtonNotification;
54
55 /**
56  * Global store of buxtond state
57  */
58 typedef struct BuxtonDaemon {
59         size_t nfds_alloc;
60         size_t accepting_alloc;
61         nfds_t nfds;
62         bool *accepting;
63         struct pollfd *pollfds;
64         client_list_item *client_list;
65         Hashmap *notify_mapping;
66         Hashmap *client_key_mapping;
67         BuxtonControl buxton;
68 } BuxtonDaemon;
69
70 /**
71  * Take a BuxtonData array and set key, layer and value items
72  * correctly
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
79  */
80 bool parse_list(BuxtonControlMessage msg, size_t count, BuxtonData *list,
81                 _BuxtonKey *key, BuxtonData **value)
82         __attribute__((warn_unused_result));
83
84 /**
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
90  */
91 bool buxtond_handle_message(BuxtonDaemon *self,
92                               client_list_item *client,
93                               size_t size)
94         __attribute__((warn_unused_result));
95
96 /**
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
102  */
103 void buxtond_notify_clients(BuxtonDaemon *self, client_list_item *client,
104                               _BuxtonKey* key, BuxtonData *value);
105
106 /**
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
113  */
114 void set_value(BuxtonDaemon *self, client_list_item *client,
115                _BuxtonKey *key, BuxtonData *value, int32_t *status);
116
117 /**
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
124  */
125 void set_label(BuxtonDaemon *self, client_list_item *client,
126                _BuxtonKey *key, BuxtonData *value, int32_t *status);
127
128 /**
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
134  */
135 void create_group(BuxtonDaemon *self, client_list_item *client,
136                   _BuxtonKey *key, int32_t *status);
137
138 /**
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
144  */
145 void remove_group(BuxtonDaemon *self, client_list_item *client,
146                   _BuxtonKey *key, int32_t *status);
147
148 /**
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
155  */
156 BuxtonData *get_value(BuxtonDaemon *self, client_list_item *client,
157                       _BuxtonKey *key, int32_t *status)
158         __attribute__((warn_unused_result));
159
160 /**
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
166  */
167 void unset_value(BuxtonDaemon *self, client_list_item *client,
168                  _BuxtonKey *key, int32_t *status);
169
170 /**
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
176  */
177 BuxtonArray *list_keys(BuxtonDaemon *self, client_list_item *client,
178                        BuxtonString *layer, int32_t *status)
179         __attribute__((warn_unused_result));
180
181 /**
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
188  */
189 void register_notification(BuxtonDaemon *self, client_list_item *client,
190                            _BuxtonKey *key, uint32_t msgid,
191                            int32_t *status);
192
193 /**
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
200  */
201 uint32_t unregister_notification(BuxtonDaemon *self, client_list_item *client,
202                                  _BuxtonKey *key, int32_t *status)
203         __attribute__((warn_unused_result));
204
205 /**
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
209  */
210 bool identify_client(client_list_item *cl)
211         __attribute__((warn_unused_result));
212
213 /**
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
219  * @return None
220  */
221 void add_pollfd(BuxtonDaemon *self, int fd, short events, bool a);
222
223 /**
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
227  * @return None
228  */
229 void del_pollfd(BuxtonDaemon *self, nfds_t i);
230
231 /**
232  * Setup a client's smack label
233  * @param cl Client to set smack label on
234  * @return None
235  */
236 void handle_smack_label(client_list_item *cl);
237
238 /**
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
244  */
245 bool handle_client(BuxtonDaemon *self, client_list_item *cl, nfds_t i)
246         __attribute__((warn_unused_result));
247
248 /**
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
253  */
254 void terminate_client(BuxtonDaemon *self, client_list_item *cl, nfds_t i);
255
256 /*
257  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
258  *
259  * Local variables:
260  * c-basic-offset: 8
261  * tab-width: 8
262  * indent-tabs-mode: t
263  * End:
264  *
265  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
266  * :indentSize=8:tabSize=8:noTabs=false:
267  */