219146b33e68f5accebf62f9434928bc72ca992e
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
);
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");
179 while((fgets(tmp
, 256, fp
) > 0) && (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
== NTFS
)
268 if(mount
->fs
> MBR
&& mount
->fs
<= EXT4
)
270 struct uci_context
*ctx
;
271 char *uci_options
, *uci_fstype
;
272 ctx
= ucix_init("mountd");
273 if(fs_names
[mount
->fs
])
275 uci_options
= ucix_get_option(ctx
, "mountd", fs_names
[mount
->fs
], "options");
276 uci_fstype
= ucix_get_option(ctx
, "mountd", fs_names
[mount
->fs
], "fstype");
278 options
= uci_options
;
281 log_printf("mount -t %s -o %s /dev/%s %s", fstype
, options
, mount
->dev
, tmp
);
282 ret
= system_printf("mount -t %s -o %s /dev/%s %s", fstype
, options
, mount
->dev
, tmp
);
286 exit(WEXITSTATUS(ret
));
288 pid
= waitpid(pid
, &ret
, 0);
289 ret
= WEXITSTATUS(ret
);
290 log_printf("----------> mount ret = %d\n", ret
);
291 if(ret
&& (ret
!= 0xff))
293 if(mount_wait_for_disc(mount
->dev
) == 0)
296 mount_dump_uci_state();
301 int mount_remove(char *path
, char *dev
)
306 snprintf(tmp
, 256, "%s%s", path
, dev
);
307 log_printf("%s has expired... unmounting\n", tmp
);
308 ret
= system_printf("/bin/umount %s", tmp
);
312 mount
= mount_find(0, dev
);
315 log_printf("finished unmounting\n");
316 mount_dump_uci_state();
320 static int dir_sort(const struct dirent
**a
, const struct dirent
**b
)
325 static int dir_filter(const struct dirent
*a
)
327 if(strstr(a
->d_name
, ":"))
332 static char* mount_get_serial(char *dev
)
335 static char tmp2
[64];
337 static struct hd_driveid hd
;
340 static char disc_id
[13];
341 snprintf(tmp
, 64, "/dev/%s", dev
);
342 disc
= open(tmp
, O_RDONLY
);
345 log_printf("Trying to open unknown disc\n");
348 i
= ioctl(disc
, HDIO_GET_IDENTITY
, &hd
);
351 serial
= (char*)hd
.serial_no
;
352 /* if we failed, it probably a usb storage device */
353 /* there must be a better way for this */
356 struct dirent
**namelist
;
357 int n
= scandir("/sys/bus/scsi/devices/", &namelist
, dir_filter
, dir_sort
);
362 char *t
= strstr(namelist
[n
]->d_name
, ":");
370 id
= atoi(namelist
[n
]->d_name
);
372 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block:%s/", namelist
[n
]->d_name
, dev
);
373 ret
= stat(tmp3
, &buf
);
376 sprintf(tmp3
, "/sys/bus/scsi/devices/%s/block/%s/", namelist
[n
]->d_name
, dev
);
377 ret
= stat(tmp3
, &buf
);
382 snprintf(tmp2
, 64, "/proc/scsi/usb-storage/%d", id
);
383 fp
= fopen(tmp2
, "r");
386 while(fgets(tmp2
, 64, fp
) > 0)
388 serial
= strstr(tmp2
, "Serial Number:");
391 serial
+= strlen("Serial Number: ");
392 serial
[strlen(serial
) - 1] = '\0';
408 log_printf("could not find a serial number for the device %s\n", dev
);
410 /* serial string id is cheap, but makes the discs anonymous */
411 unsigned char uniq
[6];
412 unsigned int *u
= (unsigned int*) uniq
;
413 int l
= strlen(serial
);
415 memset(disc_id
, 0, 13);
417 for(i
= 0; i
< l
; i
++)
419 uniq
[i
%6] += serial
[i
];
421 sprintf(disc_id
, "%08X%02X%02X", *u
, uniq
[4], uniq
[5]);
422 //log_printf("Serial number - %s %s\n", serial, disc_id);
425 sprintf(disc_id
, "000000000000");
429 static void mount_dev_add(char *dev
)
431 struct mount
*mount
= mount_find(0, dev
);
441 struct uci_context
*ctx
;
446 char sector_size
[64];
451 if (!strncmp(name
, "mmcblk", 6))
454 s
= mount_get_serial(name
);
458 if (!strncmp(name
, "mmcblk", 6)) {
459 snprintf(tmp
, 64, "part%s", &dev
[8]);
460 snprintf(node
, 64, "SD-P%s", &dev
[8]);
463 snprintf(tmp
, 64, "part%s", &dev
[3]);
464 snprintf(node
, 64, "USB-%s", &dev
[2]);
466 if(node
[4] >= 'a' && node
[4] <= 'z')
471 ctx
= ucix_init("mountd");
472 p
= ucix_get_option(ctx
, "mountd", s
, tmp
);
481 snprintf(node
, 64, "%s", p
);
486 snprintf(tmp
, 64, "/sys/class/block/%s/device/model", name
);
487 fp
= fopen(tmp
, "r");
490 snprintf(tmp
, 64, "/sys/block/%s/device/model", name
);
491 fp
= fopen(tmp
, "r");
494 snprintf(model
, 64, "unknown");
496 fgets(model
, 64, fp
);
497 model
[strlen(model
) - 1] = '\0';;
500 snprintf(tmp
, 64, "/sys/class/block/%s/device/vendor", name
);
501 fp
= fopen(tmp
, "r");
504 snprintf(tmp
, 64, "/sys/block/%s/device/vendor", name
);
505 fp
= fopen(tmp
, "r");
508 snprintf(vendor
, 64, "unknown");
510 fgets(vendor
, 64, fp
);
511 vendor
[strlen(vendor
) - 1] = '\0';
514 snprintf(tmp
, 64, "/sys/class/block/%s/device/rev", name
);
515 fp
= fopen(tmp
, "r");
518 snprintf(tmp
, 64, "/sys/block/%s/device/rev", name
);
519 fp
= fopen(tmp
, "r");
522 snprintf(rev
, 64, "unknown");
525 rev
[strlen(rev
) - 1] = '\0';
528 snprintf(tmp
, 64, "/sys/class/block/%s/size", dev
);
529 fp
= fopen(tmp
, "r");
532 snprintf(tmp
, 64, "/sys/block/%s/%s/size", name
, dev
);
533 fp
= fopen(tmp
, "r");
536 snprintf(size
, 64, "unknown");
539 size
[strlen(size
) - 1] = '\0';
544 snprintf(tmp
, 64, "/sys/block/%s/queue/hw_sector_size", tmp2
);
545 fp
= fopen(tmp
, "r");
547 snprintf(sector_size
, 64, "unknown");
549 fgets(sector_size
, 64, fp
);
550 sector_size
[strlen(sector_size
) - 1] = '\0';
553 snprintf(tmp
, 64, "/dev/%s", dev
);
554 mount_add_list(node
, dev
, s
, vendor
, model
, rev
, ignore
, size
, sector_size
, detect_fs(tmp
));
555 mount_dump_uci_state();
559 static void mount_dev_del(char *dev
)
561 struct mount
*mount
= mount_find(0, dev
);
567 snprintf(tmp
, 256, "%s%s", "/tmp/run/mountd/", mount
->name
);
568 log_printf("%s has dissappeared ... unmounting\n", tmp
);
569 snprintf(tmp
, 256, "%s%s", "/tmp/run/mountd/", mount
->dev
);
570 system_printf("/bin/umount %s", tmp
);
572 snprintf(tmp
, 64, "%s%s", uci_path
, mount
->name
);
574 mount_dump_uci_state();
579 void mount_dump_list(void)
582 list_for_each(p
, &mounts
)
584 struct mount
*q
= container_of(p
, struct mount
, list
);
585 log_printf("* %s %s %d\n", q
->name
, q
->dev
, q
->mounted
);
589 char* is_mounted(char *block
, char *path
)
592 for(i
= 0; i
< mounted_count
; i
++)
595 if(!strncmp(&mounted
[i
][0][0], block
, strlen(&mounted
[i
][0][0])))
596 return &mounted
[i
][0][0];
598 if(!strncmp(&mounted
[i
][1][1], &path
[1], strlen(&mounted
[i
][1][0])))
599 return &mounted
[i
][0][0];
604 static void mount_check_mount_list(void)
606 FILE *fp
= fopen("/proc/mounts", "r");
611 log_printf("error reading /proc/mounts");
616 while(fgets(tmp
, 256, fp
) > 0)
619 t
= strstr(tmp
, " ");
625 strncpy(&mounted
[mounted_count
][0][0], tmp
, MAX_MOUNT_NAME
);
632 strncpy(&mounted
[mounted_count
][1][0], t
, MAX_MOUNT_NAME
);
639 strncpy(&mounted
[mounted_count
][2][0], t2
, MAX_MOUNT_NAME
);
640 /* printf("%s %s %s\n",
641 mounted[mounted_count][0],
642 mounted[mounted_count][1],
643 mounted[mounted_count][2]);*/
644 if(mounted_count
< MAX_MOUNTED
- 1)
647 log_printf("found more than %d mounts \n", MAX_MOUNTED
);
652 /* FIXME: we need more intelligence here */
653 static int dir_filter2(const struct dirent
*a
)
655 if(!strncmp(a
->d_name
, "mmcblk", 6) || !strncmp(a
->d_name
, "sd", 2))
660 char block
[MAX_BLOCK
][MAX_BLOCK
];
663 static int check_block(char *b
)
666 for(i
= 0; i
< blk_cnt
; i
++)
668 if(!strcmp(block
[i
], b
))
674 static void mount_enum_drives(void)
676 struct dirent
**namelist
, **namelist2
;
677 int i
, n
= scandir("/sys/block/", &namelist
, dir_filter2
, dir_sort
);
684 if(blk_cnt
< MAX_BLOCK
)
688 snprintf(tmp
, 64, "/sys/block/%s/", namelist
[n
]->d_name
);
689 m
= scandir(tmp
, &namelist2
, dir_filter2
, dir_sort
);
692 strncpy(&block
[blk_cnt
][0], namelist2
[m
]->d_name
, MAX_BLOCK
);
705 struct mount
*q
= container_of(p
, struct mount
, list
);
707 struct uci_context
*ctx
;
710 snprintf(tmp
, 64, "part%s", &q
->dev
[3]);
711 ctx
= ucix_init("mountd");
712 t
= ucix_get_option(ctx
, "mountd", q
->serial
, tmp
);
720 } else if(!strcmp(t
, "1"))
722 if(strncmp(q
->name
, "Disc-", 5))
724 } else if(strcmp(q
->name
, t
))
729 if(!check_block(q
->dev
)||del
)
731 mount_dev_del(q
->dev
);
732 p
->prev
->next
= p
->next
;
733 p
->next
->prev
= p
->prev
;
735 log_printf("removing %s\n", q
->dev
);
736 snprintf(tmp
, 64, "%s%s", uci_path
, q
->name
);
738 system_printf("ACTION=remove DEVICE=%s NAME=%s /sbin/hotplug-call mount", q
->dev
, q
->name
);
740 mount_dump_uci_state();
741 system_printf("/etc/fonstated/ReloadSamba");
745 for(i
= 0; i
< blk_cnt
; i
++)
746 mount_dev_add(block
[i
]);
749 static void mount_check_enum(void)
751 waitpid(-1, 0, WNOHANG
);
755 void mount_init(void)
757 INIT_LIST_HEAD(&mounts
);
758 timer_add(mount_check_mount_list
, 2);
759 timer_add(mount_check_enum
, 1);
760 mount_check_mount_list();