2 #include <linux/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
23 #include <linux/auto_fs4.h>
25 #include "include/log.h"
26 #include "include/sys.h"
27 #include "include/timer.h"
28 #include "include/mount.h"
29 #include "include/signal.h"
30 #include "include/ucix.h"
32 int fdin = 0; /* data coming out of the kernel */
33 int fdout = 0;/* data going into the kernel */
39 void umount_autofs(void)
41 system_printf("umount %s 2> /dev/null", "/tmp/run/mountd/");
44 static int mount_autofs(void)
48 log_printf("trying to mount %s as the autofs root\n", "/tmp/run/mountd/");
49 if(is_mounted(0, "/tmp/run/mountd/"))
51 log_printf("%s is already mounted\n", "/tmp/run/mountd/");
55 mkdir("/tmp/run/mountd/", 0555);
58 log_printf("failed to get kernel pipe\n");
61 if(system_printf("/bin/mount -t autofs -o fd=%d,pgrp=%u,minproto=5,maxproto=5 \"mountd(pid%u)\" %s",
62 pipefd[1], (unsigned) getpgrp(), getpid(), "/tmp/run/mountd/") != 0)
64 log_printf("unable to mount autofs on %s\n", "/tmp/run/mountd/");
73 fdin = open("/tmp/run/mountd/", O_RDONLY);
79 stat("/tmp/run/mountd/", &st);
83 static void send_ready(unsigned int wait_queue_token)
85 if(ioctl(fdin, AUTOFS_IOC_READY, wait_queue_token) < 0)
86 log_printf("failed to report ready to kernel\n");
89 static void send_fail(unsigned int wait_queue_token)
91 if(ioctl(fdin, AUTOFS_IOC_FAIL, wait_queue_token) < 0)
92 log_printf("failed to report fail to kernel\n");
95 static int autofs_process_request(const struct autofs_v5_packet *pkt)
98 log_printf("kernel is requesting a mount -> %s\n", pkt->name);
99 chdir("/tmp/run/mountd/");
100 if (lstat(pkt->name, &st) == -1 || (S_ISDIR(st.st_mode) && st.st_dev == dev)) {
101 if(!mount_new("/tmp/run/mountd/", (char*)pkt->name))
103 send_ready(pkt->wait_queue_token);
105 send_fail(pkt->wait_queue_token);
106 log_printf("failed to mount %s\n", pkt->name);
109 send_ready(pkt->wait_queue_token);
116 void expire_proc(void)
118 struct autofs_packet_expire pkt;
119 while(ioctl(fdin, AUTOFS_IOC_EXPIRE, &pkt) == 0)
120 mount_remove("/tmp/run/mountd/", pkt.name);
123 static int fullread(void *ptr, size_t len)
125 char *buf = (char *) ptr;
128 ssize_t r = read(fdout, buf, len);
141 static int autofs_in(union autofs_v5_packet_union *pkt)
143 struct pollfd fds[1];
146 fds[0].events = POLLIN;
150 if(poll(fds, 2, 1000) == -1)
154 log_printf("failed while trying to read packet from kernel\n");
157 if(fds[0].revents & POLLIN)
158 return fullread(pkt, sizeof(*pkt));
162 pid_t autofs_safe_fork(void)
173 void autofs_cleanup_handler(void)
180 void autofs_init(void)
184 struct uci_context *ctx;
185 signal_init(autofs_cleanup_handler);
186 ctx = ucix_init("mountd");
187 uci_timeout = ucix_get_option_int(ctx, "mountd", "mountd", "timeout", 60);
188 p = ucix_get_option(ctx, "mountd", "mountd", "path");
191 snprintf(uci_path, 31, p);
193 snprintf(uci_path, 31, "/tmp/mounts/");
195 mkdir("/tmp/run/", 0555);
196 mkdir("/tmp/mounts", 0555);
197 system_printf("rm -rf %s*", uci_path);
202 if(mount_autofs() < 0)
207 ioctl(fdin, AUTOFS_IOC_PROTOVER, &kproto_version);
208 if(kproto_version != 5)
210 log_printf("only kernel protocol version 5 is tested. You have %d.\n",
215 ioctl(fdin, AUTOFS_IOC_SETTIMEOUT, &uci_timeout);
216 timer_add(expire_proc, 15);
219 int autofs_loop(void)
225 union autofs_v5_packet_union pkt;
230 log_printf("Got a autofs packet\n");
231 if(pkt.hdr.type == autofs_ptype_missing_indirect)
232 autofs_process_request(&pkt.missing_indirect);
234 log_printf("unknown packet type %d\n", pkt.hdr.type);
238 log_printf("... quitting\n");