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