/
usb_moded-appsync.c
479 lines (401 loc) · 11.5 KB
1
/**
2
@file usb_moded-appsync.c
3
4
Copyright (C) 2010 Nokia Corporation. All rights reserved.
5
Copyright (C) 2013-2016 Jolla Ltd.
6
7
@author: Philippe De Swert <philippe.de-swert@nokia.com>
8
9
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
@author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
10
11
This program is free software; you can redistribute it and/or
12
13
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
14
15
16
17
18
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
19
20
21
22
23
24
25
You should have received a copy of the Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
26
27
#define _GNU_SOURCE
28
29
30
31
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
32
33
#include <fcntl.h>
#include <errno.h>
34
#include <sys/time.h>
35
36
37
38
39
40
41
#include <glib.h>
#include <glib/gstdio.h>
#include "usb_moded-appsync.h"
#include "usb_moded-appsync-dbus.h"
#include "usb_moded-appsync-dbus-private.h"
42
#include "usb_moded-modesetting.h"
43
#include "usb_moded-log.h"
44
#include "usb_moded-systemd.h"
45
46
static struct list_elem *read_file(const gchar *filename, int diag);
47
48
49
50
static void enumerate_usb(void);
static gboolean enumerate_usb_cb(gpointer data);
static void start_enumerate_usb_timer(void);
static void cancel_enumerate_usb_timer(void);
51
52
53
static GList *sync_list = NULL;
54
static guint enumerate_usb_id = 0;
55
static struct timeval sync_tv;
56
57
58
59
60
61
#ifdef APP_SYNC_DBUS
static int no_dbus = 0;
#else
static int no_dbus = 0;
#endif /* APP_SYNC_DBUS */
62
63
static void free_elem(struct list_elem *elem)
64
{
65
66
67
g_free(elem->name);
g_free(elem->launch);
g_free(elem->mode);
68
69
free(elem);
}
70
71
72
73
74
75
76
static void free_elem_cb(gpointer elem, gpointer user_data)
{
(void)user_data;
free_elem(elem);
}
77
void free_appsync_list(void)
78
79
{
if( sync_list != 0 )
80
{
81
/*g_list_free_full(sync_list, free_elem); */
82
g_list_foreach (sync_list, free_elem_cb, NULL);
83
g_list_free (sync_list);
84
sync_list = 0;
85
log_debug("Appsync list freed\n");
86
87
88
}
}
89
90
91
92
93
static gint list_sort_func(gconstpointer a, gconstpointer b)
{
return strcasecmp( (char*)a, (char*)b );
}
94
void readlist(int diag)
95
{
96
97
98
99
100
GDir *confdir = 0;
const gchar *dirname;
struct list_elem *list_item;
101
free_appsync_list();
102
103
104
105
106
107
108
109
110
111
112
if(diag)
{
if( !(confdir = g_dir_open(CONF_DIR_DIAG_PATH, 0, NULL)) )
goto cleanup;
}
else
{
if( !(confdir = g_dir_open(CONF_DIR_PATH, 0, NULL)) )
goto cleanup;
}
113
114
while( (dirname = g_dir_read_name(confdir)) )
115
{
116
log_debug("Read file %s\n", dirname);
117
if( (list_item = read_file(dirname, diag)) )
118
sync_list = g_list_append(sync_list, list_item);
119
}
120
121
122
123
cleanup:
if( confdir ) g_dir_close(confdir);
124
125
126
127
/* sort list alphabetically so services for a mode
can be run in a certain order */
sync_list=g_list_sort(sync_list, list_sort_func);
128
129
130
131
132
/* set up session bus connection if app sync in use
* so we do not need to make the time consuming connect
* operation at enumeration time ... */
if( sync_list )
133
{
134
log_debug("Sync list valid\n");
135
#ifdef APP_SYN_DBUS
136
usb_moded_app_sync_init_connection();
137
#endif
138
}
139
140
}
141
static struct list_elem *read_file(const gchar *filename, int diag)
142
143
144
145
146
{
gchar *full_filename = NULL;
GKeyFile *settingsfile = NULL;
struct list_elem *list_item = NULL;
147
148
149
150
151
152
153
154
155
156
if(diag)
{
if( !(full_filename = g_strconcat(CONF_DIR_DIAG_PATH, "/", filename, NULL)) )
goto cleanup;
}
else
{
if( !(full_filename = g_strconcat(CONF_DIR_PATH, "/", filename, NULL)) )
goto cleanup;
}
157
158
159
160
161
162
163
164
165
166
167
168
169
if( !(settingsfile = g_key_file_new()) )
goto cleanup;
if( !g_key_file_load_from_file(settingsfile, full_filename, G_KEY_FILE_NONE, NULL) )
goto cleanup;
if( !(list_item = calloc(1, sizeof *list_item)) )
goto cleanup;
list_item->name = g_key_file_get_string(settingsfile, APP_INFO_ENTRY, APP_INFO_NAME_KEY, NULL);
log_debug("Appname = %s\n", list_item->name);
list_item->launch = g_key_file_get_string(settingsfile, APP_INFO_ENTRY, APP_INFO_LAUNCH_KEY, NULL);
170
171
172
log_debug("Launch = %s\n", list_item->launch);
list_item->mode = g_key_file_get_string(settingsfile, APP_INFO_ENTRY, APP_INFO_MODE_KEY, NULL);
log_debug("Launch mode = %s\n", list_item->mode);
173
174
list_item->systemd = g_key_file_get_integer(settingsfile, APP_INFO_ENTRY, APP_INFO_SYSTEMD_KEY, NULL);
log_debug("Systemd control = %d\n", list_item->systemd);
175
list_item->post = g_key_file_get_integer(settingsfile, APP_INFO_ENTRY, APP_INFO_POST, NULL);
176
list_item->state = APP_STATE_DONTCARE;
177
178
179
180
181
182
183
cleanup:
if(settingsfile)
g_key_file_free(settingsfile);
g_free(full_filename);
184
185
/* if a minimum set of required elements is not filled in we discard the list_item */
if( list_item && !(list_item->name && list_item->mode) )
186
{
187
188
189
log_debug("Element invalid, discarding\n");
free_elem(list_item);
list_item = 0;
190
}
191
192
return list_item;
193
}
194
195
/* @return 0 on succes, 1 if there is a failure */
196
int activate_sync(const char *mode)
197
{
198
GList *iter;
199
int count = 0;
200
201
202
log_debug("activate sync");
203
204
/* Get start of activation timestamp */
gettimeofday(&sync_tv, 0);
205
206
207
if( sync_list == 0 )
{
208
log_debug("No sync list! Enumerating\n");
209
enumerate_usb();
210
211
212
return 0;
}
213
214
/* Count apps that need to be activated for this mode and
* mark them as currently inactive */
215
216
217
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
218
219
if(!strcmp(data->mode, mode))
220
221
222
223
{
++count;
data->state = APP_STATE_INACTIVE;
}
224
else
225
{
226
data->state = APP_STATE_DONTCARE;
227
}
228
}
229
230
231
/* If there is nothing to activate, enumerate immediately */
if(count <= 0)
232
233
{
log_debug("Nothing to launch.\n");
234
enumerate_usb();
235
return(0);
236
237
}
238
#ifdef APP_SYNC_DBUS
239
/* check dbus initialisation, skip dbus activated services if this fails */
240
241
if(!usb_moded_app_sync_init())
{
242
243
log_debug("dbus setup failed => skipping dbus launched apps \n");
no_dbus = 1;
244
}
245
#endif /* APP_SYNC_DBUS */
246
247
/* start timer */
248
start_enumerate_usb_timer();
249
250
/* go through list and launch apps */
251
252
253
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
254
255
if(!strcmp(mode, data->mode))
{
256
/* do not launch items marked as post, will be launched after usb is up */
257
258
259
260
if(data->post)
{
continue;
}
261
log_debug("launching pre-enum-app %s\n", data->name);
262
263
if(data->systemd)
{
264
if(!systemd_control_service(data->name, SYSTEMD_START))
265
goto error;
266
mark_active(data->name, 0);
267
}
268
else if(data->launch)
269
{
270
271
/* skipping if dbus session bus is not available,
or not compiled in */
272
if(no_dbus)
273
mark_active(data->name, 0);
274
#ifdef APP_SYNC_DBUS
275
else
276
if(!usb_moded_dbus_app_launch(data->launch))
277
mark_active(data->name, 0);
278
279
else
goto error;
280
#endif /* APP_SYNC_DBUS */
281
}
282
}
283
}
284
285
return(0);
286
287
288
289
error:
log_warning("Error launching a service!\n");
return(1);
290
291
}
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
int activate_sync_post(const char *mode)
{
GList *iter;
log_debug("activate post sync");
if( sync_list == 0 )
{
log_debug("No sync list! skipping post sync\n");
return 0;
}
#ifdef APP_SYNC_DBUS
/* check dbus initialisation, skip dbus activated services if this fails */
if(!usb_moded_app_sync_init())
{
log_debug("dbus setup failed => skipping dbus launched apps \n");
no_dbus = 1;
}
#endif /* APP_SYNC_DBUS */
/* go through list and launch apps */
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
if(!strcmp(mode, data->mode))
{
/* launch only items marked as post, others are already running */
if(!data->post)
continue;
322
log_debug("launching post-enum-app %s\n", data->name);
323
324
if(data->systemd)
{
325
if(!systemd_control_service(data->name, SYSTEMD_START))
326
goto error;
327
mark_active(data->name, 1);
328
329
330
331
332
333
334
335
336
}
else if(data->launch)
{
/* skipping if dbus session bus is not available,
or not compiled in */
if(no_dbus)
continue;
#ifdef APP_SYNC_DBUS
else
337
if(usb_moded_dbus_app_launch(data->launch) != 0)
338
339
340
341
342
343
344
345
346
347
348
349
350
goto error;
#endif /* APP_SYNC_DBUS */
}
}
}
return(0);
error:
log_warning("Error launching a service!\n");
return(1);
}
351
int mark_active(const gchar *name, int post)
352
{
353
354
355
356
int ret = -1; // assume name not found
int missing = 0;
GList *iter;
357
358
log_debug("%s-enum-app %s is started\n", post ? "post" : "pre", name);
359
360
361
362
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
363
364
if(!strcmp(data->name, name))
365
{
366
/* TODO: do we need to worry about duplicate names in the list? */
367
368
ret = (data->state != APP_STATE_ACTIVE);
data->state = APP_STATE_ACTIVE;
369
370
371
/* updated + missing -> not going to enumerate */
if( missing ) break;
372
}
373
else if( data->state == APP_STATE_INACTIVE && data->post == post )
374
375
{
missing = 1;
376
377
378
379
380
/* updated + missing -> not going to enumerate */
if( ret != -1 ) break;
}
}
381
if( !post && !missing )
382
{
383
log_debug("All pre-enum-apps active. Let's enumerate\n");
384
enumerate_usb();
385
386
387
388
}
/* -1=not found, 0=already active, 1=activated now */
return ret;
389
390
}
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
static gboolean enumerate_usb_cb(gpointer data)
{
(void)data;
enumerate_usb_id = 0;
log_debug("handling enumeration timeout");
enumerate_usb();
/* return false to stop the timer from repeating */
return FALSE;
}
static void start_enumerate_usb_timer(void)
{
log_debug("scheduling enumeration timeout");
if( enumerate_usb_id )
g_source_remove(enumerate_usb_id), enumerate_usb_id = 0;
enumerate_usb_id = g_timeout_add_seconds(2, enumerate_usb_cb, NULL);
}
static void cancel_enumerate_usb_timer(void)
{
if( enumerate_usb_id )
{
log_debug("canceling enumeration timeout");
g_source_remove(enumerate_usb_id), enumerate_usb_id = 0;
}
}
static void enumerate_usb(void)
419
{
420
421
struct timeval tv;
422
423
424
/* Stop the timer in case of explicit enumeration call */
cancel_enumerate_usb_timer();
425
426
427
428
/* Debug: how long it took from sync start to get here */
gettimeofday(&tv, 0);
timersub(&tv, &sync_tv, &tv);
log_debug("sync to enum: %.3f seconds", tv.tv_sec + tv.tv_usec * 1e-6);
429
430
#ifdef APP_SYNC_DBUS
431
432
/* remove dbus service */
usb_moded_appsync_cleanup();
433
#endif /* APP_SYNC_DBUS */
434
}
435
436
static void appsync_stop_apps(int post)
437
438
439
440
441
442
{
GList *iter = 0;
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
443
444
if(data->systemd && data->state == APP_STATE_ACTIVE && data->post == post)
445
{
446
log_debug("stopping %s-enum-app %s", post ? "post" : "pre", data->name);
447
if(!systemd_control_service(data->name, SYSTEMD_STOP))
448
log_debug("Failed to stop %s\n", data->name);
449
data->state = APP_STATE_DONTCARE;
450
451
452
453
}
}
}
454
int appsync_stop(gboolean force)
455
456
457
458
459
460
461
462
463
464
465
{
/* If force arg is used, stop all applications that
* could have been started by usb-moded */
if(force)
{
GList *iter;
log_debug("assuming all applications are active");
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
466
data->state = APP_STATE_ACTIVE;
467
}
468
469
}
470
471
472
473
474
475
/* Stop post-apps 1st */
appsync_stop_apps(1);
/* Then pre-apps */
appsync_stop_apps(0);
476
477
/* Do not leave active timers behind */
cancel_enumerate_usb_timer();
478
479
return(0);
}