/
usb_moded.c
695 lines (608 loc) · 17.2 KB
/
usb_moded.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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
605
606
607
608
609
610
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
@file usb_moded.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 <getopt.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifdef NOKIA
#include <string.h>
#endif
#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-gconf.h"
#include "usb_moded-modules.h"
#include "usb_moded-log.h"
#include "usb_moded-lock.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-modules.h"
#include "usb_moded-appsync.h"
#include "usb_moded-trigger.h"
#include "usb_moded-config.h"
#include "usb_moded-config-private.h"
#include "usb_moded-network.h"
/* global definitions */
extern const char *log_name;
extern int log_level;
extern int log_type;
gboolean run_as_daemon = FALSE;
gboolean runlevel_ignore = FALSE;
struct usb_mode current_mode;
guint charging_timeout = 0;
#ifdef NOKIA
gboolean special_mode = FALSE;
guint timeout_source = 0;
#endif /* NOKIA */
#ifdef APP_SYNC
static GList *modelist;
#endif /* APP_SYNC */
/* static helper functions */
static gboolean set_disconnected(gpointer data);
static void usb_moded_init(void);
static gboolean charging_fallback(gpointer data);
static void usage(void);
static gboolean daemonize(void);
/* ============= Implementation starts here =========================================== */
/** set the usb connection status
*
* @param connected The connection status, true for connected
*
*/
void set_usb_connected(gboolean connected)
{
#ifdef NOKIA
if(special_mode)
{
/* Do nothing for the time being to leave currently loaded modules active.
Set special_mode to false so next usb connect makes things work as they should.
*/
special_mode = FALSE;
log_debug("nsu active. Not doing anything before cable disconnect/reconnect\n");
return;
}
#endif /* NOKIA */
if(connected)
{
/* do not go through the routine if already connected to avoid
spurious load/unloads due to faulty signalling
NOKIA: careful with devicelock
*/
if(current_mode.connected)
return;
#ifdef NOKIA
if(timeout_source)
{
g_source_remove(timeout_source);
timeout_source = 0;
}
#endif /* NOKIA */
if(charging_timeout)
{
g_source_remove(charging_timeout);
charging_timeout = 0;
}
current_mode.connected = TRUE;
set_usb_connected_state();
}
else
{
current_mode.connected = FALSE;
set_disconnected(NULL);
}
}
static gboolean set_disconnected(gpointer data)
{
/* only disconnect for real if we are actually still disconnected */
if(!get_usb_connection_state())
{
log_debug("usb disconnected\n");
#ifdef NOKIA
/* delayed clean-up of state */
timeout_source = g_timeout_add_seconds(3, usb_cleanup_timeout, NULL);
#else
/* signal usb disconnected */
usb_moded_send_signal(USB_DISCONNECTED);
/* unload modules and general cleanup */
usb_moded_mode_cleanup(get_usb_module());
usb_moded_module_cleanup(get_usb_module());
set_usb_mode(MODE_UNDEFINED);
#endif /* NOKIA */
}
return FALSE;
}
/** set the chosen usb state
*
*/
void set_usb_connected_state(void)
{
const char *mode_to_set;
#if defined MEEGOLOCK
int export = 0;
#endif /* MEEGOLOCK */
/* signal usb connected */
log_debug("usb connected\n");
usb_moded_send_signal(USB_CONNECTED);
mode_to_set = get_mode_setting();
#if defined MEEGOLOCK
/* check if we are allowed to export system contents 0 is unlocked */
export = usb_moded_get_export_permission();
#endif
#ifdef MEEGOLOCK
int act_dead = 0;
/* check if we are in acting dead or not, /tmp/USER will not exist in acting dead */
act_dead = access("/tmp/USER", R_OK);
if(mode_to_set && !export && !act_dead)
#else
if(mode_to_set && !export)
#endif /* MEEGOLOCK */
{
#ifdef NOKIA
/* If we switch to another mode than the one that is still set before the
clean-up timeout expired we need to clean up */
if(strcmp(mode_to_set, get_usb_mode()))
usb_moded_mode_cleanup(get_usb_module());
#endif /* NOKIA */
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 */
charging_timeout = g_timeout_add_seconds(3, charging_fallback, NULL);
/* 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
{
/* gconf is corrupted or does not return a value, fallback to charging
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.
*/
set_usb_mode(MODE_CHARGING);
}
free((void *)mode_to_set);
}
/** set the usb mode
*
* @param mode The requested USB mode
*
*/
void set_usb_mode(const char *mode)
{
/* set return to 1 to be sure to error out if no matching mode is found either */
int ret=1, net=0;
if(!strcmp(mode, MODE_MASS_STORAGE))
{
check_module_state(MODULE_MASS_STORAGE);
/* now proceed to set the mode correctly */
set_usb_module(MODULE_MASS_STORAGE);
ret = usb_moded_load_module(MODULE_MASS_STORAGE);
if(!ret)
ret = set_mass_storage_mode();
goto end;
}
else if(!strcmp(mode, MODE_CHARGING))
{
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);
goto end;
}
else if(!strcmp(mode, MODE_DEVELOPER))
{
check_module_state(MODULE_DEVELOPER);
set_usb_module(MODULE_NETWORK);
ret = usb_moded_load_module(MODULE_DEVELOPER);
net = usb_network_up();
goto end;
}
#ifdef N900
else if(!strcmp(mode, MODE_OVI_SUITE))
{
check_module_state(MODULE_NETWORK);
set_usb_module(MODULE_NETWORK);
ret = usb_moded_load_module(MODULE_NETWORK_MTP);
if(!ret)
ret = set_ovi_suite_mode();
goto end;
}
#endif /* N900 */
else if(!strcmp(mode, MODE_WINDOWS_NET))
{
check_module_state(MODULE_WINDOWS_NET);
set_usb_module(MODULE_WINDOWS_NET);
ret = usb_moded_load_module(MODULE_WINDOWS_NET);
net = usb_network_up();
goto end;
}
#ifdef DYN_MODE
/* 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))
{
check_module_state(data->mode_module);
set_usb_module(data->mode_module);
ret = usb_moded_load_module(data->mode_module);
ret = set_dynamic_mode(data);
}
}
}
#endif /* DYN_MODE */
end:
/* if ret != 0 then usb_module loading failed */
if(ret)
{
set_usb_module(MODULE_NONE);
mode = MODE_UNDEFINED;
}
free(current_mode.mode);
current_mode.mode = strdup(mode);
usb_moded_send_signal(get_usb_mode());
}
/** check if a given usb_mode exists
*
* @param mode The mode to look for
* @return 0 if mode exits, 1 if it does not exist
*
*/
int valid_mode(const char *mode)
{
if(!strcmp(MODE_MASS_STORAGE, mode) || !strcmp(MODE_OVI_SUITE, mode) || !strcmp(MODE_CHARGING, mode)
|| !strcmp(MODE_WINDOWS_NET, mode) || !strcmp(MODE_DEVELOPER,mode))
return(0);
else
#ifdef DYN_MODE
{
/* check dynamic modes */
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))
return(0);
}
}
}
#endif /* DYN_MODE */
return(1);
}
/** get the usb mode
*
* @return the currently set mode
*
*/
inline const char * get_usb_mode(void)
{
return(current_mode.mode);
}
/** set the loaded module
*
* @param module The module name for the requested mode
*
*/
void set_usb_module(const char *module)
{
free(current_mode.module);
current_mode.module = strdup(module);
}
/** get the supposedly loaded module
*
* @return The name of the loaded module
*
*/
inline const char * get_usb_module(void)
{
return(current_mode.module);
}
/** get if the cable is connected or not
*
* @ return A boolean value for connected (TRUE) or not (FALSE)
*
*/
inline gboolean get_usb_connection_state(void)
{
return current_mode.connected;
}
/** set connection status for some corner cases
*
* @param state The connection status that needs to be set. Connected (TRUE)
*
*/
inline void set_usb_connection_state(gboolean state)
{
current_mode.connected = state;
}
/*================ Static functions ===================================== */
/* set default values for usb_moded */
static void usb_moded_init(void)
{
#ifdef NOKIA_NSU
char readbuf[MAX_READ_BUF];
FILE *proc_fd;
#endif /* NOKIA_NSU */
current_mode.connected = FALSE;
current_mode.mounted = FALSE;
current_mode.mode = strdup(MODE_UNDEFINED);
current_mode.module = strdup(MODULE_NONE);
#ifdef NOKIA_NSU
proc_fd = fopen("/proc/cmdline", "r");
if(proc_fd)
{
fgets(readbuf, MAX_READ_BUF, proc_fd);
readbuf[strlen(readbuf)-1]=0;
if(strstr(readbuf, "nsu"))
special_mode = TRUE;
fclose(proc_fd);
}
#endif /* NOKIA_NSU */
#ifdef APP_SYNC
readlist();
#endif /* APP_SYNC */
#ifdef DYN_MODE
modelist = read_mode_list();
#endif /* DYN_MODE */
#ifdef UDEV
if(check_trigger())
trigger_init();
#endif /* UDEV */
/* TODO: add more start-up clean-up and init here if needed */
}
/* 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;
set_usb_mode(MODE_CHARGING);
/* since this is the fallback, we keep an indication
for the UI, as we are not really in charging mode.
*/
free(current_mode.mode);
current_mode.mode = strdup(MODE_ASK);
charging_timeout = 0;
log_info("Falling back on charging mode.\n");
return(FALSE);
}
/* Display usage information */
static void usage(void)
{
fprintf(stdout,
"Usage: usb_moded [OPTION]...\n"
"USB mode daemon\n"
"\n"
" -d, --daemon run as a daemon\n"
" -s, --force-syslog log to syslog even when not "
"daemonized\n"
" -T, --force-stderr log to stderr even when daemonized\n"
" -D, --debug turn on debug printing\n"
" -h, --help display this help and exit\n"
" -v, --version output version information and exit\n"
" -w, --watch-off do not act on runlevel change\n"
"\n");
}
/* Turn the process in a daemon */
static gboolean daemonize(void)
{
gint retries = 0;
gint i = 0;
gchar str[10];
if (getppid() == 1)
exit(0); /* Already daemonized */
/* Detach from process group */
switch (fork())
{
case -1:
/* Failure */
exit(1);
case 0:
/* Child */
break;
default:
/* Parent -- exit */
exit(0);
}
/* Detach TTY */
setsid();
/* Close all file descriptors and redirect stdio to /dev/null */
if ((i = getdtablesize()) == -1)
i = 256;
while (--i >= 0)
{
if (close(i) == -1)
{
if (retries > 10)
{
log_crit("close() was interrupted more than 10 times. Exiting.\n");
exit(1);
}
if (errno == EINTR)
{
log_err("close() was interrupted; retrying.\n");
errno = 0;
i++;
retries++;
}
else if (errno == EBADF)
{
/* fprintf(stdout, "Failed to close() fd %d; %s. Ignoring.\n",
i + 1, g_strerror(errno));*/
errno = 0;
}
else
{
log_crit("Failed to close() fd %d; %s. Exiting.", i + 1, g_strerror(errno));
exit(1);
}
}
else
retries = 0;
}
if ((i = open("/dev/null", O_RDWR)) == -1)
{
log_crit("Cannot open `/dev/null'; %s. Exiting.", g_strerror(errno));
exit(1);
}
if ((dup(i) == -1))
{
log_crit("Failed to dup() `/dev/null'; %s. Exiting.", g_strerror(errno));
exit(1);
}
if ((dup(i) == -1))
{
log_crit("Failed to dup() `/dev/null'; %s. Exiting.", g_strerror(errno));
exit(1);
}
/* Set umask */
umask(022);
/* Set working directory */
if ((chdir("/tmp") == -1))
{
log_crit("Failed to chdir() to `/tmp'; %s. Exiting.", g_strerror(errno));
exit(1);
}
/* Single instance */
if ((i = open(USB_MODED_LOCKFILE, O_RDWR | O_CREAT, 0640)) == -1)
{
log_crit("Cannot open lockfile; %s. Exiting.", g_strerror(errno));
exit(1);
}
if (lockf(i, F_TLOCK, 0) == -1)
{
log_crit("Already running. Exiting.");
exit(1);
}
sprintf(str, "%d\n", getpid());
write(i, str, strlen(str));
close(i);
/* Ignore TTY signals */
signal(SIGTSTP, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
/* Ignore child terminate signal */
signal(SIGCHLD, SIG_IGN);
log_info("Daemon running.");
return 0;
}
int main(int argc, char* argv[])
{
int result = EXIT_FAILURE;
int opt = 0, opt_idx = 0;
GMainLoop *mainloop = NULL;
struct option const options[] = {
{ "daemon", no_argument, 0, 'd' },
{ "force-syslog", no_argument, 0, 's' },
{ "force-stderr", no_argument, 0, 'T' },
{ "debug", no_argument, 0, 'D' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ 0, 0, 0, 0 }
};
log_name = basename(*argv);
/* Parse the command-line options */
while ((opt = getopt_long(argc, argv, "dsTDhvw", options, &opt_idx)) != -1)
{
switch (opt)
{
case 'd':
run_as_daemon = TRUE;
break;
case 's':
log_type = LOG_TO_SYSLOG;
break;
case 'T':
log_type = LOG_TO_STDERR;
break;
case 'D':
log_level = LOG_DEBUG;
break;
case 'h':
usage();
exit(0);
case 'v':
printf("USB mode daemon version: %s\n", VERSION);
exit(0);
case 'w':
runlevel_ignore = TRUE;
printf("Ignore runlevel changes.\n");
break;
default:
usage();
exit(0);
}
}
if(run_as_daemon == TRUE)
{
log_notice("going to daemon mode\n");
daemonize();
}
/* silence system() calls */
if(log_type != LOG_TO_STDERR || log_level != LOG_DEBUG )
{
freopen("/dev/null", "a", stdout);
freopen("/dev/null", "a", stderr);
}
g_type_init();
g_thread_init(NULL);
mainloop = g_main_loop_new(NULL, FALSE);
log_debug("usb_moded starting\n");
/* init daemon into a clean state first, then dbus and hw_abstraction last */
usb_moded_init();
if( !usb_moded_dbus_init() )
{
log_crit("dbus ipc init failed\n");
goto EXIT;
}
if( !hwal_init() )
{
log_crit("hwal init failed\n");
goto EXIT;
}
#if defined MEEGOLOCK
start_devicelock_listener();
if(!runlevel_ignore)
usb_moded_dsme_listener();
#endif /* MEEGOLOCK */
/* init succesful, run main loop */
result = EXIT_SUCCESS;
g_main_loop_run(mainloop);
EXIT:
/* exiting and clean-up when mainloop ended */
hwal_cleanup();
usb_moded_dbus_cleanup();
/* If the mainloop is initialised, unreference it */
if (mainloop != NULL)
g_main_loop_unref(mainloop);
return result;
}