1 /*        $NetBSD: mount_chfs.c,v 1.5 2021/07/16 12:09:36 andvar Exp $          */
2 
3 /*-
4  * Copyright (c) 2010 Department of Software Engineering,
5  *                        University of Szeged, Hungary
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by the Department of Software Engineering, University of Szeged, Hungary
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #ifndef lint
35 #endif /* not lint */
36 
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40 #include <ufs/ufs/ufsmount.h>
41 
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <grp.h>
46 #include <mntopts.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #include "mountprog.h"
54 #include "mount_chfs.h"
55 
56 static const struct mntopt mopts[] = {
57           MOPT_STDOPTS,
58           MOPT_GETARGS,
59           MOPT_NULL,
60 };
61 
62 /* --------------------------------------------------------------------- */
63 
64 static void usage(void) __dead;
65 
66 /* --------------------------------------------------------------------- */
67 
68 void
mount_chfs_parseargs(int argc,char * argv[],struct ufs_args * args,int * mntflags,char * canon_dev,char * canon_dir)69 mount_chfs_parseargs(int argc, char *argv[], struct ufs_args *args,
70     int *mntflags, char *canon_dev, char *canon_dir)
71 {
72           int ch;
73           mntoptparse_t mp;
74           struct stat sb;
75 
76           /* Set default values for mount point arguments. */
77           memset(args, 0, sizeof(*args));
78           *mntflags = 0;
79 
80           optind = optreset = 1;
81 
82           while ((ch = getopt(argc, argv, "o:")) != -1) {
83                     switch (ch) {
84                     case 'o':
85                               mp = getmntopts(optarg, mopts, mntflags, 0);
86                               if (mp == NULL)
87                                         err(1, "getmntopts");
88                               freemntopts(mp);
89                               break;
90                     case '?':
91                     default:
92                               usage();
93                     }
94           }
95           argc -= optind;
96           argv += optind;
97 
98           if (argc != 2)
99                     usage();
100 
101           pathadj(argv[0], canon_dev);
102           pathadj(argv[1], canon_dir);
103 
104           args->fspec = canon_dev;
105 
106           if (stat(canon_dir, &sb) == -1) {
107                     err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
108           }
109 
110 }
111 
112 /* --------------------------------------------------------------------- */
113 
114 static void
usage(void)115 usage(void)
116 {
117           (void)fprintf(stderr,
118               "usage: %s special mountpath\n",
119               getprogname());
120           exit(1);
121 }
122 
123 /* --------------------------------------------------------------------- */
124 
125 int
mount_chfs(int argc,char * argv[])126 mount_chfs(int argc, char *argv[])
127 {
128           struct ufs_args args;
129           char canon_dev[MAXPATHLEN], fs_name[MAXPATHLEN];
130           int mntflags;
131 
132           mount_chfs_parseargs(argc, argv, &args, &mntflags,
133               canon_dev, fs_name);
134 
135           if (mount(MOUNT_CHFS, fs_name, mntflags, &args, sizeof args) == -1) {
136                     err(EXIT_FAILURE, "chfs on %s", fs_name);
137           }
138 
139           return EXIT_SUCCESS;
140 }
141 
142 #ifndef MOUNT_NOMAIN
143 int
main(int argc,char * argv[])144 main(int argc, char *argv[])
145 {
146           setprogname(argv[0]);
147           return mount_chfs(argc, argv);
148 }
149 #endif
150