aboutsummaryrefslogtreecommitdiffstats
path: root/mountd/AutoMount.c
blob: bfe8ad105cf6e8e0499bb2be8a62025d6e0b787d (plain)
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
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
** mountd automount support
*/

#include "mountd.h"

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <pwd.h>
#include <stdlib.h>
#include <poll.h>

#include <sys/mount.h>
#include <sys/stat.h>
#include <linux/loop.h>
#include <sys/inotify.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/netlink.h>

#define DEVPATH    "/dev/block/"
#define DEVPATHLENGTH 11    // strlen(DEVPATH)

// FIXME - only one loop mount is supported at a time
#define LOOP_DEVICE "/dev/block/loop0"

// timeout value for poll() when retries are pending
#define POLL_TIMEOUT    1000

#define MAX_MOUNT_RETRIES   3
#define MAX_UNMOUNT_RETRIES   5

typedef enum {
    // device is unmounted
    kUnmounted,
    
    // attempting to mount device
    kMounting,
    
    // device is unmounted
    kMounted,
    
    // attempting to unmount device
    // so the media can be removed
    kUnmountingForEject,
    
    // attempting to mount device
    // so it can be shared via USB mass storage
    kUnmountingForUms,
} MountState;

typedef struct MountPoint {
    // block device to mount
    const char* device;
    
    // mount point for device
    const char* mountPoint;
    
    // true if device can be shared via
    // USB mass storage
    boolean enableUms;
    
    // true if the device is being shared via USB mass storage
    boolean umsActive;
    
    // logical unit number (for UMS)
    int lun;
    
    // current state of the mount point
    MountState state;
    
    // number of mount or unmount retries so far, 
    // when attempting to mount or unmount the device
    int retryCount; 
 
    // next in sMountPointList linked list
    struct MountPoint* next;   
} MountPoint;

// list of our mount points (does not change after initialization)
static MountPoint* sMountPointList = NULL;
static int sNextLun = 0;
boolean gMassStorageEnabled = false;
boolean gMassStorageConnected = false;

static pthread_t sAutoMountThread = 0;

// number of mount points that have timeouts pending
static int sRetriesPending = 0;

// for synchronization between sAutoMountThread and the server thread
static pthread_mutex_t sMutex = PTHREAD_MUTEX_INITIALIZER;

// requests the USB mass_storage driver to begin or end sharing a block device
// via USB mass storage.
static void SetBackingStore(MountPoint* mp, boolean enable) 
{
    char path[PATH_MAX];
    int fd;

    LOG_MOUNT("SetBackingStore enable: %s\n", (enable ? "true" : "false"));
    snprintf(path, sizeof(path), "/sys/devices/platform/usb_mass_storage/lun%d/file", mp->lun);
    fd = open(path, O_WRONLY);
    if (fd < 0)
    {
        LOG_ERROR("could not open %s\n", path);
    }
    else
    {
        if (enable)
        {
            write(fd, mp->device, strlen(mp->device));
            mp->umsActive = true;
        }
        else
        {
            char ch = 0;
            write(fd, &ch, 1);
            mp->umsActive = false;
        }
        close(fd);
    }
}

static boolean ReadMassStorageState()
{
    FILE* file = fopen("/sys/class/switch/usb_mass_storage/state", "r");
    if (file)
    {
        char    buffer[20];
        fgets(buffer, sizeof(buffer), file);
        fclose(file);
        return (strncmp(buffer, "online", strlen("online")) == 0);
    }
    else
    {
        LOG_ERROR("could not read initial mass storage state\n");
        return false;
    }
}

static boolean IsLoopMounted(const char* path)
{
    FILE* f;
    int count;
    char device[256];
    char mount_path[256];
    char rest[256];
    int result = 0;
    int path_length = strlen(path);
       
    f = fopen("/proc/mounts", "r");
    if (!f) {
        LOG_ERROR("could not open /proc/mounts\n");
        return -1;
    }

    do {
        count = fscanf(f, "%255s %255s %255s\n", device, mount_path, rest);
        if (count == 3) {
            if (strcmp(LOOP_DEVICE, device) == 0 && strcmp(path, mount_path) == 0)
            {
                result = 1;
                break;
            }
        }
    } while (count == 3);

    fclose(f);
    LOG_MOUNT("IsLoopMounted: %s returning %d\n", path, result);
    return result;
}

static int DoMountDevice(const char* device, const char* mountPoint)
{
    LOG_MOUNT("mounting %s at %s\n", device, mountPoint);

#if CREATE_MOUNT_POINTS
    // make sure mount point exists
    mkdir(mountPoint, 0000);
#endif

    int flags = 0;
    
    if (device && strncmp(device, "/dev/", 5))
    {
        // mount with the loop driver if device does not start with "/dev/"
        int file_fd, device_fd;
        
        // FIXME - only one loop mount supported at a time
        file_fd = open(device, O_RDWR);
        if (file_fd < -1) {
            LOG_ERROR("open backing file %s failed\n", device);
            return 1;
        }
        device_fd = open(LOOP_DEVICE, O_RDWR);
        if (device_fd < -1) {
            LOG_ERROR("open %s failed", LOOP_DEVICE);
            close(file_fd);
            return 1;
        }
        if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0)
        {
            LOG_ERROR("ioctl LOOP_SET_FD failed\n");
            close(file_fd);
            close(device_fd);
            return 1;
        }

        close(file_fd);
        close(device_fd);
        device = "/dev/block/loop0";
    }

    int result = access(device, R_OK);
    if (result != 0)
        return result;

    // Extra safety measures:
    flags |= MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_DIRSYNC;
    // Also, set fmask = 711 so that files cannot be marked executable,
    // and cannot by opened by uid 1000 (system). Similar, dmask = 700
    // so that directories cannot be accessed by uid 1000.
    result = mount(device, mountPoint, "vfat", flags, 
                       "utf8,uid=1000,gid=1000,fmask=711,dmask=700");
    if (result && errno == EROFS) {
        LOG_ERROR("mount failed EROFS, try again read-only\n");
        flags |= MS_RDONLY;
        result = mount(device, mountPoint, "vfat", flags,
                       "utf8,uid=1000,gid=1000,fmask=711,dmask=700");
    }
    LOG_MOUNT("mount returned %d errno: %d\n", result, errno);

    if (result == 0) {
        NotifyMediaState(mountPoint, MEDIA_MOUNTED, (flags & MS_RDONLY) != 0);
    } else if (errno == EBUSY) {
    // ignore EBUSY, since it usually means the device is already mounted
        result = 0;
    } else {
#if CREATE_MOUNT_POINTS
        rmdir(mountPoint);
#endif
        LOG_MOUNT("mount failed, errno: %d\n", errno);
    }

    return result;
}

static int DoUnmountDevice(const char* mountPoint)
{
    boolean loop = IsLoopMounted(mountPoint);
    int result = umount(mountPoint);
    LOG_MOUNT("umount returned %d errno: %d\n", result, errno);

    if (result == 0)
    {
        if (loop)
        {
            // free the loop device
            int loop_fd = open(LOOP_DEVICE, O_RDONLY);
            if (loop_fd < -1) {
                LOG_ERROR("open loop device failed\n");
            }
            if (ioctl(loop_fd, LOOP_CLR_FD, 0) < 0) {
                LOG_ERROR("ioctl LOOP_CLR_FD failed\n");
            }

            close(loop_fd);
        }

#if CREATE_MOUNT_POINTS
        rmdir(mountPoint);
#endif
        NotifyMediaState(mountPoint, MEDIA_UNMOUNTED, false);
    }

    // ignore EINVAL and ENOENT, since it usually means the device is already unmounted
    if (result && (errno == EINVAL || errno == ENOENT))
        result = 0;

    return result;
}

static int MountPartition(const char* device, const char* mountPoint)
{
    char    buf[100];
    int i;
    
    // attempt to mount subpartitions of the device
    for (i = 1; i < 10; i++)
    {
        snprintf(buf, sizeof(buf), "%sp%d", device, i);
        if (DoMountDevice(buf, mountPoint) == 0)
            return 0;
    }

    return -1;
}

/*****************************************************
 * 
 * AUTO-MOUNTER STATE ENGINE IMPLEMENTATION
 * 
 *****************************************************/

static void SetState(MountPoint* mp, MountState state)
{
    mp->state = state;
}

// Enter a state that requires retries and timeouts.
static void SetRetries(MountPoint* mp, MountState state)
{
    SetState(mp, state);
    mp->retryCount = 0;

    sRetriesPending++;
    // wake up the automounter thread if we are being called 
    // from somewhere else with no retries pending
    if (sRetriesPending == 1 && sAutoMountThread != 0 && 
            pthread_self() != sAutoMountThread)
        pthread_kill(sAutoMountThread, SIGUSR1);
}

// Exit a state that requires retries and timeouts.
static void ClearRetries(MountPoint* mp, MountState state)
{
    SetState(mp, state);
    sRetriesPending--;
}

// attempt to mount the specified mount point.
// set up retry/timeout if it does not succeed at first.
static void RequestMount(MountPoint* mp)
{
    LOG_MOUNT("RequestMount %s\n", mp->mountPoint);

    if (mp->state != kMounted && mp->state != kMounting &&
            access(mp->device, R_OK) == 0) {
        // try raw device first
        if (DoMountDevice(mp->device, mp->mountPoint) == 0 ||
            MountPartition(mp->device, mp->mountPoint) == 0)
        {
            SetState(mp, kMounted);
        }
        else 
        {
            SetState(mp, kMounting);
            mp->retryCount = 0;
            SetRetries(mp, kMounting);
        }
    }
}

// Force the kernel to drop all caches.
static void DropSystemCaches(void)
{
    int fd;

    LOG_MOUNT("Dropping system caches\n");
    fd = open("/proc/sys/vm/drop_caches", O_WRONLY);

    if (fd > 0) {
        char ch = 3;
        int rc;

        rc = write(fd, &ch, 1);
        if (rc <= 0)
            LOG_MOUNT("Error dropping caches (%d)\n", rc);
        close(fd);
    }
}

// attempt to unmount the specified mount point.
// set up retry/timeout if it does not succeed at first.
static void RequestUnmount(MountPoint* mp, MountState retryState)
{
    int result;

    LOG_MOUNT("RequestUnmount %s retryState: %d\n", mp->mountPoint, retryState);
    
    if (mp->state == kMounted)
    {
        SendUnmountRequest(mp->mountPoint);

        // do this in case the user pulls the SD card before we can successfully unmount
        sync();
        DropSystemCaches();

        if (DoUnmountDevice(mp->mountPoint) == 0) 
        {
            SetState(mp, kUnmounted);
            if (retryState == kUnmountingForUms) 
            {
                SetBackingStore(mp, true);
                NotifyMediaState(mp->mountPoint, MEDIA_SHARED, false);
            }
        }
        else 
        {
            LOG_MOUNT("unmount failed, set retry\n");
            SetRetries(mp, retryState);
        }
    } 
    else if (mp->state == kMounting)
    {
        SetState(mp, kUnmounted);
    }
}

// returns true if the mount point should be shared via USB mass storage
static boolean MassStorageEnabledForMountPoint(const MountPoint* mp)
{
    return (gMassStorageEnabled && gMassStorageConnected && mp->enableUms);
}

// handles changes in gMassStorageEnabled and gMassStorageConnected
static void MassStorageStateChanged()
{
    MountPoint* mp = sMountPointList;

    boolean enable = (gMassStorageEnabled && gMassStorageConnected);
    LOG_MOUNT("MassStorageStateChanged enable: %s\n", (enable ? "true" : "false"));
    
    while (mp)
    {
        if (mp->enableUms)
        {
            if (enable)
            {
                if (mp->state == kMounting)
                    SetState(mp, kUnmounted);
                if (mp->state == kUnmounted) 
                {
                    SetBackingStore(mp, true);
                    NotifyMediaState(mp->mountPoint, MEDIA_SHARED, false);
                }
                else
                {
                    LOG_MOUNT("MassStorageStateChanged requesting unmount\n");
                    // need to successfully unmount first
                    RequestUnmount(mp, kUnmountingForUms);
                }
            } else if (mp->umsActive) {
                SetBackingStore(mp, false);
                if (mp->state == kUnmountingForUms)
                {
                    ClearRetries(mp, kMounted);
                    NotifyMediaState(mp->mountPoint, MEDIA_MOUNTED, false);
                }
                else if (mp->state == kUnmounted)
                {
                    NotifyMediaState(mp->mountPoint, MEDIA_UNMOUNTED, false);
                    RequestMount(mp);
                }
            }
        }

        mp = mp->next;
    }
}

// called when USB mass storage connected state changes
static void HandleMassStorageOnline(boolean connected)
{
    if (connected != gMassStorageConnected)
    {
        gMassStorageConnected = connected;
        SendMassStorageConnected(connected);
        
        // we automatically reset to mass storage off after USB is connected
        if (!connected)
            gMassStorageEnabled = false;
    
        MassStorageStateChanged();
    }
}

// called when a new block device has been created
static void HandleMediaInserted(const char* device)
{
    MountPoint* mp = sMountPointList;
    
    while (mp)
    {
        // see if the device matches mount point's block device
        if (mp->state == kUnmounted &&
                strncmp(device, mp->device + DEVPATHLENGTH, strlen(mp->device) - DEVPATHLENGTH) == 0) 
        {
            if (MassStorageEnabledForMountPoint(mp))
            {
                SetBackingStore(mp, true);
                NotifyMediaState(mp->mountPoint, MEDIA_SHARED, false);
            }
            else
                RequestMount(mp);
        }  
        mp = mp->next;
    }
}

// called when a new block device has been deleted
static void HandleMediaRemoved(const char* device)
{    
    MountPoint* mp = sMountPointList;
    while (mp)
    {
        if (strncmp(device, mp->device + DEVPATHLENGTH, strlen(mp->device) - DEVPATHLENGTH) == 0)
        {
            if (mp->enableUms)
                SetBackingStore(mp, false);

             if (mp->state == kMounted) 
            {
                RequestUnmount(mp, kUnmountingForEject);
                NotifyMediaState(mp->mountPoint, MEDIA_BAD_REMOVAL, false);
            }
            
            NotifyMediaState(mp->mountPoint, MEDIA_REMOVED, false);
            break;
        }  
        mp = mp->next;
    }
}

// Handle retrying to mount or unmount devices, 
// and handle timeout condition if we have tried too many times
static void HandleRetries()
{
    MountPoint* mp = sMountPointList;
    
    while (mp)
    {
       if (mp->state == kMounting) 
       {
            if (MountPartition(mp->device, mp->mountPoint) == 0)
            {
                // mount succeeded - clear the retry for this mount point
                ClearRetries(mp, kMounted);
            } 
            else 
            {
                mp->retryCount++;
                if (mp->retryCount == MAX_MOUNT_RETRIES)
                {
                    // we failed to mount the device too many times
                    ClearRetries(mp, kUnmounted);
                    // notify that we failed to mount
                    NotifyMediaState(mp->mountPoint, MEDIA_UNMOUNTABLE, false);
                }
            }
       } 
       else if (mp->state == kUnmountingForEject || mp->state == kUnmountingForUms)
       {
            if (DoUnmountDevice(mp->mountPoint) == 0)
            {
                // unmounting succeeded
                // start mass storage, if state is kUnmountingForUms
                if (mp->state == kUnmountingForUms)
                {
                    SetBackingStore(mp, true);
                     NotifyMediaState(mp->mountPoint, MEDIA_SHARED, false);
                }
                // clear the retry for this mount point
                ClearRetries(mp, kUnmounted);
            } 
            else 
            {
                mp->retryCount++;
                if (mp->retryCount >= MAX_UNMOUNT_RETRIES)
                {
                    // kill any processes that are preventing the device from unmounting
                    // send SIGKILL instead of SIGTERM if the first attempt did not succeed
                    boolean sigkill = (mp->retryCount > MAX_UNMOUNT_RETRIES);
                    
                    // unmounting the device is failing, so start killing processes
                    KillProcessesWithOpenFiles(mp->mountPoint, sigkill);
                }
            }
       } 
        
        mp = mp->next;
    }
}

/*****************************************************
 * 
 * AUTO-MOUNTER THREAD
 * 
 *****************************************************/

static void sigusr1_handler(int signo)
{
    // don't need to do anything here
}

// create a socket for listening to inotify events
int CreateINotifySocket()
{
    // initialize inotify
    int fd = inotify_init();

    if (fd < 0) {
        LOG_ERROR("inotify_init failed, %s\n", strerror(errno));
        return -1;
    }

    fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));

    return fd;
}


// create a socket for listening to uevents
int CreateUEventSocket()
{
    struct sockaddr_nl addr;
    int sz = 64*1024;
    int fd;

    memset(&addr, 0, sizeof(addr));
    addr.nl_family = AF_NETLINK;
    addr.nl_pid = getpid();
    addr.nl_groups = 0xffffffff;

   fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if(fd < 0)
    {
        LOG_ERROR("could not create NETLINK_KOBJECT_UEVENT socket\n");
        return -1;
    }

    setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));

    if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        LOG_ERROR("could not bind NETLINK_KOBJECT_UEVENT socket\n");
        close(fd);
        return -1;
    }

    return fd;
}

/*
 * Automounter main event thread.
 * This thread listens for block devices being created and deleted via inotify,
 * and listens for changes in the USB mass storage connected/disconnected via uevents from the 
 * power supply driver.
 * This thread also handles retries and timeouts for requests to mount or unmount a device.
 */
static void* AutoMountThread(void* arg)
{
    int inotify_fd;
    int uevent_fd;
    int id;
    struct sigaction    actions;

    memset(&actions, 0, sizeof(actions));
    sigemptyset(&actions.sa_mask);
    actions.sa_flags = 0;
    actions.sa_handler = sigusr1_handler;
    sigaction(SIGUSR1, &actions, NULL);
    
    // initialize inotify
    inotify_fd = CreateINotifySocket();
    // watch for files created and deleted in "/dev"
    inotify_add_watch(inotify_fd, DEVPATH, IN_CREATE|IN_DELETE);

    // initialize uevent watcher
    uevent_fd = CreateUEventSocket();
    if (uevent_fd < 0) 
    {
        LOG_ERROR("CreateUEventSocket failed, %s\n", strerror(errno));
        return NULL;
    }
    
    while (1)
    {
        struct pollfd fds[2];
        int timeout, result;

#define INOTIFY_IDX 0
#define UEVENT_IDX  1
    
        fds[INOTIFY_IDX].fd = inotify_fd;
        fds[INOTIFY_IDX].events = POLLIN;
        fds[INOTIFY_IDX].revents = 0;
        fds[UEVENT_IDX].fd = uevent_fd;
        fds[UEVENT_IDX].events = POLLIN;
        fds[UEVENT_IDX].revents = 0;
        
        // wait for an event or a timeout to occur.
        // poll() can also return in response to a SIGUSR1 signal
        timeout = (sRetriesPending ? POLL_TIMEOUT : -1);
        result = poll(fds, 2, timeout);

        // lock the mutex while we are handling events
        pthread_mutex_lock(&sMutex);

        // handle inotify notifications for block device creation and deletion
        if (fds[INOTIFY_IDX].revents == POLLIN)
        {
            struct inotify_event    event;
            char    buffer[512];
            int length = read(inotify_fd, buffer, sizeof(buffer));
            int offset = 0;
 
            while (length >= (int)sizeof(struct inotify_event))
            {
               struct inotify_event* event = (struct inotify_event *)&buffer[offset];
               
               if (event->mask == IN_CREATE)
               {
                   LOG_MOUNT("/dev/block/%s created\n", event->name);
                   HandleMediaInserted(event->name);
               }
               else if (event->mask == IN_DELETE)
               {
                   LOG_MOUNT("/dev/block/%s deleted\n", event->name);
                   HandleMediaRemoved(event->name);
               }
               
               int size = sizeof(struct inotify_event) + event->len;
               length -= size;
               offset += size;
            }
        }

        // handle uevent notifications for USB state changes
        if (fds[UEVENT_IDX].revents == POLLIN)
        {
            char buffer[64*1024];
            int count;
            
            count = recv(uevent_fd, buffer, sizeof(buffer), 0);
            if (count > 0) {
                char* s = buffer;
                char* end = s + count;
                char* type = NULL;
                char* online = NULL;
                char* switchName = NULL;
                char* switchState = NULL;
                                
                while (s < end) {
                    if (!strncmp("POWER_SUPPLY_TYPE=", s, strlen("POWER_SUPPLY_TYPE=")))
                        type = s + strlen("POWER_SUPPLY_TYPE=");
                    else if (!strncmp("POWER_SUPPLY_ONLINE=", s, strlen("POWER_SUPPLY_ONLINE=")))
                        online = s + strlen("POWER_SUPPLY_ONLINE=");                    
                    else if (!strncmp("SWITCH_NAME=", s, strlen("SWITCH_NAME=")))
                        switchName = s + strlen("SWITCH_NAME=");                    
                    else if (!strncmp("SWITCH_STATE=", s, strlen("SWITCH_STATE=")))
                        switchState = s + strlen("SWITCH_STATE=");                    
                    s += (strlen(s) + 1);
                }

                // we use the usb_mass_storage switch state to tell us when USB is online
                if (switchName && switchState && 
                        !strcmp(switchName, "usb_mass_storage") && !strcmp(switchState, "online"))
                {
                    LOG_MOUNT("USB online\n");
                    HandleMassStorageOnline(true);
                }
                
                // and we use the power supply state to tell us when USB is offline
                // we can't rely on the switch for offline detection because we get false positives
                // when USB is reenumerated by the host.
                if (type && online && !strcmp(type, "USB") && !strcmp(online, "0"))
                {
                    LOG_MOUNT("USB offline\n");
                    HandleMassStorageOnline(false);
                }
            }
        }

       // handle retries
       if (sRetriesPending)
            HandleRetries();

        // done handling events, so unlock the mutex
        pthread_mutex_unlock(&sMutex);
    }

    inotify_rm_watch(inotify_fd, id);
    close(inotify_fd);
    close(uevent_fd);

    return NULL;
}

/*****************************************************
 * 
 * THESE FUNCTIONS ARE CALLED FROM THE SERVER THREAD
 * 
 *****************************************************/

// Called to enable or disable USB mass storage support
void EnableMassStorage(boolean enable)
{
    pthread_mutex_lock(&sMutex);

    LOG_MOUNT("EnableMassStorage %s\n", (enable ? "true" : "false"));
    gMassStorageEnabled = enable;
    MassStorageStateChanged();
    pthread_mutex_unlock(&sMutex);
 }

// Called to request that the specified mount point be mounted
void MountMedia(const char* mountPoint)
{
    MountPoint* mp = sMountPointList;
    
    pthread_mutex_lock(&sMutex);
    while (mp)
    {
        if (strcmp(mp->mountPoint, mountPoint) == 0)
        {
            if (mp->state == kUnmountingForEject)
            {
                // handle the case where we try to remount before we actually unmounted
                ClearRetries(mp, kMounted);
            }
            
            // don't attempt to mount if mass storage is active
            if (!MassStorageEnabledForMountPoint(mp))
                RequestMount(mp);
        }
        
        mp = mp->next;
    }
    pthread_mutex_unlock(&sMutex);
 }

// Called to request that the specified mount point be unmounted
void UnmountMedia(const char* mountPoint)
{
    MountPoint* mp = sMountPointList;
    
    pthread_mutex_lock(&sMutex);
    while (mp)
    {
        if (strcmp(mp->mountPoint, mountPoint) == 0)
            RequestUnmount(mp, kUnmountingForEject);
        
        mp = mp->next;
    }
    pthread_mutex_unlock(&sMutex);
}

boolean IsMassStorageEnabled()
{
    return gMassStorageEnabled;
}

boolean IsMassStorageConnected()
{
    return gMassStorageConnected;
}

/***********************************************
 * 
 * THESE FUNCTIONS ARE CALLED ONLY AT STARTUP
 * 
 ***********************************************/
 
void AddMountPoint(const char* device, const char* mountPoint, boolean enableUms)
{
    MountPoint* newMountPoint;
    
    LOG_MOUNT("AddMountPoint device: %s, mountPoint: %s\n", device, mountPoint);
    // add a new MountPoint to the head of our linked list
    newMountPoint = (MountPoint *)malloc(sizeof(MountPoint));
    newMountPoint->device = device;
    newMountPoint->mountPoint = mountPoint;
    newMountPoint->enableUms = enableUms;
    newMountPoint->umsActive = false;
    if (enableUms)
        newMountPoint->lun = sNextLun++;
    newMountPoint->state = kUnmounted;
    newMountPoint->retryCount = 0;

    // add to linked list
    newMountPoint->next = sMountPointList;
    sMountPointList = newMountPoint;
}

static void MountDevices()
{
    MountPoint* mp = sMountPointList;
    while (mp)
    {
        RequestMount(mp);
        mp = mp->next;
    }
}

void StartAutoMounter()
{
    gMassStorageConnected = ReadMassStorageState();
    LOG_MOUNT(gMassStorageConnected ? "USB online\n" : "USB offline\n");

    MountDevices();
    pthread_create(&sAutoMountThread, NULL, AutoMountThread, NULL);
}