Bring in the "Port PUFFS from NetBSD/FreeBSD" GSoC 2011 project results.
[dragonfly.git] / share / examples / puffs / pnullfs / pnullfs.c
1 /*      $NetBSD: pnullfs.c,v 1.18 2008/11/26 14:03:48 pooka Exp $       */
2
3 /*
4  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 /*
29  * pnullfs: puffs nullfs example
30  */
31
32 #include <err.h>
33 #include <puffs.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 static void usage(void);
39
40 static void
41 usage()
42 {
43
44         errx(1, "usage: %s [-s] [-o mntopts] nullpath mountpath",
45             getprogname());
46 }
47
48 int
49 main(int argc, char *argv[])
50 {
51         struct puffs_usermount *pu;
52         struct puffs_ops *pops;
53         struct puffs_pathobj *po_root;
54         struct puffs_node *pn_root;
55         struct stat sb;
56         int mntflags, pflags;
57         int ch;
58         int detach;
59
60         setprogname(argv[0]);
61
62         if (argc < 3)
63                 usage();
64
65         pflags = mntflags = 0;
66         detach = 1;
67         while ((ch = getopt(argc, argv, "o:s")) != -1) {
68                 switch (ch) {
69                 case 'o':
70                         getmntopts(optarg, puffsmopts, &mntflags, &pflags);
71                         break;
72                 case 's':
73                         detach = 0;
74                         break;
75                 }
76         }
77         pflags |= PUFFS_FLAG_BUILDPATH;
78         argv += optind;
79         argc -= optind;
80
81         if (pflags & PUFFS_FLAG_OPDUMP)
82                 detach = 0;
83
84         if (argc != 2)
85                 usage();
86
87         if (lstat(argv[0], &sb) == -1)
88                 err(1, "stat %s", argv[0]);
89         if ((sb.st_mode & S_IFDIR) == 0)
90                 errx(1, "%s is not a directory", argv[0]);
91
92         PUFFSOP_INIT(pops);
93         puffs_null_setops(pops);
94
95         if ((pu = puffs_init(pops, argv[0], "pnullfs", NULL, pflags)) == NULL)
96                 err(1, "init");
97
98         pn_root = puffs_pn_new(pu, NULL);
99         if (pn_root == NULL)
100                 err(1, "puffs_pn_new");
101         puffs_setroot(pu, pn_root);
102         puffs_setfhsize(pu, 0, PUFFS_FHFLAG_PASSTHROUGH);
103
104         po_root = puffs_getrootpathobj(pu);
105         if (po_root == NULL)
106                 err(1, "getrootpathobj");
107         po_root->po_path = argv[0];
108         po_root->po_len = strlen(argv[0]);
109         puffs_stat2vattr(&pn_root->pn_va, &sb);
110
111         if (detach)
112                 if (puffs_daemon(pu, 1, 1) == -1)
113                         err(1, "puffs_daemon");
114
115         if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
116                 err(1, "puffs_mount");
117         if (puffs_mainloop(pu) == -1)
118                 err(1, "mainloop");
119
120         return 0;
121 }