803acc1fa8c7b644da82fb4d1bdf3573d59cf9f1
9 #include <linux/hdreg.h>
13 #include <sys/inotify.h>
15 #include <sys/types.h>
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"
32 int mount_new(char *path
, char *dev
);
34 static struct list_head mounts
;
37 struct list_head list
;
51 static char *fs_names
[] = {
67 #define MAX_MOUNTED 32
68 #define MAX_MOUNT_NAME 32
70 static char mounted
[MAX_MOUNTED
][3][MAX_MOUNT_NAME
];
71 static int mounted_count
= 0;
72 extern char uci_path
[32];
74 static void mount_dump_uci_state(void)
76 struct uci_context
*ctx
;
78 char mountd
[] = {"mountd"};
79 char type
[] = {"mountd_disc"};
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
)
88 struct mount
*q
= container_of(p
, struct mount
, list
);
92 ucix_add_section(ctx
, mountd
, q
->serial
, type
);
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
)
106 snprintf(t
, 64, "fs%d", atoi(&q
->dev
[3]));
107 ucix_add_option(ctx
, mountd
, q
->serial
, t
, fs_names
[q
->fs
]);
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
)));
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");
121 static struct mount
* mount_find(char *name
, char *dev
)
124 list_for_each(p
, &mounts
)
126 struct mount
*q
= container_of(p
, struct mount
, list
);
128 if(!strcmp(q
->name
, name
))
131 if(!strcmp(q
->dev
, dev
))
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
)
141 char tmp
[64], tmp2
[64];
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
;
156 list_add(&mount
->list
, &mounts
);
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
);
163 if (!mount_new("/tmp/run/mountd/", dev
))
164 system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev
, name
);
168 static int mount_check_disc(char *disc
)
170 FILE *fp
= fopen("/proc/mounts", "r");
175 log_printf("error reading /proc/mounts");
178 while((fgets(tmp
, 256, fp
) != NULL
) && (avail
== -1))
182 t
= strstr(tmp
, " ");
187 l
= snprintf(tmp2
, 31, "/dev/%s", disc
);
189 if(!strncmp(tmp
, tmp2
, l
))
197 static int mount_wait_for_disc(char *disc
)
202 int ret
= mount_check_disc(disc
);
210 int mount_new(char *path
, char *dev
)
216 mount
= mount_find(0, dev
);
219 log_printf("request for invalid path %s%s\n", path
, dev
);
222 if(mount
->ignore
|| mount
->mounted
|| mount
->fs
== EXTENDED
)
224 snprintf(tmp
, 256, "%s%s", path
, mount
->dev
);
225 log_printf("mounting %s\n", tmp
);
228 pid
= autofs_safe_fork();
231 char *options
, *fstype
;
232 if(mount
->fs
== EXFAT
)
234 options
= "rw,uid=1000,gid=1000";
239 options
= "rw,uid=1000,gid=1000";
242 if(mount
->fs
== EXT4
)
244 options
= "rw,defaults";
247 if(mount
->fs
== EXT3
)
249 options
= "rw,defaults";
252 if(mount
->fs
== EXT2
)
254 options
= "rw,defaults";
257 if(mount
->fs
== HFSPLUS
)
259 options
= "rw,defaults,uid=1000,gid=1000";
262 if(mount
->fs
== HFSPLUSJOURNAL
)
264 options
= "ro,defaults,uid=1000,gid=1000";
267 if(mount
->fs
== NTFS
)
272 if(mount
->fs
> MBR
&& mount
->fs
<= LASTFS
)
274 struct uci_context
*ctx
;
275 char *uci_options
, *uci_fstype
;
276 ctx
= ucix_init("mountd");
277 if(fs_names
[mount
->fs
])
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");
282 options
= uci_options
;
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
);
290 exit(WEXITSTATUS(ret
));
292 pid
= waitpid(pid
, &ret
, 0);
293 ret
= WEXITSTATUS(ret
);
294 log_printf("----------> mount ret = %d\n", ret
);
295 if(ret
&& (ret
!= 0xff))
297 if(mount_wait_for_disc(mount
->dev
) == 0)
300 mount_dump_uci_state();
305 int mount_remove(char *path
, char *dev
)
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
);
316 mount
= mount_find(0, dev
);
319 log_printf("finished unmounting\n");
320 mount_dump_uci_state();
324 static int dir_sort(const struct dirent
**a
, const struct dirent
**b
)
329 static int dir_filter(const struct dirent
*a
)
331 if(strstr(a
->d_name
, ":"))
336 static char* mount_get_serial(char *dev
)
339 static char tmp2
[64];
341 static struct hd_driveid hd
;
344 static char disc_id
[13];
345 snprintf(tmp
, 64, "/dev/%s", dev
);
346 disc
= open(tmp
, O_RDONLY
);
349 log_printf("Trying to open unknown disc\n");
352 i
= ioctl(disc
, HDIO_GET_IDENTITY
, &hd
);
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 */
360 struct dirent
**namelist
;
361 int n
= scandir("/sys/bus/scsi/devices/", &namelist
, dir_filter
, dir_sort
);
366 char *t
= strstr(namelist
[n
]->d_name
, ":");
374 id
= atoi(namelist
[n
]->d_name
);
376 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block:%s/", namelist
[n
]->d_name
, dev
);
377 ret
= stat(tmp3
, &buf
);
380 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block/%s/", namelist
[n
]->d_name
, dev
);
381 ret
= stat(tmp3
, &buf
);
386 snprintf(tmp2
, 64, "/proc/scsi/usb-storage/%d", id
);
387 fp
= fopen(tmp2
, "r");
390 while(fgets(tmp2
, 64, fp
) != NULL
)
392 serial
= strstr(tmp2
, "Serial Number:");
395 serial
+= strlen("Serial Number: ");
396 serial
[strlen(serial
) - 1] = '\0';
412 log_printf("could not find a serial number for the device %s\n", dev
);
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
);
419 memset(disc_id
, 0, 13);
421 for(i
= 0; i
< l
; i
++)
423 uniq
[i
%6] += serial
[i
];
425 sprintf(disc_id
, "%08X%02X%02X", *u
, uniq
[4], uniq
[5]);
426 //log_printf("Serial number - %s %s\n", serial, disc_id);
429 sprintf(disc_id
, "000000000000");
433 static void mount_dev_add(char *dev
)
435 struct mount
*mount
= mount_find(0, dev
);
445 struct uci_context
*ctx
;
450 char sector_size
[64];
456 if (!strncmp(name
, "mmcblk", 6))
459 s
= mount_get_serial(name
);
463 if (!strncmp(name
, "mmcblk", 6)) {
464 snprintf(tmp
, 64, "part%s", &dev
[8]);
465 snprintf(node
, 64, "SD-P%s", &dev
[8]);
468 snprintf(tmp
, 64, "part%s", &dev
[3]);
469 snprintf(node
, 64, "USB-%s", &dev
[2]);
471 if(node
[4] >= 'a' && node
[4] <= 'z')
476 ctx
= ucix_init("mountd");
477 p
= ucix_get_option(ctx
, "mountd", s
, tmp
);
486 snprintf(node
, 64, "%s", p
);
491 snprintf(tmp
, 64, "/sys/class/block/%s/device/model", name
);
492 fp
= fopen(tmp
, "r");
495 snprintf(tmp
, 64, "/sys/block/%s/device/model", name
);
496 fp
= fopen(tmp
, "r");
499 snprintf(model
, 64, "unknown");
501 fgets(model
, 64, fp
);
502 model
[strlen(model
) - 1] = '\0';;
505 snprintf(tmp
, 64, "/sys/class/block/%s/device/vendor", name
);
506 fp
= fopen(tmp
, "r");
509 snprintf(tmp
, 64, "/sys/block/%s/device/vendor", name
);
510 fp
= fopen(tmp
, "r");
513 snprintf(vendor
, 64, "unknown");
515 fgets(vendor
, 64, fp
);
516 vendor
[strlen(vendor
) - 1] = '\0';
519 snprintf(tmp
, 64, "/sys/class/block/%s/device/rev", name
);
520 fp
= fopen(tmp
, "r");
523 snprintf(tmp
, 64, "/sys/block/%s/device/rev", name
);
524 fp
= fopen(tmp
, "r");
527 snprintf(rev
, 64, "unknown");
530 rev
[strlen(rev
) - 1] = '\0';
533 snprintf(tmp
, 64, "/sys/class/block/%s/size", dev
);
534 fp
= fopen(tmp
, "r");
537 snprintf(tmp
, 64, "/sys/block/%s/%s/size", name
, dev
);
538 fp
= fopen(tmp
, "r");
541 snprintf(size
, 64, "unknown");
544 size
[strlen(size
) - 1] = '\0';
549 snprintf(tmp
, 64, "/sys/block/%s/queue/hw_sector_size", tmp2
);
550 fp
= fopen(tmp
, "r");
552 snprintf(sector_size
, 64, "unknown");
554 fgets(sector_size
, 64, fp
);
555 sector_size
[strlen(sector_size
) - 1] = '\0';
558 snprintf(tmp
, 64, "/dev/%s", dev
);
560 if (fs
<= MBR
|| fs
> LASTFS
) {
563 mount_add_list(node
, dev
, s
, vendor
, model
, rev
, ignore
, size
, sector_size
, fs
);
564 mount_dump_uci_state();
568 static void mount_dev_del(char *dev
)
570 struct mount
*mount
= mount_find(0, dev
);
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
);
581 snprintf(tmp
, 64, "%s%s", uci_path
, mount
->name
);
583 mount_dump_uci_state();
588 void mount_dump_list(void)
591 list_for_each(p
, &mounts
)
593 struct mount
*q
= container_of(p
, struct mount
, list
);
594 log_printf("* %s %s %d\n", q
->name
, q
->dev
, q
->mounted
);
598 char* is_mounted(char *block
, char *path
)
601 for(i
= 0; i
< mounted_count
; i
++)
604 if(!strncmp(&mounted
[i
][0][0], block
, strlen(&mounted
[i
][0][0])))
605 return &mounted
[i
][0][0];
607 if(!strncmp(&mounted
[i
][1][1], &path
[1], strlen(&mounted
[i
][1][0])))
608 return &mounted
[i
][0][0];
613 static void mount_check_mount_list(void)
615 FILE *fp
= fopen("/proc/mounts", "r");
620 log_printf("error reading /proc/mounts");
624 while(fgets(tmp
, 256, fp
) != NULL
)
627 t
= strstr(tmp
, " ");
633 strncpy(&mounted
[mounted_count
][0][0], tmp
, MAX_MOUNT_NAME
);
640 strncpy(&mounted
[mounted_count
][1][0], t
, MAX_MOUNT_NAME
);
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)
655 log_printf("found more than %d mounts \n", MAX_MOUNTED
);
660 /* FIXME: we need more intelligence here */
661 static int dir_filter2(const struct dirent
*a
)
663 if(!strncmp(a
->d_name
, "mmcblk", 6) || !strncmp(a
->d_name
, "sd", 2))
668 static char block
[MAX_BLOCK
][MAX_BLOCK
];
669 static int blk_cnt
= 0;
671 static int check_block(char *b
)
674 for(i
= 0; i
< blk_cnt
; i
++)
676 if(!strcmp(block
[i
], b
))
682 static void mount_enum_drives(void)
684 struct dirent
**namelist
, **namelist2
;
685 int i
, n
= scandir("/sys/block/", &namelist
, dir_filter2
, dir_sort
);
692 if(blk_cnt
< MAX_BLOCK
)
696 snprintf(tmp
, 64, "/sys/block/%s/", namelist
[n
]->d_name
);
697 m
= scandir(tmp
, &namelist2
, dir_filter2
, dir_sort
);
702 strncpy(&block
[blk_cnt
][0], namelist2
[m
]->d_name
, MAX_BLOCK
);
708 strncpy(&block
[blk_cnt
][0], namelist
[n
]->d_name
, MAX_BLOCK
);
719 struct mount
*q
= container_of(p
, struct mount
, list
);
721 struct uci_context
*ctx
;
724 snprintf(tmp
, 64, "part%s", &q
->dev
[3]);
725 ctx
= ucix_init("mountd");
726 t
= ucix_get_option(ctx
, "mountd", q
->serial
, tmp
);
734 } else if(!strcmp(t
, "1"))
736 if(strncmp(q
->name
, "Disc-", 5))
738 } else if(strcmp(q
->name
, t
))
743 if(!check_block(q
->dev
)||del
)
745 mount_dev_del(q
->dev
);
746 p
->prev
->next
= p
->next
;
747 p
->next
->prev
= p
->prev
;
749 log_printf("removing %s\n", q
->dev
);
750 snprintf(tmp
, 64, "%s%s", "/tmp/run/mountd/", q
->dev
);
752 snprintf(tmp
, 64, "%s%s", uci_path
, q
->name
);
754 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q
->dev
, q
->name
);
756 mount_dump_uci_state();
757 system_printf("/etc/fonstated/ReloadSamba");
761 for(i
= 0; i
< blk_cnt
; i
++)
762 mount_dev_add(block
[i
]);
765 static void mount_check_enum(void)
767 waitpid(-1, 0, WNOHANG
);
771 void mount_init(void)
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();