mount.c revision 1.3 1 /* $NetBSD: mount.c,v 1.3 2013/04/22 13:27:49 yamt Exp $ */
2
3 /*-
4 * Copyright (c)2010,2011 YAMAMOTO Takashi,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: mount.c,v 1.3 2013/04/22 13:27:49 yamt Exp $");
32 #endif /* not lint */
33
34 #include <err.h>
35 #include <errno.h>
36 #include <locale.h>
37 #include <mntopts.h>
38 #include <paths.h>
39 #include <puffs.h>
40 #include <stdbool.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 #include "pgfs.h"
45 #include "pgfs_db.h"
46
47 #define PGFS_MNT_ALT_DUMMY 1
48 #define PGFS_MNT_ALT_DEBUG 2
49
50 int
51 main(int argc, char *argv[])
52 {
53 extern char *optarg;
54 extern int optind;
55 mntoptparse_t mp;
56 struct puffs_usermount *pu;
57 struct puffs_ops *pops;
58 int mntflags;
59 int altmntflags;
60 int ch;
61 int error;
62 const char *dbname = NULL;
63 const char *dbuser = NULL;
64 static const struct mntopt mopts[] = {
65 MOPT_STDOPTS,
66 MOPT_SYNC,
67 { .m_option = "dbname", .m_inverse = 0,
68 .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
69 { .m_option = "dbuser", .m_inverse = 0,
70 .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
71 { .m_option = "debug", .m_inverse = 0,
72 .m_flag = PGFS_MNT_ALT_DEBUG, .m_altloc = 1, },
73 { .m_option = "nconn", .m_inverse = 0,
74 .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
75 MOPT_NULL,
76 };
77 uint32_t pflags = PUFFS_KFLAG_IAONDEMAND;
78 unsigned int nconn = 8;
79 bool debug = false;
80 bool dosync;
81
82 setlocale(LC_ALL, "");
83
84 mntflags = 0;
85 altmntflags = 0;
86 while ((ch = getopt(argc, argv, "o:")) != -1) {
87 long v;
88
89 switch (ch) {
90 case 'o':
91 mp = getmntopts(optarg, mopts, &mntflags,
92 &altmntflags);
93 if (mp == NULL) {
94 err(EXIT_FAILURE, "getmntopts");
95 }
96 getmnt_silent = 1; /* XXX silly api */
97 dbname = getmntoptstr(mp, "dbname");
98 dbuser = getmntoptstr(mp, "dbuser");
99 v = getmntoptnum(mp, "nconn");
100 getmnt_silent = 0;
101 if (v != -1) {
102 nconn = v;
103 }
104 if ((altmntflags & PGFS_MNT_ALT_DEBUG) != 0) {
105 debug = true;
106 }
107 freemntopts(mp);
108 break;
109 }
110 }
111 argc -= optind;
112 argv += optind;
113
114 PUFFSOP_INIT(pops);
115 PUFFSOP_SETFSNOP(pops, unmount);
116 PUFFSOP_SETFSNOP(pops, sync);
117 PUFFSOP_SET(pops, pgfs, fs, statvfs);
118 PUFFSOP_SET(pops, pgfs, node, readdir);
119 PUFFSOP_SET(pops, pgfs, node, getattr);
120 PUFFSOP_SET(pops, pgfs, node, lookup);
121 PUFFSOP_SET(pops, pgfs, node, mkdir);
122 PUFFSOP_SET(pops, pgfs, node, create);
123 PUFFSOP_SET(pops, pgfs, node, read);
124 PUFFSOP_SET(pops, pgfs, node, write);
125 PUFFSOP_SET(pops, pgfs, node, link);
126 PUFFSOP_SET(pops, pgfs, node, remove);
127 PUFFSOP_SET(pops, pgfs, node, rmdir);
128 PUFFSOP_SET(pops, pgfs, node, inactive);
129 PUFFSOP_SET(pops, pgfs, node, setattr);
130 PUFFSOP_SET(pops, pgfs, node, rename);
131 PUFFSOP_SET(pops, pgfs, node, symlink);
132 PUFFSOP_SET(pops, pgfs, node, readlink);
133 PUFFSOP_SET(pops, pgfs, node, access);
134 dosync = (mntflags & MNT_SYNCHRONOUS) != 0;
135 if (!dosync) {
136 PUFFSOP_SET(pops, pgfs, node, fsync);
137 }
138 if (debug) {
139 pflags |= PUFFS_FLAG_OPDUMP;
140 }
141 pu = puffs_init(pops, _PATH_PUFFS, "pgfs", NULL, pflags);
142 if (pu == NULL) {
143 err(EXIT_FAILURE, "puffs_init");
144 }
145 error = pgfs_connectdb(pu, dbname, dbuser, debug, dosync, nconn);
146 if (error != 0) {
147 errno = error;
148 err(EXIT_FAILURE, "pgfs_connectdb");
149 }
150 if (!debug) {
151 if (puffs_daemon(pu, 1, 1)) {
152 err(EXIT_FAILURE, "puffs_daemon");
153 }
154 }
155 if (puffs_mount(pu, argv[1], mntflags, pgfs_root_cookie()) == -1) {
156 err(EXIT_FAILURE, "puffs_mount");
157 }
158 if (!debug) {
159 char tmpl[] = "/tmp/pgfs.XXXXXXXX";
160 const char *path;
161 int fd;
162 int ret;
163
164 path = mkdtemp(tmpl);
165 if (path == NULL) {
166 err(EXIT_FAILURE, "mkdtemp");
167 }
168 fd = open(path, O_RDONLY | O_DIRECTORY);
169 if (fd == -1) {
170 err(EXIT_FAILURE, "open %s", path);
171 }
172 ret = rmdir(path);
173 if (ret != 0) {
174 err(EXIT_FAILURE, "rmdir %s", path);
175 }
176 ret = fchroot(fd);
177 if (ret != 0) {
178 err(EXIT_FAILURE, "fchroot");
179 }
180 ret = close(fd);
181 if (ret != 0) {
182 err(EXIT_FAILURE, "close");
183 }
184 }
185 if (puffs_mainloop(pu) == -1) {
186 err(EXIT_FAILURE, "puffs_mainloop");
187 }
188 exit(EXIT_SUCCESS);
189 }
190