803acc1fa8c7b644da82fb4d1bdf3573d59cf9f1
[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                 return -1;
297         if(mount_wait_for_disc(mount->dev) == 0)
298         {
299                 mount->mounted = 1;
300                 mount_dump_uci_state();
301         } else return -1;
302         return 0;
303 }
304
305 int mount_remove(char *path, char *dev)
306 {
307         struct mount *mount;
308         char tmp[256];
309         int ret;
310         snprintf(tmp, 256, "%s%s", path, dev);
311         log_printf("%s has expired... unmounting\n", tmp);
312         ret = system_printf("/bin/umount %s", tmp);
313         if(ret != 0)
314                 return 0;
315         rmdir(tmp);
316         mount = mount_find(0, dev);
317         if(mount)
318                 mount->mounted = 0;
319         log_printf("finished unmounting\n");
320         mount_dump_uci_state();
321         return 0;
322 }
323
324 static int dir_sort(const struct dirent **a, const struct dirent **b)
325 {
326         return 0;
327 }
328
329 static int dir_filter(const struct dirent *a)
330 {
331         if(strstr(a->d_name, ":"))
332                 return 1;
333         return 0;
334 }
335
336 static char* mount_get_serial(char *dev)
337 {
338         static char tmp[64];
339         static char tmp2[64];
340         int disc;
341         static struct hd_driveid hd;
342         int i;
343         static char *serial;
344         static char disc_id[13];
345         snprintf(tmp, 64, "/dev/%s", dev);
346         disc = open(tmp, O_RDONLY);
347         if(!disc)
348         {
349                 log_printf("Trying to open unknown disc\n");
350                 return 0;
351         }
352         i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
353         close(disc);
354         if(!i)
355                 serial = (char*)hd.serial_no;
356         /* if we failed, it probably a usb storage device */
357         /* there must be a better way for this */
358         if(i)
359         {
360                 struct dirent **namelist;
361                 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
362                 if(n > 0)
363                 {
364                         while(n--)
365                         {
366                                 char *t = strstr(namelist[n]->d_name, ":");
367                                 if(t)
368                                 {
369                                         int id;
370                                         struct stat buf;
371                                         char tmp3[64];
372                                         int ret;
373                                         *t = 0;
374                                         id = atoi(namelist[n]->d_name);
375                                         *t = ':';
376                                         sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
377                                         ret = stat(tmp3, &buf);
378                                         if(ret)
379                                         {
380                                                 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
381                                                 ret = stat(tmp3, &buf);
382                                         }
383                                         if(!ret)
384                                         {
385                                                 FILE *fp;
386                                                 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
387                                                 fp = fopen(tmp2, "r");
388                                                 if(fp)
389                                                 {
390                                                         while(fgets(tmp2, 64, fp) != NULL)
391                                                         {
392                                                                 serial = strstr(tmp2, "Serial Number:");
393                                                                 if(serial)
394                                                                 {
395                                                                         serial += strlen("Serial Number: ");
396                                                                         serial[strlen(serial) - 1] = '\0';
397                                                                         i = 0;
398                                                                         break;
399                                                                 }
400                                                         }
401                                                         fclose(fp);
402                                                 }
403                                         }
404                                 }
405                                 free(namelist[n]);
406                         }
407                         free(namelist);
408                 }
409         }
410         if(i)
411         {
412                 log_printf("could not find a serial number for the device %s\n", dev);
413         } else {
414                 /* serial string id is cheap, but makes the discs anonymous */
415                 unsigned char uniq[6];
416                 unsigned int *u = (unsigned int*) uniq;
417                 int l = strlen(serial);
418                 int i;
419                 memset(disc_id, 0, 13);
420                 memset(uniq, 0, 6);
421                 for(i = 0; i < l; i++)
422                 {
423                         uniq[i%6] += serial[i];
424                 }
425                 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
426                 //log_printf("Serial number - %s %s\n", serial, disc_id);
427                 return disc_id;
428         }
429         sprintf(disc_id, "000000000000");
430         return disc_id;
431 }
432
433 static void mount_dev_add(char *dev)
434 {
435         struct mount *mount = mount_find(0, dev);
436         if(!mount)
437         {
438                 char node[64];
439                 char name[64];
440                 int ignore = 0;
441                 char *s;
442                 char tmp[64];
443                 char tmp2[64];
444                 char *p;
445                 struct uci_context *ctx;
446                 char vendor[64];
447                 char model[64];
448                 char rev[64];
449                 char size[64];
450                 char sector_size[64];
451                 FILE *fp;
452                 int offset = 3;
453                 int fs;
454
455                 strcpy(name, dev);
456                 if (!strncmp(name, "mmcblk", 6))
457                         offset = 7;
458                 name[offset] = '\0';
459                 s = mount_get_serial(name);
460                 if(!s) {
461                         return;
462                 }
463                 if (!strncmp(name, "mmcblk", 6)) {
464                         snprintf(tmp, 64, "part%s", &dev[8]);
465                         snprintf(node, 64, "SD-P%s", &dev[8]);
466
467                 } else {
468                         snprintf(tmp, 64, "part%s", &dev[3]);
469                         snprintf(node, 64, "USB-%s", &dev[2]);
470                 }
471                 if(node[4] >= 'a' && node[4] <= 'z')
472                 {
473                         node[4] -= 'a';
474                         node[4] += 'A';
475                 }
476                 ctx = ucix_init("mountd");
477                 p = ucix_get_option(ctx, "mountd", s, tmp);
478                 ucix_cleanup(ctx);
479                 if(p)
480                 {
481                         if(strlen(p) == 1)
482                         {
483                                 if(*p == '0')
484                                         ignore = 1;
485                         } else {
486                                 snprintf(node, 64, "%s", p);
487                         }
488                 }
489                 strcpy(name, dev);
490                 name[3] = '\0';
491                 snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
492                 fp = fopen(tmp, "r");
493                 if(!fp)
494                 {
495                         snprintf(tmp, 64, "/sys/block/%s/device/model", name);
496                         fp = fopen(tmp, "r");
497                 }
498                 if(!fp)
499                         snprintf(model, 64, "unknown");
500                 else {
501                         fgets(model, 64, fp);
502                         model[strlen(model) - 1] = '\0';;
503                         fclose(fp);
504                 }
505                 snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
506                 fp = fopen(tmp, "r");
507                 if(!fp)
508                 {
509                         snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
510                         fp = fopen(tmp, "r");
511                 }
512                 if(!fp)
513                         snprintf(vendor, 64, "unknown");
514                 else {
515                         fgets(vendor, 64, fp);
516                         vendor[strlen(vendor) - 1] = '\0';
517                         fclose(fp);
518                 }
519                 snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
520                 fp = fopen(tmp, "r");
521                 if(!fp)
522                 {
523                         snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
524                         fp = fopen(tmp, "r");
525                 }
526                 if(!fp)
527                         snprintf(rev, 64, "unknown");
528                 else {
529                         fgets(rev, 64, fp);
530                         rev[strlen(rev) - 1] = '\0';
531                         fclose(fp);
532                 }
533                 snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
534                 fp = fopen(tmp, "r");
535                 if(!fp)
536                 {
537                         snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
538                         fp = fopen(tmp, "r");
539                 }
540                 if(!fp)
541                         snprintf(size, 64, "unknown");
542                 else {
543                         fgets(size, 64, fp);
544                         size[strlen(size) - 1] = '\0';
545                         fclose(fp);
546                 }
547                 strcpy(tmp2, dev);
548                 tmp2[3] = '\0';
549                 snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
550                 fp = fopen(tmp, "r");
551                 if(!fp)
552                         snprintf(sector_size, 64, "unknown");
553                 else {
554                         fgets(sector_size, 64, fp);
555                         sector_size[strlen(sector_size) - 1] = '\0';
556                         fclose(fp);
557                 }
558                 snprintf(tmp, 64, "/dev/%s", dev);
559                 fs = detect_fs(tmp);
560                 if (fs <= MBR || fs > LASTFS) {
561                         ignore = 1;
562                 }
563                 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, fs);
564                 mount_dump_uci_state();
565         }
566 }
567
568 static void mount_dev_del(char *dev)
569 {
570         struct mount *mount = mount_find(0, dev);
571         char tmp[256];
572         if(mount)
573         {
574                 if(mount->mounted)
575                 {
576                         snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->name);
577                         log_printf("%s has dissappeared ... unmounting\n", tmp);
578                         snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
579                         system_printf("/bin/umount %s", tmp);
580                         rmdir(tmp);
581                         snprintf(tmp, 64, "%s%s", uci_path, mount->name);
582                         unlink(tmp);
583                         mount_dump_uci_state();
584                 }
585         }
586 }
587
588 void mount_dump_list(void)
589 {
590         struct list_head *p;
591         list_for_each(p, &mounts)
592         {
593                 struct mount *q = container_of(p, struct mount, list);
594                 log_printf("* %s %s %d\n", q->name, q->dev, q->mounted);
595         }
596 }
597
598 char* is_mounted(char *block, char *path)
599 {
600         int i;
601         for(i = 0; i < mounted_count; i++)
602         {
603                 if(block)
604                         if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
605                                 return &mounted[i][0][0];
606                 if(path)
607                         if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
608                                 return &mounted[i][0][0];
609         }
610         return 0;
611 }
612
613 static void mount_check_mount_list(void)
614 {
615         FILE *fp = fopen("/proc/mounts", "r");
616         char tmp[256];
617
618         if(!fp)
619         {
620                 log_printf("error reading /proc/mounts");
621                 return;
622         }
623         mounted_count = 0;
624         while(fgets(tmp, 256, fp) != NULL)
625         {
626                 char *t, *t2;
627                 t = strstr(tmp, " ");
628                 if(t)
629                 {
630                         *t = '\0';
631                         t++;
632                 } else t = tmp;
633                 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
634                 t2 = strstr(t, " ");
635                 if(t2)
636                 {
637                         *t2 = '\0';
638                         t2++;
639                 } else t2 = t;
640                 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
641                 t = strstr(t2, " ");
642                 if(t)
643                 {
644                         *t = '\0';
645                         t++;
646                 } else t = tmp;
647                 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
648         /*      printf("%s %s %s\n",
649                         mounted[mounted_count][0],
650                         mounted[mounted_count][1],
651                         mounted[mounted_count][2]);*/
652                 if(mounted_count < MAX_MOUNTED - 1)
653                         mounted_count++;
654                 else
655                         log_printf("found more than %d mounts \n", MAX_MOUNTED);
656         }
657         fclose(fp);
658 }
659
660 /* FIXME: we need more intelligence here */
661 static int dir_filter2(const struct dirent *a)
662 {
663         if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
664                 return 1;
665         return 0;
666 }
667 #define MAX_BLOCK       64
668 static char block[MAX_BLOCK][MAX_BLOCK];
669 static int blk_cnt = 0;
670
671 static int check_block(char *b)
672 {
673         int i;
674         for(i = 0; i < blk_cnt; i++)
675         {
676                 if(!strcmp(block[i], b))
677                         return 1;
678         }
679         return 0;
680 }
681
682 static void mount_enum_drives(void)
683 {
684         struct dirent **namelist, **namelist2;
685         int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
686         struct list_head *p;
687         blk_cnt = 0;
688         if(n > 0)
689         {
690                 while(n--)
691                 {
692                         if(blk_cnt < MAX_BLOCK)
693                         {
694                                 int m;
695                                 char tmp[64];
696                                 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
697                                 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
698                                 if(m > 0)
699                                 {
700                                         while(m--)
701                                         {
702                                                 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
703                                                 blk_cnt++;
704                                                 free(namelist2[m]);
705                                         }
706                                         free(namelist2);
707                                 } else {
708                                         strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
709                                         blk_cnt++;
710                                 }
711                         }
712                         free(namelist[n]);
713                 }
714                 free(namelist);
715         }
716         p = mounts.next;
717         while(p != &mounts)
718         {
719                 struct mount *q = container_of(p, struct mount, list);
720                 char tmp[64];
721                 struct uci_context *ctx;
722                 int del = 0;
723                 char *t;
724                 snprintf(tmp, 64, "part%s", &q->dev[3]);
725                 ctx = ucix_init("mountd");
726                 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
727                 ucix_cleanup(ctx);
728                 if(t && !q->mounted)
729                 {
730                         if(!strcmp(t, "0"))
731                         {
732                                 if(!q->ignore)
733                                         del = 1;
734                         } else if(!strcmp(t, "1"))
735                         {
736                                 if(strncmp(q->name, "Disc-", 5))
737                                         del = 1;
738                         } else if(strcmp(q->name, t))
739                         {
740                                 del = 1;
741                         }
742                 }
743                 if(!check_block(q->dev)||del)
744                 {
745                         mount_dev_del(q->dev);
746                         p->prev->next = p->next;
747                         p->next->prev = p->prev;
748                         p = p->next;
749                         log_printf("removing %s\n", q->dev);
750                         snprintf(tmp, 64, "%s%s", "/tmp/run/mountd/", q->dev);
751                         rmdir(tmp);
752                         snprintf(tmp, 64, "%s%s", uci_path, q->name);
753                         unlink(tmp);
754                         system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
755                         free(q);
756                         mount_dump_uci_state();
757                         system_printf("/etc/fonstated/ReloadSamba");
758                 } else p = p->next;
759         }
760
761         for(i = 0; i < blk_cnt; i++)
762                 mount_dev_add(block[i]);
763 }
764
765 static void mount_check_enum(void)
766 {
767         waitpid(-1, 0, WNOHANG);
768         mount_enum_drives();
769 }
770
771 void mount_init(void)
772 {
773         INIT_LIST_HEAD(&mounts);
774         timer_add(mount_check_mount_list, 2);
775         timer_add(mount_check_enum, 1);
776         mount_check_mount_list();
777 }