Skip to content

Latest commit

 

History

History
908 lines (765 loc) · 26.7 KB

usb_moded.c

File metadata and controls

908 lines (765 loc) · 26.7 KB
 
Mar 22, 2011
Mar 22, 2011
1
/**
Aug 24, 2018
Aug 24, 2018
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
* @file usb_moded.c
*
* Copyright (C) 2010 Nokia Corporation. All rights reserved.
* Copyright (C) 2012-2018 Jolla. All rights reserved.
*
* @author: Philippe De Swert <philippe.de-swert@nokia.com>
* @author: Philippe De Swert <phdeswer@lumi.maa>
* @author: Philippe De Swert <philippedeswert@gmail.com>
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Jonni Rainisto <jonni.rainisto@jollamobile.com>
* @author: Pekka Lundstrom <pekka.lundstrom@jollamobile.com>
* @author: Vesa Halttunen <vesa.halttunen@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
* @author: Thomas Perl <thomas.perl@jolla.com>
* @author: Matti Lehtimaki <matti.lehtimaki@gmail.com>
* @author: Thomas Perl <m@thp.io>
* @author: Martin Jones <martin.jones@jollamobile.com>
* @author: Andrew den Exter <andrew.den.exter@jolla.com>
* @author: Andrew den Exter <andrew.den.exter@jollamobile.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
*/
Mar 22, 2011
Mar 22, 2011
37
#include "usb_moded.h"
Sep 5, 2018
Sep 5, 2018
38
39
40
41
42
43
#include "usb_moded-android.h"
#include "usb_moded-appsync.h"
#include "usb_moded-config-private.h"
#include "usb_moded-configfs.h"
#include "usb_moded-control.h"
Mar 22, 2011
Mar 22, 2011
44
#include "usb_moded-dbus-private.h"
Aug 24, 2018
Aug 24, 2018
45
#include "usb_moded-devicelock.h"
Sep 5, 2018
Sep 5, 2018
46
47
#include "usb_moded-log.h"
#include "usb_moded-mac.h"
Mar 22, 2011
Mar 22, 2011
48
49
#include "usb_moded-modesetting.h"
#include "usb_moded-modules.h"
Jan 13, 2012
Jan 13, 2012
50
#include "usb_moded-network.h"
Sep 5, 2018
Sep 5, 2018
51
#include "usb_moded-sigpipe.h"
Nov 7, 2016
Nov 7, 2016
52
#include "usb_moded-systemd.h"
Sep 5, 2018
Sep 5, 2018
53
54
#include "usb_moded-trigger.h"
#include "usb_moded-udev.h"
Sep 5, 2018
Sep 5, 2018
55
#include "usb_moded-worker.h"
Mar 13, 2017
Mar 13, 2017
56
Aug 21, 2013
Aug 21, 2013
57
#ifdef MEEGOLOCK
Aug 24, 2018
Aug 24, 2018
58
# include "usb_moded-dsme.h"
Aug 21, 2013
Aug 21, 2013
59
#endif
Mar 22, 2011
Mar 22, 2011
60
Sep 5, 2018
Sep 5, 2018
61
62
63
64
65
66
67
68
69
70
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef SYSTEMD
# include <systemd/sd-daemon.h>
#endif
Aug 24, 2018
Aug 24, 2018
71
72
73
74
/* ========================================================================= *
* Constants
* ========================================================================= */
Feb 14, 2017
Feb 14, 2017
75
76
77
78
79
/* Wakelogging is noisy, do not log it by default */
#ifndef VERBOSE_WAKELOCKING
# define VERBOSE_WAKELOCKING 0
#endif
Apr 26, 2016
Apr 26, 2016
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** Default allowed cable detection delay
*
* To comply with USB standards, the delay should be
* less than 2 seconds to ensure timely enumeration.
*
* Any value <= zero means no delay.
*/
#define CABLE_CONNECTION_DELAY_DEFAULT 0
/** Maximum allowed cable detection delay
*
* Must be shorter than initial probing delay expected by
* dsme (currently 5 seconds) to avoid reboot loops in
* act dead mode.
*
* And shorter than USB_MODED_SUSPEND_DELAY_DEFAULT_MS to
* allow the timer to trigger also in display off scenarios.
*/
#define CABLE_CONNECTION_DELAY_MAXIMUM 4000
Aug 24, 2018
Aug 24, 2018
101
102
103
104
105
106
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- usbmoded -- */
Sep 5, 2018
Sep 5, 2018
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
GList *usbmoded_get_modelist (void);
void usbmoded_load_modelist (void);
void usbmoded_free_modelist (void);
bool usbmoded_get_rescue_mode (void);
void usbmoded_set_rescue_mode (bool rescue_mode);
bool usbmoded_get_diag_mode (void);
void usbmoded_set_diag_mode (bool diag_mode);
void usbmoded_set_cable_connection_delay(int delay_ms);
int usbmoded_get_cable_connection_delay(void);
static gboolean usbmoded_allow_suspend_timer_cb (gpointer aptr);
void usbmoded_allow_suspend (void);
void usbmoded_delay_suspend (void);
bool usbmoded_init_done_p (void);
void usbmoded_set_init_done (bool reached);
void usbmoded_probe_init_done (void);
bool usbmoded_can_export (void);
void usbmoded_exit_mainloop (int exitcode);
void usbmoded_handle_signal (int signum);
static bool usbmoded_init (void);
static void usbmoded_cleanup (void);
static void usbmoded_usage (void);
static void usbmoded_parse_options (int argc, char *argv[]);
Mar 28, 2018
Mar 28, 2018
129
Aug 24, 2018
Aug 24, 2018
130
/* ========================================================================= *
Sep 5, 2018
Sep 5, 2018
131
132
* Data
* ========================================================================= */
Nov 7, 2016
Nov 7, 2016
133
Sep 5, 2018
Sep 5, 2018
134
135
static int usbmoded_exitcode = EXIT_FAILURE;
static GMainLoop *usbmoded_mainloop = NULL;
Sep 5, 2018
Sep 5, 2018
136
Sep 5, 2018
Sep 5, 2018
137
138
139
140
static bool usbmoded_hw_fallback = false;
#ifdef SYSTEMD
static bool usbmoded_systemd_notify = false;
#endif
Mar 22, 2011
Mar 22, 2011
141
Sep 5, 2018
Sep 5, 2018
142
143
144
/* ========================================================================= *
* Functions
* ========================================================================= */
Mar 22, 2011
Mar 22, 2011
145
Sep 5, 2018
Sep 5, 2018
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
/* ------------------------------------------------------------------------- *
* MODELIST
* ------------------------------------------------------------------------- */
static GList *usbmoded_modelist = 0;
GList *
usbmoded_get_modelist(void)
{
return usbmoded_modelist;
}
void
usbmoded_load_modelist(void)
{
if( !usbmoded_modelist ) {
log_notice("load modelist");
usbmoded_modelist = dynconfig_read_mode_list(usbmoded_get_diag_mode());
}
}
void
usbmoded_free_modelist(void)
{
if( usbmoded_modelist ) {
log_notice("free modelist");
dynconfig_free_mode_list(usbmoded_modelist),
usbmoded_modelist = 0;
}
}
Sep 5, 2018
Sep 5, 2018
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
/* ------------------------------------------------------------------------- *
* RESCUE_MODE
* ------------------------------------------------------------------------- */
/** Rescue mode flag
*
* When enabled, usb-moded allows developer_mode etc when device is
* booted up with cable connected without requiring device unlock.
* Which can be useful if UI for some reason does not come up.
*/
static bool usbmoded_rescue_mode = false;
bool usbmoded_get_rescue_mode(void)
{
return usbmoded_rescue_mode;
}
void usbmoded_set_rescue_mode(bool rescue_mode)
{
if( usbmoded_rescue_mode != rescue_mode ) {
log_info("rescue_mode: %d -> %d", usbmoded_rescue_mode, rescue_mode);
usbmoded_rescue_mode = rescue_mode;
}
}
Sep 5, 2018
Sep 5, 2018
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* ------------------------------------------------------------------------- *
* DIAG_MODE
* ------------------------------------------------------------------------- */
/** Diagnostic mode active
*
* In diag mode usb-moded uses separate mode configuration which
* should have exactly one mode defined / available.
*/
static bool usbmoded_diag_mode = false;
bool usbmoded_get_diag_mode(void)
{
return usbmoded_diag_mode;
}
void usbmoded_set_diag_mode(bool diag_mode)
{
if( usbmoded_diag_mode != diag_mode ) {
log_info("diag_mode: %d -> %d", usbmoded_diag_mode, diag_mode);
usbmoded_diag_mode = diag_mode;
}
}
Sep 5, 2018
Sep 5, 2018
226
227
228
229
/* ------------------------------------------------------------------------- *
* CABLE_CONNECT_DELAY
* ------------------------------------------------------------------------- */
Sep 5, 2018
Sep 5, 2018
230
231
232
233
234
235
236
237
238
/** PC connection delay
*
* Slow cable insert / similar physical issues can lead to a charger
* getting initially recognized as a pc connection. This defines how
* long we should wait and see if pc connection gets corrected to a
* charger kind.
*/
static int usbmoded_cable_connection_delay = CABLE_CONNECTION_DELAY_DEFAULT;
Aug 24, 2018
Aug 24, 2018
239
240
241
242
/** Helper for setting allowed cable detection delay
*
* Used for implementing --max-cable-delay=<ms> option.
*/
Sep 5, 2018
Sep 5, 2018
243
244
void
usbmoded_set_cable_connection_delay(int delay_ms)
Apr 4, 2016
Apr 4, 2016
245
{
Sep 5, 2018
Sep 5, 2018
246
247
248
249
250
251
252
253
254
if( delay_ms > CABLE_CONNECTION_DELAY_MAXIMUM )
delay_ms = CABLE_CONNECTION_DELAY_MAXIMUM;
if( delay_ms < 0 )
delay_ms = 0;
if( usbmoded_cable_connection_delay != delay_ms ) {
log_info("cable_connection_delay: %d -> %d",
usbmoded_cable_connection_delay,
delay_ms);
Aug 24, 2018
Aug 24, 2018
255
usbmoded_cable_connection_delay = delay_ms;
Apr 4, 2016
Apr 4, 2016
256
257
258
}
}
Sep 5, 2018
Sep 5, 2018
259
260
261
262
263
264
265
266
/** Helper for getting allowed cable detection delay
*/
int
usbmoded_get_cable_connection_delay(void)
{
return usbmoded_cable_connection_delay;
}
Sep 5, 2018
Sep 5, 2018
267
268
269
270
271
272
273
274
275
276
/* ------------------------------------------------------------------------- *
* SUSPEND_BLOCKING
* ------------------------------------------------------------------------- */
/** Flag for: USB_MODED_WAKELOCK_STATE_CHANGE has been acquired */
static bool usbmoded_blocking_suspend = false;
/** Timer for releasing USB_MODED_WAKELOCK_STATE_CHANGE */
static guint usbmoded_allow_suspend_timer_id = 0;
Aug 24, 2018
Aug 24, 2018
277
/** Timer callback for releasing wakelock acquired via usbmoded_delay_suspend()
Apr 26, 2016
Apr 26, 2016
278
*
Aug 24, 2018
Aug 24, 2018
279
* @param aptr callback argument (not used)
Apr 26, 2016
Apr 26, 2016
280
*/
Aug 24, 2018
Aug 24, 2018
281
static gboolean usbmoded_allow_suspend_timer_cb(gpointer aptr)
Apr 26, 2016
Apr 26, 2016
282
{
Aug 24, 2018
Aug 24, 2018
283
284
(void)aptr;
Sep 5, 2018
Sep 5, 2018
285
usbmoded_allow_suspend_timer_id = 0;
Aug 24, 2018
Aug 24, 2018
286
287
288
289
usbmoded_allow_suspend();
return FALSE;
Apr 26, 2016
Apr 26, 2016
290
291
}
Aug 24, 2018
Aug 24, 2018
292
/** Release wakelock acquired via usbmoded_delay_suspend()
Apr 26, 2016
Apr 26, 2016
293
*
Aug 24, 2018
Aug 24, 2018
294
295
* Meant to be called on usb-moded exit so that wakelocks
* are not left behind.
Apr 26, 2016
Apr 26, 2016
296
*/
Aug 24, 2018
Aug 24, 2018
297
void usbmoded_allow_suspend(void)
Apr 26, 2016
Apr 26, 2016
298
{
Sep 5, 2018
Sep 5, 2018
299
300
301
if( usbmoded_allow_suspend_timer_id ) {
g_source_remove(usbmoded_allow_suspend_timer_id),
usbmoded_allow_suspend_timer_id = 0;
Aug 24, 2018
Aug 24, 2018
302
}
Apr 26, 2016
Apr 26, 2016
303
Sep 5, 2018
Sep 5, 2018
304
305
306
if( usbmoded_blocking_suspend ) {
usbmoded_blocking_suspend = false;
common_release_wakelock(USB_MODED_WAKELOCK_STATE_CHANGE);
Aug 24, 2018
Aug 24, 2018
307
}
Apr 26, 2016
Apr 26, 2016
308
309
310
311
312
313
314
315
316
317
318
319
}
/** Block suspend briefly
*
* Meant to be called in situations usb activity might have woken
* up the device (cable connect while display is off), or could
* allow device to suspend (cable disconnect while display is off).
*
* Allows usb moded some time to finish asynchronous activity and
* other processes to receive and process state changes broadcast
* by usb-moded.
*/
Aug 24, 2018
Aug 24, 2018
320
void usbmoded_delay_suspend(void)
Apr 26, 2016
Apr 26, 2016
321
{
Aug 24, 2018
Aug 24, 2018
322
323
/* Use of automatically terminating wakelocks also means we need
* to renew the wakelock when extending the suspend delay. */
Sep 5, 2018
Sep 5, 2018
324
common_acquire_wakelock(USB_MODED_WAKELOCK_STATE_CHANGE);
Apr 26, 2016
Apr 26, 2016
325
Sep 5, 2018
Sep 5, 2018
326
usbmoded_blocking_suspend = true;
Apr 26, 2016
Apr 26, 2016
327
Sep 5, 2018
Sep 5, 2018
328
329
if( usbmoded_allow_suspend_timer_id )
g_source_remove(usbmoded_allow_suspend_timer_id);
Apr 26, 2016
Apr 26, 2016
330
Sep 5, 2018
Sep 5, 2018
331
usbmoded_allow_suspend_timer_id =
Aug 24, 2018
Aug 24, 2018
332
333
g_timeout_add(USB_MODED_SUSPEND_DELAY_DEFAULT_MS,
usbmoded_allow_suspend_timer_cb, 0);
Apr 26, 2016
Apr 26, 2016
334
335
}
Sep 5, 2018
Sep 5, 2018
336
337
338
339
/* ------------------------------------------------------------------------- *
* CAN_EXPORT
* ------------------------------------------------------------------------- */
Aug 24, 2018
Aug 24, 2018
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/** Check if exposing device data is currently allowed
*
* @return true exposing data is ok, or false otherwise
*/
bool usbmoded_can_export(void)
{
bool can_export = true;
#ifdef MEEGOLOCK
/* Modes that potentially expose data are allowed only when
* device is running in user mode and device is unlocked */
can_export = (dsme_in_user_state() &&
devicelock_have_export_permission());
/* Having bootup rescue mode active is an exception */
Sep 5, 2018
Sep 5, 2018
355
if( usbmoded_get_rescue_mode() )
Aug 24, 2018
Aug 24, 2018
356
357
358
359
360
361
can_export = true;
#endif
return can_export;
}
Sep 5, 2018
Sep 5, 2018
362
363
364
365
366
367
368
369
370
371
/* ------------------------------------------------------------------------- *
* INIT_DONE
* ------------------------------------------------------------------------- */
/** Path to init-done flag file */
static const char usbmoded_init_done_flagfile[] = "/run/systemd/boot-status/init-done";
/** cached init-done-reached state */
static bool usbmoded_init_done_reached = false;
Oct 18, 2016
Oct 18, 2016
372
373
374
375
/** Check if system has already been successfully booted up
*
* @return true if init-done has been reached, or false otherwise
*/
Aug 24, 2018
Aug 24, 2018
376
bool usbmoded_init_done_p(void)
Jun 5, 2018
Jun 5, 2018
377
{
Sep 5, 2018
Sep 5, 2018
378
return usbmoded_init_done_reached;
Jun 5, 2018
Jun 5, 2018
379
380
381
}
/** Update cached init-done-reached state */
Aug 24, 2018
Aug 24, 2018
382
void usbmoded_set_init_done(bool reached)
Oct 18, 2016
Oct 18, 2016
383
{
Sep 5, 2018
Sep 5, 2018
384
385
if( usbmoded_init_done_reached != reached ) {
usbmoded_init_done_reached = reached;
Aug 24, 2018
Aug 24, 2018
386
log_warning("init_done -> %s",
Sep 5, 2018
Sep 5, 2018
387
usbmoded_init_done_reached ? "reached" : "not reached");
Aug 24, 2018
Aug 24, 2018
388
}
Jun 5, 2018
Jun 5, 2018
389
390
391
}
/** Check whether init-done flag file exists */
Aug 24, 2018
Aug 24, 2018
392
void usbmoded_probe_init_done(void)
Jun 5, 2018
Jun 5, 2018
393
{
Sep 5, 2018
Sep 5, 2018
394
usbmoded_set_init_done(access(usbmoded_init_done_flagfile, F_OK) == 0);
Oct 18, 2016
Oct 18, 2016
395
396
}
Sep 5, 2018
Sep 5, 2018
397
398
399
400
/* ------------------------------------------------------------------------- *
* MAINLOOP
* ------------------------------------------------------------------------- */
Nov 7, 2016
Nov 7, 2016
401
402
/** Request orderly exit from mainloop
*/
Aug 24, 2018
Aug 24, 2018
403
404
405
406
void usbmoded_exit_mainloop(int exitcode)
{
/* In case multiple exit request get done, retain the
* highest exit code used. */
Sep 5, 2018
Sep 5, 2018
407
408
if( usbmoded_exitcode < exitcode )
usbmoded_exitcode = exitcode;
Aug 24, 2018
Aug 24, 2018
409
410
/* If there is no mainloop to exit, terminate immediately */
Sep 5, 2018
Sep 5, 2018
411
if( !usbmoded_mainloop )
Aug 24, 2018
Aug 24, 2018
412
413
{
log_warning("exit requested outside mainloop; exit(%d) now",
Sep 5, 2018
Sep 5, 2018
414
415
usbmoded_exitcode);
exit(usbmoded_exitcode);
Aug 24, 2018
Aug 24, 2018
416
417
418
}
log_debug("stopping usb-moded mainloop");
Sep 5, 2018
Sep 5, 2018
419
g_main_loop_quit(usbmoded_mainloop);
Aug 24, 2018
Aug 24, 2018
420
421
}
Sep 5, 2018
Sep 5, 2018
422
void usbmoded_handle_signal(int signum)
Nov 7, 2016
Nov 7, 2016
423
{
Aug 24, 2018
Aug 24, 2018
424
425
426
427
428
429
430
431
432
433
log_debug("handle signal: %s\n", strsignal(signum));
if( signum == SIGTERM )
{
/* Assume: Stopped by init process */
usbmoded_exit_mainloop(EXIT_SUCCESS);
}
else if( signum == SIGHUP )
{
/* free and read in modelist again */
Sep 5, 2018
Sep 5, 2018
434
435
usbmoded_free_modelist();
usbmoded_load_modelist();
Aug 24, 2018
Aug 24, 2018
436
Sep 5, 2018
Sep 5, 2018
437
438
common_send_supported_modes_signal();
common_send_available_modes_signal();
Aug 24, 2018
Aug 24, 2018
439
440
// FIXME invalidate current mode
Aug 24, 2018
Aug 24, 2018
441
442
443
444
445
446
447
}
else
{
usbmoded_exit_mainloop(EXIT_FAILURE);
}
}
Sep 5, 2018
Sep 5, 2018
448
449
/* Prepare usb-moded for running the mainloop */
static bool usbmoded_init(void)
Aug 24, 2018
Aug 24, 2018
450
{
Sep 5, 2018
Sep 5, 2018
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
bool ack = false;
/* Check if we are in mid-bootup */
usbmoded_probe_init_done();
if( !worker_init() ) {
log_crit("worker thread init failed");
goto EXIT;
}
if( !sigpipe_init() ) {
log_crit("signal handler init failed");
goto EXIT;
}
Sep 5, 2018
Sep 5, 2018
466
467
if( usbmoded_get_rescue_mode() && usbmoded_init_done_p() ) {
usbmoded_set_rescue_mode(false);
Sep 5, 2018
Sep 5, 2018
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
log_warning("init done passed; rescue mode ignored");
}
/* Connect to SystemBus */
if( !umdbus_init_connection() ) {
log_crit("dbus systembus connection failed");
goto EXIT;
}
/* Start DBus trackers that do async initialization
* so that initial method calls are on the way while
* we do initialization actions that might block. */
/* DSME listener maintains in-user-mode state and is relevant
* only when MEEGOLOCK configure option has been chosen. */
#ifdef MEEGOLOCK
if( !dsme_listener_start() ) {
log_crit("dsme tracking could not be started");
goto EXIT;
}
#endif
/* Devicelock listener maintains devicelock state and is relevant
* only when MEEGOLOCK configure option has been chosen. */
#ifdef MEEGOLOCK
if( !devicelock_start_listener() ) {
log_crit("devicelock tracking could not be started");
goto EXIT;
}
#endif
/* Set daemon config/state data to sane state */
modesetting_init();
Aug 24, 2018
Aug 24, 2018
502
/* check config, merge or create if outdated */
Sep 5, 2018
Sep 5, 2018
503
504
505
if( config_merge_conf_file() != 0 ) {
log_crit("Cannot create or find a valid configuration");
goto EXIT;
Aug 24, 2018
Aug 24, 2018
506
507
508
}
#ifdef APP_SYNC
Sep 5, 2018
Sep 5, 2018
509
appsync_read_list(usbmoded_get_diag_mode());
Aug 24, 2018
Aug 24, 2018
510
511
512
#endif
/* always read dyn modes even if appsync is not used */
Sep 5, 2018
Sep 5, 2018
513
usbmoded_load_modelist();
Aug 24, 2018
Aug 24, 2018
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
if(config_check_trigger())
trigger_init();
/* Set-up mac address before kmod */
if(access("/etc/modprobe.d/g_ether.conf", F_OK) != 0)
{
mac_generate_random_mac();
}
/* During bootup the sysfs control structures might
* not be already in there when usb-moded starts up.
* Retry few times unless init done is / gets reached
* while waiting.
*/
for( int i = 10; ; ) {
if( configfs_init_values() )
break;
if( android_init_values() )
break;
Sep 5, 2018
Sep 5, 2018
536
/* Must probe / poll since we're not yet running mainloop */
Aug 24, 2018
Aug 24, 2018
537
538
539
540
541
542
543
544
usbmoded_probe_init_done();
if( usbmoded_init_done_p() || --i <= 0 ) {
if( !modules_init() )
log_crit("No supported usb control mechanisms found");
break;
}
Sep 5, 2018
Sep 5, 2018
545
common_msleep(1000);
Aug 24, 2018
Aug 24, 2018
546
547
}
Sep 5, 2018
Sep 5, 2018
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/* Allow making systemd control ipc */
if( !systemd_control_start() ) {
log_crit("systemd control could not be started");
goto EXIT;
}
/* If usb-moded happens to crash, it could leave appsync processes
* running. To make sure things are in the order expected by usb-moded
* force stopping of appsync processes during usb-moded startup.
*
* The exception is: When usb-moded starts as a part of bootup. Then
* we can be relatively sure that usb-moded has not been running yet
* and therefore no appsync processes have been started and we can
* skip the blocking ipc required to stop the appsync systemd units. */
#ifdef APP_SYNC
if( usbmoded_init_done_p() ) {
log_warning("usb-moded started after init-done; "
"forcing appsync stop");
appsync_stop(true);
}
#endif
/* Claim D-Bus service name before proceeding with things that
* could result in dbus signal broadcasts from usb-moded interface.
*/
if( !umdbus_init_service() ) {
log_crit("usb-moded dbus service init failed");
goto EXIT;
}
/* Initialize udev listener. Can cause mode changes.
*
* Failing here is allowed if '--fallback' commandline option is used.
*/
if( !umudev_init() && !usbmoded_hw_fallback ) {
log_crit("hwal init failed");
goto EXIT;
}
/* Broadcast supported / hidden modes */
// TODO: should this happen before umudev_init()?
common_send_supported_modes_signal();
common_send_available_modes_signal();
common_send_hidden_modes_signal();
common_send_whitelisted_modes_signal();
/* Act on '--fallback' commandline option */
if( usbmoded_hw_fallback ) {
log_warning("Forcing USB state to connected always. ASK mode non functional!");
/* Since there will be no disconnect signals coming from hw the state should not change */
control_set_cable_state(CABLE_STATE_PC_CONNECTED);
}
ack = true;
EXIT:
return ack;
Aug 24, 2018
Aug 24, 2018
605
606
607
608
609
610
}
/** Release resources allocated by usbmoded_init()
*/
static void usbmoded_cleanup(void)
{
Sep 5, 2018
Sep 5, 2018
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/* Stop the worker thread first to avoid confusion about shared
* resources we are just about to release. */
worker_quit();
/* Detach from SystemBus. Components that hold reference to the
* shared bus connection can still perform cleanup tasks, but new
* references can't be obtained anymore and usb-moded method call
* processing no longer occurs. */
umdbus_cleanup();
/* Stop appsync processes that have been started by usb-moded */
#ifdef APP_SYNC
appsync_stop(false);
#endif
/* Deny making systemd control ipc */
systemd_control_stop();
/* Stop tracking devicelock status */
#ifdef MEEGOLOCK
devicelock_stop_listener();
#endif
/* Stop tracking device state */
#ifdef MEEGOLOCK
dsme_listener_stop();
#endif
/* Stop udev listener */
umudev_quit();
Aug 24, 2018
Aug 24, 2018
642
643
644
645
646
647
/* Undo modules_init() */
modules_quit();
/* Undo trigger_init() */
trigger_stop();
Sep 5, 2018
Sep 5, 2018
648
649
/* Undo usbmoded_load_modelist() */
usbmoded_free_modelist();
Aug 24, 2018
Aug 24, 2018
650
651
652
653
654
655
656
#ifdef APP_SYNC
/* Undo appsync_read_list() */
appsync_free_appsync_list();
#endif
/* Release dynamic memory */
Sep 5, 2018
Sep 5, 2018
657
worker_clear_kernel_module();
Sep 5, 2018
Sep 5, 2018
658
659
660
661
worker_clear_hardware_mode();
control_clear_cable_state();
control_clear_internal_mode();
control_clear_external_mode();
Sep 5, 2018
Sep 5, 2018
662
663
664
665
666
667
668
669
670
671
672
modesetting_quit();
/* Detach from SessionBus connection used for APP_SYNC_DBUS.
*
* Can be handled separately from SystemBus side wind down. */
#ifdef APP_SYNC
# ifdef APP_SYNC_DBUS
dbusappsync_cleanup();
# endif
#endif
Aug 24, 2018
Aug 24, 2018
673
674
675
676
677
678
}
/* ========================================================================= *
* MAIN ENTRY
* ========================================================================= */
Sep 5, 2018
Sep 5, 2018
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
static const char usbmoded_usage_info[] =
"Usage: usb_moded [OPTION]...\n"
"USB mode daemon\n"
"\n"
" -a, --android_usb_broken\n"
" keep gadget active on broken android kernels\n"
" -i, --android_usb_broken_udev_events\n"
" ignore incorrect disconnect events after mode setting\n"
" -f, --fallback \n"
" assume always connected\n"
" -s, --force-syslog \n"
" log to syslog\n"
" -T, --force-stderr \n"
" log to stderr\n"
" -l, --log-line-info\n"
" log to stderr and show origin of logging\n"
" -D, --debug \n"
" turn on debug printing\n"
" -d, --diag \n"
" turn on diag mode\n"
" -h, --help \n"
" display this help and exit\n"
" -r, --rescue \n"
" rescue mode\n"
#ifdef SYSTEMD
" -n, --systemd \n"
" notify systemd when started up\n"
#endif
" -v, --version \n"
" output version information and exit\n"
" -m, --max-cable-delay=<ms>\n"
" maximum delay before accepting cable connection\n"
" -b, --android-bootup-function=<function>\n"
" Setup given function during bootup. Might be required\n"
" on some devices to make enumeration work on the 1st\n"
" cable connect.\n"
"\n";
static const struct option const usbmoded_long_options[] =
{
{ "android_usb_broken", no_argument, 0, 'a' },
{ "android_usb_broken_udev_events", no_argument, 0, 'i' },
{ "fallback", no_argument, 0, 'd' },
{ "force-syslog", no_argument, 0, 's' },
{ "force-stderr", no_argument, 0, 'T' },
{ "log-line-info", no_argument, 0, 'l' },
{ "debug", no_argument, 0, 'D' },
{ "diag", no_argument, 0, 'd' },
{ "help", no_argument, 0, 'h' },
{ "rescue", no_argument, 0, 'r' },
{ "systemd", no_argument, 0, 'n' },
{ "version", no_argument, 0, 'v' },
{ "max-cable-delay", required_argument, 0, 'm' },
{ "android-bootup-function", required_argument, 0, 'b' },
{ 0, 0, 0, 0 }
};
static const char usbmoded_short_options[] = "aifsTlDdhrnvm:b:";
Aug 24, 2018
Aug 24, 2018
738
739
740
/* Display usbmoded_usage information */
static void usbmoded_usage(void)
{
Sep 5, 2018
Sep 5, 2018
741
fprintf(stdout, "%s", usbmoded_usage_info);
Mar 24, 2017
Mar 24, 2017
742
}
Mar 24, 2017
Mar 24, 2017
743
Sep 5, 2018
Sep 5, 2018
744
static void usbmoded_parse_options(int argc, char* argv[])
Mar 22, 2011
Mar 22, 2011
745
{
Aug 24, 2018
Aug 24, 2018
746
/* Parse the command-line options */
Sep 5, 2018
Sep 5, 2018
747
748
749
750
751
752
753
754
for( ;; ) {
int opt = getopt_long(argc, argv,
usbmoded_short_options,
usbmoded_long_options,
0);
if( opt == -1 )
break;
Aug 24, 2018
Aug 24, 2018
755
756
757
switch (opt)
{
case 'a':
Aug 24, 2018
Aug 24, 2018
758
log_warning("Deprecated option: --android_usb_broken");
Aug 24, 2018
Aug 24, 2018
759
760
break;
case 'i':
Aug 24, 2018
Aug 24, 2018
761
log_warning("Deprecated option: --android_usb_broken_udev_events");
Aug 24, 2018
Aug 24, 2018
762
763
break;
case 'f':
Sep 5, 2018
Sep 5, 2018
764
usbmoded_hw_fallback = true;
Aug 24, 2018
Aug 24, 2018
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
break;
case 's':
log_set_type(LOG_TO_SYSLOG);
break;
case 'T':
log_set_type(LOG_TO_STDERR);
break;
case 'D':
log_set_level(LOG_DEBUG);
break;
case 'l':
log_set_type(LOG_TO_STDERR);
log_set_lineinfo(true);
break;
case 'd':
Sep 5, 2018
Sep 5, 2018
784
usbmoded_set_diag_mode(true);
Aug 24, 2018
Aug 24, 2018
785
786
787
788
break;
case 'h':
usbmoded_usage();
Sep 5, 2018
Sep 5, 2018
789
exit(EXIT_SUCCESS);
Aug 24, 2018
Aug 24, 2018
790
791
case 'r':
Sep 5, 2018
Sep 5, 2018
792
usbmoded_set_rescue_mode(true);
Aug 24, 2018
Aug 24, 2018
793
break;
Jan 20, 2014
Jan 20, 2014
794
#ifdef SYSTEMD
Aug 24, 2018
Aug 24, 2018
795
case 'n':
Sep 5, 2018
Sep 5, 2018
796
usbmoded_systemd_notify = true;
Aug 24, 2018
Aug 24, 2018
797
798
799
800
break;
#endif
case 'v':
printf("USB mode daemon version: %s\n", VERSION);
Sep 5, 2018
Sep 5, 2018
801
exit(EXIT_SUCCESS);
Aug 24, 2018
Aug 24, 2018
802
803
804
805
806
807
case 'm':
usbmoded_set_cable_connection_delay(strtol(optarg, 0, 0));
break;
case 'b':
Aug 24, 2018
Aug 24, 2018
808
log_warning("Deprecated option: --android-bootup-function");
Aug 24, 2018
Aug 24, 2018
809
810
811
812
break;
default:
usbmoded_usage();
Sep 5, 2018
Sep 5, 2018
813
exit(EXIT_FAILURE);
Aug 24, 2018
Aug 24, 2018
814
815
}
}
Sep 5, 2018
Sep 5, 2018
816
}
Nov 7, 2016
Nov 7, 2016
817
Sep 5, 2018
Sep 5, 2018
818
819
820
821
822
int main(int argc, char* argv[])
{
/* Library init calls that should be made before
* using library functionality.
*/
Sep 10, 2013
Sep 10, 2013
823
#if !GLIB_CHECK_VERSION(2, 36, 0)
Aug 24, 2018
Aug 24, 2018
824
g_type_init();
Sep 10, 2013
Sep 10, 2013
825
#endif
Aug 15, 2012
Aug 15, 2012
826
#if !GLIB_CHECK_VERSION(2, 31, 0)
Aug 24, 2018
Aug 24, 2018
827
g_thread_init(NULL);
Aug 15, 2012
Aug 15, 2012
828
#endif
Aug 24, 2018
Aug 24, 2018
829
830
dbus_threads_init_default();
Sep 5, 2018
Sep 5, 2018
831
832
833
/* - - - - - - - - - - - - - - - - - - - *
* OPTIONS
* - - - - - - - - - - - - - - - - - - - */
Aug 24, 2018
Aug 24, 2018
834
Sep 5, 2018
Sep 5, 2018
835
836
837
/* Set logging defaults */
log_init();
log_set_name(basename(*argv));
Nov 7, 2016
Nov 7, 2016
838
Sep 5, 2018
Sep 5, 2018
839
840
/* Parse command line options */
usbmoded_parse_options(argc, argv);
Aug 24, 2018
Aug 24, 2018
841
Sep 5, 2018
Sep 5, 2018
842
843
fprintf(stderr, "usb_moded %s starting\n", VERSION);
fflush(stderr);
Aug 24, 2018
Aug 24, 2018
844
Sep 5, 2018
Sep 5, 2018
845
846
/* Silence common_system() calls */
if( log_get_type() != LOG_TO_STDERR && log_get_level() != LOG_DEBUG )
Aug 24, 2018
Aug 24, 2018
847
{
Sep 5, 2018
Sep 5, 2018
848
849
850
851
852
853
if( !freopen("/dev/null", "a", stdout) ) {
log_err("can't redirect stdout: %m");
}
if( !freopen("/dev/null", "a", stderr) ) {
log_err("can't redirect stderr: %m");
}
Aug 24, 2018
Aug 24, 2018
854
}
Mar 22, 2011
Mar 22, 2011
855
Sep 5, 2018
Sep 5, 2018
856
857
858
/* - - - - - - - - - - - - - - - - - - - *
* INITIALIZE
* - - - - - - - - - - - - - - - - - - - */
Aug 24, 2018
Aug 24, 2018
859
Sep 5, 2018
Sep 5, 2018
860
if( !usbmoded_init() )
Aug 24, 2018
Aug 24, 2018
861
862
863
864
865
866
867
goto EXIT;
/* - - - - - - - - - - - - - - - - - - - *
* EXECUTE
* - - - - - - - - - - - - - - - - - - - */
/* Tell systemd that we have started up */
Nov 7, 2016
Nov 7, 2016
868
#ifdef SYSTEMD
Sep 5, 2018
Sep 5, 2018
869
870
if( usbmoded_systemd_notify ) {
log_debug("notifying systemd");
Aug 24, 2018
Aug 24, 2018
871
872
sd_notify(0, "READY=1");
}
Nov 7, 2016
Nov 7, 2016
873
874
#endif
Aug 24, 2018
Aug 24, 2018
875
/* init succesful, run main loop */
Sep 5, 2018
Sep 5, 2018
876
877
usbmoded_exitcode = EXIT_SUCCESS;
usbmoded_mainloop = g_main_loop_new(NULL, FALSE);
Nov 7, 2016
Nov 7, 2016
878
Aug 24, 2018
Aug 24, 2018
879
log_debug("enter usb-moded mainloop");
Sep 5, 2018
Sep 5, 2018
880
g_main_loop_run(usbmoded_mainloop);
Aug 24, 2018
Aug 24, 2018
881
log_debug("leave usb-moded mainloop");
Nov 7, 2016
Nov 7, 2016
882
Sep 5, 2018
Sep 5, 2018
883
884
g_main_loop_unref(usbmoded_mainloop),
usbmoded_mainloop = 0;
Nov 7, 2016
Nov 7, 2016
885
Aug 24, 2018
Aug 24, 2018
886
887
888
/* - - - - - - - - - - - - - - - - - - - *
* CLEANUP
* - - - - - - - - - - - - - - - - - - - */
Mar 22, 2011
Mar 22, 2011
889
EXIT:
Aug 24, 2018
Aug 24, 2018
890
usbmoded_cleanup();
Nov 7, 2016
Nov 7, 2016
891
Sep 5, 2018
Sep 5, 2018
892
893
894
/* Memory leak debugging - instruct libdbus to flush resources. */
#if 0
dbus_shutdown();
Nov 7, 2016
Nov 7, 2016
895
896
#endif
Sep 5, 2018
Sep 5, 2018
897
898
899
900
/* - - - - - - - - - - - - - - - - - - - *
* EXIT
* - - - - - - - - - - - - - - - - - - - */
Aug 24, 2018
Aug 24, 2018
901
902
903
/* Must be done just before exit to make sure no more wakelocks
* are taken and left behind on exit path */
usbmoded_allow_suspend();
Nov 7, 2016
Nov 7, 2016
904
Aug 24, 2018
Aug 24, 2018
905
log_debug("usb-moded return from main, with exit code %d",
Sep 5, 2018
Sep 5, 2018
906
907
usbmoded_exitcode);
return usbmoded_exitcode;
Mar 22, 2011
Mar 22, 2011
908
}