1*ef672e50Syamt /* $NetBSD: mount.c,v 1.4 2013/04/22 13:28:28 yamt Exp $ */
249413e37Syamt
349413e37Syamt /*-
449413e37Syamt * Copyright (c)2010,2011 YAMAMOTO Takashi,
549413e37Syamt * All rights reserved.
649413e37Syamt *
749413e37Syamt * Redistribution and use in source and binary forms, with or without
849413e37Syamt * modification, are permitted provided that the following conditions
949413e37Syamt * are met:
1049413e37Syamt * 1. Redistributions of source code must retain the above copyright
1149413e37Syamt * notice, this list of conditions and the following disclaimer.
1249413e37Syamt * 2. Redistributions in binary form must reproduce the above copyright
1349413e37Syamt * notice, this list of conditions and the following disclaimer in the
1449413e37Syamt * documentation and/or other materials provided with the distribution.
1549413e37Syamt *
1649413e37Syamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1749413e37Syamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1849413e37Syamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1949413e37Syamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2049413e37Syamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2149413e37Syamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2249413e37Syamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2349413e37Syamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2449413e37Syamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2549413e37Syamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2649413e37Syamt * SUCH DAMAGE.
2749413e37Syamt */
2849413e37Syamt
2949413e37Syamt #include <sys/cdefs.h>
3049413e37Syamt #ifndef lint
31*ef672e50Syamt __RCSID("$NetBSD: mount.c,v 1.4 2013/04/22 13:28:28 yamt Exp $");
3249413e37Syamt #endif /* not lint */
3349413e37Syamt
3449413e37Syamt #include <err.h>
3549413e37Syamt #include <errno.h>
366b3d697cSyamt #include <locale.h>
3749413e37Syamt #include <mntopts.h>
3849413e37Syamt #include <paths.h>
3949413e37Syamt #include <puffs.h>
4049413e37Syamt #include <stdbool.h>
4149413e37Syamt #include <stdlib.h>
4249413e37Syamt #include <unistd.h>
43*ef672e50Syamt #include <util.h>
4449413e37Syamt
4549413e37Syamt #include "pgfs.h"
4649413e37Syamt #include "pgfs_db.h"
4749413e37Syamt
4849413e37Syamt #define PGFS_MNT_ALT_DUMMY 1
4949413e37Syamt #define PGFS_MNT_ALT_DEBUG 2
5049413e37Syamt
51*ef672e50Syamt static char *
xstrcpy(const char * str)52*ef672e50Syamt xstrcpy(const char *str)
53*ef672e50Syamt {
54*ef672e50Syamt char *n;
55*ef672e50Syamt size_t len;
56*ef672e50Syamt
57*ef672e50Syamt if (str == NULL) {
58*ef672e50Syamt return NULL;
59*ef672e50Syamt }
60*ef672e50Syamt len = strlen(str);
61*ef672e50Syamt n = emalloc(len + 1);
62*ef672e50Syamt memcpy(n, str, len + 1);
63*ef672e50Syamt return n;
64*ef672e50Syamt }
65*ef672e50Syamt
6649413e37Syamt int
main(int argc,char * argv[])6749413e37Syamt main(int argc, char *argv[])
6849413e37Syamt {
6949413e37Syamt extern char *optarg;
7049413e37Syamt extern int optind;
7149413e37Syamt mntoptparse_t mp;
7249413e37Syamt struct puffs_usermount *pu;
7349413e37Syamt struct puffs_ops *pops;
7449413e37Syamt int mntflags;
7549413e37Syamt int altmntflags;
7649413e37Syamt int ch;
7749413e37Syamt int error;
7849413e37Syamt const char *dbname = NULL;
7949413e37Syamt const char *dbuser = NULL;
8049413e37Syamt static const struct mntopt mopts[] = {
8149413e37Syamt MOPT_STDOPTS,
8249413e37Syamt MOPT_SYNC,
8349413e37Syamt { .m_option = "dbname", .m_inverse = 0,
8449413e37Syamt .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
8549413e37Syamt { .m_option = "dbuser", .m_inverse = 0,
8649413e37Syamt .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
8749413e37Syamt { .m_option = "debug", .m_inverse = 0,
8849413e37Syamt .m_flag = PGFS_MNT_ALT_DEBUG, .m_altloc = 1, },
8949413e37Syamt { .m_option = "nconn", .m_inverse = 0,
9049413e37Syamt .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
9149413e37Syamt MOPT_NULL,
9249413e37Syamt };
93c47d2fd4Syamt uint32_t pflags = PUFFS_KFLAG_IAONDEMAND;
9449413e37Syamt unsigned int nconn = 8;
9549413e37Syamt bool debug = false;
9649413e37Syamt bool dosync;
9749413e37Syamt
986b3d697cSyamt setlocale(LC_ALL, "");
996b3d697cSyamt
10049413e37Syamt mntflags = 0;
10149413e37Syamt altmntflags = 0;
10249413e37Syamt while ((ch = getopt(argc, argv, "o:")) != -1) {
10349413e37Syamt long v;
10449413e37Syamt
10549413e37Syamt switch (ch) {
10649413e37Syamt case 'o':
10749413e37Syamt mp = getmntopts(optarg, mopts, &mntflags,
10849413e37Syamt &altmntflags);
10949413e37Syamt if (mp == NULL) {
11049413e37Syamt err(EXIT_FAILURE, "getmntopts");
11149413e37Syamt }
11249413e37Syamt getmnt_silent = 1; /* XXX silly api */
113*ef672e50Syamt dbname = xstrcpy(getmntoptstr(mp, "dbname"));
114*ef672e50Syamt dbuser = xstrcpy(getmntoptstr(mp, "dbuser"));
11549413e37Syamt v = getmntoptnum(mp, "nconn");
11649413e37Syamt getmnt_silent = 0;
11749413e37Syamt if (v != -1) {
11849413e37Syamt nconn = v;
11949413e37Syamt }
12049413e37Syamt if ((altmntflags & PGFS_MNT_ALT_DEBUG) != 0) {
12149413e37Syamt debug = true;
12249413e37Syamt }
12349413e37Syamt freemntopts(mp);
12449413e37Syamt break;
12549413e37Syamt }
12649413e37Syamt }
12749413e37Syamt argc -= optind;
12849413e37Syamt argv += optind;
12949413e37Syamt
13049413e37Syamt PUFFSOP_INIT(pops);
13149413e37Syamt PUFFSOP_SETFSNOP(pops, unmount);
13249413e37Syamt PUFFSOP_SETFSNOP(pops, sync);
13349413e37Syamt PUFFSOP_SET(pops, pgfs, fs, statvfs);
13449413e37Syamt PUFFSOP_SET(pops, pgfs, node, readdir);
13549413e37Syamt PUFFSOP_SET(pops, pgfs, node, getattr);
13649413e37Syamt PUFFSOP_SET(pops, pgfs, node, lookup);
13749413e37Syamt PUFFSOP_SET(pops, pgfs, node, mkdir);
13849413e37Syamt PUFFSOP_SET(pops, pgfs, node, create);
13949413e37Syamt PUFFSOP_SET(pops, pgfs, node, read);
14049413e37Syamt PUFFSOP_SET(pops, pgfs, node, write);
14149413e37Syamt PUFFSOP_SET(pops, pgfs, node, link);
14249413e37Syamt PUFFSOP_SET(pops, pgfs, node, remove);
14349413e37Syamt PUFFSOP_SET(pops, pgfs, node, rmdir);
14449413e37Syamt PUFFSOP_SET(pops, pgfs, node, inactive);
14549413e37Syamt PUFFSOP_SET(pops, pgfs, node, setattr);
14649413e37Syamt PUFFSOP_SET(pops, pgfs, node, rename);
14749413e37Syamt PUFFSOP_SET(pops, pgfs, node, symlink);
14849413e37Syamt PUFFSOP_SET(pops, pgfs, node, readlink);
14949413e37Syamt PUFFSOP_SET(pops, pgfs, node, access);
15049413e37Syamt dosync = (mntflags & MNT_SYNCHRONOUS) != 0;
15149413e37Syamt if (!dosync) {
15249413e37Syamt PUFFSOP_SET(pops, pgfs, node, fsync);
15349413e37Syamt }
15449413e37Syamt if (debug) {
15549413e37Syamt pflags |= PUFFS_FLAG_OPDUMP;
15649413e37Syamt }
15749413e37Syamt pu = puffs_init(pops, _PATH_PUFFS, "pgfs", NULL, pflags);
15849413e37Syamt if (pu == NULL) {
15949413e37Syamt err(EXIT_FAILURE, "puffs_init");
16049413e37Syamt }
16149413e37Syamt error = pgfs_connectdb(pu, dbname, dbuser, debug, dosync, nconn);
162*ef672e50Syamt free(__UNCONST(dbname));
163*ef672e50Syamt free(__UNCONST(dbuser));
16449413e37Syamt if (error != 0) {
16549413e37Syamt errno = error;
16649413e37Syamt err(EXIT_FAILURE, "pgfs_connectdb");
16749413e37Syamt }
16849413e37Syamt if (!debug) {
16949413e37Syamt if (puffs_daemon(pu, 1, 1)) {
17049413e37Syamt err(EXIT_FAILURE, "puffs_daemon");
17149413e37Syamt }
17249413e37Syamt }
17349413e37Syamt if (puffs_mount(pu, argv[1], mntflags, pgfs_root_cookie()) == -1) {
17449413e37Syamt err(EXIT_FAILURE, "puffs_mount");
17549413e37Syamt }
17649413e37Syamt if (!debug) {
17749413e37Syamt char tmpl[] = "/tmp/pgfs.XXXXXXXX";
17849413e37Syamt const char *path;
17949413e37Syamt int fd;
18049413e37Syamt int ret;
18149413e37Syamt
18249413e37Syamt path = mkdtemp(tmpl);
18349413e37Syamt if (path == NULL) {
18449413e37Syamt err(EXIT_FAILURE, "mkdtemp");
18549413e37Syamt }
18649413e37Syamt fd = open(path, O_RDONLY | O_DIRECTORY);
18749413e37Syamt if (fd == -1) {
18849413e37Syamt err(EXIT_FAILURE, "open %s", path);
18949413e37Syamt }
19049413e37Syamt ret = rmdir(path);
19149413e37Syamt if (ret != 0) {
19249413e37Syamt err(EXIT_FAILURE, "rmdir %s", path);
19349413e37Syamt }
19449413e37Syamt ret = fchroot(fd);
19549413e37Syamt if (ret != 0) {
19649413e37Syamt err(EXIT_FAILURE, "fchroot");
19749413e37Syamt }
19849413e37Syamt ret = close(fd);
19949413e37Syamt if (ret != 0) {
20049413e37Syamt err(EXIT_FAILURE, "close");
20149413e37Syamt }
20249413e37Syamt }
20349413e37Syamt if (puffs_mainloop(pu) == -1) {
20449413e37Syamt err(EXIT_FAILURE, "puffs_mainloop");
20549413e37Syamt }
20649413e37Syamt exit(EXIT_SUCCESS);
20749413e37Syamt }
208