tizen 2.3 release
[external/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         BuxtonControl buxton;
67 } BuxtonDaemon;
68
69 /**
70  * Take a BuxtonData array and set key, layer and value items
71  * correctly
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
78  */
79 bool parse_list(BuxtonControlMessage msg, size_t count, BuxtonData *list,
80                 _BuxtonKey *key, BuxtonData **value)
81         __attribute__((warn_unused_result));
82
83 /**
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
89  */
90 bool buxtond_handle_message(BuxtonDaemon *self,
91                               client_list_item *client,
92                               size_t size)
93         __attribute__((warn_unused_result));
94
95 /**
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
101  */
102 void buxtond_notify_clients(BuxtonDaemon *self, client_list_item *client,
103                               _BuxtonKey* key, BuxtonData *value);
104
105 /**
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
112  */
113 void set_value(BuxtonDaemon *self, client_list_item *client,
114                _BuxtonKey *key, BuxtonData *value, int32_t *status);
115
116 /**
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
123  */
124 void set_label(BuxtonDaemon *self, client_list_item *client,
125                _BuxtonKey *key, BuxtonData *value, int32_t *status);
126
127 /**
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
133  */
134 void create_group(BuxtonDaemon *self, client_list_item *client,
135                   _BuxtonKey *key, int32_t *status);
136
137 /**
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
143  */
144 void remove_group(BuxtonDaemon *self, client_list_item *client,
145                   _BuxtonKey *key, int32_t *status);
146
147 /**
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
154  */
155 BuxtonData *get_value(BuxtonDaemon *self, client_list_item *client,
156                       _BuxtonKey *key, int32_t *status)
157         __attribute__((warn_unused_result));
158
159 /**
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
165  */
166 void unset_value(BuxtonDaemon *self, client_list_item *client,
167                  _BuxtonKey *key, int32_t *status);
168
169 /**
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
175  */
176 BuxtonArray *list_keys(BuxtonDaemon *self, client_list_item *client,
177                        BuxtonString *layer, int32_t *status)
178         __attribute__((warn_unused_result));
179
180 /**
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
187  */
188 void register_notification(BuxtonDaemon *self, client_list_item *client,
189                            _BuxtonKey *key, uint32_t msgid,
190                            int32_t *status);
191
192 /**
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
199  */
200 uint32_t unregister_notification(BuxtonDaemon *self, client_list_item *client,
201                                  _BuxtonKey *key, int32_t *status)
202         __attribute__((warn_unused_result));
203
204 /**
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
208  */
209 bool identify_client(client_list_item *cl)
210         __attribute__((warn_unused_result));
211
212 /**
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
218  * @return None
219  */
220 void add_pollfd(BuxtonDaemon *self, int fd, short events, bool a);
221
222 /**
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
226  * @return None
227  */
228 void del_pollfd(BuxtonDaemon *self, nfds_t i);
229
230 /**
231  * Setup a client's smack label
232  * @param cl Client to set smack label on
233  * @return None
234  */
235 void handle_smack_label(client_list_item *cl);
236
237 /**
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
243  */
244 bool handle_client(BuxtonDaemon *self, client_list_item *cl, nfds_t i)
245         __attribute__((warn_unused_result));
246
247 /**
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
252  */
253 void terminate_client(BuxtonDaemon *self, client_list_item *cl, nfds_t i);
254
255 /*
256  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
257  *
258  * Local variables:
259  * c-basic-offset: 8
260  * tab-width: 8
261  * indent-tabs-mode: t
262  * End:
263  *
264  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
265  * :indentSize=8:tabSize=8:noTabs=false:
266  */