/
usb_moded-worker.c
1020 lines (787 loc) · 24.9 KB
/
usb_moded-worker.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
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
738
739
740
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
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
/**
* @file usb_moded-worker.c
*
* Copyright (C) 2013-2019 Jolla. All rights reserved.
*
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@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
*/
#include "usb_moded-worker.h"
#include "usb_moded-android.h"
#include "usb_moded-configfs.h"
#include "usb_moded-control.h"
#include "usb_moded-dyn-config.h"
#include "usb_moded-log.h"
#include "usb_moded-modes.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-modules.h"
#include <sys/eventfd.h>
#include <pthread.h> // NOTRIM
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* WORKER
* ------------------------------------------------------------------------- */
static bool worker_thread_p (void);
bool worker_bailing_out (void);
static bool worker_mode_is_mtp_mode (const char *mode);
static bool worker_is_mtpd_running (void);
static bool worker_mtpd_running_p (void *aptr);
static bool worker_mtpd_stopped_p (void *aptr);
static bool worker_stop_mtpd (void);
static bool worker_start_mtpd (void);
static bool worker_switch_to_charging (void);
const char *worker_get_kernel_module (void);
bool worker_set_kernel_module (const char *module);
void worker_clear_kernel_module (void);
const modedata_t *worker_get_usb_mode_data (void);
modedata_t *worker_dup_usb_mode_data (void);
void worker_set_usb_mode_data (const modedata_t *data);
static const char *worker_get_activated_mode_locked(void);
static bool worker_set_activated_mode_locked(const char *mode);
static const char *worker_get_requested_mode_locked(void);
static bool worker_set_requested_mode_locked(const char *mode);
void worker_request_hardware_mode (const char *mode);
void worker_clear_hardware_mode (void);
static void worker_execute (void);
static void worker_switch_to_mode (const char *mode);
static guint worker_add_iowatch (int fd, bool close_on_unref, GIOCondition cnd, GIOFunc io_cb, gpointer aptr);
static void *worker_thread_cb (void *aptr);
static gboolean worker_notify_cb (GIOChannel *chn, GIOCondition cnd, gpointer data);
static bool worker_start_thread (void);
static void worker_stop_thread (void);
static void worker_delete_eventfd (void);
static bool worker_create_eventfd (void);
bool worker_init (void);
void worker_quit (void);
void worker_wakeup (void);
static void worker_notify (void);
/* ========================================================================= *
* Data
* ========================================================================= */
static pthread_t worker_thread_id = 0;
static pthread_mutex_t worker_mutex = PTHREAD_MUTEX_INITIALIZER;
/** Flag for: Main thread has changed target mode worker should apply
*
* Worker should bailout from synchronous activities related to
* ongoing activation of a usb mode.
*/
static volatile bool worker_bailout_requested = false;
/** Flag for: Worker thread is cleaning up after abandoning mode switch
*
* Asynchronous activities on mode cleanup should be executed without
* bailing out.
*/
static volatile bool worker_bailout_handled = false;
#define WORKER_LOCKED_ENTER do {\
if( pthread_mutex_lock(&worker_mutex) != 0 ) { \
log_crit("WORKER LOCK FAILED");\
_exit(EXIT_FAILURE);\
}\
}while(0)
#define WORKER_LOCKED_LEAVE do {\
if( pthread_mutex_unlock(&worker_mutex) != 0 ) { \
log_crit("WORKER UNLOCK FAILED");\
_exit(EXIT_FAILURE);\
}\
}while(0)
/* ========================================================================= *
* Functions
* ========================================================================= */
static bool
worker_thread_p(void)
{
LOG_REGISTER_CONTEXT;
return worker_thread_id && worker_thread_id == pthread_self();
}
bool
worker_bailing_out(void)
{
LOG_REGISTER_CONTEXT;
// ref: see common_msleep_()
return (worker_thread_p() &&
worker_bailout_requested &&
!worker_bailout_handled);
}
/* ------------------------------------------------------------------------- *
* MTP_DAEMON
* ------------------------------------------------------------------------- */
/** Maximum time to wait for mtpd to start [ms]
*
* This needs to include time to start systemd unit
* plus however long it might take for mtpd to scan
* all files exposed over mtp. On a slow device with
* lots of files it can easily take over 30 seconds,
* especially during the 1st mtp connect after reboot.
*
* Use two minutes as some kind of worst case estimate.
*/
static unsigned worker_mtp_start_delay = 120 * 1000;
/** Maximum time to wait for mtpd to stop [ms]
*
* This is just regular service stop. Expected to
* take max couple of seconds, but use someting
* in the ballbark of systemd default i.e. 15 seconds
*/
static unsigned worker_mtp_stop_delay = 15 * 1000;
/** Flag for: We have started mtp daemon
*
* If we have issued systemd unit start, we should also
* issue systemd unit stop even if probing for mtpd
* presense gives negative result.
*/
static bool worker_mtp_service_started = false;
static bool worker_mode_is_mtp_mode(const char *mode)
{
LOG_REGISTER_CONTEXT;
return mode && !strcmp(mode, "mtp_mode");
}
static bool worker_is_mtpd_running(void)
{
LOG_REGISTER_CONTEXT;
/* ep0 becomes available when /dev/mtp is mounted.
*
* ep1, ep2, ep3 exist while mtp daemon is running,
* has ep0 opened and has written config data to it.
*/
static const char * const lut[] = {
"/dev/mtp/ep0",
"/dev/mtp/ep1",
"/dev/mtp/ep2",
"/dev/mtp/ep3",
0
};
bool ack = true;
for( size_t i = 0; lut[i]; ++i ) {
if( access(lut[i], F_OK) == -1 ) {
ack = false;
break;
}
}
return ack;
}
static bool
worker_mtpd_running_p(void *aptr)
{
LOG_REGISTER_CONTEXT;
(void)aptr;
return worker_is_mtpd_running();
}
static bool
worker_mtpd_stopped_p(void *aptr)
{
LOG_REGISTER_CONTEXT;
(void)aptr;
return !worker_is_mtpd_running();
}
static bool
worker_stop_mtpd(void)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( !worker_mtp_service_started && worker_mtpd_stopped_p(0) ) {
log_debug("mtp daemon is not running");
goto SUCCESS;
}
int rc = common_system("systemctl-user stop buteo-mtp.service");
if( rc != 0 ) {
log_warning("failed to stop mtp daemon; exit code = %d", rc);
goto FAILURE;
}
/* Have succesfully stopped mtp service */
worker_mtp_service_started = false;
if( common_wait(worker_mtp_stop_delay, worker_mtpd_stopped_p, 0) != WAIT_READY ) {
log_warning("failed to stop mtp daemon; giving up");
goto FAILURE;
}
log_debug("mtp daemon has stopped");
SUCCESS:
ack = true;
FAILURE:
return ack;
}
static bool
worker_start_mtpd(void)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( worker_mtpd_running_p(0) ) {
log_debug("mtp daemon is running");
goto SUCCESS;
}
/* Have attempted to start mtp service */
worker_mtp_service_started = true;
int rc = common_system("systemctl-user start buteo-mtp.service");
if( rc != 0 ) {
log_warning("failed to start mtp daemon; exit code = %d", rc);
goto FAILURE;
}
if( common_wait(worker_mtp_start_delay, worker_mtpd_running_p, 0) != WAIT_READY ) {
log_warning("failed to start mtp daemon; giving up");
goto FAILURE;
}
log_debug("mtp daemon has started");
SUCCESS:
ack = true;
FAILURE:
return ack;
}
static bool worker_switch_to_charging(void)
{
LOG_REGISTER_CONTEXT;
bool ack = true;
if( android_set_charging_mode() )
goto SUCCESS;
if( configfs_set_charging_mode() )
goto SUCCESS;
if( modules_in_use() ) {
if( worker_set_kernel_module(MODULE_MASS_STORAGE) )
goto SUCCESS;
worker_set_kernel_module(MODULE_NONE);
}
log_err("switch to charging mode failed");
ack = false;
SUCCESS:
return ack;
}
/* ------------------------------------------------------------------------- *
* KERNEL_MODULE
* ------------------------------------------------------------------------- */
/** The module name for the specific mode */
static char *worker_kernel_module = NULL;
/** get the supposedly loaded module
*
* @return The name of the loaded module
*
*/
const char * worker_get_kernel_module(void)
{
LOG_REGISTER_CONTEXT;
return worker_kernel_module ?: MODULE_NONE;
}
/** set the loaded module
*
* @param module The module name for the requested mode
*
*/
bool worker_set_kernel_module(const char *module)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( !module )
module = MODULE_NONE;
const char *current = worker_get_kernel_module();
log_debug("current module: %s -> %s", current, module);
if( !g_strcmp0(current, module) )
goto SUCCESS;
if( modules_unload_module(current) != 0 )
goto EXIT;
free(worker_kernel_module), worker_kernel_module = 0;
if( modules_load_module(module) != 0 )
goto EXIT;
if( g_strcmp0(module, MODULE_NONE) )
worker_kernel_module = strdup(module);
SUCCESS:
ack = true;
EXIT:
return ack;
}
void worker_clear_kernel_module(void)
{
LOG_REGISTER_CONTEXT;
free(worker_kernel_module), worker_kernel_module = 0;
}
/* ------------------------------------------------------------------------- *
* MODE_DATA
* ------------------------------------------------------------------------- */
/** Contains the mode data */
static modedata_t *worker_mode_data = NULL;
/** get the usb mode data
*
* Note: This function should be called only from the worker thread.
*
* @return a pointer to the usb mode data
*/
const modedata_t *worker_get_usb_mode_data(void)
{
LOG_REGISTER_CONTEXT;
return worker_mode_data;
}
/** get clone of the usb mode data
*
* Caller must release the returned object via #modedata_free().
*
* @return a pointer to the usb mode data
*/
modedata_t *worker_dup_usb_mode_data(void)
{
LOG_REGISTER_CONTEXT;
WORKER_LOCKED_ENTER;
modedata_t *modedata = modedata_copy(worker_mode_data);
WORKER_LOCKED_LEAVE;
return modedata;;
}
/** set the modedata_t data
*
* Note: This function should be called only from the worker thread,
*
* @param data mode_list_element pointer
*/
void worker_set_usb_mode_data(const modedata_t *data)
{
LOG_REGISTER_CONTEXT;
WORKER_LOCKED_ENTER;
modedata_free(worker_mode_data),
worker_mode_data = modedata_copy(data);
WORKER_LOCKED_LEAVE;
}
/* ------------------------------------------------------------------------- *
* HARDWARE_MODE
* ------------------------------------------------------------------------- */
/* The hardware mode name
*
* How the usb hardware has been configured.
*
* For example internal_mode=MODE_ASK gets
* mapped to hardware_mode=MODE_CHARGING */
static gchar *worker_requested_mode = NULL;
static gchar *worker_activated_mode = NULL;
static const char *
worker_get_activated_mode_locked(void)
{
LOG_REGISTER_CONTEXT;
return worker_activated_mode ?: MODE_UNDEFINED;
}
static bool
worker_set_activated_mode_locked(const char *mode)
{
LOG_REGISTER_CONTEXT;
bool changed = false;
const char *prev = worker_get_activated_mode_locked();
if( !g_strcmp0(prev, mode) )
goto EXIT;
log_debug("activated_mode: %s -> %s", prev, mode);
g_free(worker_activated_mode),
worker_activated_mode = g_strdup(mode);
changed = true;
EXIT:
return changed;
}
static const char *
worker_get_requested_mode_locked(void)
{
LOG_REGISTER_CONTEXT;
return worker_requested_mode ?: MODE_UNDEFINED;
}
static bool
worker_set_requested_mode_locked(const char *mode)
{
LOG_REGISTER_CONTEXT;
bool changed = false;
const char *prev = worker_get_requested_mode_locked();
if( !g_strcmp0(prev, mode) )
goto EXIT;
log_debug("requested_mode: %s -> %s", prev, mode);
g_free(worker_requested_mode),
worker_requested_mode = g_strdup(mode);
changed = true;
EXIT:
return changed;
}
void worker_request_hardware_mode(const char *mode)
{
LOG_REGISTER_CONTEXT;
WORKER_LOCKED_ENTER;
if( !worker_set_requested_mode_locked(mode) )
goto EXIT;
worker_wakeup();
EXIT:
WORKER_LOCKED_LEAVE;
return;
}
void worker_clear_hardware_mode(void)
{
LOG_REGISTER_CONTEXT;
WORKER_LOCKED_ENTER;
g_free(worker_requested_mode), worker_requested_mode = 0;
WORKER_LOCKED_LEAVE;
}
static void
worker_execute(void)
{
LOG_REGISTER_CONTEXT;
WORKER_LOCKED_ENTER;
const char *activated = worker_get_activated_mode_locked();
const char *requested = worker_get_requested_mode_locked();
const char *activate = common_map_mode_to_hardware(requested);
log_debug("activated = %s", activated);
log_debug("requested = %s", requested);
log_debug("activate = %s", activate);
bool changed = g_strcmp0(activated, activate) != 0;
gchar *mode = g_strdup(activate);
WORKER_LOCKED_LEAVE;
if( changed )
worker_switch_to_mode(mode);
else
worker_notify();
g_free(mode);
return;
}
/* ------------------------------------------------------------------------- *
* MODE_SWITCH
* ------------------------------------------------------------------------- */
static void
worker_switch_to_mode(const char *mode)
{
LOG_REGISTER_CONTEXT;
const char *override = 0;
modedata_t *data = 0;
/* set return to 1 to be sure to error out if no matching mode is found either */
log_debug("Cleaning up previous mode");
/* Either mtp daemon is not needed, or it must be *started* in
* correct phase of gadget configuration when entering mtp mode.
*/
worker_stop_mtpd();
if( worker_get_usb_mode_data() ) {
modesetting_leave_dynamic_mode();
worker_set_usb_mode_data(NULL);
}
log_debug("Setting %s\n", mode);
/* Mode mapping should mean we only see MODE_CHARGING here, but just
* in case redirect fixed charging related things to charging ... */
if( !strcmp(mode, MODE_CHARGING) ||
!strcmp(mode, MODE_CHARGING_FALLBACK) ||
!strcmp(mode, MODE_CHARGER) ||
!strcmp(mode, MODE_UNDEFINED) ||
!strcmp(mode, MODE_ASK)) {
goto CHARGE;
}
if( !usbmoded_can_export() ) {
log_warning("Policy does not allow mode: %s", mode);
goto FAILED;
}
if( (data = usbmoded_dup_modedata(mode)) ) {
log_debug("Matching mode %s found.\n", mode);
/* set data before calling any of the dynamic mode functions
* as they will use the worker_get_usb_mode_data function */
worker_set_usb_mode_data(data);
/* When dealing with configfs, we can't enable UDC without
* already having mtpd running */
if( worker_mode_is_mtp_mode(mode) && configfs_in_use() ) {
if( !worker_start_mtpd() )
goto FAILED;
}
if( !worker_set_kernel_module(data->mode_module) )
goto FAILED;
if( !modesetting_enter_dynamic_mode() )
goto FAILED;
/* When dealing with android usb, it must be enabled before
* we can start mtpd. Assumption is that the same applies
* when using kernel modules. */
if( worker_mode_is_mtp_mode(mode) && !configfs_in_use() ) {
if( !worker_start_mtpd() )
goto FAILED;
}
goto SUCCESS;
}
log_warning("Matching mode %s was not found.", mode);
FAILED:
worker_bailout_handled = true;
/* Undo any changes we might have might have already done */
if( worker_get_usb_mode_data() ) {
log_debug("Cleaning up failed mode switch");
worker_stop_mtpd();
modesetting_leave_dynamic_mode();
worker_set_usb_mode_data(NULL);
}
/* From usb configuration point of view MODE_UNDEFINED and
* MODE_CHARGING are the same, but for the purposes of exposing
* a sane state over D-Bus we need to differentiate between
* "failure to set mode" and "aborting mode setting due to cable
* disconnect" by inspecting whether target mode has been
* switched to undefined.
*/
WORKER_LOCKED_ENTER;
const char *requested = worker_get_requested_mode_locked();
if( !g_strcmp0(requested, MODE_UNDEFINED) )
override = MODE_UNDEFINED;
else
override = MODE_CHARGING;
WORKER_LOCKED_LEAVE;
log_warning("mode setting failed, try %s", override);
CHARGE:
if( worker_switch_to_charging() )
goto SUCCESS;
log_crit("failed to activate charging, all bets are off");
/* FIXME: double check this error path */
/* If we get here then usb_module loading failed,
* no mode matched, and charging setup failed too.
*/
override = MODE_UNDEFINED;
log_warning("mode setting failed, fallback to %s", override);
worker_set_kernel_module(MODULE_NONE);
SUCCESS:
WORKER_LOCKED_ENTER;
if( override ) {
worker_set_requested_mode_locked(override);
override = common_map_mode_to_hardware(override);
worker_set_activated_mode_locked(override);
}
else {
worker_set_activated_mode_locked(mode);
}
WORKER_LOCKED_LEAVE;
worker_notify();
modedata_free(data);
return;
}
/* ------------------------------------------------------------------------- *
* WORKER_THREAD
* ------------------------------------------------------------------------- */
/** eventfd descriptor for waking up worker thread after adding new jobs */
static int worker_req_evfd = -1;
/** eventfd descriptor for waking up main thread after executing jobs */
static int worker_rsp_evfd = -1;
/** I/O watch identifier for worker_rsp_evfd */
static guint worker_rsp_wid = 0;
static guint
worker_add_iowatch(int fd, bool close_on_unref,
GIOCondition cnd, GIOFunc io_cb, gpointer aptr)
{
LOG_REGISTER_CONTEXT;
guint wid = 0;
GIOChannel *chn = 0;
if( !(chn = g_io_channel_unix_new(fd)) )
goto cleanup;
g_io_channel_set_close_on_unref(chn, close_on_unref);
cnd |= G_IO_ERR | G_IO_HUP | G_IO_NVAL;
if( !(wid = g_io_add_watch(chn, cnd, io_cb, aptr)) )
goto cleanup;
cleanup:
if( chn != 0 ) g_io_channel_unref(chn);
return wid;
}
static void *worker_thread_cb(void *aptr)
{
LOG_REGISTER_CONTEXT;
(void)aptr;
/* Async cancellation, but disabled */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
/* Leave INT/TERM signal processing up to the main thread */
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGINT);
sigaddset(&ss, SIGTERM);
pthread_sigmask(SIG_BLOCK, &ss, 0);
/* Loop until explicitly canceled */
for( ;; ) {
/* Async cancellation point at wait() */
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
uint64_t cnt = 0;
int rc = read(worker_req_evfd, &cnt, sizeof cnt);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
if( rc == -1 ) {
if( errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK )
continue;
log_err("read: %m");
goto EXIT;
}
if( rc != sizeof cnt )
continue;
if( cnt > 0 ) {
worker_bailout_requested = false;
worker_bailout_handled = false;
worker_execute();
}
}
EXIT:
return 0;
}
static gboolean
worker_notify_cb(GIOChannel *chn, GIOCondition cnd, gpointer data)
{
LOG_REGISTER_CONTEXT;
(void)data;
gboolean keep_going = FALSE;
if( !worker_rsp_wid )
goto cleanup_nak;
int fd = g_io_channel_unix_get_fd(chn);
if( fd < 0 )
goto cleanup_nak;
if( cnd & ~G_IO_IN )
goto cleanup_nak;
if( !(cnd & G_IO_IN) )
goto cleanup_ack;
uint64_t cnt = 0;
int rc = read(fd, &cnt, sizeof cnt);
if( rc == 0 ) {
log_err("unexpected eof");
goto cleanup_nak;
}
if( rc == -1 ) {
if( errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK )
goto cleanup_ack;
log_err("read error: %m");
goto cleanup_nak;
}
if( rc != sizeof cnt )
goto cleanup_nak;
{
WORKER_LOCKED_ENTER;
const char *mode = worker_get_requested_mode_locked();
gchar *work = g_strdup(mode);
WORKER_LOCKED_LEAVE;
control_mode_switched(work);
g_free(work);
}
cleanup_ack:
keep_going = TRUE;
cleanup_nak:
if( !keep_going ) {
worker_rsp_wid = 0;
log_crit("worker notifications disabled");
}
return keep_going;
}
static bool
worker_start_thread(void)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
int err = pthread_create(&worker_thread_id, 0, worker_thread_cb, 0);
if( err ) {
worker_thread_id = 0;
log_err("failed to start worker thread");
}
else {
ack = true;
log_debug("worker thread started");
}
return ack;
}
static void
worker_stop_thread(void)
{
LOG_REGISTER_CONTEXT;
if( !worker_thread_id )
goto EXIT;
log_debug("stopping worker thread");
int err = pthread_cancel(worker_thread_id);
if( err ) {
log_err("failed to cancel worker thread");
}
else {
log_debug("waiting for worker thread to exit ...");
void *ret = 0;
struct timespec tmo = { 0, 0};
clock_gettime(CLOCK_REALTIME, &tmo);
tmo.tv_sec += 3;
err = pthread_timedjoin_np(worker_thread_id, &ret, &tmo);
if( err ) {
log_err("worker thread did not exit");
}
else {
log_debug("worker thread terminated");
worker_thread_id = 0;
}
}
if( worker_thread_id ) {
/* Orderly exit is not safe, just die */
_exit(EXIT_FAILURE);
}
EXIT:
return;
}
static void
worker_delete_eventfd(void)
{
LOG_REGISTER_CONTEXT;
if( worker_req_evfd != -1 )
close(worker_req_evfd), worker_req_evfd = -1;
if( worker_rsp_wid )
g_source_remove(worker_rsp_wid), worker_rsp_wid = 0;
if( worker_rsp_evfd != -1 )
close(worker_rsp_evfd), worker_req_evfd = -1;
}
static bool
worker_create_eventfd(void)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
/* Setup notify pipeline */
if( (worker_rsp_evfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) == -1 )
goto EXIT;
worker_rsp_wid = worker_add_iowatch(worker_rsp_evfd, false, G_IO_IN,
worker_notify_cb, 0);
if( !worker_rsp_wid )
goto EXIT;
/* Setup request pipeline */
if( (worker_req_evfd = eventfd(0, EFD_CLOEXEC)) == -1 )
goto EXIT;
ack = true;
EXIT:
return ack;
}
bool
worker_init(void)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( !worker_create_eventfd() )
goto EXIT;
if( !worker_start_thread() )
goto EXIT;
ack = true;
EXIT:
if( !ack )
worker_quit();
return ack;
}
void
worker_quit(void)
{
LOG_REGISTER_CONTEXT;
worker_stop_thread();
worker_delete_eventfd();
/* Worker thread is stopped and resources can be released. */
worker_set_usb_mode_data(0);
}
void
worker_wakeup(void)
{