/
usb_moded.c
1544 lines (1322 loc) · 39.1 KB
1
2
3
4
/**
@file usb_moded.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
5
Copyright (C) 2012-2016 Jolla. All rights reserved.
6
7
@author: Philippe De Swert <philippe.de-swert@nokia.com>
8
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
9
@author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 <getopt.h>
28
#include <stdio.h>
29
30
31
#include <sys/stat.h>
#include <sys/wait.h>
32
#include <signal.h>
33
34
#include <libkmod.h>
35
36
37
38
#ifdef SYSTEMD
#include <systemd/sd-daemon.h>
#endif
39
40
41
42
43
44
45
46
#include "usb_moded.h"
#include "usb_moded-modes.h"
#include "usb_moded-dbus.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded-hw-ab.h"
#include "usb_moded-modules.h"
#include "usb_moded-log.h"
47
#include "usb_moded-lock.h"
48
49
50
#include "usb_moded-modesetting.h"
#include "usb_moded-modules.h"
#include "usb_moded-appsync.h"
51
52
#include "usb_moded-trigger.h"
#include "usb_moded-config.h"
53
#include "usb_moded-config-private.h"
54
#include "usb_moded-network.h"
55
#include "usb_moded-mac.h"
56
#include "usb_moded-android.h"
57
#include "usb_moded-systemd.h"
58
59
60
61
#ifdef MEEGOLOCK
#include "usb_moded-dsme.h"
#endif
62
63
64
65
66
67
/* Wakelogging is noisy, do not log it by default */
#ifndef VERBOSE_WAKELOCKING
# define VERBOSE_WAKELOCKING 0
#endif
68
69
/* global definitions */
70
71
72
static int usb_moded_exitcode = EXIT_FAILURE;
static GMainLoop *usb_moded_mainloop = NULL;
73
74
gboolean rescue_mode = FALSE;
gboolean diag_mode = FALSE;
75
gboolean hw_fallback = FALSE;
76
gboolean android_broken_usb = FALSE;
77
78
gboolean android_ignore_udev_events = FALSE;
gboolean android_ignore_next_udev_disconnect_event = FALSE;
79
80
81
#ifdef SYSTEMD
static gboolean systemd_notify = FALSE;
#endif
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
/** 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
/** Currently allowed cable detection delay
*/
int cable_connection_delay = CABLE_CONNECTION_DELAY_DEFAULT;
/** Helper for setting allowed cable detection delay
*
* Used for implementing --max-cable-delay=<ms> option.
*/
static void set_cable_connection_delay(int delay_ms)
{
if( delay_ms < CABLE_CONNECTION_DELAY_MAXIMUM )
cable_connection_delay = delay_ms;
else {
cable_connection_delay = CABLE_CONNECTION_DELAY_MAXIMUM;
log_warning("using maximum connection delay: %d ms",
cable_connection_delay);
}
}
123
struct usb_mode current_mode;
124
guint charging_timeout = 0;
125
static GList *modelist;
126
127
/* static helper functions */
128
static gboolean set_disconnected(gpointer data);
129
static gboolean set_disconnected_silent(gpointer data);
130
131
132
static void usb_moded_init(void);
static gboolean charging_fallback(gpointer data);
static void usage(void);
133
static bool init_done_p(void);
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* ============= Implementation starts here =========================================== */
/** set the usb connection status
*
* @param connected The connection status, true for connected
*
*/
void set_usb_connected(gboolean connected)
{
if(connected)
{
/* do not go through the routine if already connected to avoid
spurious load/unloads due to faulty signalling
NOKIA: careful with devicelock
*/
150
if(current_mode.connected == connected)
151
return;
152
153
154
155
156
157
if(charging_timeout)
{
g_source_remove(charging_timeout);
charging_timeout = 0;
}
158
current_mode.connected = TRUE;
159
160
161
/* signal usb connected */
log_debug("usb connected\n");
usb_moded_send_signal(USB_CONNECTED);
162
163
164
set_usb_connected_state();
}
else
165
{
166
167
if(current_mode.connected == FALSE)
return;
168
169
170
171
if(android_ignore_next_udev_disconnect_event) {
android_ignore_next_udev_disconnect_event = FALSE;
return;
}
172
173
current_mode.connected = FALSE;
set_disconnected(NULL);
174
175
176
177
178
179
/* Some android kernels check for an active gadget to enable charging and
* cable detection, meaning USB is completely dead unless we keep the gadget
* active
*/
if(current_mode.android_usb_broken)
set_android_charging_mode();
180
181
182
if (android_ignore_udev_events) {
android_ignore_next_udev_disconnect_event = TRUE;
}
183
}
184
185
186
187
188
}
static gboolean set_disconnected(gpointer data)
{
189
/* let usb settle */
190
usb_moded_sleep(1);
191
192
193
194
/* only disconnect for real if we are actually still disconnected */
if(!get_usb_connection_state())
{
log_debug("usb disconnected\n");
195
196
/* signal usb disconnected */
usb_moded_send_signal(USB_DISCONNECTED);
197
/* unload modules and general cleanup if not charging */
198
199
if(strcmp(get_usb_mode(), MODE_CHARGING) ||
strcmp(get_usb_mode(), MODE_CHARGING_FALLBACK))
200
usb_moded_mode_cleanup(get_usb_module());
201
/* Nothing else as we do not need to do anything for cleaning up charging mode */
202
usb_moded_module_cleanup(get_usb_module());
203
set_usb_mode(MODE_UNDEFINED);
204
205
206
}
return FALSE;
207
208
}
209
210
211
212
213
214
215
/* set disconnected without sending signals. */
static gboolean set_disconnected_silent(gpointer data)
{
if(!get_usb_connection_state())
{
log_debug("Resetting connection data after HUP\n");
/* unload modules and general cleanup if not charging */
216
217
if(strcmp(get_usb_mode(), MODE_CHARGING) ||
strcmp(get_usb_mode(), MODE_CHARGING_FALLBACK))
218
219
220
221
222
223
224
225
226
usb_moded_mode_cleanup(get_usb_module());
/* Nothing else as we do not need to do anything for cleaning up charging mode */
usb_moded_module_cleanup(get_usb_module());
set_usb_mode(MODE_UNDEFINED);
}
return FALSE;
}
227
228
229
230
231
/** set and track charger state
*
*/
void set_charger_connected(gboolean state)
{
232
233
/* check if charger is already connected
to avoid spamming dbus */
234
if(current_mode.connected == state)
235
236
return;
237
238
239
240
if(state)
{
usb_moded_send_signal(CHARGER_CONNECTED);
set_usb_mode(MODE_CHARGER);
241
current_mode.connected = TRUE;
242
243
244
}
else
{
245
current_mode.connected = FALSE;
246
247
usb_moded_send_signal(CHARGER_DISCONNECTED);
set_usb_mode(MODE_UNDEFINED);
248
current_mode.connected = FALSE;
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
/** Check if we can/should leave charging fallback mode
*/
void
rethink_usb_charging_fallback(void)
{
/* Cable must be connected and suitable usb-mode mode
* selected for any of this to apply.
*/
if( !get_usb_connection_state() )
goto EXIT;
const char *usb_mode = get_usb_mode();
if( strcmp(usb_mode, MODE_UNDEFINED) &&
strcmp(usb_mode, MODE_CHARGING_FALLBACK) )
goto EXIT;
/* If device locking is supported, the device must be in
* unlocked state (or rescue mode must be active).
*/
#ifdef MEEGOLOCK
if( !usb_moded_get_export_permission() && !rescue_mode ) {
log_notice("device is locked; stay in %s", usb_mode);
goto EXIT;
}
#endif
/* Device must be in USER state or in rescue mode
*/
280
#ifdef MEEGOLOCK
281
282
283
284
if( !is_in_user_state() && !rescue_mode ) {
log_notice("device is not in USER mode; stay in %s", usb_mode);
goto EXIT;
}
285
#endif
286
287
288
289
290
291
292
log_debug("attempt to leave %s", usb_mode);
set_usb_connected_state();
EXIT:
return;
}
293
294
295
296
297
298
/** set the chosen usb state
*
*/
void set_usb_connected_state(void)
{
299
char *mode_to_set;
300
301
302
if(rescue_mode)
{
303
log_debug("Entering rescue mode!\n");
304
305
306
set_usb_mode(MODE_DEVELOPER);
return;
}
307
else if(diag_mode)
308
309
310
311
312
313
314
315
316
317
{
log_debug("Entering diagnostic mode!\n");
if(modelist)
{
GList *iter = modelist;
struct mode_list_elem *data = iter->data;
set_usb_mode(data->mode_name);
}
return;
}
318
319
320
mode_to_set = get_mode_setting();
321
#ifdef MEEGOLOCK
322
/* check if we are allowed to export system contents 0 is unlocked */
323
gboolean can_export = usb_moded_get_export_permission();
324
325
/* We check also if the device is in user state or not.
If not we do not export anything. We presume ACT_DEAD charging */
326
if(mode_to_set && can_export && is_in_user_state())
327
#else
328
if(mode_to_set)
329
#endif /* MEEGOLOCK */
330
{
331
332
333
334
335
336
/* This is safe to do here as the starting condition is
MODE_UNDEFINED, and having a devicelock being activated when
a mode is set will not interrupt it */
if(!strcmp(mode_to_set, current_mode.mode))
goto end;
337
338
339
340
341
342
343
344
345
346
347
348
349
if (!strcmp(MODE_ASK, mode_to_set))
{
/*! If charging mode is the only available selection, don't ask
just select it */
gchar *available_modes = get_mode_list(AVAILABLE_MODES_LIST);
if (!strcmp(MODE_CHARGING, available_modes)) {
gchar *temp = mode_to_set;
mode_to_set = available_modes;
available_modes = temp;
}
g_free(available_modes);
}
350
351
352
353
354
355
356
if(!strcmp(MODE_ASK, mode_to_set))
{
/* send signal, mode will be set when the dialog service calls
the set_mode method call.
*/
usb_moded_send_signal(USB_CONNECTED_DIALOG_SHOW);
/* fallback to charging mode after 3 seconds */
357
358
if( charging_timeout )
g_source_remove(charging_timeout);
359
charging_timeout = g_timeout_add_seconds(3, charging_fallback, NULL);
360
361
362
363
364
365
366
367
368
/* in case there was nobody listening for the UI, they will know
that the UI is needed by requesting the current mode */
set_usb_mode(MODE_ASK);
}
else
set_usb_mode(mode_to_set);
}
else
{
369
/* config is corrupted or we do not have a mode configured, fallback to charging
370
371
372
We also fall back here in case the device is locked and we do not
export the system contents. Or if we are in acting dead mode.
*/
373
set_usb_mode(MODE_CHARGING_FALLBACK);
374
}
375
end:
376
free(mode_to_set);
377
378
379
380
381
382
383
384
385
}
/** set the usb mode
*
* @param mode The requested USB mode
*
*/
void set_usb_mode(const char *mode)
{
386
387
/* set return to 1 to be sure to error out if no matching mode is found either */
int ret=1, net=0;
388
389
390
log_debug("Setting %s\n", mode);
391
392
/* CHARGING AND FALLBACK CHARGING are always ok to set, so this can be done
before the optional second device lock check */
393
if(!strcmp(mode, MODE_CHARGING) || !strcmp(mode, MODE_CHARGING_FALLBACK))
394
395
396
397
398
399
{
check_module_state(MODULE_MASS_STORAGE);
/* for charging we use a fake file_storage (blame USB certification for this insanity */
set_usb_module(MODULE_MASS_STORAGE);
/* MODULE_CHARGING has all the parameters defined, so it will not match the g_file_storage rule in usb_moded_load_module */
ret = usb_moded_load_module(MODULE_CHARGING);
400
401
/* if charging mode setting did not succeed we might be dealing with android */
if(ret)
402
{
403
404
405
if (android_ignore_udev_events) {
android_ignore_next_udev_disconnect_event = TRUE;
}
406
set_usb_module(MODULE_NONE);
407
ret = set_android_charging_mode();
408
}
409
410
goto end;
}
411
412
413
414
415
416
417
418
/* Dedicated charger mode needs nothing to be done and no user interaction */
if(!strcmp(mode, MODE_CHARGER))
{
ret = 0;
goto end;
}
419
420
421
422
423
#ifdef MEEGOLOCK
/* check if we are allowed to export system contents 0 is unlocked */
/* In ACTDEAD export is always ok */
if(is_in_user_state())
{
424
gboolean can_export = usb_moded_get_export_permission();
425
426
if(!can_export && !rescue_mode)
427
428
429
430
431
432
433
{
log_debug("Secondary device lock check failed. Not setting mode!\n");
goto end;
}
}
#endif /* MEEGOLOCK */
434
435
436
/* nothing needs to be done for this mode but signalling.
Handled here to avoid issues with ask menu and devicelock */
if(!strcmp(mode, MODE_ASK))
437
438
{
ret = 0;
439
goto end;
440
}
441
442
443
444
445
446
447
448
449
450
451
/* go through all the dynamic modes if the modelist exists*/
if(modelist)
{
GList *iter;
for( iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
if(!strcmp(mode, data->mode_name))
{
452
log_debug("Matching mode %s found.\n", mode);
453
454
455
check_module_state(data->mode_module);
set_usb_module(data->mode_module);
ret = usb_moded_load_module(data->mode_module);
456
457
/* set data before calling any of the dynamic mode functions
as they will use the get_usb_mode_data function */
458
set_usb_mode_data(data);
459
/* check if modules are ok before continuing */
460
461
462
463
if(!ret) {
if (android_ignore_udev_events) {
android_ignore_next_udev_disconnect_event = TRUE;
}
464
ret = set_dynamic_mode();
465
}
466
467
468
469
}
}
}
470
end:
471
472
/* if ret != 0 then usb_module loading failed
no mode matched or MODE_UNDEFINED was requested */
473
474
475
476
if(ret)
{
set_usb_module(MODULE_NONE);
mode = MODE_UNDEFINED;
477
set_usb_mode_data(NULL);
478
log_debug("mode setting failed or device disconnected, mode to set was = %s\n", mode);
479
}
480
481
if(net)
log_debug("Network setting failed!\n");
482
483
free(current_mode.mode);
current_mode.mode = strdup(mode);
484
485
486
487
488
/* CHARGING_FALLBACK is an internal mode not to be broadcasted outside */
if(!strcmp(mode, MODE_CHARGING_FALLBACK))
usb_moded_send_signal(MODE_CHARGING);
else
usb_moded_send_signal(get_usb_mode());
489
}
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* check if a mode is in a list */
static bool mode_in_list(const char *mode, char * const *modes)
{
int i;
if (!modes)
return false;
for(i = 0; modes[i] != NULL; i++)
{
if(!strcmp(modes[i], mode))
return true;
}
return false;
}
507
508
509
/** check if a given usb_mode exists
*
* @param mode The mode to look for
510
* @return 0 if mode exists, 1 if it does not exist
511
512
513
514
*
*/
int valid_mode(const char *mode)
{
515
int valid = 1;
516
517
518
/* MODE_ASK, MODE_CHARGER and MODE_CHARGING_FALLBACK are not modes that are settable seen their special 'internal' status
so we only check the modes that are announed outside. Only exception is the built in MODE_CHARGING */
if(!strcmp(MODE_CHARGING, mode))
519
valid = 0;
520
521
else
{
522
523
524
525
526
527
528
529
530
531
char *whitelist;
gchar **whitelist_split = NULL;
whitelist = get_mode_whitelist();
if (whitelist)
{
whitelist_split = g_strsplit(whitelist, ",", 0);
g_free(whitelist);
}
532
533
534
535
536
537
538
539
/* check dynamic modes */
if(modelist)
{
GList *iter;
for( iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
540
541
542
543
544
545
if(!strcmp(mode, data->mode_name))
{
if (!whitelist_split || mode_in_list(data->mode_name, whitelist_split))
valid = 0;
break;
}
546
}
547
548
g_strfreev(whitelist_split);
549
550
}
}
551
return valid;
552
553
}
554
555
556
/** make a list of all available usb modes
*
557
* @param type The type of list to return. Supported or available.
558
559
560
* @return a comma-separated list of modes (MODE_ASK not included as it is not a real mode)
*
*/
561
gchar *get_mode_list(mode_list_type_t type)
562
{
563
564
565
GString *modelist_str;
modelist_str = g_string_new(NULL);
566
567
if(!diag_mode)
568
569
{
/* check dynamic modes */
570
if(modelist)
571
572
{
GList *iter;
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
char *hidden_modes_list, *whitelist;
gchar **hidden_mode_split = NULL, **whitelist_split = NULL;
hidden_modes_list = get_hidden_modes();
if(hidden_modes_list)
{
hidden_mode_split = g_strsplit(hidden_modes_list, ",", 0);
g_free(hidden_modes_list);
}
if (type == AVAILABLE_MODES_LIST)
{
whitelist = get_mode_whitelist();
if (whitelist)
{
whitelist_split = g_strsplit(whitelist, ",", 0);
g_free(whitelist);
}
}
592
593
594
595
for( iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
596
597
598
599
600
601
602
603
604
/* skip items in the hidden list */
if (mode_in_list(data->mode_name, hidden_mode_split))
continue;
/* if there is a whitelist skip items not in the list */
if (whitelist_split && !mode_in_list(data->mode_name, whitelist_split))
continue;
605
modelist_str = g_string_append(modelist_str, data->mode_name);
606
modelist_str = g_string_append(modelist_str, ", ");
607
}
608
609
610
611
g_strfreev(hidden_mode_split);
g_strfreev(whitelist_split);
}
612
613
614
615
616
617
618
619
620
621
/* end with charging mode */
g_string_append(modelist_str, MODE_CHARGING);
return(g_string_free(modelist_str, FALSE));
}
else
{
/* diag mode. there is only one active mode */
g_string_append(modelist_str, MODE_DIAG);
return(g_string_free(modelist_str, FALSE));
622
623
624
}
}
625
626
627
628
629
/** get the usb mode
*
* @return the currently set mode
*
*/
630
inline const char * get_usb_mode(void)
631
632
633
634
635
636
637
638
639
{
return(current_mode.mode);
}
/** set the loaded module
*
* @param module The module name for the requested mode
*
*/
640
void set_usb_module(const char *module)
641
{
642
643
free(current_mode.module);
current_mode.module = strdup(module);
644
645
646
647
648
649
650
}
/** get the supposedly loaded module
*
* @return The name of the loaded module
*
*/
651
inline const char * get_usb_module(void)
652
653
654
655
656
657
658
659
660
{
return(current_mode.module);
}
/** get if the cable is connected or not
*
* @ return A boolean value for connected (TRUE) or not (FALSE)
*
*/
661
inline gboolean get_usb_connection_state(void)
662
663
664
665
{
return current_mode.connected;
}
666
667
/** set connection status for some corner cases
*
668
* @param state The connection status that needs to be set. Connected (TRUE)
669
670
*
*/
671
inline void set_usb_connection_state(gboolean state)
672
673
674
{
current_mode.connected = state;
}
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/** set the mode_list_elem data
*
* @param data mode_list_element pointer
*
*/
void set_usb_mode_data(struct mode_list_elem *data)
{
current_mode.data = data;
}
/** get the usb mode data
*
* @return a pointer to the usb mode data
*
*/
inline struct mode_list_elem * get_usb_mode_data(void)
{
return(current_mode.data);
}
696
697
698
699
700
701
702
/*================ Static functions ===================================== */
/* set default values for usb_moded */
static void usb_moded_init(void)
{
current_mode.connected = FALSE;
current_mode.mounted = FALSE;
703
704
current_mode.mode = strdup(MODE_UNDEFINED);
current_mode.module = strdup(MODULE_NONE);
705
706
707
708
if(android_broken_usb)
current_mode.android_usb_broken = TRUE;
709
710
711
712
713
714
715
/* check config, merge or create if outdated */
if(conf_file_merge() != 0)
{
log_err("Cannot create or find a valid configuration. Exiting.\n");
exit(1);
}
716
#ifdef APP_SYNC
717
readlist(diag_mode);
718
#endif
719
720
721
722
/* always read dyn modes even if appsync is not used */
modelist = read_mode_list(diag_mode);
723
724
if(check_trigger())
trigger_init();
725
726
/* Set-up mac address before kmod */
727
if(access("/etc/modprobe.d/g_ether.conf", F_OK) != 0)
728
729
730
{
generate_random_mac();
}
731
732
/* kmod init */
733
usb_moded_module_ctx_init();
734
735
736
737
/* Android specific stuff */
if(android_settings())
android_init_values();
738
739
740
/* TODO: add more start-up clean-up and init here if needed */
}
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/** Release resources allocated by usb_moded_init()
*/
static void usb_moded_cleanup(void)
{
/* Undo usb_moded_module_ctx_init() */
usb_moded_module_ctx_cleanup();
/* Undo trigger_init() */
trigger_stop();
/* Undo read_mode_list() */
free_mode_list(modelist);
#ifdef APP_SYNC
/* Undo readlist() */
free_appsync_list();
#endif
/* Release dynamic memory */
free(current_mode.module),
current_mode.module = 0;
free(current_mode.mode),
current_mode.mode = 0;
}
767
768
769
770
771
772
773
774
/* charging fallback handler */
static gboolean charging_fallback(gpointer data)
{
/* if a mode has been set we don't want it changed to charging
* after 5 seconds. We set it to ask, so anything different
* means a mode has been set */
if(strcmp(get_usb_mode(), MODE_ASK) != 0)
return FALSE;
775
776
set_usb_mode(MODE_CHARGING_FALLBACK);
777
778
779
/* since this is the fallback, we keep an indication
for the UI, as we are not really in charging mode.
*/
780
781
free(current_mode.mode);
current_mode.mode = strdup(MODE_ASK);
782
current_mode.data = NULL;
783
charging_timeout = 0;
784
785
786
787
788
log_info("Falling back on charging mode.\n");
return(FALSE);
}
789
790
static void sigint_handler(int signum)
{
791
log_debug("handle signal: %s\n", strsignal(signum));
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
if( signum == SIGTERM )
{
/* Assume: Stopped by init process */
usb_moded_stop(EXIT_SUCCESS);
}
else if( signum == SIGHUP )
{
struct mode_list_elem *data;
/* clean up current mode */
data = get_usb_mode_data();
set_disconnected_silent(data);
/* clear existing data to be sure */
set_usb_mode_data(NULL);
/* free and read in modelist again */
free_mode_list(modelist);
809
810
modelist = read_mode_list(diag_mode);
811
812
send_supported_modes_signal();
813
send_available_modes_signal();
814
815
816
817
818
}
else
{
usb_moded_stop(EXIT_FAILURE);
}
819
820
}
821
822
823
824
825
826
827
/* Display usage information */
static void usage(void)
{
fprintf(stdout,
"Usage: usb_moded [OPTION]...\n"
"USB mode daemon\n"
"\n"
828
" -a, --android_usb_broken \tkeep gadget active on broken android kernels\n"
829
" -i, --android_usb_broken_udev_events \tignore incorrect disconnect events after mode setting\n"
830
831
832
" -f, --fallback \tassume always connected\n"
" -s, --force-syslog \t\tlog to syslog\n"
" -T, --force-stderr \t\tlog to stderr\n"
833
" -l, --log-line-info \t\tlog to stderr and show origin of logging\n"
834
835
836
837
" -D, --debug \t\tturn on debug printing\n"
" -d, --diag \t\tturn on diag mode\n"
" -h, --help \t\tdisplay this help and exit\n"
" -r, --rescue \t\trescue mode\n"
838
#ifdef SYSTEMD
839
" -n, --systemd \t\tnotify systemd when started up\n"
840
#endif
841
" -v, --version \t\toutput version information and exit\n"
842
" -m, --max-cable-delay=<ms>\tmaximum delay before accepting cable connection\n"
843
844
845
"\n");
}
846
void send_supported_modes_signal(void)
847
848
{
/* Send supported modes signal */
849
gchar *mode_list = get_mode_list(SUPPORTED_MODES_LIST);
850
851
852
853
usb_moded_send_supported_modes_signal(mode_list);
g_free(mode_list);
}
854
855
856
857
858
859
860
void send_available_modes_signal(void)
{
gchar *mode_list = get_mode_list(AVAILABLE_MODES_LIST);
usb_moded_send_available_modes_signal(mode_list);
g_free(mode_list);
}
861
862
863
864
865
866
867
868
869
870
void send_hidden_modes_signal(void)
{
/* Send hidden modes signal */
gchar *mode_list = get_hidden_modes();
if(mode_list) {
usb_moded_send_hidden_modes_signal(mode_list);
g_free(mode_list);
}
}
871
872
873
874
875
876
877
878
879
void send_whitelisted_modes_signal(void)
{
gchar *mode_list = get_mode_whitelist();
if(mode_list) {
usb_moded_send_whitelisted_modes_signal(mode_list);
g_free(mode_list);
}
}
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Pipe fd for transferring signals to mainloop context */
static int sigpipe_fd = -1;
/** Glib io watch callback for reading signals from signal pipe
*
* @param channel glib io channel
* @param condition wakeup reason
* @param data user data (unused)
*
* @return TRUE to keep the iowatch, or FALSE to disable it
*/
static gboolean sigpipe_read_signal_cb(GIOChannel *channel,
GIOCondition condition,
gpointer data)
{
gboolean keep_watch = FALSE;
int fd, rc, sig;
(void)data;
/* Should never happen, but we must disable the io watch
* if the pipe fd still goes into unexpected state ... */
if( condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL) )
goto EXIT;
if( (fd = g_io_channel_unix_get_fd(channel)) == -1 )
goto EXIT;
/* If the actual read fails, terminate with core dump */
rc = TEMP_FAILURE_RETRY(read(fd, &sig, sizeof sig));
if( rc != (int)sizeof sig )
abort();
/* handle the signal */
sigint_handler(sig);
keep_watch = TRUE;
EXIT:
if( !keep_watch )
log_crit("disabled signal handler io watch\n");
return keep_watch;
}
/** Async signal handler for writing signals to signal pipe
*
* @param sig the signal number to pass to mainloop via pipe
*/
static void sigpipe_write_signal_cb(int sig)
{
/* NOTE: This function *MUST* be kept async-signal-safe! */
static volatile int exit_tries = 0;
int rc;
/* Restore signal handler */
signal(sig, sigpipe_write_signal_cb);
switch( sig )
{
case SIGINT:
case SIGQUIT:
case SIGTERM:
/* If we receive multiple signals that should have
* caused the process to exit, assume that mainloop
* is stuck and terminate with core dump. */
if( ++exit_tries >= 2 )
abort();
break;
default:
break;
}
/* Transfer the signal to mainloop via pipe ... */
rc = TEMP_FAILURE_RETRY(write(sigpipe_fd, &sig, sizeof sig));
/* ... or terminate with core dump in case of failures */
if( rc != (int)sizeof sig )
abort();
}
/** Create a pipe and io watch for handling signal from glib mainloop
*
* @return TRUE on success, or FALSE in case of errors
*/
static gboolean sigpipe_crate_pipe(void)
{
gboolean res = FALSE;
GIOChannel *chn = 0;
int pfd[2] = { -1, -1 };
if( pipe2(pfd, O_CLOEXEC) == -1 )
goto EXIT;
if( (chn = g_io_channel_unix_new(pfd[0])) == 0 )
goto EXIT;
if( !g_io_add_watch(chn, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
sigpipe_read_signal_cb, 0) )
goto EXIT;
g_io_channel_set_close_on_unref(chn, TRUE), pfd[0] = -1;
sigpipe_fd = pfd[1], pfd[1] = -1;
res = TRUE;
EXIT:
if( chn ) g_io_channel_unref(chn);
if( pfd[0] != -1 ) close(pfd[0]);
if( pfd[1] != -1 ) close(pfd[1]);
return res;
}
/** Install async signal handlers
*/
static void sigpipe_trap_signals(void)