Debian Codegraph @

src/notification_proxy.c

raw
1 /*
2 * notification_proxy.c
3 * com.apple.mobile.notification_proxy service implementation.
4 *
5 * Copyright (c) 2009 Nikias Bassen, All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <string.h>
26 #include <stdlib.h>
27
28 #ifndef _MSC_VER
29 #include <unistd.h>
30 #endif
31
32 #include <plist/plist.h>
33
34 #include "notification_proxy.h"
35 #include "property_list_service.h"
36 #include "common/debug.h"
37
38 #ifdef _WIN32
39 #include <windows.h>
40 #define sleep(x) Sleep(x*1000)
41 #endif
42
43 struct np_thread {
44 np_client_t client;
45 np_notify_cb_t cbfunc;
46 void *user_data;
47 };
48
49 /**
50 * Locks a notification_proxy client, used for thread safety.
51 *
52 * @param client notification_proxy client to lock
53 */
54 static void np_lock(np_client_t client)
55 {
56 debug_info("Locked");
57 mutex_lock(&client->mutex);
58 }
59
60 /**
61 * Unlocks a notification_proxy client, used for thread safety.
62 *
63 * @param client notification_proxy client to unlock
64 */
65 static void np_unlock(np_client_t client)
66 {
67 debug_info("Unlocked");
68 mutex_unlock(&client->mutex);
69 }
70
71 /**
72 * Convert a property_list_service_error_t value to an np_error_t value.
73 * Used internally to get correct error codes.
74 *
75 * @param err A property_list_service_error_t error code
76 *
77 * @return A matching np_error_t error code,
78 * NP_E_UNKNOWN_ERROR otherwise.
79 */
80 static np_error_t np_error(property_list_service_error_t err)
81 {
82 switch (err) {
83 case PROPERTY_LIST_SERVICE_E_SUCCESS:
84 return NP_E_SUCCESS;
85 case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
86 return NP_E_INVALID_ARG;
87 case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
88 return NP_E_PLIST_ERROR;
89 case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
90 return NP_E_CONN_FAILED;
91 default:
92 break;
93 }
94 return NP_E_UNKNOWN_ERROR;
95 }
96
97 np_error_t np_client_new(idevice_t device, lockdownd_service_descriptor_t service, np_client_t *client)
98 {
99 property_list_service_client_t plistclient = NULL;
100 np_error_t err = np_error(property_list_service_client_new(device, service, &plistclient));
101 if (err != NP_E_SUCCESS) {
102 return err;
103 }
104
105 np_client_t client_loc = (np_client_t) malloc(sizeof(struct np_client_private));
106 client_loc->parent = plistclient;
107
108 mutex_init(&client_loc->mutex);
109 client_loc->notifier = THREAD_T_NULL;
110
111 *client = client_loc;
112 return NP_E_SUCCESS;
113 }
114
115 np_error_t np_client_start_service(idevice_t device, np_client_t* client, const char* label)
116 {
117 int32_t err = NP_E_UNKNOWN_ERROR;
118 service_client_factory_start_service(device, NP_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(np_client_new), &err);
119 return err;
120 }
121
122 np_error_t np_client_free(np_client_t client)
123 {
124 plist_t dict;
125 property_list_service_client_t parent;
126
127 if (!client)
128 return NP_E_INVALID_ARG;
129
130 dict = plist_new_dict();
131 plist_dict_set_item(dict,"Command", plist_new_string("Shutdown"));
132 property_list_service_send_xml_plist(client->parent, dict);
133 plist_free(dict);
134
135 parent = client->parent;
136 /* notifies the client->notifier thread that it should terminate */
137 client->parent = NULL;
138
139 if (client->notifier) {
140 debug_info("joining np callback");
141 thread_join(client->notifier);
142 thread_free(client->notifier);
143 client->notifier = THREAD_T_NULL;
144 } else {
145 dict = NULL;
146 property_list_service_receive_plist(parent, &dict);
147 if (dict) {
148 #ifndef STRIP_DEBUG_CODE
149 char *cmd_value = NULL;
150 plist_t cmd_value_node = plist_dict_get_item(dict, "Command");
151 if (plist_get_node_type(cmd_value_node) == PLIST_STRING) {
152 plist_get_string_val(cmd_value_node, &cmd_value);
153 }
154 if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) {
155 // this is the expected answer
156 } else {
157 debug_info("Did not get ProxyDeath but:");
158 debug_plist(dict);
159 }
160 if (cmd_value) {
161 free(cmd_value);
162 }
163 #endif
164 plist_free(dict);
165 }
166 }
167
168 property_list_service_client_free(parent);
169
170 mutex_destroy(&client->mutex);
171 free(client);
172
173 return NP_E_SUCCESS;
174 }
175
176 np_error_t np_post_notification(np_client_t client, const char *notification)
177 {
178 if (!client || !notification) {
179 return NP_E_INVALID_ARG;
180 }
181 np_lock(client);
182
183 plist_t dict = plist_new_dict();
184 plist_dict_set_item(dict,"Command", plist_new_string("PostNotification"));
185 plist_dict_set_item(dict,"Name", plist_new_string(notification));
186
187 np_error_t res = np_error(property_list_service_send_xml_plist(client->parent, dict));
188 plist_free(dict);
189
190 if (res != NP_E_SUCCESS) {
191 debug_info("Error sending XML plist to device!");
192 }
193 np_unlock(client);
194 return res;
195 }
196
197 static np_error_t internal_np_observe_notification(np_client_t client, const char *notification)
198 {
199 plist_t dict = plist_new_dict();
200 plist_dict_set_item(dict,"Command", plist_new_string("ObserveNotification"));
201 plist_dict_set_item(dict,"Name", plist_new_string(notification));
202
203 np_error_t res = np_error(property_list_service_send_xml_plist(client->parent, dict));
204 if (res != NP_E_SUCCESS) {
205 debug_info("Error sending XML plist to device!");
206 }
207 plist_free(dict);
208
209 return res;
210 }
211
212 np_error_t np_observe_notification( np_client_t client, const char *notification )
213 {
214 if (!client || !notification) {
215 return NP_E_INVALID_ARG;
216 }
217 np_lock(client);
218 np_error_t res = internal_np_observe_notification(client, notification);
219 np_unlock(client);
220 return res;
221 }
222
223 np_error_t np_observe_notifications(np_client_t client, const char **notification_spec)
224 {
225 int i = 0;
226 np_error_t res = NP_E_UNKNOWN_ERROR;
227 const char **notifications = notification_spec;
228
229 if (!client) {
230 return NP_E_INVALID_ARG;
231 }
232
233 if (!notifications) {
234 return NP_E_INVALID_ARG;
235 }
236
237 np_lock(client);
238 while (notifications[i]) {
239 res = internal_np_observe_notification(client, notifications[i]);
240 if (res != NP_E_SUCCESS) {
241 break;
242 }
243 i++;
244 }
245 np_unlock(client);
246
247 return res;
248 }
249
250 /**
251 * Checks if a notification has been sent by the device.
252 *
253 * @param client NP to get a notification from
254 * @param notification Pointer to a buffer that will be allocated and filled
255 * with the notification that has been received.
256 *
257 * @return 0 if a notification has been received or nothing has been received,
258 * or a negative value if an error occurred.
259 *
260 * @note You probably want to check out np_set_notify_callback
261 * @see np_set_notify_callback
262 */
263 static int np_get_notification(np_client_t client, char **notification)
264 {
265 int res = 0;
266 plist_t dict = NULL;
267
268 if (!client || !client->parent || *notification)
269 return -1;
270
271 np_lock(client);
272
273 property_list_service_error_t perr = property_list_service_receive_plist_with_timeout(client->parent, &dict, 500);
274 if (perr == PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT) {
275 debug_info("NotificationProxy: no notification received!");
276 res = 0;
277 } else if (perr != PROPERTY_LIST_SERVICE_E_SUCCESS) {
278 debug_info("NotificationProxy: error %d occurred!", perr);
279 res = perr;
280 }
281 if (dict) {
282 char *cmd_value = NULL;
283 plist_t cmd_value_node = plist_dict_get_item(dict, "Command");
284
285 if (plist_get_node_type(cmd_value_node) == PLIST_STRING) {
286 plist_get_string_val(cmd_value_node, &cmd_value);
287 }
288
289 if (cmd_value && !strcmp(cmd_value, "RelayNotification")) {
290 char *name_value = NULL;
291 plist_t name_value_node = plist_dict_get_item(dict, "Name");
292
293 if (plist_get_node_type(name_value_node) == PLIST_STRING) {
294 plist_get_string_val(name_value_node, &name_value);
295 }
296
297 res = -2;
298 if (name_value_node && name_value) {
299 *notification = name_value;
300 debug_info("got notification %s", __func__, name_value);
301 res = 0;
302 }
303 } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) {
304 debug_info("NotificationProxy died!");
305 res = -1;
306 } else if (cmd_value) {
307 debug_info("unknown NotificationProxy command '%s' received!", cmd_value);
308 res = -1;
309 } else {
310 res = -2;
311 }
312 if (cmd_value) {
313 free(cmd_value);
314 }
315 plist_free(dict);
316 dict = NULL;
317 }
318
319 np_unlock(client);
320
321 return res;
322 }
323
324 /**
325 * Internally used thread function.
326 */
327 void* np_notifier( void* arg )
328 {
329 char *notification = NULL;
330 struct np_thread *npt = (struct np_thread*)arg;
331
332 if (!npt) return NULL;
333
334 debug_info("starting callback.");
335 while (npt->client->parent) {
336 if (np_get_notification(npt->client, &notification) < 0) {
337 npt->cbfunc("", npt->user_data);
338 break;
339 }
340 if (notification) {
341 npt->cbfunc(notification, npt->user_data);
342 free(notification);
343 notification = NULL;
344 }
345 sleep(1);
346 }
347 if (npt) {
348 free(npt);
349 }
350
351 return NULL;
352 }
353
354 np_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb, void *user_data )
355 {
356 if (!client)
357 return NP_E_INVALID_ARG;
358
359 np_error_t res = NP_E_UNKNOWN_ERROR;
360
361 np_lock(client);
362 if (client->notifier) {
363 debug_info("callback already set, removing");
364 property_list_service_client_t parent = client->parent;
365 client->parent = NULL;
366 thread_join(client->notifier);
367 thread_free(client->notifier);
368 client->notifier = THREAD_T_NULL;
369 client->parent = parent;
370 }
371
372 if (notify_cb) {
373 struct np_thread *npt = (struct np_thread*)malloc(sizeof(struct np_thread));
374 if (npt) {
375 npt->client = client;
376 npt->cbfunc = notify_cb;
377 npt->user_data = user_data;
378
379 if (thread_new(&client->notifier, np_notifier, npt) == 0) {
380 res = NP_E_SUCCESS;
381 }
382 }
383 } else {
384 debug_info("no callback set");
385 }
386 np_unlock(client);
387
388 return res;
389 }