/
usb_moded-appsync.c
444 lines (374 loc) · 10.5 KB
/
usb_moded-appsync.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
@file usb_moded-appsync.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
@author: Philippe De Swert <philippe.de-swert@nokia.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
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.
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
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#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"
#include "usb_moded-modesetting.h"
#include "usb_moded-log.h"
#include "usb_moded-upstart.h"
#include "usb_moded-systemd.h"
static struct list_elem *read_file(const gchar *filename);
static gboolean enumerate_usb(gpointer data);
static GList *sync_list = NULL;
static unsigned sync_tag = 0;
static unsigned enum_tag = 0;
static struct timeval sync_tv;
#ifdef APP_SYNC_DBUS
static int no_dbus = 0;
#else
static int no_dbus = 0;
#endif /* APP_SYNC_DBUS */
static void free_elem(gpointer aptr)
{
struct list_elem *elem = aptr;
free(elem->name);
free(elem->launch);
free(elem->mode);
free(elem);
}
void free_appsync_list(void)
{
if( sync_list != 0 )
{
/*g_list_free_full(sync_list, free_elem); */
g_list_foreach (sync_list, (GFunc) free_elem, NULL);
g_list_free (sync_list);
sync_list = 0;
log_debug("Appsync list freed\n");
}
}
void readlist(int diag)
{
GDir *confdir = 0;
const gchar *dirname;
struct list_elem *list_item;
free_appsync_list();
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;
}
while( (dirname = g_dir_read_name(confdir)) )
{
log_debug("Read file %s\n", dirname);
if( (list_item = read_file(dirname)) )
sync_list = g_list_append(sync_list, list_item);
}
cleanup:
if( confdir ) g_dir_close(confdir);
/* 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 )
{
log_debug("Sync list valid\n");
#ifdef APP_SYN_DBUS
usb_moded_app_sync_init_connection();
#endif
}
}
static struct list_elem *read_file(const gchar *filename)
{
gchar *full_filename = NULL;
GKeyFile *settingsfile = NULL;
struct list_elem *list_item = NULL;
if( !(full_filename = g_strconcat(CONF_DIR_PATH, "/", filename, NULL)) )
goto cleanup;
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);
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);
list_item->upstart = g_key_file_get_integer(settingsfile, APP_INFO_ENTRY, APP_INFO_UPSTART_KEY, NULL);
log_debug("Upstart control = %d\n", list_item->upstart);
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);
list_item->post = g_key_file_get_integer(settingsfile, APP_INFO_ENTRY, APP_INFO_POST, NULL);
cleanup:
if(settingsfile)
g_key_file_free(settingsfile);
g_free(full_filename);
/* 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) )
{
log_debug("Element invalid, discarding\n");
free_elem(list_item);
list_item = 0;
}
return list_item;
}
/* @return 0 on succes, 1 if there is a failure */
int activate_sync(const char *mode)
{
GList *iter;
int count = 0, count2 = 0;
log_debug("activate sync");
/* Bump tag, see enumerate_usb() */
++sync_tag; gettimeofday(&sync_tv, 0);
if( sync_list == 0 )
{
log_debug("No sync list! Enumerating\n");
enumerate_usb(NULL);
return 0;
}
/* set list to inactive, mark other modes as active already */
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
count++;
if(!strcmp(data->mode, mode))
data->active = 0;
else
{
count2++;
data->active = 1;
}
}
/* if the number of active modes is equal to the number of existing modes
we enumerate immediately */
if(count == count2)
{
log_debug("Nothing to launch.\n");
enumerate_usb(NULL);
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 */
/* start timer */
log_debug("Starting appsync timer\n");
g_timeout_add_seconds(2, enumerate_usb, NULL);
/* 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 items marked as post, will be launched after usb is up */
if(data->post)
{
mark_active(data->name);
continue;
}
log_debug("launching app %s\n", data->name);
if(data->systemd)
{
if(!systemd_control_service(data->name, SYSTEMD_START))
mark_active(data->name);
else
goto error;
}
#ifdef UPSTART
else if(data->upstart)
{
if(!upstart_control_job(data->name, UPSTART_START))
mark_active(data->name);
else
goto error;
}
#endif /* UPSTART */
else if(data->launch)
{
/* skipping if dbus session bus is not available,
or not compiled in */
if(no_dbus)
mark_active(data->name);
#ifdef APP_SYNC_DBUS
else
if(!usb_moded_dbus_app_launch(data->launch))
mark_active(data->name);
else
goto error;
#endif /* APP_SYNC_DBUS */
}
}
}
return(0);
error:
log_warning("Error launching a service!\n");
return(1);
}
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;
log_debug("launching app %s\n", data->name);
if(data->systemd)
{
if(systemd_control_service(data->name, SYSTEMD_START))
goto error;
}
#ifdef UPSTART
else if(data->upstart)
{
if(upstart_control_job(data->name, UPSTART_START))
goto error;
}
#endif /* UPSTART */
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
if(usb_moded_dbus_app_launch(data->launch != 0))
goto error;
#endif /* APP_SYNC_DBUS */
}
}
}
return(0);
error:
log_warning("Error launching a service!\n");
return(1);
}
int mark_active(const gchar *name)
{
int ret = -1; // assume name not found
int missing = 0;
GList *iter;
log_debug("App %s notified it is ready\n", name);
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
if(!strcmp(data->name, name))
{
/* TODO: do we need to worry about duplicate names in the list? */
ret = !data->active;
data->active = 1;
/* updated + missing -> not going to enumerate */
if( missing ) break;
}
else if( data->active == 0 )
{
missing = 1;
/* updated + missing -> not going to enumerate */
if( ret != -1 ) break;
}
}
if( !missing )
{
log_debug("All apps active. Let's enumerate\n");
enumerate_usb(NULL);
}
/* -1=not found, 0=already active, 1=activated now */
return ret;
}
static gboolean enumerate_usb(gpointer data)
{
struct timeval tv;
/* We arrive here twice: when app sync is done
* and when the app sync timeout gets triggered.
* The tags are used to filter out these repeats.
*/
if( enum_tag == sync_tag )
{
log_debug("ignoring enumeration trigger");
}
else
{
#ifdef NOKIA
/* activate usb connection/enumeration */
write_to_file("/sys/devices/platform/musb_hdrc/gadget/softconnect", "1");
log_debug("Softconnect enumeration done\n");
#endif
enum_tag = sync_tag;
/* 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);
#ifdef APP_SYNC_DBUS
/* remove dbus service */
usb_moded_appsync_cleanup();
#endif /* APP_SYNC_DBUS */
}
/* return false to stop the timer from repeating */
return FALSE;
}
int appsync_stop(void)
{
GList *iter = 0;
for( iter = sync_list; iter; iter = g_list_next(iter) )
{
struct list_elem *data = iter->data;
if(data->systemd)
{
if(!systemd_control_service(data->name, SYSTEMD_STOP))
log_debug("Failed to stop %s\n", data->name);
}
#ifdef UPSTART
else if(data->upstart)
{
log_debug("Stopping %s\n", data->launch);
if(upstart_control_job(data->name, UPSTART_STOP))
log_debug("Failed to stop %s\n", data->name);
}
#endif /* UPSTART */
}
return(0);
}