92606da32d155fa7cca87a258b04bbe70b6ded8f
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];
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");
179 while((fgets(tmp
, 256, fp
) != NULL
) && (avail
== -1))
183 t
= strstr(tmp
, " ");
188 l
= snprintf(tmp2
, 31, "/dev/%s", disc
);
190 if(!strncmp(tmp
, tmp2
, l
))
198 static int mount_wait_for_disc(char *disc
)
203 int ret
= mount_check_disc(disc
);
211 int mount_new(char *path
, char *dev
)
217 mount
= mount_find(0, dev
);
220 log_printf("request for invalid path %s%s\n", path
, dev
);
223 if(mount
->ignore
|| mount
->mounted
|| mount
->fs
== EXTENDED
)
225 snprintf(tmp
, 256, "%s%s", path
, mount
->dev
);
226 log_printf("mounting %s\n", tmp
);
229 pid
= autofs_safe_fork();
232 char *options
, *fstype
;
233 if(mount
->fs
== EXFAT
)
235 options
= "rw,uid=1000,gid=1000";
240 options
= "rw,uid=1000,gid=1000";
243 if(mount
->fs
== EXT4
)
245 options
= "rw,defaults";
248 if(mount
->fs
== EXT3
)
250 options
= "rw,defaults";
253 if(mount
->fs
== EXT2
)
255 options
= "rw,defaults";
258 if(mount
->fs
== HFSPLUS
)
260 options
= "rw,defaults,uid=1000,gid=1000";
263 if(mount
->fs
== HFSPLUSJOURNAL
)
265 options
= "ro,defaults,uid=1000,gid=1000";
268 if(mount
->fs
== NTFS
)
273 if(mount
->fs
> MBR
&& mount
->fs
<= LASTFS
)
275 struct uci_context
*ctx
;
276 char *uci_options
, *uci_fstype
;
277 ctx
= ucix_init("mountd");
278 if(fs_names
[mount
->fs
])
280 uci_options
= ucix_get_option(ctx
, "mountd", fs_names
[mount
->fs
], "options");
281 uci_fstype
= ucix_get_option(ctx
, "mountd", fs_names
[mount
->fs
], "fstype");
283 options
= uci_options
;
286 log_printf("mount -t %s -o %s /dev/%s %s", fstype
, options
, mount
->dev
, tmp
);
287 ret
= system_printf("mount -t %s -o %s /dev/%s %s", fstype
, options
, mount
->dev
, tmp
);
291 exit(WEXITSTATUS(ret
));
293 pid
= waitpid(pid
, &ret
, 0);
294 ret
= WEXITSTATUS(ret
);
295 log_printf("----------> mount ret = %d\n", ret
);
296 if(ret
&& (ret
!= 0xff))
298 if(mount_wait_for_disc(mount
->dev
) == 0)
301 mount_dump_uci_state();
306 int mount_remove(char *path
, char *dev
)
311 snprintf(tmp
, 256, "%s%s", path
, dev
);
312 log_printf("%s has expired... unmounting\n", tmp
);
313 ret
= system_printf("/bin/umount %s", tmp
);
317 mount
= mount_find(0, dev
);
320 log_printf("finished unmounting\n");
321 mount_dump_uci_state();
325 static int dir_sort(const struct dirent
**a
, const struct dirent
**b
)
330 static int dir_filter(const struct dirent
*a
)
332 if(strstr(a
->d_name
, ":"))
337 static char* mount_get_serial(char *dev
)
340 static char tmp2
[64];
342 static struct hd_driveid hd
;
345 static char disc_id
[13];
346 snprintf(tmp
, 64, "/dev/%s", dev
);
347 disc
= open(tmp
, O_RDONLY
);
350 log_printf("Trying to open unknown disc\n");
353 i
= ioctl(disc
, HDIO_GET_IDENTITY
, &hd
);
356 serial
= (char*)hd
.serial_no
;
357 /* if we failed, it probably a usb storage device */
358 /* there must be a better way for this */
361 struct dirent
**namelist
;
362 int n
= scandir("/sys/bus/scsi/devices/", &namelist
, dir_filter
, dir_sort
);
367 char *t
= strstr(namelist
[n
]->d_name
, ":");
375 id
= atoi(namelist
[n
]->d_name
);
377 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block:%s/", namelist
[n
]->d_name
, dev
);
378 ret
= stat(tmp3
, &buf
);
381 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block/%s/", namelist
[n
]->d_name
, dev
);
382 ret
= stat(tmp3
, &buf
);
387 snprintf(tmp2
, 64, "/proc/scsi/usb-storage/%d", id
);
388 fp
= fopen(tmp2
, "r");
391 while(fgets(tmp2
, 64, fp
) != NULL
)
393 serial
= strstr(tmp2
, "Serial Number:");
396 serial
+= strlen("Serial Number: ");
397 serial
[strlen(serial
) - 1] = '\0';
413 log_printf("could not find a serial number for the device %s\n", dev
);
415 /* serial string id is cheap, but makes the discs anonymous */
416 unsigned char uniq
[6];
417 unsigned int *u
= (unsigned int*) uniq
;
418 int l
= strlen(serial
);
420 memset(disc_id
, 0, 13);
422 for(i
= 0; i
< l
; i
++)
424 uniq
[i
%6] += serial
[i
];
426 sprintf(disc_id
, "%08X%02X%02X", *u
, uniq
[4], uniq
[5]);
427 //log_printf("Serial number - %s %s\n", serial, disc_id);
430 sprintf(disc_id
, "000000000000");
434 static void mount_dev_add(char *dev
)
436 struct mount
*mount
= mount_find(0, dev
);
446 struct uci_context
*ctx
;
451 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
);
559 mount_add_list(node
, dev
, s
, vendor
, model
, rev
, ignore
, size
, sector_size
, detect_fs(tmp
));
560 mount_dump_uci_state();
564 static void mount_dev_del(char *dev
)
566 struct mount
*mount
= mount_find(0, dev
);
572 snprintf(tmp
, 256, "%s%s", "/tmp/run/mountd/", mount
->name
);
573 log_printf("%s has dissappeared ... unmounting\n", tmp
);
574 snprintf(tmp
, 256, "%s%s", "/tmp/run/mountd/", mount
->dev
);
575 system_printf("/bin/umount %s", tmp
);
577 snprintf(tmp
, 64, "%s%s", uci_path
, mount
->name
);
579 mount_dump_uci_state();
584 void mount_dump_list(void)
587 list_for_each(p
, &mounts
)
589 struct mount
*q
= container_of(p
, struct mount
, list
);
590 log_printf("* %s %s %d\n", q
->name
, q
->dev
, q
->mounted
);
594 char* is_mounted(char *block
, char *path
)
597 for(i
= 0; i
< mounted_count
; i
++)
600 if(!strncmp(&mounted
[i
][0][0], block
, strlen(&mounted
[i
][0][0])))
601 return &mounted
[i
][0][0];
603 if(!strncmp(&mounted
[i
][1][1], &path
[1], strlen(&mounted
[i
][1][0])))
604 return &mounted
[i
][0][0];
609 static void mount_check_mount_list(void)
611 FILE *fp
= fopen("/proc/mounts", "r");
616 log_printf("error reading /proc/mounts");
620 while(fgets(tmp
, 256, fp
) != NULL
)
623 t
= strstr(tmp
, " ");
629 strncpy(&mounted
[mounted_count
][0][0], tmp
, MAX_MOUNT_NAME
);
636 strncpy(&mounted
[mounted_count
][1][0], t
, MAX_MOUNT_NAME
);
643 strncpy(&mounted
[mounted_count
][2][0], t2
, MAX_MOUNT_NAME
);
644 /* printf("%s %s %s\n",
645 mounted[mounted_count][0],
646 mounted[mounted_count][1],
647 mounted[mounted_count][2]);*/
648 if(mounted_count
< MAX_MOUNTED
- 1)
651 log_printf("found more than %d mounts \n", MAX_MOUNTED
);
656 /* FIXME: we need more intelligence here */
657 static int dir_filter2(const struct dirent
*a
)
659 if(!strncmp(a
->d_name
, "mmcblk", 6) || !strncmp(a
->d_name
, "sd", 2))
664 static char block
[MAX_BLOCK
][MAX_BLOCK
];
665 static int blk_cnt
= 0;
667 static int check_block(char *b
)
670 for(i
= 0; i
< blk_cnt
; i
++)
672 if(!strcmp(block
[i
], b
))
678 static void mount_enum_drives(void)
680 struct dirent
**namelist
, **namelist2
;
681 int i
, n
= scandir("/sys/block/", &namelist
, dir_filter2
, dir_sort
);
688 if(blk_cnt
< MAX_BLOCK
)
692 snprintf(tmp
, 64, "/sys/block/%s/", namelist
[n
]->d_name
);
693 m
= scandir(tmp
, &namelist2
, dir_filter2
, dir_sort
);
698 strncpy(&block
[blk_cnt
][0], namelist2
[m
]->d_name
, MAX_BLOCK
);
704 strncpy(&block
[blk_cnt
][0], namelist
[n
]->d_name
, MAX_BLOCK
);
715 struct mount
*q
= container_of(p
, struct mount
, list
);
717 struct uci_context
*ctx
;
720 snprintf(tmp
, 64, "part%s", &q
->dev
[3]);
721 ctx
= ucix_init("mountd");
722 t
= ucix_get_option(ctx
, "mountd", q
->serial
, tmp
);
730 } else if(!strcmp(t
, "1"))
732 if(strncmp(q
->name
, "Disc-", 5))
734 } else if(strcmp(q
->name
, t
))
739 if(!check_block(q
->dev
)||del
)
741 mount_dev_del(q
->dev
);
742 p
->prev
->next
= p
->next
;
743 p
->next
->prev
= p
->prev
;
745 log_printf("removing %s\n", q
->dev
);
746 snprintf(tmp
, 64, "%s%s", "/tmp/run/mountd/", q
->dev
);
748 snprintf(tmp
, 64, "%s%s", uci_path
, q
->name
);
750 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q
->dev
, q
->name
);
752 mount_dump_uci_state();
753 system_printf("/etc/fonstated/ReloadSamba");
757 for(i
= 0; i
< blk_cnt
; i
++)
758 mount_dev_add(block
[i
]);
761 static void mount_check_enum(void)
763 waitpid(-1, 0, WNOHANG
);
767 void mount_init(void)
769 INIT_LIST_HEAD(&mounts
);
770 timer_add(mount_check_mount_list
, 2);
771 timer_add(mount_check_enum
, 1);
772 mount_check_mount_list();