1d581ea2358f10c3182fb326a8e6338fa73268ea
[oweals/mountd.git] / mount.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <sys/ioctl.h>
10 #include <linux/hdreg.h>
11 #include <scsi/sg.h>
12 #include <dirent.h>
13 #include <sys/wait.h>
14 #include <sys/inotify.h>
15 #include <sys/mount.h>
16 #include <glob.h>
17 #include <libgen.h>
18 #include <poll.h>
19 #include <syslog.h>
20
21 #include "include/log.h"
22 #include "include/list.h"
23 #include "include/sys.h"
24 #include "include/signal.h"
25 #include "include/timer.h"
26 #include "include/autofs.h"
27 #include "include/ucix.h"
28 #include "include/fs.h"
29 #include "include/mount.h"
30
31 int mount_new(char *path, char *dev);
32
33 static struct list_head mounts;
34
35 /**
36  * enum status - status of mount entry
37  *
38  * @STATUS_UNMOUNTED: currently not mounted
39  * @STATUS_MOUNTED: mounted & ready for usage
40  * @STATUS_EXPIRED: mount expired & *temporary* unmounted
41  * @STATUS_IGNORE: entry should be ignored and never mounted
42  */
43 enum status {
44         STATUS_UNMOUNTED = 0,
45         STATUS_MOUNTED,
46         STATUS_EXPIRED,
47         STATUS_IGNORE,
48 };
49
50 struct mount {
51         struct list_head list;
52         char name[64];
53         char dev[64];
54         char serial[64];
55         char vendor[64];
56         char model[64];
57         char rev[64];
58         enum status status;
59         char size[64];
60         char sector_size[64];
61         int fs;
62 };
63
64 static char *fs_names[] = {
65         "",
66         "",
67         "mbr",
68         "ext2",
69         "ext3",
70         "fat",
71         "hfsplus",
72         "",
73         "ntfs",
74         "",
75         "exfat",
76         "ext4",
77         "hfsplusjournal"
78 };
79
80 #define MAX_MOUNTED             32
81 #define MAX_MOUNT_NAME  32
82
83 static char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
84 static int mounted_count = 0;
85 extern char uci_path[32];
86
87 static void mount_dump_uci_state(void)
88 {
89         struct uci_context *ctx;
90         struct list_head *p;
91         char mountd[] = {"mountd"};
92         char type[] = {"mountd_disc"};
93         int mounted = 0;
94         unsigned long long int size = 0;
95         unlink("/var/state/mountd");
96         ctx = ucix_init("mountd");
97         uci_set_savedir(ctx, "/var/state/");
98         ucix_add_option_int(ctx, mountd, mountd, "count", list_count(&mounts));
99         list_for_each(p, &mounts)
100         {
101                 struct mount *q = container_of(p, struct mount, list);
102                 char t[64];
103                 if(q->fs == EXTENDED)
104                         continue;
105                 ucix_add_section(ctx, mountd, q->serial, type);
106                 strcpy(t, q->dev);
107                 t[3] = '\0';
108                 ucix_add_option(ctx, mountd, q->serial, "disc", t);
109                 ucix_add_option(ctx, mountd, q->serial, "sector_size", q->sector_size);
110                 snprintf(t, 64, "part%dmounted", atoi(&q->dev[3]));
111                 ucix_add_option(ctx, mountd, q->serial, t, q->status == STATUS_MOUNTED ? "1" : "0");
112                 ucix_add_option(ctx, mountd, q->serial, "vendor", q->vendor);
113                 ucix_add_option(ctx, mountd, q->serial, "model", q->model);
114                 ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
115                 snprintf(t, 64, "size%d", atoi(&q->dev[3]));
116                 ucix_add_option(ctx, mountd, q->serial, t, q->size);
117                 if(q->fs > MBR && q->fs <= LASTFS)
118                 {
119                         snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
120                         ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
121                 }
122                 if (q->status == STATUS_MOUNTED)
123                         mounted++;
124                 if ((q->status != STATUS_IGNORE) && q->size && q->sector_size)
125                         size = size + (((unsigned long long int)atoi(q->size)) * ((unsigned long long int)atoi(q->sector_size)));
126         }
127         ucix_add_option_int(ctx, mountd, mountd, "mounted", mounted);
128         ucix_add_option_int(ctx, mountd, mountd, "total", size);
129         system_printf("echo -n %llu > /tmp/run/mountd_size", size);
130         ucix_save_state(ctx, "mountd");
131         ucix_cleanup(ctx);
132 }
133
134 static struct mount* mount_find(char *name, char *dev)
135 {
136         struct list_head *p;
137         list_for_each(p, &mounts)
138         {
139                 struct mount *q = container_of(p, struct mount, list);
140                 if(name)
141                         if(!strcmp(q->name, name))
142                                 return q;
143                 if(dev)
144                         if(!strcmp(q->dev, dev))
145                                 return q;
146         }
147         return 0;
148 }
149
150 static void mount_add_list(char *name, char *dev, char *serial,
151         char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
152 {
153         struct mount *mount;
154         char dev_path[64], dev_link[64], tmp[64];
155
156         mount  = malloc(sizeof(struct mount));
157         INIT_LIST_HEAD(&mount->list);
158         strncpy(mount->vendor, vendor, 64);
159         strncpy(mount->model, model, 64);
160         strncpy(mount->rev, rev, 64);
161         strncpy(mount->name, name, 64);
162         strncpy(mount->dev, dev, 64);
163         strncpy(mount->serial, serial, 64);
164         strncpy(mount->size, size, 64);
165         strncpy(mount->sector_size, sector_size, 64);
166         mount->status = STATUS_UNMOUNTED;
167         mount->fs = fs;
168         list_add(&mount->list, &mounts);
169
170         if (ignore) {
171                 mount->status = STATUS_IGNORE;
172         } else {
173                 struct stat st;
174
175                 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
176
177                 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, name);
178                 snprintf(dev_path, sizeof(dev_path), "%s%s", "/tmp/run/mountd/", dev);
179                 /* If link aleady exists - replace it */
180                 if (lstat(dev_link, &st) ==  0 && S_ISLNK(st.st_mode)) {
181                         snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
182                         symlink(dev_path, tmp);
183                         rename(tmp, dev_link);
184                 } else {
185                         symlink(dev_path, dev_link);
186                 }
187                 if (!mount_new("/tmp/run/mountd/", dev))
188                         system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
189         }
190 }
191
192 static int mount_check_disc(char *disc)
193 {
194         FILE *fp = fopen("/proc/mounts", "r");
195         char tmp[256];
196         int avail = -1;
197         if(!fp)
198         {
199                 log_printf("error reading /proc/mounts");
200                 return avail;
201         }
202         while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
203         {
204                 char *t;
205                 char tmp2[32];
206                 t = strstr(tmp, " ");
207                 if(t)
208                 {
209                         int l;
210                         *t = '\0';
211                         l = snprintf(tmp2, 31, "/dev/%s", disc);
212
213                         if(!strncmp(tmp, tmp2, l))
214                                 avail = 0;
215                 }
216         }
217         fclose(fp);
218         return avail;
219 }
220
221 static int mount_wait_for_disc(char *disc)
222 {
223         int i = 10;
224         while(i--)
225         {
226                 int ret = mount_check_disc(disc);
227                 if(!ret)
228                         return ret;
229                 poll(0, 0, 100);
230         }
231         return -1;
232 }
233
234 int mount_new(char *path, char *dev)
235 {
236         struct mount *mount;
237         char tmp[256];
238         int ret = 1;
239         pid_t pid;
240         mount = mount_find(0, dev);
241         if(!mount)
242         {
243                 log_printf("request for invalid path %s%s\n", path, dev);
244                 return -1;
245         }
246         if (mount->status == STATUS_IGNORE || mount->status == STATUS_MOUNTED || mount->fs == EXTENDED)
247                 return -1;
248         snprintf(tmp, 256, "%s%s", path, mount->dev);
249         log_printf("mounting %s\n", tmp);
250         mkdir(tmp, 777);
251
252         pid = autofs_safe_fork();
253         if(!pid)
254         {
255                 char *options, *fstype;
256                 if(mount->fs == EXFAT)
257                 {
258                         options = "rw,uid=1000,gid=1000";
259                         fstype = "exfat";
260                 }
261                 if(mount->fs == FAT)
262                 {
263                         options = "rw,uid=1000,gid=1000";
264                         fstype = "vfat";
265                 }
266                 if(mount->fs == EXT4)
267                 {
268                         options = "rw,defaults";
269                         fstype = "ext4";
270                 }
271                 if(mount->fs == EXT3)
272                 {
273                         options = "rw,defaults";
274                         fstype = "ext3";
275                 }
276                 if(mount->fs == EXT2)
277                 {
278                         options = "rw,defaults";
279                         fstype = "ext2";
280                 }
281                 if(mount->fs == HFSPLUS)
282                 {
283                         options = "rw,defaults,uid=1000,gid=1000";
284                         fstype = "hfsplus";
285                 }
286                 if(mount->fs == HFSPLUSJOURNAL)
287                 {
288                         options = "ro,defaults,uid=1000,gid=1000";
289                         fstype = "hfsplus";
290                 }
291                 if(mount->fs == NTFS)
292                 {
293                         options = "force";
294                         fstype = "ntfs-3g";
295                 }
296                 if(mount->fs > MBR && mount->fs <= LASTFS)
297                 {
298                         struct uci_context *ctx;
299                         char *uci_options, *uci_fstype;
300                         ctx = ucix_init("mountd");
301                         if(fs_names[mount->fs])
302                         {
303                                 uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
304                                 uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
305                                 if(uci_options)
306                                         options = uci_options;
307                                 if(uci_fstype)
308                                         fstype = uci_fstype;
309                                 log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
310                                 ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
311                         }
312                         ucix_cleanup(ctx);
313                 }
314                 exit(WEXITSTATUS(ret));
315         }
316         pid = waitpid(pid, &ret, 0);
317         ret = WEXITSTATUS(ret);
318         log_printf("----------> mount ret = %d\n", ret);
319         if (ret && ret != 0xff) {
320                 rmdir(tmp);
321                 return -1;
322         }
323         if(mount_wait_for_disc(mount->dev) == 0)
324         {
325                 mount->status = STATUS_MOUNTED;
326                 mount_dump_uci_state();
327         } else return -1;
328         return 0;
329 }
330
331 int mount_remove(char *path, char *dev)
332 {
333         struct mount *mount;
334         char tmp[256];
335         int ret;
336         snprintf(tmp, 256, "%s%s", path, dev);
337         log_printf("device %s has expired... unmounting %s\n", dev, tmp);
338         ret = system_printf("/bin/umount %s", tmp);
339         if(ret != 0)
340                 return 0;
341         rmdir(tmp);
342         mount = mount_find(0, dev);
343         if(mount)
344                 mount->status = STATUS_EXPIRED;
345         log_printf("finished unmounting\n");
346         mount_dump_uci_state();
347         return 0;
348 }
349
350 static int dir_sort(const struct dirent **a, const struct dirent **b)
351 {
352         return 0;
353 }
354
355 static int dir_filter(const struct dirent *a)
356 {
357         if(strstr(a->d_name, ":"))
358                 return 1;
359         return 0;
360 }
361
362 static char* mount_get_serial(char *dev)
363 {
364         static char tmp[64];
365         static char tmp2[64];
366         int disc;
367         static struct hd_driveid hd;
368         int i;
369         static char *serial;
370         static char disc_id[13];
371         snprintf(tmp, 64, "/dev/%s", dev);
372         disc = open(tmp, O_RDONLY);
373         if(!disc)
374         {
375                 log_printf("Trying to open unknown disc\n");
376                 return 0;
377         }
378         i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
379         close(disc);
380         if(!i)
381                 serial = (char*)hd.serial_no;
382         /* if we failed, it probably a usb storage device */
383         /* there must be a better way for this */
384         if(i)
385         {
386                 struct dirent **namelist;
387                 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
388                 if(n > 0)
389                 {
390                         while(n--)
391                         {
392                                 char *t = strstr(namelist[n]->d_name, ":");
393                                 if(t)
394                                 {
395                                         int id;
396                                         struct stat buf;
397                                         char tmp3[64];
398                                         int ret;
399                                         *t = 0;
400                                         id = atoi(namelist[n]->d_name);
401                                         *t = ':';
402                                         sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
403                                         ret = stat(tmp3, &buf);
404                                         if(ret)
405                                         {
406                                                 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
407                                                 ret = stat(tmp3, &buf);
408                                         }
409                                         if(!ret)
410                                         {
411                                                 FILE *fp;
412                                                 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
413                                                 fp = fopen(tmp2, "r");
414                                                 if(fp)
415                                                 {
416                                                         while(fgets(tmp2, 64, fp) != NULL)
417                                                         {
418                                                                 serial = strstr(tmp2, "Serial Number:");
419                                                                 if(serial)
420                                                                 {
421                                                                         serial += strlen("Serial Number: ");
422                                                                         serial[strlen(serial) - 1] = '\0';
423                                                                         i = 0;
424                                                                         break;
425                                                                 }
426                                                         }
427                                                         fclose(fp);
428                                                 }
429                                         }
430                                 }
431                                 free(namelist[n]);
432                         }
433                         free(namelist);
434                 }
435         }
436         if(i)
437         {
438                 log_printf("could not find a serial number for the device %s\n", dev);
439         } else {
440                 /* serial string id is cheap, but makes the discs anonymous */
441                 unsigned char uniq[6];
442                 unsigned int *u = (unsigned int*) uniq;
443                 int l = strlen(serial);
444                 int i;
445                 memset(disc_id, 0, 13);
446                 memset(uniq, 0, 6);
447                 for(i = 0; i < l; i++)
448                 {
449                         uniq[i%6] += serial[i];
450                 }
451                 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
452                 //log_printf("Serial number - %s %s\n", serial, disc_id);
453                 return disc_id;
454         }
455         sprintf(disc_id, "000000000000");
456         return disc_id;
457 }
458
459 static void mount_dev_add(char *dev)
460 {
461         struct mount *mount = mount_find(0, dev);
462         if(!mount)
463         {
464                 char node[64];
465                 char name[64];
466                 int ignore = 0;
467                 char *s;
468                 char tmp[64];
469                 char tmp2[64];
470                 char *p;
471                 struct uci_context *ctx;
472                 char vendor[64];
473                 char model[64];
474                 char rev[64];
475                 char size[64];
476                 char sector_size[64];
477                 FILE *fp;
478                 int offset = 3;
479                 int fs;
480
481                 strcpy(name, dev);
482                 if (!strncmp(name, "mmcblk", 6))
483                         offset = 7;
484                 name[offset] = '\0';
485                 s = mount_get_serial(name);
486                 if(!s) {
487                         return;
488                 }
489                 if (!strncmp(name, "mmcblk", 6)) {
490                         snprintf(tmp, 64, "part%s", &dev[8]);
491                         snprintf(node, 64, "SD-P%s", &dev[8]);
492
493                 } else {
494                         snprintf(tmp, 64, "part%s", &dev[3]);
495                         snprintf(node, 64, "USB-%s", &dev[2]);
496                 }
497                 if(node[4] >= 'a' && node[4] <= 'z')
498                 {
499                         node[4] -= 'a';
500                         node[4] += 'A';
501                 }
502                 ctx = ucix_init("mountd");
503                 p = ucix_get_option(ctx, "mountd", s, tmp);
504                 ucix_cleanup(ctx);
505                 if(p)
506                 {
507                         if(strlen(p) == 1)
508                         {
509                                 if(*p == '0')
510                                         ignore = 1;
511                         } else {
512                                 snprintf(node, 64, "%s", p);
513                         }
514                 }
515                 strcpy(name, dev);
516                 name[3] = '\0';
517                 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
518                 fp = fopen(tmp, "r");
519                 if(!fp)
520                 {
521                         snprintf(tmp, 64, "/sys/block/%s/device/model", name);
522                         fp = fopen(tmp, "r");
523                 }
524                 if(!fp)
525                         snprintf(model, 64, "unknown");
526                 else {
527                         fgets(model, 64, fp);
528                         model[strlen(model) - 1] = '\0';;
529                         fclose(fp);
530                 }
531                 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
532                 fp = fopen(tmp, "r");
533                 if(!fp)
534                 {
535                         snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
536                         fp = fopen(tmp, "r");
537                 }
538                 if(!fp)
539                         snprintf(vendor, 64, "unknown");
540                 else {
541                         fgets(vendor, 64, fp);
542                         vendor[strlen(vendor) - 1] = '\0';
543                         fclose(fp);
544                 }
545                 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
546                 fp = fopen(tmp, "r");
547                 if(!fp)
548                 {
549                         snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
550                         fp = fopen(tmp, "r");
551                 }
552                 if(!fp)
553                         snprintf(rev, 64, "unknown");
554                 else {
555                         fgets(rev, 64, fp);
556                         rev[strlen(rev) - 1] = '\0';
557                         fclose(fp);
558                 }
559                 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
560                 fp = fopen(tmp, "r");
561                 if(!fp)
562                 {
563                         snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
564                         fp = fopen(tmp, "r");
565                 }
566                 if(!fp)
567                         snprintf(size, 64, "unknown");
568                 else {
569                         fgets(size, 64, fp);
570                         size[strlen(size) - 1] = '\0';
571                         fclose(fp);
572                 }
573                 strcpy(tmp2, dev);
574                 tmp2[3] = '\0';
575                 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
576                 fp = fopen(tmp, "r");
577                 if(!fp)
578                         snprintf(sector_size, 64, "unknown");
579                 else {
580                         fgets(sector_size, 64, fp);
581                         sector_size[strlen(sector_size) - 1] = '\0';
582                         fclose(fp);
583                 }
584                 snprintf(tmp, 64, "/dev/%s", dev);
585                 fs = detect_fs(tmp);
586                 if (fs <= MBR || fs > LASTFS) {
587                         ignore = 1;
588                 }
589                 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
590                 mount_dump_uci_state();
591         }
592 }
593
594 static int mount_dev_del(struct mount *mount)
595 {
596         char tmp[256];
597         int err = 0;
598
599         if (mount->status == STATUS_MOUNTED) {
600                 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
601                 log_printf("device %s has disappeared ... unmounting %s\n", mount->dev, tmp);
602                 if (umount(tmp)) {
603                         err = -errno;
604                         umount2(tmp, MNT_DETACH);
605                 }
606                 rmdir(tmp);
607                 mount_dump_uci_state();
608         }
609
610         return err;
611 }
612
613 void mount_dump_list(void)
614 {
615         struct list_head *p;
616         list_for_each(p, &mounts)
617         {
618                 struct mount *q = container_of(p, struct mount, list);
619                 log_printf("* %s %s %d\n", q->name, q->dev, q->status == STATUS_MOUNTED);
620         }
621 }
622
623 char* is_mounted(char *block, char *path)
624 {
625         int i;
626         for(i = 0; i < mounted_count; i++)
627         {
628                 if(block)
629                         if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
630                                 return &mounted[i][0][0];
631                 if(path)
632                         if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
633                                 return &mounted[i][0][0];
634         }
635         return 0;
636 }
637
638 static void mount_update_mount_list(void)
639 {
640         FILE *fp = fopen("/proc/mounts", "r");
641         char tmp[256];
642
643         if(!fp)
644         {
645                 log_printf("error reading /proc/mounts");
646                 return;
647         }
648         mounted_count = 0;
649         while(fgets(tmp, 256, fp) != NULL)
650         {
651                 char *t, *t2;
652
653                 if (mounted_count + 1 > MAX_MOUNTED) {
654                         log_printf("found more than %d mounts \n", MAX_MOUNTED);
655                         break;
656                 }
657
658                 t = strstr(tmp, " ");
659                 if(t)
660                 {
661                         *t = '\0';
662                         t++;
663                 } else t = tmp;
664                 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
665                 t2 = strstr(t, " ");
666                 if(t2)
667                 {
668                         *t2 = '\0';
669                         t2++;
670                 } else t2 = t;
671                 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
672                 t = strstr(t2, " ");
673                 if(t)
674                 {
675                         *t = '\0';
676                         t++;
677                 } else t = tmp;
678                 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
679         /*      printf("%s %s %s\n",
680                         mounted[mounted_count][0],
681                         mounted[mounted_count][1],
682                         mounted[mounted_count][2]);*/
683
684                 mounted_count++;
685         }
686         fclose(fp);
687 }
688
689 /* FIXME: we need more intelligence here */
690 static int dir_filter2(const struct dirent *a)
691 {
692         if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
693                 return 1;
694         return 0;
695 }
696 #define MAX_BLOCK       64
697 static char block[MAX_BLOCK][MAX_BLOCK];
698 static int blk_cnt = 0;
699
700 static int check_block(char *b)
701 {
702         int i;
703         for(i = 0; i < blk_cnt; i++)
704         {
705                 if(!strcmp(block[i], b))
706                         return 1;
707         }
708         return 0;
709 }
710
711 static void mount_enum_drives(void)
712 {
713         struct dirent **namelist, **namelist2;
714         int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
715         struct list_head *p;
716         blk_cnt = 0;
717         if(n > 0)
718         {
719                 while(n--)
720                 {
721                         if(blk_cnt < MAX_BLOCK)
722                         {
723                                 int m;
724                                 char tmp[64];
725                                 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
726                                 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
727                                 if(m > 0)
728                                 {
729                                         while(m--)
730                                         {
731                                                 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
732                                                 blk_cnt++;
733                                                 free(namelist2[m]);
734                                         }
735                                         free(namelist2);
736                                 } else {
737                                         strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
738                                         blk_cnt++;
739                                 }
740                         }
741                         free(namelist[n]);
742                 }
743                 free(namelist);
744         }
745         p = mounts.next;
746         while(p != &mounts)
747         {
748                 struct mount *q = container_of(p, struct mount, list);
749                 char tmp[64];
750                 struct uci_context *ctx;
751                 int del = 0;
752                 char *t;
753                 snprintf(tmp, 64, "part%s", &q->dev[3]);
754                 ctx = ucix_init("mountd");
755                 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
756                 ucix_cleanup(ctx);
757                 if (t && q->status != STATUS_MOUNTED)
758                 {
759                         if(!strcmp(t, "0"))
760                         {
761                                 if (q->status != STATUS_IGNORE)
762                                         del = 1;
763                         } else if(!strcmp(t, "1"))
764                         {
765                                 if(strncmp(q->name, "Disc-", 5))
766                                         del = 1;
767                         } else if(strcmp(q->name, t))
768                         {
769                                 del = 1;
770                         }
771                 }
772                 if(!check_block(q->dev)||del)
773                 {
774                         if (q->status == STATUS_MOUNTED || q->status == STATUS_EXPIRED) {
775                                 char dev_link[64];
776                                 int err;
777
778                                 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
779
780                                 err = mount_dev_del(q);
781
782                                 snprintf(dev_link, sizeof(dev_link), "%s%s", uci_path, q->name);
783                                 if (err == -EBUSY) {
784                                         /* Create "tmp" symlink to non-existing path */
785                                         snprintf(tmp, sizeof(tmp), "%s%s", uci_path, "tmp");
786                                         symlink("## DEVICE MISSING ##", tmp);
787
788                                         /* Replace old symlink with the not working one */
789                                         rename(tmp, dev_link);
790                                 } else {
791                                         log_printf("unlinking %s\n", dev_link);
792                                         unlink(dev_link);
793                                 }
794                         }
795
796                         p->prev->next = p->next;
797                         p->next->prev = p->prev;
798                         p = p->next;
799                         free(q);
800
801                         mount_dump_uci_state();
802                         system_printf("/etc/fonstated/ReloadSamba");
803                 } else p = p->next;
804         }
805
806         for(i = 0; i < blk_cnt; i++)
807                 mount_dev_add(block[i]);
808 }
809
810 static void mount_check_enum(void)
811 {
812         waitpid(-1, 0, WNOHANG);
813         mount_enum_drives();
814 }
815
816 void mount_init(void)
817 {
818         INIT_LIST_HEAD(&mounts);
819         timer_add(mount_update_mount_list, 2);
820         timer_add(mount_check_enum, 1);
821         mount_update_mount_list();
822 }