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 struct list_head mounts
;
37 struct list_head list
;
66 #define MAX_MOUNTED 32
67 #define MAX_MOUNT_NAME 32
69 char mounted
[MAX_MOUNTED
][3][MAX_MOUNT_NAME
];
70 int mounted_count
= 0;
71 extern char uci_path
[32];
73 static void mount_dump_uci_state(void)
75 struct uci_context
*ctx
;
77 char mountd
[] = {"mountd"};
78 char type
[] = {"mountd_disc"};
80 unsigned long long int size
= 0;
81 unlink("/var/state/mountd");
82 ctx
= ucix_init("mountd");
83 uci_set_savedir(ctx
, "/var/state/");
84 ucix_add_option_int(ctx
, mountd
, mountd
, "count", list_count(&mounts
));
85 list_for_each(p
, &mounts
)
87 struct mount
*q
= container_of(p
, struct mount
, list
);
91 ucix_add_section(ctx
, mountd
, q
->serial
, type
);
94 ucix_add_option(ctx
, mountd
, q
->serial
, "disc", t
);
95 ucix_add_option(ctx
, mountd
, q
->serial
, "sector_size", q
->sector_size
);
96 snprintf(t
, 64, "part%dmounted", atoi(&q
->dev
[3]));
97 ucix_add_option(ctx
, mountd
, q
->serial
, t
, (q
->mounted
)?("1"):("0"));
98 ucix_add_option(ctx
, mountd
, q
->serial
, "vendor", q
->vendor
);
99 ucix_add_option(ctx
, mountd
, q
->serial
, "model", q
->model
);
100 ucix_add_option(ctx
, mountd
, q
->serial
, "rev", q
->rev
);
101 snprintf(t
, 64, "size%d", atoi(&q
->dev
[3]));
102 ucix_add_option(ctx
, mountd
, q
->serial
, t
, q
->size
);
103 if(q
->fs
> MBR
&& q
->fs
<= EXT4
)
105 snprintf(t
, 64, "fs%d", atoi(&q
->dev
[3]));
106 ucix_add_option(ctx
, mountd
, q
->serial
, t
, fs_names
[q
->fs
]);
110 if((!q
->ignore
) && q
->size
&& q
->sector_size
)
111 size
= size
+ (((unsigned long long int)atoi(q
->size
)) * ((unsigned long long int)atoi(q
->sector_size
)));
113 ucix_add_option_int(ctx
, mountd
, mountd
, "mounted", mounted
);
114 ucix_add_option_int(ctx
, mountd
, mountd
, "total", size
);
115 system_printf("echo -n %llu > /tmp/run/mountd_size", size
);
116 ucix_save_state(ctx
, "mountd");
120 static struct mount
* mount_find(char *name
, char *dev
)
123 list_for_each(p
, &mounts
)
125 struct mount
*q
= container_of(p
, struct mount
, list
);
127 if(!strcmp(q
->name
, name
))
130 if(!strcmp(q
->dev
, dev
))
136 static void mount_add_list(char *name
, char *dev
, char *serial
,
137 char *vendor
, char *model
, char *rev
, int ignore
, char *size
, char *sector_size
, int fs
)
140 char tmp
[64], tmp2
[64];
141 if(fs
<= MBR
|| fs
> EXT4
)
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
);
157 if((!mount
->ignore
) && (mount
->fs
> MBR
) && (mount
->fs
<= EXT4
))
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 mount_new("/tmp/run/mountd/", dev
);
167 static int mount_check_disc(char *disc
)
169 FILE *fp
= fopen("/proc/mounts", "r");
174 log_printf("error reading /proc/mounts");
178 while((fgets(tmp
, 256, fp
) > 0) && (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 if(mount
->fs
== EXFAT
)
233 log_printf("mount -t exfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount
->dev
, tmp
);
234 ret
= system_printf("mount -t exfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount
->dev
, tmp
);
238 log_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount
->dev
, tmp
);
239 ret
= system_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount
->dev
, tmp
);
241 if(mount
->fs
== EXT4
)
243 log_printf("mount -t ext4 -o rw,defaults /dev/%s %s", mount
->dev
, tmp
);
244 ret
= system_printf("mount -t ext4 -o rw,defaults /dev/%s %s", mount
->dev
, tmp
);
246 if(mount
->fs
== EXT3
)
248 log_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount
->dev
, tmp
);
249 ret
= system_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount
->dev
, tmp
);
251 if(mount
->fs
== EXT2
)
253 log_printf("mount -t ext2 -o rw,defaults /dev/%s %s", mount
->dev
, tmp
);
254 ret
= system_printf("mount -t ext2 -o rw,defaults /dev/%s %s", mount
->dev
, tmp
);
256 if(mount
->fs
== HFSPLUS
)
258 log_printf("mount -t hfsplus -o rw,defaults,uid=1000,gid=1000 /dev/%s %s", mount
->dev
, tmp
);
259 ret
= system_printf("mount -t hfsplus -o rw,defaults,uid=1000,gid=1000 /dev/%s %s", mount
->dev
, tmp
);
261 if(mount
->fs
== NTFS
)
263 log_printf("ntfs-3g /dev/%s %s -o force", mount
->dev
, tmp
);
264 ret
= system_printf("ntfs-3g /dev/%s %s -o force", mount
->dev
, tmp
);
266 exit(WEXITSTATUS(ret
));
268 pid
= waitpid(pid
, &ret
, 0);
269 ret
= WEXITSTATUS(ret
);
270 log_printf("----------> mount ret = %d\n", ret
);
271 if(ret
&& (ret
!= 0xff))
273 if(mount_wait_for_disc(mount
->dev
) == 0)
276 mount_dump_uci_state();
281 int mount_remove(char *path
, char *dev
)
286 snprintf(tmp
, 256, "%s%s", path
, dev
);
287 log_printf("%s has expired... unmounting\n", tmp
);
288 ret
= system_printf("/bin/umount %s", tmp
);
292 mount
= mount_find(0, dev
);
295 log_printf("finished unmounting\n");
296 mount_dump_uci_state();
300 static int dir_sort(const struct dirent
**a
, const struct dirent
**b
)
305 static int dir_filter(const struct dirent
*a
)
307 if(strstr(a
->d_name
, ":"))
312 static char* mount_get_serial(char *dev
)
315 static char tmp2
[64];
317 static struct hd_driveid hd
;
320 static char disc_id
[13];
321 snprintf(tmp
, 64, "/dev/%s", dev
);
322 disc
= open(tmp
, O_RDONLY
);
325 log_printf("Trying to open unknown disc\n");
328 i
= ioctl(disc
, HDIO_GET_IDENTITY
, &hd
);
331 serial
= (char*)hd
.serial_no
;
332 /* if we failed, it probably a usb storage device */
333 /* there must be a better way for this */
336 struct dirent
**namelist
;
337 int n
= scandir("/sys/bus/scsi/devices/", &namelist
, dir_filter
, dir_sort
);
342 char *t
= strstr(namelist
[n
]->d_name
, ":");
350 id
= atoi(namelist
[n
]->d_name
);
352 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block:%s/", namelist
[n
]->d_name
, dev
);
353 ret
= stat(tmp3
, &buf
);
356 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block/%s/", namelist
[n
]->d_name
, dev
);
357 ret
= stat(tmp3
, &buf
);
362 snprintf(tmp2
, 64, "/proc/scsi/usb-storage/%d", id
);
363 fp
= fopen(tmp2
, "r");
366 while(fgets(tmp2
, 64, fp
) > 0)
368 serial
= strstr(tmp2
, "Serial Number:");
371 serial
+= strlen("Serial Number: ");
372 serial
[strlen(serial
) - 1] = '\0';
388 log_printf("could not find a serial number for the device %s\n", dev
);
390 /* serial string id is cheap, but makes the discs anonymous */
391 unsigned char uniq
[6];
392 unsigned int *u
= (unsigned int*) uniq
;
393 int l
= strlen(serial
);
395 memset(disc_id
, 0, 13);
397 for(i
= 0; i
< l
; i
++)
399 uniq
[i
%6] += serial
[i
];
401 sprintf(disc_id
, "%08X%02X%02X", *u
, uniq
[4], uniq
[5]);
402 //log_printf("Serial number - %s %s\n", serial, disc_id);
405 sprintf(disc_id
, "000000000000");
409 static void mount_dev_add(char *dev
)
411 struct mount
*mount
= mount_find(0, dev
);
421 struct uci_context
*ctx
;
426 char sector_size
[64];
431 if (!strncmp(name
, "mmcblk", 6))
434 s
= mount_get_serial(name
);
438 if (!strncmp(name
, "mmcblk", 6)) {
439 snprintf(tmp
, 64, "part%s", &dev
[8]);
440 snprintf(node
, 64, "SD-P%s", &dev
[8]);
443 snprintf(tmp
, 64, "part%s", &dev
[3]);
444 snprintf(node
, 64, "USB-%s", &dev
[2]);
446 if(node
[4] >= 'a' && node
[4] <= 'z')
451 ctx
= ucix_init("mountd");
452 p
= ucix_get_option(ctx
, "mountd", s
, tmp
);
461 snprintf(node
, 64, "%s", p
);
466 snprintf(tmp
, 64, "/sys/class/block/%s/device/model", name
);
467 fp
= fopen(tmp
, "r");
470 snprintf(tmp
, 64, "/sys/block/%s/device/model", name
);
471 fp
= fopen(tmp
, "r");
474 snprintf(model
, 64, "unknown");
476 fgets(model
, 64, fp
);
477 model
[strlen(model
) - 1] = '\0';;
480 snprintf(tmp
, 64, "/sys/class/block/%s/device/vendor", name
);
481 fp
= fopen(tmp
, "r");
484 snprintf(tmp
, 64, "/sys/block/%s/device/vendor", name
);
485 fp
= fopen(tmp
, "r");
488 snprintf(vendor
, 64, "unknown");
490 fgets(vendor
, 64, fp
);
491 vendor
[strlen(vendor
) - 1] = '\0';
494 snprintf(tmp
, 64, "/sys/class/block/%s/device/rev", name
);
495 fp
= fopen(tmp
, "r");
498 snprintf(tmp
, 64, "/sys/block/%s/device/rev", name
);
499 fp
= fopen(tmp
, "r");
502 snprintf(rev
, 64, "unknown");
505 rev
[strlen(rev
) - 1] = '\0';
508 snprintf(tmp
, 64, "/sys/class/block/%s/size", dev
);
509 fp
= fopen(tmp
, "r");
512 snprintf(tmp
, 64, "/sys/block/%s/%s/size", name
, dev
);
513 fp
= fopen(tmp
, "r");
516 snprintf(size
, 64, "unknown");
519 size
[strlen(size
) - 1] = '\0';
524 snprintf(tmp
, 64, "/sys/block/%s/queue/hw_sector_size", tmp2
);
525 fp
= fopen(tmp
, "r");
527 snprintf(sector_size
, 64, "unknown");
529 fgets(sector_size
, 64, fp
);
530 sector_size
[strlen(sector_size
) - 1] = '\0';
533 snprintf(tmp
, 64, "/dev/%s", dev
);
534 mount_add_list(node
, dev
, s
, vendor
, model
, rev
, ignore
, size
, sector_size
, detect_fs(tmp
));
535 mount_dump_uci_state();
539 static void mount_dev_del(char *dev
)
541 struct mount
*mount
= mount_find(0, dev
);
547 snprintf(tmp
, 256, "%s%s", "/tmp/run/mountd/", mount
->name
);
548 log_printf("%s has dissappeared ... unmounting\n", tmp
);
549 snprintf(tmp
, 256, "%s%s", "/tmp/run/mountd/", mount
->dev
);
550 system_printf("/bin/umount %s", tmp
);
552 snprintf(tmp
, 64, "%s%s", uci_path
, mount
->name
);
554 mount_dump_uci_state();
559 void mount_dump_list(void)
562 list_for_each(p
, &mounts
)
564 struct mount
*q
= container_of(p
, struct mount
, list
);
565 log_printf("* %s %s %d\n", q
->name
, q
->dev
, q
->mounted
);
569 char* is_mounted(char *block
, char *path
)
572 for(i
= 0; i
< mounted_count
; i
++)
575 if(!strncmp(&mounted
[i
][0][0], block
, strlen(&mounted
[i
][0][0])))
576 return &mounted
[i
][0][0];
578 if(!strncmp(&mounted
[i
][1][1], &path
[1], strlen(&mounted
[i
][1][0])))
579 return &mounted
[i
][0][0];
584 static void mount_check_mount_list(void)
586 FILE *fp
= fopen("/proc/mounts", "r");
591 log_printf("error reading /proc/mounts");
596 while(fgets(tmp
, 256, fp
) > 0)
599 t
= strstr(tmp
, " ");
605 strncpy(&mounted
[mounted_count
][0][0], tmp
, MAX_MOUNT_NAME
);
612 strncpy(&mounted
[mounted_count
][1][0], t
, MAX_MOUNT_NAME
);
619 strncpy(&mounted
[mounted_count
][2][0], t2
, MAX_MOUNT_NAME
);
620 /* printf("%s %s %s\n",
621 mounted[mounted_count][0],
622 mounted[mounted_count][1],
623 mounted[mounted_count][2]);*/
624 if(mounted_count
< MAX_MOUNTED
- 1)
627 log_printf("found more than %d mounts \n", MAX_MOUNTED
);
632 /* FIXME: we need more intelligence here */
633 static int dir_filter2(const struct dirent
*a
)
635 if(!strncmp(a
->d_name
, "mmcblk", 6) || !strncmp(a
->d_name
, "sd", 2))
640 char block
[MAX_BLOCK
][MAX_BLOCK
];
643 static int check_block(char *b
)
646 for(i
= 0; i
< blk_cnt
; i
++)
648 if(!strcmp(block
[i
], b
))
654 static void mount_enum_drives(void)
656 struct dirent
**namelist
, **namelist2
;
657 int i
, n
= scandir("/sys/block/", &namelist
, dir_filter2
, dir_sort
);
664 if(blk_cnt
< MAX_BLOCK
)
668 snprintf(tmp
, 64, "/sys/block/%s/", namelist
[n
]->d_name
);
669 m
= scandir(tmp
, &namelist2
, dir_filter2
, dir_sort
);
672 strncpy(&block
[blk_cnt
][0], namelist2
[m
]->d_name
, MAX_BLOCK
);
685 struct mount
*q
= container_of(p
, struct mount
, list
);
687 struct uci_context
*ctx
;
690 snprintf(tmp
, 64, "part%s", &q
->dev
[3]);
691 ctx
= ucix_init("mountd");
692 t
= ucix_get_option(ctx
, "mountd", q
->serial
, tmp
);
700 } else if(!strcmp(t
, "1"))
702 if(strncmp(q
->name
, "Disc-", 5))
704 } else if(strcmp(q
->name
, t
))
709 if(!check_block(q
->dev
)||del
)
711 mount_dev_del(q
->dev
);
712 p
->prev
->next
= p
->next
;
713 p
->next
->prev
= p
->prev
;
715 log_printf("removing %s\n", q
->dev
);
716 snprintf(tmp
, 64, "%s%s", uci_path
, q
->name
);
718 system_printf("/etc/mountd/event remove %s %s", q
->dev
, q
->name
);
720 mount_dump_uci_state();
721 system_printf("/etc/fonstated/ReloadSamba");
725 for(i
= 0; i
< blk_cnt
; i
++)
726 mount_dev_add(block
[i
]);
729 static void mount_check_enum(void)
731 waitpid(-1, 0, WNOHANG
);
735 void mount_init(void)
737 INIT_LIST_HEAD(&mounts
);
738 timer_add(mount_check_mount_list
, 2);
739 timer_add(mount_check_enum
, 1);
740 mount_check_mount_list();