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