0c2862a06f081e1323bff3e18063772526afb1b8
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
;
67 #define MAX_MOUNTED 32
68 #define MAX_MOUNT_NAME 32
70 char mounted
[MAX_MOUNTED
][3][MAX_MOUNT_NAME
];
71 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];
142 if(fs
<= MBR
|| fs
> LASTFS
)
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
;
157 list_add(&mount
->list
, &mounts
);
158 if((!mount
->ignore
) && (mount
->fs
> MBR
) && (mount
->fs
<= LASTFS
))
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
);
164 mount_new("/tmp/run/mountd/", dev
);
165 system_printf("ACTION=add DEVICE=%s NAME=%s /sbin/hotplug-call mount", dev
, name
);
169 static int mount_check_disc(char *disc
)
171 FILE *fp
= fopen("/proc/mounts", "r");
176 log_printf("error reading /proc/mounts");
180 while((fgets(tmp
, 256, fp
) > 0) && (avail
== -1))
184 t
= strstr(tmp
, " ");
189 l
= snprintf(tmp2
, 31, "/dev/%s", disc
);
191 if(!strncmp(tmp
, tmp2
, l
))
199 static int mount_wait_for_disc(char *disc
)
204 int ret
= mount_check_disc(disc
);
212 int mount_new(char *path
, char *dev
)
218 mount
= mount_find(0, dev
);
221 log_printf("request for invalid path %s%s\n", path
, dev
);
224 if(mount
->ignore
|| mount
->mounted
|| mount
->fs
== EXTENDED
)
226 snprintf(tmp
, 256, "%s%s", path
, mount
->dev
);
227 log_printf("mounting %s\n", tmp
);
230 pid
= autofs_safe_fork();
233 char *options
, *fstype
;
234 if(mount
->fs
== EXFAT
)
236 options
= "rw,uid=1000,gid=1000";
241 options
= "rw,uid=1000,gid=1000";
244 if(mount
->fs
== EXT4
)
246 options
= "rw,defaults";
249 if(mount
->fs
== EXT3
)
251 options
= "rw,defaults";
254 if(mount
->fs
== EXT2
)
256 options
= "rw,defaults";
259 if(mount
->fs
== HFSPLUS
)
261 options
= "rw,defaults,uid=1000,gid=1000";
264 if(mount
->fs
== HFSPLUSJOURNAL
)
266 options
= "ro,defaults,uid=1000,gid=1000";
269 if(mount
->fs
== NTFS
)
274 if(mount
->fs
> MBR
&& mount
->fs
<= LASTFS
)
276 struct uci_context
*ctx
;
277 char *uci_options
, *uci_fstype
;
278 ctx
= ucix_init("mountd");
279 if(fs_names
[mount
->fs
])
281 uci_options
= ucix_get_option(ctx
, "mountd", fs_names
[mount
->fs
], "options");
282 uci_fstype
= ucix_get_option(ctx
, "mountd", fs_names
[mount
->fs
], "fstype");
284 options
= uci_options
;
287 log_printf("mount -t %s -o %s /dev/%s %s", fstype
, options
, mount
->dev
, tmp
);
288 ret
= system_printf("mount -t %s -o %s /dev/%s %s", fstype
, options
, mount
->dev
, tmp
);
292 exit(WEXITSTATUS(ret
));
294 pid
= waitpid(pid
, &ret
, 0);
295 ret
= WEXITSTATUS(ret
);
296 log_printf("----------> mount ret = %d\n", ret
);
297 if(ret
&& (ret
!= 0xff))
299 if(mount_wait_for_disc(mount
->dev
) == 0)
302 mount_dump_uci_state();
307 int mount_remove(char *path
, char *dev
)
312 snprintf(tmp
, 256, "%s%s", path
, dev
);
313 log_printf("%s has expired... unmounting\n", tmp
);
314 ret
= system_printf("/bin/umount %s", tmp
);
318 mount
= mount_find(0, dev
);
321 log_printf("finished unmounting\n");
322 mount_dump_uci_state();
326 static int dir_sort(const struct dirent
**a
, const struct dirent
**b
)
331 static int dir_filter(const struct dirent
*a
)
333 if(strstr(a
->d_name
, ":"))
338 static char* mount_get_serial(char *dev
)
341 static char tmp2
[64];
343 static struct hd_driveid hd
;
346 static char disc_id
[13];
347 snprintf(tmp
, 64, "/dev/%s", dev
);
348 disc
= open(tmp
, O_RDONLY
);
351 log_printf("Trying to open unknown disc\n");
354 i
= ioctl(disc
, HDIO_GET_IDENTITY
, &hd
);
357 serial
= (char*)hd
.serial_no
;
358 /* if we failed, it probably a usb storage device */
359 /* there must be a better way for this */
362 struct dirent
**namelist
;
363 int n
= scandir("/sys/bus/scsi/devices/", &namelist
, dir_filter
, dir_sort
);
368 char *t
= strstr(namelist
[n
]->d_name
, ":");
376 id
= atoi(namelist
[n
]->d_name
);
378 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block:%s/", namelist
[n
]->d_name
, dev
);
379 ret
= stat(tmp3
, &buf
);
382 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block/%s/", namelist
[n
]->d_name
, dev
);
383 ret
= stat(tmp3
, &buf
);
388 snprintf(tmp2
, 64, "/proc/scsi/usb-storage/%d", id
);
389 fp
= fopen(tmp2
, "r");
392 while(fgets(tmp2
, 64, fp
) > 0)
394 serial
= strstr(tmp2
, "Serial Number:");
397 serial
+= strlen("Serial Number: ");
398 serial
[strlen(serial
) - 1] = '\0';
414 log_printf("could not find a serial number for the device %s\n", dev
);
416 /* serial string id is cheap, but makes the discs anonymous */
417 unsigned char uniq
[6];
418 unsigned int *u
= (unsigned int*) uniq
;
419 int l
= strlen(serial
);
421 memset(disc_id
, 0, 13);
423 for(i
= 0; i
< l
; i
++)
425 uniq
[i
%6] += serial
[i
];
427 sprintf(disc_id
, "%08X%02X%02X", *u
, uniq
[4], uniq
[5]);
428 //log_printf("Serial number - %s %s\n", serial, disc_id);
431 sprintf(disc_id
, "000000000000");
435 static void mount_dev_add(char *dev
)
437 struct mount
*mount
= mount_find(0, dev
);
447 struct uci_context
*ctx
;
452 char sector_size
[64];
457 if (!strncmp(name
, "mmcblk", 6))
460 s
= mount_get_serial(name
);
464 if (!strncmp(name
, "mmcblk", 6)) {
465 snprintf(tmp
, 64, "part%s", &dev
[8]);
466 snprintf(node
, 64, "SD-P%s", &dev
[8]);
469 snprintf(tmp
, 64, "part%s", &dev
[3]);
470 snprintf(node
, 64, "USB-%s", &dev
[2]);
472 if(node
[4] >= 'a' && node
[4] <= 'z')
477 ctx
= ucix_init("mountd");
478 p
= ucix_get_option(ctx
, "mountd", s
, tmp
);
487 snprintf(node
, 64, "%s", p
);
492 snprintf(tmp
, 64, "/sys/class/block/%s/device/model", name
);
493 fp
= fopen(tmp
, "r");
496 snprintf(tmp
, 64, "/sys/block/%s/device/model", name
);
497 fp
= fopen(tmp
, "r");
500 snprintf(model
, 64, "unknown");
502 fgets(model
, 64, fp
);
503 model
[strlen(model
) - 1] = '\0';;
506 snprintf(tmp
, 64, "/sys/class/block/%s/device/vendor", name
);
507 fp
= fopen(tmp
, "r");
510 snprintf(tmp
, 64, "/sys/block/%s/device/vendor", name
);
511 fp
= fopen(tmp
, "r");
514 snprintf(vendor
, 64, "unknown");
516 fgets(vendor
, 64, fp
);
517 vendor
[strlen(vendor
) - 1] = '\0';
520 snprintf(tmp
, 64, "/sys/class/block/%s/device/rev", name
);
521 fp
= fopen(tmp
, "r");
524 snprintf(tmp
, 64, "/sys/block/%s/device/rev", name
);
525 fp
= fopen(tmp
, "r");
528 snprintf(rev
, 64, "unknown");
531 rev
[strlen(rev
) - 1] = '\0';
534 snprintf(tmp
, 64, "/sys/class/block/%s/size", dev
);
535 fp
= fopen(tmp
, "r");
538 snprintf(tmp
, 64, "/sys/block/%s/%s/size", name
, dev
);
539 fp
= fopen(tmp
, "r");
542 snprintf(size
, 64, "unknown");
545 size
[strlen(size
) - 1] = '\0';
550 snprintf(tmp
, 64, "/sys/block/%s/queue/hw_sector_size", tmp2
);
551 fp
= fopen(tmp
, "r");
553 snprintf(sector_size
, 64, "unknown");
555 fgets(sector_size
, 64, fp
);
556 sector_size
[strlen(sector_size
) - 1] = '\0';
559 snprintf(tmp
, 64, "/dev/%s", dev
);
560 mount_add_list(node
, dev
, s
, vendor
, model
, rev
, ignore
, size
, sector_size
, detect_fs(tmp
));
561 mount_dump_uci_state();
565 static void mount_dev_del(char *dev
)
567 struct mount
*mount
= mount_find(0, dev
);
573 snprintf(tmp
, 256, "%s%s", "/tmp/run/mountd/", mount
->name
);
574 log_printf("%s has dissappeared ... unmounting\n", tmp
);
575 snprintf(tmp
, 256, "%s%s", "/tmp/run/mountd/", mount
->dev
);
576 system_printf("/bin/umount %s", tmp
);
578 snprintf(tmp
, 64, "%s%s", uci_path
, mount
->name
);
580 mount_dump_uci_state();
585 void mount_dump_list(void)
588 list_for_each(p
, &mounts
)
590 struct mount
*q
= container_of(p
, struct mount
, list
);
591 log_printf("* %s %s %d\n", q
->name
, q
->dev
, q
->mounted
);
595 char* is_mounted(char *block
, char *path
)
598 for(i
= 0; i
< mounted_count
; i
++)
601 if(!strncmp(&mounted
[i
][0][0], block
, strlen(&mounted
[i
][0][0])))
602 return &mounted
[i
][0][0];
604 if(!strncmp(&mounted
[i
][1][1], &path
[1], strlen(&mounted
[i
][1][0])))
605 return &mounted
[i
][0][0];
610 static void mount_check_mount_list(void)
612 FILE *fp
= fopen("/proc/mounts", "r");
617 log_printf("error reading /proc/mounts");
622 while(fgets(tmp
, 256, fp
) > 0)
625 t
= strstr(tmp
, " ");
631 strncpy(&mounted
[mounted_count
][0][0], tmp
, MAX_MOUNT_NAME
);
638 strncpy(&mounted
[mounted_count
][1][0], t
, MAX_MOUNT_NAME
);
645 strncpy(&mounted
[mounted_count
][2][0], t2
, MAX_MOUNT_NAME
);
646 /* printf("%s %s %s\n",
647 mounted[mounted_count][0],
648 mounted[mounted_count][1],
649 mounted[mounted_count][2]);*/
650 if(mounted_count
< MAX_MOUNTED
- 1)
653 log_printf("found more than %d mounts \n", MAX_MOUNTED
);
658 /* FIXME: we need more intelligence here */
659 static int dir_filter2(const struct dirent
*a
)
661 if(!strncmp(a
->d_name
, "mmcblk", 6) || !strncmp(a
->d_name
, "sd", 2))
666 char block
[MAX_BLOCK
][MAX_BLOCK
];
669 static int check_block(char *b
)
672 for(i
= 0; i
< blk_cnt
; i
++)
674 if(!strcmp(block
[i
], b
))
680 static void mount_enum_drives(void)
682 struct dirent
**namelist
, **namelist2
;
683 int i
, n
= scandir("/sys/block/", &namelist
, dir_filter2
, dir_sort
);
690 if(blk_cnt
< MAX_BLOCK
)
694 snprintf(tmp
, 64, "/sys/block/%s/", namelist
[n
]->d_name
);
695 m
= scandir(tmp
, &namelist2
, dir_filter2
, dir_sort
);
698 strncpy(&block
[blk_cnt
][0], namelist2
[m
]->d_name
, MAX_BLOCK
);
711 struct mount
*q
= container_of(p
, struct mount
, list
);
713 struct uci_context
*ctx
;
716 snprintf(tmp
, 64, "part%s", &q
->dev
[3]);
717 ctx
= ucix_init("mountd");
718 t
= ucix_get_option(ctx
, "mountd", q
->serial
, tmp
);
726 } else if(!strcmp(t
, "1"))
728 if(strncmp(q
->name
, "Disc-", 5))
730 } else if(strcmp(q
->name
, t
))
735 if(!check_block(q
->dev
)||del
)
737 mount_dev_del(q
->dev
);
738 p
->prev
->next
= p
->next
;
739 p
->next
->prev
= p
->prev
;
741 log_printf("removing %s\n", q
->dev
);
742 snprintf(tmp
, 64, "%s%s", uci_path
, q
->name
);
744 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q
->dev
, q
->name
);
746 mount_dump_uci_state();
747 system_printf("/etc/fonstated/ReloadSamba");
751 for(i
= 0; i
< blk_cnt
; i
++)
752 mount_dev_add(block
[i
]);
755 static void mount_check_enum(void)
757 waitpid(-1, 0, WNOHANG
);
761 void mount_init(void)
763 INIT_LIST_HEAD(&mounts
);
764 timer_add(mount_check_mount_list
, 2);
765 timer_add(mount_check_enum
, 1);
766 mount_check_mount_list();