Skip to content

Latest commit

 

History

History
664 lines (577 loc) · 16.3 KB

usb_moded.c

File metadata and controls

664 lines (577 loc) · 16.3 KB
 
Mar 22, 2011
Mar 22, 2011
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
/**
@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-gconf-private.h"
#include "usb_moded-modules.h"
#include "usb_moded-log.h"
#include "usb_moded-devicelock.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-modules.h"
#include "usb_moded-appsync.h"
May 16, 2011
May 16, 2011
45
46
#include "usb_moded-trigger.h"
#include "usb_moded-config.h"
Mar 22, 2011
Mar 22, 2011
47
48
49
50
51
52
53
54
55
56
/* 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;
May 17, 2011
May 17, 2011
57
guint debounce = 0;
Mar 22, 2011
Mar 22, 2011
58
59
60
61
#ifdef NOKIA
gboolean special_mode = FALSE;
guint timeout_source = 0;
#endif /* NOKIA */
May 10, 2011
May 10, 2011
62
63
64
#ifdef APP_SYNC
static GList *modelist;
#endif /* APP_SYNC */
Mar 22, 2011
Mar 22, 2011
65
66
/* static helper functions */
May 17, 2011
May 17, 2011
67
static gboolean set_disconnected(gpointer data);
Mar 22, 2011
Mar 22, 2011
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
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;
May 17, 2011
May 17, 2011
103
104
105
106
107
108
if(debounce)
{
g_source_remove(debounce);
debounce = 0;
}
Mar 22, 2011
Mar 22, 2011
109
110
111
112
113
114
115
116
117
118
119
#ifdef NOKIA
if(timeout_source)
{
g_source_remove(timeout_source);
timeout_source = 0;
}
#endif /* NOKIA */
current_mode.connected = TRUE;
set_usb_connected_state();
}
else
May 17, 2011
May 17, 2011
120
121
122
123
124
125
126
127
if(!debounce)
debounce = g_timeout_add_seconds(2, set_disconnected, NULL);
}
static gboolean set_disconnected(gpointer data)
{
Mar 22, 2011
Mar 22, 2011
128
129
130
131
132
133
134
current_mode.connected = FALSE;
/* signal usb disconnected */
log_debug("usb disconnected\n");
/* clean-up state */
usb_moded_mode_cleanup(get_usb_module());
usb_moded_send_signal(USB_DISCONNECTED);
#ifdef NOKIA
Apr 8, 2011
Apr 8, 2011
135
timeout_source = g_timeout_add_seconds(5, usb_module_timeout_cleanup, NULL);
Mar 22, 2011
Mar 22, 2011
136
137
138
139
140
141
#else
/* unload modules and general cleanup */
usb_moded_module_cleanup(get_usb_module());
#endif /* NOKIA */
set_usb_mode(MODE_UNDEFINED);
May 17, 2011
May 17, 2011
142
return FALSE;
Mar 22, 2011
Mar 22, 2011
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
}
/** set the chosen usb state
*
*/
void set_usb_connected_state(void)
{
const char *mode_to_set;
#ifdef NOKIA
int export = 0, act_dead = 0;
#endif /* NOKIA */
/* signal usb connected */
log_debug("usb connected\n");
usb_moded_send_signal(USB_CONNECTED);
mode_to_set = get_mode_setting();
#ifdef NOKIA
/* check if we are allowed to export system contents 0 is unlocked */
export = usb_moded_get_export_permission();
/* 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)
#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 */
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);
}
}
/** set the usb mode
*
* @param mode The requested USB mode
*
*/
void set_usb_mode(const char *mode)
{
int ret=0, 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;
}
May 10, 2011
May 10, 2011
216
else if(!strcmp(mode, MODE_CHARGING))
Mar 22, 2011
Mar 22, 2011
217
218
219
220
221
222
223
224
225
{
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;
}
Apr 15, 2011
Apr 15, 2011
226
#ifdef N900
May 10, 2011
May 10, 2011
227
else if(!strcmp(mode, MODE_OVI_SUITE))
Mar 22, 2011
Mar 22, 2011
228
229
230
231
232
{
check_module_state(MODULE_NETWORK);
set_usb_module(MODULE_NETWORK);
ret = usb_moded_load_module(MODULE_NETWORK_MTP);
if(!ret)
Apr 21, 2011
Apr 21, 2011
233
ret = set_ovi_suite_mode();
Mar 22, 2011
Mar 22, 2011
234
}
Apr 15, 2011
Apr 15, 2011
235
#endif /* N900 */
Mar 22, 2011
Mar 22, 2011
236
May 10, 2011
May 10, 2011
237
else if(!strcmp(mode, MODE_WINDOWS_NET))
Mar 22, 2011
Mar 22, 2011
238
239
240
241
242
243
244
{
check_module_state(MODULE_WINDOWS_NET);
set_usb_module(MODULE_WINDOWS_NET);
ret = usb_moded_load_module(MODULE_WINDOWS_NET);
net = system("ifdown usb0 ; ifup usb0");
}
May 10, 2011
May 10, 2011
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#ifdef APP_SYNC
/* 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 /* APP_SYNC */
Mar 22, 2011
Mar 22, 2011
265
266
267
268
269
270
271
272
273
274
275
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());
}
May 10, 2011
May 10, 2011
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
/** 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))
return(0);
else
#ifdef APP_SYNC
{
/* 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 /* APP_SYNC */
return(1);
}
Mar 22, 2011
Mar 22, 2011
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
/** get the usb mode
*
* @return the currently set mode
*
*/
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
*
*/
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)
*
*/
gboolean get_usb_connection_state(void)
{
return current_mode.connected;
}
May 17, 2011
May 17, 2011
349
350
351
352
353
354
355
356
357
/** set connection status for some corner cases
*
* @param: connection status that needs to be set. Connected (TRUE)
*
*/
void set_usb_connection_state(gboolean state)
{
current_mode.connected = state;
}
Mar 22, 2011
Mar 22, 2011
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
/*================ Static functions ===================================== */
/* set default values for usb_moded */
static void usb_moded_init(void)
{
#ifdef NOKIA
char readbuf[MAX_READ_BUF];
FILE *proc_fd;
#endif /* NOKIA */
current_mode.connected = FALSE;
current_mode.mounted = FALSE;
current_mode.mode = strdup(MODE_UNDEFINED);
current_mode.module = g_strdup(MODULE_NONE);
#ifdef NOKIA
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 */
#ifdef APP_SYNC
Apr 21, 2011
Apr 21, 2011
388
readlist();
May 10, 2011
May 10, 2011
389
modelist = read_mode_list();
Mar 22, 2011
Mar 22, 2011
390
#endif /* APP_SYNC */
May 16, 2011
May 16, 2011
391
392
393
394
#ifdef UDEV
if(check_trigger())
trigger_init();
#endif /* UDEV */
Mar 22, 2011
Mar 22, 2011
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
/* 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);
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();
Apr 6, 2011
Apr 6, 2011
629
g_thread_init(NULL);
Mar 22, 2011
Mar 22, 2011
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
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;
}
#ifdef NOKIA
start_devicelock_listener();
if(!runlevel_ignore)
usb_moded_dsme_listener();
#endif /* NOKIA */
/* 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;
}