6d95de95d1d05aff2dc575e0a5f6d6077a0fcad1
[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 if(fs <= MBR || fs > LASTFS)
143 return;
144 mount = malloc(sizeof(struct mount));
145 INIT_LIST_HEAD(&mount->list);
146 strncpy(mount->vendor, vendor, 64);
147 strncpy(mount->model, model, 64);
148 strncpy(mount->rev, rev, 64);
149 strncpy(mount->name, name, 64);
150 strncpy(mount->dev, dev, 64);
151 strncpy(mount->serial, serial, 64);
152 strncpy(mount->size, size, 64);
153 strncpy(mount->sector_size, sector_size, 64);
154 mount->ignore = ignore;
155 mount->mounted = 0;
156 mount->fs = fs;
157 list_add(&mount->list, &mounts);
158 if (!mount->ignore)
159 {
160 log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
161 snprintf(tmp, 64, "%s%s", uci_path, name);
162 snprintf(tmp2, 64, "/tmp/run/mountd/%s", dev);
163 symlink(tmp2, tmp);
164 mount_new("/tmp/run/mountd/", dev);
165 system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev, name);
166 }
167 }
168
169 static int mount_check_disc(char *disc)
170 {
171 FILE *fp = fopen("/proc/mounts", "r");
172 char tmp[256];
173 int avail = -1;
174 if(!fp)
175 {
176 log_printf("error reading /proc/mounts");
177 return avail;
178 }
179 while((fgets(tmp, 256, fp) != NULL) && (avail == -1))
180 {
181 char *t;
182 char tmp2[32];
183 t = strstr(tmp, " ");
184 if(t)
185 {
186 int l;
187 *t = '\0';
188 l = snprintf(tmp2, 31, "/dev/%s", disc);
189
190 if(!strncmp(tmp, tmp2, l))
191 avail = 0;
192 }
193 }
194 fclose(fp);
195 return avail;
196 }
197
198 static int mount_wait_for_disc(char *disc)
199 {
200 int i = 10;
201 while(i--)
202 {
203 int ret = mount_check_disc(disc);
204 if(!ret)
205 return ret;
206 poll(0, 0, 100);
207 }
208 return -1;
209 }
210
211 int mount_new(char *path, char *dev)
212 {
213 struct mount *mount;
214 char tmp[256];
215 int ret = 1;
216 pid_t pid;
217 mount = mount_find(0, dev);
218 if(!mount)
219 {
220 log_printf("request for invalid path %s%s\n", path, dev);
221 return -1;
222 }
223 if(mount->ignore || mount->mounted || mount->fs == EXTENDED)
224 return -1;
225 snprintf(tmp, 256, "%s%s", path, mount->dev);
226 log_printf("mounting %s\n", tmp);
227 mkdir(tmp, 777);
228
229 pid = autofs_safe_fork();
230 if(!pid)
231 {
232 char *options, *fstype;
233 if(mount->fs == EXFAT)
234 {
235 options = "rw,uid=1000,gid=1000";
236 fstype = "exfat";
237 }
238 if(mount->fs == FAT)
239 {
240 options = "rw,uid=1000,gid=1000";
241 fstype = "vfat";
242 }
243 if(mount->fs == EXT4)
244 {
245 options = "rw,defaults";
246 fstype = "ext4";
247 }
248 if(mount->fs == EXT3)
249 {
250 options = "rw,defaults";
251 fstype = "ext3";
252 }
253 if(mount->fs == EXT2)
254 {
255 options = "rw,defaults";
256 fstype = "ext2";
257 }
258 if(mount->fs == HFSPLUS)
259 {
260 options = "rw,defaults,uid=1000,gid=1000";
261 fstype = "hfsplus";
262 }
263 if(mount->fs == HFSPLUSJOURNAL)
264 {
265 options = "ro,defaults,uid=1000,gid=1000";
266 fstype = "hfsplus";
267 }
268 if(mount->fs == NTFS)
269 {
270 options = "force";
271 fstype = "ntfs-3g";
272 }
273 if(mount->fs > MBR && mount->fs <= LASTFS)
274 {
275 struct uci_context *ctx;
276 char *uci_options, *uci_fstype;
277 ctx = ucix_init("mountd");
278 if(fs_names[mount->fs])
279 {
280 uci_options = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "options");
281 uci_fstype = ucix_get_option(ctx, "mountd", fs_names[mount->fs], "fstype");
282 if(uci_options)
283 options = uci_options;
284 if(uci_fstype)
285 fstype = uci_fstype;
286 log_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
287 ret = system_printf("mount -t %s -o %s /dev/%s %s", fstype, options, mount->dev, tmp);
288 }
289 ucix_cleanup(ctx);
290 }
291 exit(WEXITSTATUS(ret));
292 }
293 pid = waitpid(pid, &ret, 0);
294 ret = WEXITSTATUS(ret);
295 log_printf("----------> mount ret = %d\n", ret);
296 if(ret && (ret != 0xff))
297 return -1;
298 if(mount_wait_for_disc(mount->dev) == 0)
299 {
300 mount->mounted = 1;
301 mount_dump_uci_state();
302 } else return -1;
303 return 0;
304 }
305
306 int mount_remove(char *path, char *dev)
307 {
308 struct mount *mount;
309 char tmp[256];
310 int ret;
311 snprintf(tmp, 256, "%s%s", path, dev);
312 log_printf("%s has expired... unmounting\n", tmp);
313 ret = system_printf("/bin/umount %s", tmp);
314 if(ret != 0)
315 return 0;
316 rmdir(tmp);
317 mount = mount_find(0, dev);
318 if(mount)
319 mount->mounted = 0;
320 log_printf("finished unmounting\n");
321 mount_dump_uci_state();
322 return 0;
323 }
324
325 static int dir_sort(const struct dirent **a, const struct dirent **b)
326 {
327 return 0;
328 }
329
330 static int dir_filter(const struct dirent *a)
331 {
332 if(strstr(a->d_name, ":"))
333 return 1;
334 return 0;
335 }
336
337 static char* mount_get_serial(char *dev)
338 {
339 static char tmp[64];
340 static char tmp2[64];
341 int disc;
342 static struct hd_driveid hd;
343 int i;
344 static char *serial;
345 static char disc_id[13];
346 snprintf(tmp, 64, "/dev/%s", dev);
347 disc = open(tmp, O_RDONLY);
348 if(!disc)
349 {
350 log_printf("Trying to open unknown disc\n");
351 return 0;
352 }
353 i = ioctl(disc, HDIO_GET_IDENTITY, &hd);
354 close(disc);
355 if(!i)
356 serial = (char*)hd.serial_no;
357 /* if we failed, it probably a usb storage device */
358 /* there must be a better way for this */
359 if(i)
360 {
361 struct dirent **namelist;
362 int n = scandir("/sys/bus/scsi/devices/", &namelist, dir_filter, dir_sort);
363 if(n > 0)
364 {
365 while(n--)
366 {
367 char *t = strstr(namelist[n]->d_name, ":");
368 if(t)
369 {
370 int id;
371 struct stat buf;
372 char tmp3[64];
373 int ret;
374 *t = 0;
375 id = atoi(namelist[n]->d_name);
376 *t = ':';
377 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block:%s/", namelist[n]->d_name, dev);
378 ret = stat(tmp3, &buf);
379 if(ret)
380 {
381 sprintf(tmp3, "/sys/bus/scsi/devices/%s/block/%s/", namelist[n]->d_name, dev);
382 ret = stat(tmp3, &buf);
383 }
384 if(!ret)
385 {
386 FILE *fp;
387 snprintf(tmp2, 64, "/proc/scsi/usb-storage/%d", id);
388 fp = fopen(tmp2, "r");
389 if(fp)
390 {
391 while(fgets(tmp2, 64, fp) != NULL)
392 {
393 serial = strstr(tmp2, "Serial Number:");
394 if(serial)
395 {
396 serial += strlen("Serial Number: ");
397 serial[strlen(serial) - 1] = '\0';
398 i = 0;
399 break;
400 }
401 }
402 fclose(fp);
403 }
404 }
405 }
406 free(namelist[n]);
407 }
408 free(namelist);
409 }
410 }
411 if(i)
412 {
413 log_printf("could not find a serial number for the device %s\n", dev);
414 } else {
415 /* serial string id is cheap, but makes the discs anonymous */
416 unsigned char uniq[6];
417 unsigned int *u = (unsigned int*) uniq;
418 int l = strlen(serial);
419 int i;
420 memset(disc_id, 0, 13);
421 memset(uniq, 0, 6);
422 for(i = 0; i < l; i++)
423 {
424 uniq[i%6] += serial[i];
425 }
426 sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
427 //log_printf("Serial number - %s %s\n", serial, disc_id);
428 return disc_id;
429 }
430 sprintf(disc_id, "000000000000");
431 return disc_id;
432 }
433
434 static void mount_dev_add(char *dev)
435 {
436 struct mount *mount = mount_find(0, dev);
437 if(!mount)
438 {
439 char node[64];
440 char name[64];
441 int ignore = 0;
442 char *s;
443 char tmp[64];
444 char tmp2[64];
445 char *p;
446 struct uci_context *ctx;
447 char vendor[64];
448 char model[64];
449 char rev[64];
450 char size[64];
451 char sector_size[64];
452 FILE *fp;
453 int offset = 3;
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 mount_add_list(node, dev, s, vendor, model, rev, ignore, size, sector_size, detect_fs(tmp));
560 mount_dump_uci_state();
561 }
562 }
563
564 static void mount_dev_del(char *dev)
565 {
566 struct mount *mount = mount_find(0, dev);
567 char tmp[256];
568 if(mount)
569 {
570 if(mount->mounted)
571 {
572 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->name);
573 log_printf("%s has dissappeared ... unmounting\n", tmp);
574 snprintf(tmp, 256, "%s%s", "/tmp/run/mountd/", mount->dev);
575 system_printf("/bin/umount %s", tmp);
576 rmdir(tmp);
577 snprintf(tmp, 64, "%s%s", uci_path, mount->name);
578 unlink(tmp);
579 mount_dump_uci_state();
580 }
581 }
582 }
583
584 void mount_dump_list(void)
585 {
586 struct list_head *p;
587 list_for_each(p, &mounts)
588 {
589 struct mount *q = container_of(p, struct mount, list);
590 log_printf("* %s %s %d\n", q->name, q->dev, q->mounted);
591 }
592 }
593
594 char* is_mounted(char *block, char *path)
595 {
596 int i;
597 for(i = 0; i < mounted_count; i++)
598 {
599 if(block)
600 if(!strncmp(&mounted[i][0][0], block, strlen(&mounted[i][0][0])))
601 return &mounted[i][0][0];
602 if(path)
603 if(!strncmp(&mounted[i][1][1], &path[1], strlen(&mounted[i][1][0])))
604 return &mounted[i][0][0];
605 }
606 return 0;
607 }
608
609 static void mount_check_mount_list(void)
610 {
611 FILE *fp = fopen("/proc/mounts", "r");
612 char tmp[256];
613
614 if(!fp)
615 {
616 log_printf("error reading /proc/mounts");
617 return;
618 }
619 mounted_count = 0;
620 while(fgets(tmp, 256, fp) != NULL)
621 {
622 char *t, *t2;
623 t = strstr(tmp, " ");
624 if(t)
625 {
626 *t = '\0';
627 t++;
628 } else t = tmp;
629 strncpy(&mounted[mounted_count][0][0], tmp, MAX_MOUNT_NAME);
630 t2 = strstr(t, " ");
631 if(t2)
632 {
633 *t2 = '\0';
634 t2++;
635 } else t2 = t;
636 strncpy(&mounted[mounted_count][1][0], t, MAX_MOUNT_NAME);
637 t = strstr(t2, " ");
638 if(t)
639 {
640 *t = '\0';
641 t++;
642 } else t = tmp;
643 strncpy(&mounted[mounted_count][2][0], t2, MAX_MOUNT_NAME);
644 /* printf("%s %s %s\n",
645 mounted[mounted_count][0],
646 mounted[mounted_count][1],
647 mounted[mounted_count][2]);*/
648 if(mounted_count < MAX_MOUNTED - 1)
649 mounted_count++;
650 else
651 log_printf("found more than %d mounts \n", MAX_MOUNTED);
652 }
653 fclose(fp);
654 }
655
656 /* FIXME: we need more intelligence here */
657 static int dir_filter2(const struct dirent *a)
658 {
659 if(!strncmp(a->d_name, "mmcblk", 6) || !strncmp(a->d_name, "sd", 2))
660 return 1;
661 return 0;
662 }
663 #define MAX_BLOCK 64
664 static char block[MAX_BLOCK][MAX_BLOCK];
665 static int blk_cnt = 0;
666
667 static int check_block(char *b)
668 {
669 int i;
670 for(i = 0; i < blk_cnt; i++)
671 {
672 if(!strcmp(block[i], b))
673 return 1;
674 }
675 return 0;
676 }
677
678 static void mount_enum_drives(void)
679 {
680 struct dirent **namelist, **namelist2;
681 int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
682 struct list_head *p;
683 blk_cnt = 0;
684 if(n > 0)
685 {
686 while(n--)
687 {
688 if(blk_cnt < MAX_BLOCK)
689 {
690 int m;
691 char tmp[64];
692 snprintf(tmp, 64, "/sys/block/%s/", namelist[n]->d_name);
693 m = scandir(tmp, &namelist2, dir_filter2, dir_sort);
694 if(m > 0)
695 {
696 while(m--)
697 {
698 strncpy(&block[blk_cnt][0], namelist2[m]->d_name, MAX_BLOCK);
699 blk_cnt++;
700 free(namelist2[m]);
701 }
702 free(namelist2);
703 } else {
704 strncpy(&block[blk_cnt][0], namelist[n]->d_name, MAX_BLOCK);
705 blk_cnt++;
706 }
707 }
708 free(namelist[n]);
709 }
710 free(namelist);
711 }
712 p = mounts.next;
713 while(p != &mounts)
714 {
715 struct mount *q = container_of(p, struct mount, list);
716 char tmp[64];
717 struct uci_context *ctx;
718 int del = 0;
719 char *t;
720 snprintf(tmp, 64, "part%s", &q->dev[3]);
721 ctx = ucix_init("mountd");
722 t = ucix_get_option(ctx, "mountd", q->serial, tmp);
723 ucix_cleanup(ctx);
724 if(t && !q->mounted)
725 {
726 if(!strcmp(t, "0"))
727 {
728 if(!q->ignore)
729 del = 1;
730 } else if(!strcmp(t, "1"))
731 {
732 if(strncmp(q->name, "Disc-", 5))
733 del = 1;
734 } else if(strcmp(q->name, t))
735 {
736 del = 1;
737 }
738 }
739 if(!check_block(q->dev)||del)
740 {
741 mount_dev_del(q->dev);
742 p->prev->next = p->next;
743 p->next->prev = p->prev;
744 p = p->next;
745 log_printf("removing %s\n", q->dev);
746 snprintf(tmp, 64, "%s%s", "/tmp/run/mountd/", q->dev);
747 rmdir(tmp);
748 snprintf(tmp, 64, "%s%s", uci_path, q->name);
749 unlink(tmp);
750 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q->dev, q->name);
751 free(q);
752 mount_dump_uci_state();
753 system_printf("/etc/fonstated/ReloadSamba");
754 } else p = p->next;
755 }
756
757 for(i = 0; i < blk_cnt; i++)
758 mount_dev_add(block[i]);
759 }
760
761 static void mount_check_enum(void)
762 {
763 waitpid(-1, 0, WNOHANG);
764 mount_enum_drives();
765 }
766
767 void mount_init(void)
768 {
769 INIT_LIST_HEAD(&mounts);
770 timer_add(mount_check_mount_list, 2);
771 timer_add(mount_check_enum, 1);
772 mount_check_mount_list();
773 }