mount: rename /proc/mount parser to mount_update_mount_list()
[oweals/mountd.git] / mount.c
diff --git a/mount.c b/mount.c
index 219146b33e68f5accebf62f9434928bc72ca992e..f995745eb2c07b51de7ebae21812370f67caaeec 100644 (file)
--- a/mount.c
+++ b/mount.c
@@ -31,7 +31,7 @@
 
 int mount_new(char *path, char *dev);
 
-struct list_head mounts;
+static struct list_head mounts;
 
 struct mount {
        struct list_head list;
@@ -48,7 +48,7 @@ struct mount {
        int fs;
 };
 
-char *fs_names[] = {
+static char *fs_names[] = {
        "",
        "",
        "mbr",
@@ -60,14 +60,15 @@ char *fs_names[] = {
        "ntfs",
        "",
        "exfat",
-       "ext4"
+       "ext4",
+       "hfsplusjournal"
 };
 
 #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];
 
 static void mount_dump_uci_state(void)
@@ -100,7 +101,7 @@ static void mount_dump_uci_state(void)
                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 <= EXT4)
+               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]);
@@ -138,8 +139,7 @@ static void mount_add_list(char *name, char *dev, char *serial,
 {
        struct mount *mount;
        char tmp[64], tmp2[64];
-       if(fs <= MBR || fs > EXT4)
-               return;
+
        mount  = malloc(sizeof(struct mount));
        INIT_LIST_HEAD(&mount->list);
        strncpy(mount->vendor, vendor, 64);
@@ -154,14 +154,14 @@ static void mount_add_list(char *name, char *dev, char *serial,
        mount->mounted = 0;
        mount->fs = fs;
        list_add(&mount->list, &mounts);
-       if((!mount->ignore) && (mount->fs > MBR) && (mount->fs <= EXT4))
+       if (!mount->ignore)
        {
                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);
-               system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
+               if (!mount_new("/tmp/run/mountd/", dev))
+                       system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
        }
 }
 
@@ -173,10 +173,9 @@ static int mount_check_disc(char *disc)
        if(!fp)
        {
                log_printf("error reading /proc/mounts");
-               fclose(fp);
                return avail;
        }
-       while((fgets(tmp, 256, fp) > 0) && (avail == -1))
+       while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
        {
                char *t;
                char tmp2[32];
@@ -260,12 +259,17 @@ int mount_new(char *path, char *dev)
                        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)
                {
                        options = "force";
                        fstype = "ntfs-3g";
                }
-               if(mount->fs > MBR && mount->fs <= EXT4)
+               if(mount->fs > MBR && mount->fs <= LASTFS)
                {
                        struct uci_context *ctx;
                        char *uci_options, *uci_fstype;
@@ -288,8 +292,10 @@ int mount_new(char *path, char *dev)
        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;
+       }
        if(mount_wait_for_disc(mount->dev) == 0)
        {
                mount->mounted = 1;
@@ -383,7 +389,7 @@ static char* mount_get_serial(char *dev)
                                                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)
@@ -446,6 +452,7 @@ static void mount_dev_add(char *dev)
                char sector_size[64];
                FILE *fp;
                int offset = 3;
+               int fs;
 
                strcpy(name, dev);
                if (!strncmp(name, "mmcblk", 6))
@@ -551,7 +558,11 @@ static void mount_dev_add(char *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();
        }
 }
@@ -601,7 +612,7 @@ char* is_mounted(char *block, char *path)
        return 0;
 }
 
-static void mount_check_mount_list(void)
+static void mount_update_mount_list(void)
 {
        FILE *fp = fopen("/proc/mounts", "r");
        char tmp[256];
@@ -609,13 +620,18 @@ static void mount_check_mount_list(void)
        if(!fp)
        {
                log_printf("error reading /proc/mounts");
-               fclose(fp);
                return;
        }
        mounted_count = 0;
-       while(fgets(tmp, 256, fp) > 0)
+       while(fgets(tmp, 256, fp) != NULL)
        {
                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)
                {
@@ -641,10 +657,8 @@ static void mount_check_mount_list(void)
                        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);
 }
@@ -657,8 +671,8 @@ static int dir_filter2(const struct dirent *a)
        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;
 
 static int check_block(char *b)
 {
@@ -687,13 +701,19 @@ static 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);
-                               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++;
-                                       free(namelist2[m]);
                                }
-                               free(namelist2);
                        }
                        free(namelist[n]);
                }
@@ -733,9 +753,13 @@ static void mount_enum_drives(void)
                        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("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
+                       if (q->mounted) {
+                               snprintf(tmp, 64, "%s%s", "/tmp/run/mountd/", q->dev);
+                               rmdir(tmp);
+                               snprintf(tmp, 64, "%s%s", uci_path, q->name);
+                               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");
@@ -755,7 +779,7 @@ static void mount_check_enum(void)
 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);
-       mount_check_mount_list();
+       mount_update_mount_list();
 }