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