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
;
65 #define MAX_MOUNTED 32
66 #define MAX_MOUNT_NAME 32
68 char mounted
[MAX_MOUNTED
][3][MAX_MOUNT_NAME
];
69 int mounted_count
= 0;
70 extern char uci_path
[32];
72 static void mount_dump_uci_state(void)
74 struct uci_context
*ctx
;
76 char mountd
[] = {"mountd"};
77 char type
[] = {"mountd_disc"};
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
)
86 struct mount
*q
= container_of(p
, struct mount
, list
);
90 ucix_add_section(ctx
, mountd
, q
->serial
, type
);
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
)
104 snprintf(t
, 64, "fs%d", atoi(&q
->dev
[3]));
105 ucix_add_option(ctx
, mountd
, q
->serial
, t
, fs_names
[q
->fs
]);
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
)));
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");
119 static struct mount
* mount_find(char *name
, char *dev
)
122 list_for_each(p
, &mounts
)
124 struct mount
*q
= container_of(p
, struct mount
, list
);
126 if(!strcmp(q
->name
, name
))
129 if(!strcmp(q
->dev
, dev
))
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
)
139 char tmp
[64], tmp2
[64];
140 if(fs
<= MBR
|| fs
> EXT4
)
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
;
155 list_add(&mount
->list
, &mounts
);
156 if((!mount
->ignore
) && (mount
->fs
> MBR
) && (mount
->fs
<= EXT4
))
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
);
162 mount_new("/tmp/run/mountd/", dev
);
166 static int mount_check_disc(char *disc
)
168 FILE *fp
= fopen("/proc/mounts", "r");
173 log_printf("error reading /proc/mounts");
177 while((fgets(tmp
, 256, fp
) > 0) && (avail
== -1))
181 t
= strstr(tmp
, " ");
186 l
= snprintf(tmp2
, 31, "/dev/%s", disc
);
188 if(!strncmp(tmp
, tmp2
, l
))
196 static int mount_wait_for_disc(char *disc
)
201 int ret
= mount_check_disc(disc
);
209 int mount_new(char *path
, char *dev
)
215 mount
= mount_find(0, dev
);
218 log_printf("request for invalid path %s%s\n", path
, dev
);
221 if(mount
->ignore
|| mount
->mounted
|| mount
->fs
== EXTENDED
)
223 snprintf(tmp
, 256, "%s%s", path
, mount
->dev
);
224 log_printf("mounting %s\n", tmp
);
227 pid
= autofs_safe_fork();
230 if(mount
->fs
== EXFAT
)
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
);
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
);
240 if(mount
->fs
== EXT4
)
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
);
245 if(mount
->fs
== EXT3
)
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
);
250 if(mount
->fs
== EXT2
)
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
);
255 if(mount
->fs
== HFSPLUS
)
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
);
260 if(mount
->fs
== NTFS
)
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
);
265 exit(WEXITSTATUS(ret
));
267 pid
= waitpid(pid
, &ret
, 0);
268 ret
= WEXITSTATUS(ret
);
269 log_printf("----------> mount ret = %d\n", ret
);
270 if(ret
&& (ret
!= 0xff))
272 if(mount_wait_for_disc(mount
->dev
) == 0)
275 mount_dump_uci_state();
280 int mount_remove(char *path
, char *dev
)
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
);
291 mount
= mount_find(0, dev
);
294 log_printf("finished unmounting\n");
295 mount_dump_uci_state();
299 static int dir_sort(const struct dirent
**a
, const struct dirent
**b
)
304 static int dir_filter(const struct dirent
*a
)
306 if(strstr(a
->d_name
, ":"))
311 static char* mount_get_serial(char *dev
)
314 static char tmp2
[64];
316 static struct hd_driveid hd
;
319 static char disc_id
[13];
320 snprintf(tmp
, 64, "/dev/%s", dev
);
321 disc
= open(tmp
, O_RDONLY
);
324 log_printf("Trying to open unknown disc\n");
327 i
= ioctl(disc
, HDIO_GET_IDENTITY
, &hd
);
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 */
335 struct dirent
**namelist
;
336 int n
= scandir("/sys/bus/scsi/devices/", &namelist
, dir_filter
, dir_sort
);
341 char *t
= strstr(namelist
[n
]->d_name
, ":");
349 id
= atoi(namelist
[n
]->d_name
);
351 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block:%s/", namelist
[n
]->d_name
, dev
);
352 ret
= stat(tmp3
, &buf
);
355 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block/%s/", namelist
[n
]->d_name
, dev
);
356 ret
= stat(tmp3
, &buf
);
361 snprintf(tmp2
, 64, "/proc/scsi/usb-storage/%d", id
);
362 fp
= fopen(tmp2
, "r");
365 while(fgets(tmp2
, 64, fp
) > 0)
367 serial
= strstr(tmp2
, "Serial Number:");
370 serial
+= strlen("Serial Number: ");
371 serial
[strlen(serial
) - 1] = '\0';
387 log_printf("could not find a serial number for the device %s\n", dev
);
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
);
394 memset(disc_id
, 0, 13);
396 for(i
= 0; i
< l
; i
++)
398 uniq
[i
%6] += serial
[i
];
400 sprintf(disc_id
, "%08X%02X%02X", *u
, uniq
[4], uniq
[5]);
401 //log_printf("Serial number - %s %s\n", serial, disc_id);
404 sprintf(disc_id
, "000000000000");
408 static void mount_dev_add(char *dev
)
410 struct mount
*mount
= mount_find(0, dev
);
420 struct uci_context
*ctx
;
425 char sector_size
[64];
430 if (!strncmp(name
, "mmcblk", 6))
433 s
= mount_get_serial(name
);
437 if (!strncmp(name
, "mmcblk", 6)) {
438 snprintf(tmp
, 64, "part%s", &dev
[8]);
439 snprintf(node
, 64, "SD-P%s", &dev
[8]);
442 snprintf(tmp
, 64, "part%s", &dev
[3]);
443 snprintf(node
, 64, "USB-%s", &dev
[2]);
445 if(node
[4] >= 'a' && node
[4] <= 'z')
450 ctx
= ucix_init("mountd");
451 p
= ucix_get_option(ctx
, "mountd", s
, tmp
);
460 snprintf(node
, 64, "%s", p
);
465 snprintf(tmp
, 64, "/sys/class/block/%s/device/model", name
);
466 fp
= fopen(tmp
, "r");
469 snprintf(tmp
, 64, "/sys/block/%s/device/model", name
);
470 fp
= fopen(tmp
, "r");
473 snprintf(model
, 64, "unknown");
475 fgets(model
, 64, fp
);
476 model
[strlen(model
) - 1] = '\0';;
479 snprintf(tmp
, 64, "/sys/class/block/%s/device/vendor", name
);
480 fp
= fopen(tmp
, "r");
483 snprintf(tmp
, 64, "/sys/block/%s/device/vendor", name
);
484 fp
= fopen(tmp
, "r");
487 snprintf(vendor
, 64, "unknown");
489 fgets(vendor
, 64, fp
);
490 vendor
[strlen(vendor
) - 1] = '\0';
493 snprintf(tmp
, 64, "/sys/class/block/%s/device/rev", name
);
494 fp
= fopen(tmp
, "r");
497 snprintf(tmp
, 64, "/sys/block/%s/device/rev", name
);
498 fp
= fopen(tmp
, "r");
501 snprintf(rev
, 64, "unknown");
504 rev
[strlen(rev
) - 1] = '\0';
507 snprintf(tmp
, 64, "/sys/class/block/%s/size", dev
);
508 fp
= fopen(tmp
, "r");
511 snprintf(tmp
, 64, "/sys/block/%s/%s/size", name
, dev
);
512 fp
= fopen(tmp
, "r");
515 snprintf(size
, 64, "unknown");
518 size
[strlen(size
) - 1] = '\0';
523 snprintf(tmp
, 64, "/sys/block/%s/queue/hw_sector_size", tmp2
);
524 fp
= fopen(tmp
, "r");
526 snprintf(sector_size
, 64, "unknown");
528 fgets(sector_size
, 64, fp
);
529 sector_size
[strlen(sector_size
) - 1] = '\0';
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();
538 static void mount_dev_del(char *dev
)
540 struct mount
*mount
= mount_find(0, dev
);
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
);
551 snprintf(tmp
, 64, "%s%s", uci_path
, mount
->name
);
553 mount_dump_uci_state();
558 void mount_dump_list(void)
561 list_for_each(p
, &mounts
)
563 struct mount
*q
= container_of(p
, struct mount
, list
);
564 log_printf("* %s %s %d\n", q
->name
, q
->dev
, q
->mounted
);
568 char* is_mounted(char *block
, char *path
)
571 for(i
= 0; i
< mounted_count
; i
++)
574 if(!strncmp(&mounted
[i
][0][0], block
, strlen(&mounted
[i
][0][0])))
575 return &mounted
[i
][0][0];
577 if(!strncmp(&mounted
[i
][1][1], &path
[1], strlen(&mounted
[i
][1][0])))
578 return &mounted
[i
][0][0];
583 static void mount_check_mount_list(void)
585 FILE *fp
= fopen("/proc/mounts", "r");
590 log_printf("error reading /proc/mounts");
595 while(fgets(tmp
, 256, fp
) > 0)
598 t
= strstr(tmp
, " ");
604 strncpy(&mounted
[mounted_count
][0][0], tmp
, MAX_MOUNT_NAME
);
611 strncpy(&mounted
[mounted_count
][1][0], t
, MAX_MOUNT_NAME
);
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)
626 log_printf("found more than %d mounts \n", MAX_MOUNTED
);
631 /* FIXME: we need more intelligence here */
632 static int dir_filter2(const struct dirent
*a
)
634 if(!strncmp(a
->d_name
, "mmcblk", 6) || !strncmp(a
->d_name
, "sd", 2))
639 char block
[MAX_BLOCK
][MAX_BLOCK
];
642 static int check_block(char *b
)
645 for(i
= 0; i
< blk_cnt
; i
++)
647 if(!strcmp(block
[i
], b
))
653 static void mount_enum_drives(void)
655 struct dirent
**namelist
, **namelist2
;
656 int i
, n
= scandir("/sys/block/", &namelist
, dir_filter2
, dir_sort
);
663 if(blk_cnt
< MAX_BLOCK
)
667 snprintf(tmp
, 64, "/sys/block/%s/", namelist
[n
]->d_name
);
668 m
= scandir(tmp
, &namelist2
, dir_filter2
, dir_sort
);
671 strncpy(&block
[blk_cnt
][0], namelist2
[m
]->d_name
, MAX_BLOCK
);
684 struct mount
*q
= container_of(p
, struct mount
, list
);
686 struct uci_context
*ctx
;
689 snprintf(tmp
, 64, "part%s", &q
->dev
[3]);
690 ctx
= ucix_init("mountd");
691 t
= ucix_get_option(ctx
, "mountd", q
->serial
, tmp
);
699 } else if(!strcmp(t
, "1"))
701 if(strncmp(q
->name
, "Disc-", 5))
703 } else if(strcmp(q
->name
, t
))
708 if(!check_block(q
->dev
)||del
)
710 mount_dev_del(q
->dev
);
711 p
->prev
->next
= p
->next
;
712 p
->next
->prev
= p
->prev
;
714 log_printf("removing %s\n", q
->dev
);
715 snprintf(tmp
, 64, "%s%s", uci_path
, q
->name
);
717 system_printf("/etc/mountd/event remove %s %s", q
->dev
, q
->name
);
719 mount_dump_uci_state();
720 system_printf("/etc/fonstated/ReloadSamba");
724 for(i
= 0; i
< blk_cnt
; i
++)
725 mount_dev_add(block
[i
]);
728 static void mount_check_enum(void)
730 waitpid(-1, 0, WNOHANG
);
734 void mount_init(void)
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();