mount: drop duplicated rmdir() call from the mount_enum_drives()
[project/mountd.git] / mount.c
diff --git a/mount.c b/mount.c
index 16c3529..bf5fbfd 100644 (file)
--- a/mount.c
+++ b/mount.c
@@ -16,6 +16,8 @@
 #include <glob.h>
 #include <libgen.h>
 #include <poll.h>
 #include <glob.h>
 #include <libgen.h>
 #include <poll.h>
+#include <dirent.h>
+#include <syslog.h>
 
 #include "include/log.h"
 #include "include/list.h"
 
 #include "include/log.h"
 #include "include/list.h"
 #include "include/autofs.h"
 #include "include/ucix.h"
 #include "include/fs.h"
 #include "include/autofs.h"
 #include "include/ucix.h"
 #include "include/fs.h"
+#include "include/mount.h"
 
 int mount_new(char *path, char *dev);
 
 
 int mount_new(char *path, char *dev);
 
-struct list_head mounts;
+static struct list_head mounts;
+
+/**
+ * enum status - status of mount entry
+ *
+ * @STATUS_UNMOUNTED: currently not mounted
+ * @STATUS_MOUNTED: mounted & ready for usage
+ * @STATUS_EXPIRED: mount expired & *temporary* unmounted
+ * @STATUS_IGNORE: entry should be ignored and never mounted
+ */
+enum status {
+       STATUS_UNMOUNTED = 0,
+       STATUS_MOUNTED,
+       STATUS_EXPIRED,
+       STATUS_IGNORE,
+};
 
 struct mount {
        struct list_head list;
 
 struct mount {
        struct list_head list;
@@ -38,32 +56,36 @@ struct mount {
        char vendor[64];
        char model[64];
        char rev[64];
        char vendor[64];
        char model[64];
        char rev[64];
-       int mounted;
-       int ignore;
+       enum status status;
        char size[64];
        char sector_size[64];
        int fs;
 };
 
        char size[64];
        char sector_size[64];
        int fs;
 };
 
-char *fs_names[] = {
+static char *fs_names[] = {
        "",
        "",
        "",
        "",
-       "MBR",
-       "EXT2",
-       "EXT3",
-       "FAT",
-       "HFSPLUS",
-       "NTFS"
+       "mbr",
+       "ext2",
+       "ext3",
+       "fat",
+       "hfsplus",
+       "",
+       "ntfs",
+       "",
+       "exfat",
+       "ext4",
+       "hfsplusjournal"
 };
 
 #define MAX_MOUNTED            32
 #define MAX_MOUNT_NAME 32
 
 };
 
 #define MAX_MOUNTED            32
 #define MAX_MOUNT_NAME 32
 
-char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
-int mounted_count = 0;
+static char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
+static int mounted_count = 0;
 extern char uci_path[32];
 
 extern char uci_path[32];
 
-void mount_dump_uci_state(void)
+static void mount_dump_uci_state(void)
 {
        struct uci_context *ctx;
        struct list_head *p;
 {
        struct uci_context *ctx;
        struct list_head *p;
@@ -87,20 +109,20 @@ void mount_dump_uci_state(void)
                ucix_add_option(ctx, mountd, q->serial, "disc", t);
                ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
                snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
                ucix_add_option(ctx, mountd, q->serial, "disc", t);
                ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
                snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
-               ucix_add_option(ctx, mountd, q->serial, t, (q->mounted)?("1"):("0"));
+               ucix_add_option(ctx, mountd, q->serial, t, q->status == STATUS_MOUNTED ? "1" : "0");
                ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
                ucix_add_option(ctx, mountd, q->serial, "model", q->model);
                ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
                snprintf(t, 64, "size%d", atoi(&q->dev[3]));
                ucix_add_option(ctx, mountd, q->serial, t, q->size);
                ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
                ucix_add_option(ctx, mountd, q->serial, "model", q->model);
                ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
                snprintf(t, 64, "size%d", atoi(&q->dev[3]));
                ucix_add_option(ctx, mountd, q->serial, t, q->size);
-               if(q->fs > MBR && q->fs <= NTFS)
+               if(q->fs > MBR && q->fs <= LASTFS)
                {
                        snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
                        ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
                }
                {
                        snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
                        ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
                }
-               if(q->mounted)
+               if (q->status == STATUS_MOUNTED)
                        mounted++;
                        mounted++;
-               if((!q->ignore) && q->size && q->sector_size)
+               if ((q->status != STATUS_IGNORE) && q->size && q->sector_size)
                        size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
        }
        ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
                        size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
        }
        ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
@@ -110,7 +132,7 @@ void mount_dump_uci_state(void)
        ucix_cleanup(ctx);
 }
 
        ucix_cleanup(ctx);
 }
 
-struct mount* mount_find(char *name, char *dev)
+static struct mount* mount_find(char *name, char *dev)
 {
        struct list_head *p;
        list_for_each(p, &mounts)
 {
        struct list_head *p;
        list_for_each(p, &mounts)
@@ -126,13 +148,12 @@ struct mount* mount_find(char *name, char *dev)
        return 0;
 }
 
        return 0;
 }
 
-void mount_add_list(char *name, char *dev, char *serial,
+static void mount_add_list(char *name, char *dev, char *serial,
        char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
 {
        struct mount *mount;
        char tmp[64], tmp2[64];
        char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
 {
        struct mount *mount;
        char tmp[64], tmp2[64];
-       if(fs <= MBR || fs > NTFS)
-               return;
+
        mount  = malloc(sizeof(struct mount));
        INIT_LIST_HEAD(&mount->list);
        strncpy(mount->vendor, vendor, 64);
        mount  = malloc(sizeof(struct mount));
        INIT_LIST_HEAD(&mount->list);
        strncpy(mount->vendor, vendor, 64);
@@ -143,21 +164,23 @@ void mount_add_list(char *name, char *dev, char *serial,
        strncpy(mount->serial, serial, 64);
        strncpy(mount->size, size, 64);
        strncpy(mount->sector_size, sector_size, 64);
        strncpy(mount->serial, serial, 64);
        strncpy(mount->size, size, 64);
        strncpy(mount->sector_size, sector_size, 64);
-       mount->ignore = ignore;
-       mount->mounted = 0;
+       mount->status = STATUS_UNMOUNTED;
        mount->fs = fs;
        list_add(&mount->list, &mounts);
        mount->fs = fs;
        list_add(&mount->list, &mounts);
-       if((!mount->ignore) && (mount->fs > MBR) && (mount->fs <= NTFS))
-       {
+
+       if (ignore) {
+               mount->status = STATUS_IGNORE;
+       } else {
                log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
                snprintf(tmp, 64, "%s%s", uci_path, name);
                snprintf(tmp2, 64, "/tmp/run/mountd/%s", dev);
                symlink(tmp2, tmp);
                log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
                snprintf(tmp, 64, "%s%s", uci_path, name);
                snprintf(tmp2, 64, "/tmp/run/mountd/%s", dev);
                symlink(tmp2, tmp);
-               mount_new("/tmp/run/mountd/", dev);
+               if (!mount_new("/tmp/run/mountd/", dev))
+                       system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
        }
 }
 
        }
 }
 
-int mount_check_disc(char *disc)
+static int mount_check_disc(char *disc)
 {
        FILE *fp = fopen("/proc/mounts", "r");
        char tmp[256];
 {
        FILE *fp = fopen("/proc/mounts", "r");
        char tmp[256];
@@ -165,10 +188,9 @@ int mount_check_disc(char *disc)
        if(!fp)
        {
                log_printf("error reading /proc/mounts");
        if(!fp)
        {
                log_printf("error reading /proc/mounts");
-               fclose(fp);
                return avail;
        }
                return avail;
        }
-       while((fgets(tmp, 256, fp) > 0) && (avail == -1))
+       while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
        {
                char *t;
                char tmp2[32];
        {
                char *t;
                char tmp2[32];
@@ -187,7 +209,7 @@ int mount_check_disc(char *disc)
        return avail;
 }
 
        return avail;
 }
 
-int mount_wait_for_disc(char *disc)
+static int mount_wait_for_disc(char *disc)
 {
        int i = 10;
        while(i--)
 {
        int i = 10;
        while(i--)
@@ -212,7 +234,7 @@ int mount_new(char *path, char *dev)
                log_printf("request for invalid path %s%s\n", path, dev);
                return -1;
        }
                log_printf("request for invalid path %s%s\n", path, dev);
                return -1;
        }
-       if(mount->ignore || mount->mounted || mount->fs == EXTENDED)
+       if (mount->status == STATUS_IGNORE || mount->status == STATUS_MOUNTED || mount->fs == EXTENDED)
                return -1;
        snprintf(tmp, 256, "%s%s", path, mount->dev);
        log_printf("mounting %s\n", tmp);
                return -1;
        snprintf(tmp, 256, "%s%s", path, mount->dev);
        log_printf("mounting %s\n", tmp);
@@ -221,41 +243,77 @@ int mount_new(char *path, char *dev)
        pid = autofs_safe_fork();
        if(!pid)
        {
        pid = autofs_safe_fork();
        if(!pid)
        {
+               char *options, *fstype;
+               if(mount->fs == EXFAT)
+               {
+                       options = "rw,uid=1000,gid=1000";
+                       fstype = "exfat";
+               }
                if(mount->fs == FAT)
                {
                if(mount->fs == FAT)
                {
-                       log_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
-                       ret = system_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
+                       options = "rw,uid=1000,gid=1000";
+                       fstype = "vfat";
+               }
+               if(mount->fs == EXT4)
+               {
+                       options = "rw,defaults";
+                       fstype = "ext4";
                }
                if(mount->fs == EXT3)
                {
                }
                if(mount->fs == EXT3)
                {
-                       log_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount->dev, tmp);
-                       ret = system_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount->dev, tmp);
+                       options = "rw,defaults";
+                       fstype = "ext3";
                }
                if(mount->fs == EXT2)
                {
                }
                if(mount->fs == EXT2)
                {
-                       log_printf("mount -t ext2 -o rw,defaults /dev/%s %s", mount->dev, tmp);
-                       ret = system_printf("mount -t ext2 -o rw,defaults /dev/%s %s", mount->dev, tmp);
+                       options = "rw,defaults";
+                       fstype = "ext2";
                }
                if(mount->fs == HFSPLUS)
                {
                }
                if(mount->fs == HFSPLUS)
                {
-                       log_printf("mount -t hfsplus -o rw,defaults,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
-                       ret = system_printf("mount -t hfsplus -o rw,defaults,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
+                       options = "rw,defaults,uid=1000,gid=1000";
+                       fstype = "hfsplus";
+               }
+               if(mount->fs == HFSPLUSJOURNAL)
+               {
+                       options = "ro,defaults,uid=1000,gid=1000";
+                       fstype = "hfsplus";
                }
                if(mount->fs == NTFS)
                {
                }
                if(mount->fs == NTFS)
                {
-                       log_printf("ntfs-3g /dev/%s %s -o force", mount->dev, tmp);
-                       ret = system_printf("ntfs-3g /dev/%s %s -o force", mount->dev, tmp);
+                       options = "force";
+                       fstype = "ntfs-3g";
+               }
+               if(mount->fs > MBR && mount->fs <= LASTFS)
+               {
+                       struct uci_context *ctx;
+                       char *uci_options, *uci_fstype;
+                       ctx = ucix_init("mountd");
+                       if(fs_names[mount->fs])
+                       {
+                               uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
+                               uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
+                               if(uci_options)
+                                       options = uci_options;
+                               if(uci_fstype)
+                                       fstype = uci_fstype;
+                               log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
+                               ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
+                       }
+                       ucix_cleanup(ctx);
                }
                exit(WEXITSTATUS(ret));
        }
        pid = waitpid(pid, &ret, 0);
        ret = WEXITSTATUS(ret);
        log_printf("----------> mount ret = %d\n", ret);
                }
                exit(WEXITSTATUS(ret));
        }
        pid = waitpid(pid, &ret, 0);
        ret = WEXITSTATUS(ret);
        log_printf("----------> mount ret = %d\n", ret);
-       if(ret && (ret != 0xff))
+       if (ret && ret != 0xff) {
+               rmdir(tmp);
                return -1;
                return -1;
+       }
        if(mount_wait_for_disc(mount->dev) == 0)
        {
        if(mount_wait_for_disc(mount->dev) == 0)
        {
-               mount->mounted = 1;
+               mount->status = STATUS_MOUNTED;
                mount_dump_uci_state();
        } else return -1;
        return 0;
                mount_dump_uci_state();
        } else return -1;
        return 0;
@@ -267,32 +325,32 @@ int mount_remove(char *path, char *dev)
        char tmp[256];
        int ret;
        snprintf(tmp, 256, "%s%s", path, dev);
        char tmp[256];
        int ret;
        snprintf(tmp, 256, "%s%s", path, dev);
-       log_printf("%s has expired... unmounting\n", tmp);
+       log_printf("device %s has expired... unmounting %s\n", dev, tmp);
        ret = system_printf("/bin/umount %s", tmp);
        if(ret != 0)
                return 0;
        rmdir(tmp);
        mount = mount_find(0, dev);
        if(mount)
        ret = system_printf("/bin/umount %s", tmp);
        if(ret != 0)
                return 0;
        rmdir(tmp);
        mount = mount_find(0, dev);
        if(mount)
-               mount->mounted = 0;
+               mount->status = STATUS_EXPIRED;
        log_printf("finished unmounting\n");
        mount_dump_uci_state();
        return 0;
 }
 
        log_printf("finished unmounting\n");
        mount_dump_uci_state();
        return 0;
 }
 
-int dir_sort(const void *a, const void *b)
+static int dir_sort(const struct dirent **a, const struct dirent **b)
 {
        return 0;
 }
 
 {
        return 0;
 }
 
-int dir_filter(const struct dirent *a)
+static int dir_filter(const struct dirent *a)
 {
        if(strstr(a->d_name, ":"))
                return 1;
        return 0;
 }
 
 {
        if(strstr(a->d_name, ":"))
                return 1;
        return 0;
 }
 
-char* mount_get_serial(char *dev)
+static char* mount_get_serial(char *dev)
 {
        static char tmp[64];
        static char tmp2[64];
 {
        static char tmp[64];
        static char tmp2[64];
@@ -300,6 +358,7 @@ char* mount_get_serial(char *dev)
        static struct hd_driveid hd;
        int i;
        static char *serial;
        static struct hd_driveid hd;
        int i;
        static char *serial;
+       static char disc_id[13];
        snprintf(tmp, 64, "/dev/%s", dev);
        disc = open(tmp, O_RDONLY);
        if(!disc)
        snprintf(tmp, 64, "/dev/%s", dev);
        disc = open(tmp, O_RDONLY);
        if(!disc)
@@ -345,7 +404,7 @@ char* mount_get_serial(char *dev)
                                                fp = fopen(tmp2, "r");
                                                if(fp)
                                                {
                                                fp = fopen(tmp2, "r");
                                                if(fp)
                                                {
-                                                       while(fgets(tmp2, 64, fp) > 0)
+                                                       while(fgets(tmp2, 64, fp) != NULL)
                                                        {
                                                                serial = strstr(tmp2, "Serial Number:");
                                                                if(serial)
                                                        {
                                                                serial = strstr(tmp2, "Serial Number:");
                                                                if(serial)
@@ -371,23 +430,24 @@ char* mount_get_serial(char *dev)
        } else {
                /* serial string id is cheap, but makes the discs anonymous */
                unsigned char uniq[6];
        } else {
                /* serial string id is cheap, but makes the discs anonymous */
                unsigned char uniq[6];
+               unsigned int *u = (unsigned int*) uniq;
                int l = strlen(serial);
                int i;
                int l = strlen(serial);
                int i;
-               static char disc_id[13];
                memset(disc_id, 0, 13);
                memset(uniq, 0, 6);
                for(i = 0; i < l; i++)
                {
                        uniq[i%6] += serial[i];
                }
                memset(disc_id, 0, 13);
                memset(uniq, 0, 6);
                for(i = 0; i < l; i++)
                {
                        uniq[i%6] += serial[i];
                }
-               sprintf(disc_id, "%08X%02X%02X", *((unsigned int*)&uniq[0]), uniq[4], uniq[5]);
+               sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
                //log_printf("Serial number - %s %s\n", serial, disc_id);
                return disc_id;
        }
                //log_printf("Serial number - %s %s\n", serial, disc_id);
                return disc_id;
        }
-       return 0;
+       sprintf(disc_id, "000000000000");
+       return disc_id;
 }
 
 }
 
-void mount_dev_add(char *dev)
+static void mount_dev_add(char *dev)
 {
        struct mount *mount = mount_find(0, dev);
        if(!mount)
 {
        struct mount *mount = mount_find(0, dev);
        if(!mount)
@@ -406,17 +466,29 @@ void mount_dev_add(char *dev)
                char size[64];
                char sector_size[64];
                FILE *fp;
                char size[64];
                char sector_size[64];
                FILE *fp;
+               int offset = 3;
+               int fs;
+
                strcpy(name, dev);
                strcpy(name, dev);
-               name[3] = '\0';
+               if (!strncmp(name, "mmcblk", 6))
+                       offset = 7;
+               name[offset] = '\0';
                s = mount_get_serial(name);
                s = mount_get_serial(name);
-               if(!s)
+               if(!s) {
                        return;
                        return;
-               snprintf(tmp, 64, "part%s", &dev[3]);
-               snprintf(node, 64, "Disc-%s", &dev[2]);
-               if(node[5] >= 'a' && node[5] <= 'z')
+               }
+               if (!strncmp(name, "mmcblk", 6)) {
+                       snprintf(tmp, 64, "part%s", &dev[8]);
+                       snprintf(node, 64, "SD-P%s", &dev[8]);
+
+               } else {
+                       snprintf(tmp, 64, "part%s", &dev[3]);
+                       snprintf(node, 64, "USB-%s", &dev[2]);
+               }
+               if(node[4] >= 'a' && node[4] <= 'z')
                {
                {
-                       node[5] -= 'a';
-                       node[5] += 'A';
+                       node[4] -= 'a';
+                       node[4] += 'A';
                }
                ctx = ucix_init("mountd");
                p = ucix_get_option(ctx, "mountd", s, tmp);
                }
                ctx = ucix_init("mountd");
                p = ucix_get_option(ctx, "mountd", s, tmp);
@@ -501,28 +573,25 @@ void mount_dev_add(char *dev)
                        fclose(fp);
                }
                snprintf(tmp, 64, "/dev/%s", dev);
                        fclose(fp);
                }
                snprintf(tmp, 64, "/dev/%s", dev);
-               mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, detect_fs(tmp));
+               fs = detect_fs(tmp);
+               if (fs <= MBR || fs > LASTFS) {
+                       ignore = 1;
+               }
+               mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
                mount_dump_uci_state();
        }
 }
 
                mount_dump_uci_state();
        }
 }
 
-void mount_dev_del(char *dev)
+static void mount_dev_del(struct mount *mount)
 {
 {
-       struct mount *mount = mount_find(0, dev);
        char tmp[256];
        char tmp[256];
-       if(mount)
-       {
-               if(mount->mounted)
-               {
-                       snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->name);
-                       log_printf("%s has dissappeared ... unmounting\n", tmp);
-                       snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
-                       system_printf("/bin/umount %s", tmp);
-                       rmdir(tmp);
-                       snprintf(tmp, 64, "%s%s", uci_path, mount->name);
-                       unlink(tmp);
-                       mount_dump_uci_state();
-               }
+
+       if (mount->status == STATUS_MOUNTED) {
+               snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
+               log_printf("device %s has disappeared ... unmounting %s\n", mount->dev, tmp);
+               system_printf("/bin/umount %s", tmp);
+               rmdir(tmp);
+               mount_dump_uci_state();
        }
 }
 
        }
 }
 
@@ -532,7 +601,7 @@ void mount_dump_list(void)
        list_for_each(p, &mounts)
        {
                struct mount *q = container_of(p, struct mount, list);
        list_for_each(p, &mounts)
        {
                struct mount *q = container_of(p, struct mount, list);
-               log_printf("* %s %s %d\n", q->name, q->dev, q->mounted);
+               log_printf("* %s %s %d\n", q->name, q->dev, q->status == STATUS_MOUNTED);
        }
 }
 
        }
 }
 
@@ -551,7 +620,7 @@ char* is_mounted(char *block, char *path)
        return 0;
 }
 
        return 0;
 }
 
-void mount_check_mount_list(void)
+static void mount_update_mount_list(void)
 {
        FILE *fp = fopen("/proc/mounts", "r");
        char tmp[256];
 {
        FILE *fp = fopen("/proc/mounts", "r");
        char tmp[256];
@@ -559,13 +628,18 @@ void mount_check_mount_list(void)
        if(!fp)
        {
                log_printf("error reading /proc/mounts");
        if(!fp)
        {
                log_printf("error reading /proc/mounts");
-               fclose(fp);
                return;
        }
        mounted_count = 0;
                return;
        }
        mounted_count = 0;
-       while(fgets(tmp, 256, fp) > 0)
+       while(fgets(tmp, 256, fp) != NULL)
        {
                char *t, *t2;
        {
                char *t, *t2;
+
+               if (mounted_count + 1 > MAX_MOUNTED) {
+                       log_printf("found more than %d mounts \n", MAX_MOUNTED);
+                       break;
+               }
+
                t = strstr(tmp, " ");
                if(t)
                {
                t = strstr(tmp, " ");
                if(t)
                {
@@ -591,26 +665,24 @@ void mount_check_mount_list(void)
                        mounted[mounted_count][0],
                        mounted[mounted_count][1],
                        mounted[mounted_count][2]);*/
                        mounted[mounted_count][0],
                        mounted[mounted_count][1],
                        mounted[mounted_count][2]);*/
-               if(mounted_count < MAX_MOUNTED - 1)
-                       mounted_count++;
-               else
-                       log_printf("found more than %d mounts \n", MAX_MOUNTED);
+
+               mounted_count++;
        }
        fclose(fp);
 }
 
        }
        fclose(fp);
 }
 
-/* FIXME: we need ore intelligence here */
-int dir_filter2(const struct dirent *a)
+/* FIXME: we need more intelligence here */
+static int dir_filter2(const struct dirent *a)
 {
 {
-       if(/*strcmp(a->d_name, "sda") &&*/(!strncmp(a->d_name, "sd", 2)))
+       if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
                return 1;
        return 0;
 }
 #define MAX_BLOCK      64
                return 1;
        return 0;
 }
 #define MAX_BLOCK      64
-char block[MAX_BLOCK][MAX_BLOCK];
-int blk_cnt = 0;
+static char block[MAX_BLOCK][MAX_BLOCK];
+static int blk_cnt = 0;
 
 
-int check_block(char *b)
+static int check_block(char *b)
 {
        int i;
        for(i = 0; i < blk_cnt; i++)
 {
        int i;
        for(i = 0; i < blk_cnt; i++)
@@ -621,7 +693,7 @@ int check_block(char *b)
        return 0;
 }
 
        return 0;
 }
 
-void mount_enum_drives(void)
+static void mount_enum_drives(void)
 {
        struct dirent **namelist, **namelist2;
        int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
 {
        struct dirent **namelist, **namelist2;
        int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
@@ -637,13 +709,19 @@ void mount_enum_drives(void)
                                char tmp[64];
                                snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
                                m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
                                char tmp[64];
                                snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
                                m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
-                               while(m--)
+                               if(m > 0)
                                {
                                {
-                                       strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
+                                       while(m--)
+                                       {
+                                               strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
+                                               blk_cnt++;
+                                               free(namelist2[m]);
+                                       }
+                                       free(namelist2);
+                               } else {
+                                       strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
                                        blk_cnt++;
                                        blk_cnt++;
-                                       free(namelist2[m]);
                                }
                                }
-                               free(namelist2);
                        }
                        free(namelist[n]);
                }
                        }
                        free(namelist[n]);
                }
@@ -661,11 +739,11 @@ void mount_enum_drives(void)
                ctx = ucix_init("mountd");
                t = ucix_get_option(ctx, "mountd", q->serial, tmp);
                ucix_cleanup(ctx);
                ctx = ucix_init("mountd");
                t = ucix_get_option(ctx, "mountd", q->serial, tmp);
                ucix_cleanup(ctx);
-               if(t && !q->mounted)
+               if (t && q->status != STATUS_MOUNTED)
                {
                        if(!strcmp(t, "0"))
                        {
                {
                        if(!strcmp(t, "0"))
                        {
-                               if(!q->ignore)
+                               if (q->status != STATUS_IGNORE)
                                        del = 1;
                        } else if(!strcmp(t, "1"))
                        {
                                        del = 1;
                        } else if(!strcmp(t, "1"))
                        {
@@ -678,14 +756,16 @@ void mount_enum_drives(void)
                }
                if(!check_block(q->dev)||del)
                {
                }
                if(!check_block(q->dev)||del)
                {
-                       mount_dev_del(q->dev);
+                       mount_dev_del(q);
                        p->prev->next = p->next;
                        p->next->prev = p->prev;
                        p = p->next;
                        p->prev->next = p->next;
                        p->next->prev = p->prev;
                        p = p->next;
-                       log_printf("removing %s\n", q->dev);
-                       snprintf(tmp, 64, "%s%s", uci_path, q->name);
-                       unlink(tmp);
-                       system_printf("/etc/mountd/event remove %s %s", q->dev, q->name);
+                       if (q->status == STATUS_MOUNTED || q->status == STATUS_EXPIRED) {
+                               snprintf(tmp, 64, "%s%s", uci_path, q->name);
+                               log_printf("unlinking %s\n", tmp);
+                               unlink(tmp);
+                               system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
+                       }
                        free(q);
                        mount_dump_uci_state();
                        system_printf("/etc/fonstated/ReloadSamba");
                        free(q);
                        mount_dump_uci_state();
                        system_printf("/etc/fonstated/ReloadSamba");
@@ -696,7 +776,7 @@ void mount_enum_drives(void)
                mount_dev_add(block[i]);
 }
 
                mount_dev_add(block[i]);
 }
 
-void mount_check_enum(void)
+static void mount_check_enum(void)
 {
        waitpid(-1, 0, WNOHANG);
        mount_enum_drives();
 {
        waitpid(-1, 0, WNOHANG);
        mount_enum_drives();
@@ -705,7 +785,7 @@ void mount_check_enum(void)
 void mount_init(void)
 {
        INIT_LIST_HEAD(&mounts);
 void mount_init(void)
 {
        INIT_LIST_HEAD(&mounts);
-       timer_add(mount_check_mount_list, 2);
+       timer_add(mount_update_mount_list, 2);
        timer_add(mount_check_enum, 1);
        timer_add(mount_check_enum, 1);
-       mount_check_mount_list();
+       mount_update_mount_list();
 }
 }