mount_lfs.c revision 1.14 1 /* $NetBSD: mount_lfs.c,v 1.14 2002/09/21 18:43:35 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. 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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)mount_lfs.c 8.4 (Berkeley) 4/26/95";
45 #else
46 __RCSID("$NetBSD: mount_lfs.c,v 1.14 2002/09/21 18:43:35 christos Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/mount.h>
52
53 #include <ufs/ufs/ufsmount.h>
54
55 #include <err.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <paths.h>
62
63 #include <signal.h>
64
65 #include "mntopts.h"
66 #include "pathnames.h"
67
68 static const struct mntopt mopts[] = {
69 MOPT_STDOPTS,
70 MOPT_UPDATE,
71 MOPT_GETARGS,
72 { NULL }
73 };
74
75 int main __P((int, char *[]));
76 int mount_lfs __P((int, char *[]));
77 static void invoke_cleaner __P((char *));
78 static void usage __P((void));
79 static void kill_daemon __P((char *));
80 static void kill_cleaner __P((char *));
81
82 static int short_rds, cleaner_debug, cleaner_bytes;
83 static char *nsegs;
84
85 #ifndef MOUNT_NOMAIN
86 int
87 main(argc, argv)
88 int argc;
89 char **argv;
90 {
91 return mount_lfs(argc, argv);
92 }
93 #endif
94
95 int
96 mount_lfs(argc, argv)
97 int argc;
98 char *argv[];
99 {
100 struct ufs_args args;
101 int ch, mntflags, noclean, mntsize, oldflags, i;
102 char *fs_name, *options;
103
104 const char *errcause;
105 struct statfs *mntbuf;
106
107 options = NULL;
108 nsegs = "4";
109 mntflags = noclean = 0;
110 cleaner_bytes = 1;
111 while ((ch = getopt(argc, argv, "bdN:no:s")) != -1)
112 switch (ch) {
113 case 'b':
114 cleaner_bytes = !cleaner_bytes;
115 break;
116 case 'd':
117 cleaner_debug = 1;
118 break;
119 case 'n':
120 noclean = 1;
121 break;
122 case 'N':
123 nsegs = optarg;
124 break;
125 case 'o':
126 getmntopts(optarg, mopts, &mntflags, 0);
127 break;
128 case 's':
129 short_rds = 1;
130 break;
131 case '?':
132 default:
133 usage();
134 }
135 argc -= optind;
136 argv += optind;
137
138 if (argc != 2)
139 usage();
140
141 args.fspec = argv[0]; /* the name of the device file */
142 fs_name = argv[1]; /* the mount point */
143
144 #define DEFAULT_ROOTUID -2
145 args.export.ex_root = DEFAULT_ROOTUID;
146 if (mntflags & MNT_RDONLY) {
147 args.export.ex_flags = MNT_EXRDONLY;
148 noclean = 1;
149 } else
150 args.export.ex_flags = 0;
151
152 /*
153 * Record the previous status of this filesystem (if any) before
154 * performing the mount, so we can know whether to start or
155 * kill the cleaner process below.
156 */
157 oldflags = MNT_RDONLY; /* If not mounted, pretend r/o */
158 if (mntflags & MNT_UPDATE) {
159 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
160 err(1, "getmntinfo");
161 for (i = 0; i < mntsize; i++) {
162 if (strcmp(mntbuf[i].f_mntfromname, args.fspec) == 0) {
163 oldflags = mntbuf[i].f_flags;
164 break;
165 }
166 }
167 }
168
169 if (mount(MOUNT_LFS, fs_name, mntflags, &args)) {
170 switch (errno) {
171 case EMFILE:
172 errcause = "mount table full";
173 break;
174 case EINVAL:
175 if (mntflags & MNT_UPDATE)
176 errcause =
177 "specified device does not match mounted device";
178 else
179 errcause = "incorrect super block";
180 break;
181 default:
182 errcause = strerror(errno);
183 break;
184 }
185 errx(1, "%s on %s: %s", args.fspec, fs_name, errcause);
186 }
187
188 /* Not mounting fresh or upgrading to r/w; don't start the cleaner */
189 if (!(oldflags & MNT_RDONLY) || (mntflags & MNT_RDONLY))
190 noclean = 1;
191 if (!noclean)
192 invoke_cleaner(fs_name);
193 /* NOTREACHED */
194
195 /* Downgrade to r/o; kill the cleaner */
196 if ((mntflags & MNT_RDONLY) && !(oldflags & MNT_RDONLY))
197 kill_cleaner(fs_name);
198
199 exit(0);
200 }
201
202 static void
203 kill_daemon(pidname)
204 char *pidname;
205 {
206 FILE *fp;
207 char s[80];
208 pid_t pid;
209
210 fp = fopen(pidname, "r");
211 if (fp) {
212 fgets(s, 80, fp);
213 pid = atoi(s);
214 if (pid)
215 kill(pid, SIGINT);
216 fclose(fp);
217 }
218 }
219
220 static void
221 kill_cleaner(name)
222 char *name;
223 {
224 char *pidname;
225 char *cp;
226 int off;
227
228 /* Parent first */
229 pidname = malloc(strlen(name) + 20 + strlen(_PATH_VARRUN));
230 sprintf(pidname, "%slfs_cleanerd:m:%s.pid", _PATH_VARRUN, name);
231 off = strlen(_PATH_VARRUN);
232 while((cp = strchr(pidname + off, '/')) != NULL)
233 *cp = '|';
234 kill_daemon(pidname);
235
236 /* Then child */
237 sprintf(pidname, "%slfs_cleanerd:s:%s.pid", _PATH_VARRUN, name);
238 off = strlen(_PATH_VARRUN);
239 while((cp = strchr(pidname + off, '/')) != NULL)
240 *cp = '|';
241 kill_daemon(pidname);
242 }
243
244 static void
245 invoke_cleaner(name)
246 char *name;
247 {
248 char *args[6], **ap = args;
249
250 /* Build the argument list. */
251 *ap++ = _PATH_LFS_CLEANERD;
252 if (cleaner_bytes)
253 *ap++ = "-b";
254 if (nsegs) {
255 *ap++ = "-n";
256 *ap++ = nsegs;
257 }
258 if (short_rds)
259 *ap++ = "-s";
260 if (cleaner_debug)
261 *ap++ = "-d";
262 *ap++ = name;
263 *ap = NULL;
264
265 execv(args[0], args);
266 err(1, "exec %s", _PATH_LFS_CLEANERD);
267 }
268
269 static void
270 usage()
271 {
272 (void)fprintf(stderr,
273 "usage: mount_lfs [-dns] [-o options] special node\n");
274 exit(1);
275 }
276