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