xref: /freebsd/sbin/nfsiod/nfsiod.c (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif not lint
42 
43 #ifndef lint
44 static char sccsid[] = "@(#)nfsiod.c	8.4 (Berkeley) 5/3/95";
45 #endif not lint
46 
47 #include <sys/param.h>
48 #include <sys/ioctl.h>
49 #include <sys/syslog.h>
50 #include <sys/ucred.h>
51 #include <sys/wait.h>
52 #include <sys/mount.h>
53 #include <sys/time.h>
54 
55 #include <nfs/rpcv2.h>
56 #include <nfs/nfsproto.h>
57 #include <nfs/nfs.h>
58 
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <signal.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66 
67 /* Global defs */
68 #ifdef DEBUG
69 int debug = 1;
70 #else
71 int debug = 0;
72 #endif
73 
74 void nonfs __P((int));
75 void reapchild __P((int));
76 void usage __P((void));
77 
78 /*
79  * Nfsiod does asynchronous buffered I/O on behalf of the NFS client.
80  * It does not have to be running for correct operation, but will
81  * improve throughput.
82  */
83 int
84 main(argc, argv)
85 	int argc;
86 	char *argv[];
87 {
88 	int ch, num_servers;
89 	struct vfsconf vfc;
90 	int error;
91 
92 	error = getvfsbyname("nfs", &vfc);
93 	if (error && vfsisloadable("nfs")) {
94 		if (vfsload("nfs"))
95 			err(1, "vfsload(nfs)");
96 		endvfsent();	/* flush cache */
97 		error = getvfsbyname("nfs", &vfc);
98 	}
99 	if(error)
100 		errx(1, "NFS support is not available in the running kernel");
101 
102 #define	MAXNFSDCNT      20
103 #define	DEFNFSDCNT       1
104 	num_servers = DEFNFSDCNT;
105 	while ((ch = getopt(argc, argv, "n:")) != EOF)
106 		switch (ch) {
107 		case 'n':
108 			num_servers = atoi(optarg);
109 			if (num_servers < 1 || num_servers > MAXNFSDCNT) {
110 				warnx("nfsiod count %d; reset to %d",
111 				    DEFNFSDCNT);
112 				num_servers = DEFNFSDCNT;
113 			}
114 			break;
115 		case '?':
116 		default:
117 			usage();
118 		}
119 	argc -= optind;
120 	argv += optind;
121 
122 	/*
123 	 * XXX
124 	 * Backward compatibility, trailing number is the count of daemons.
125 	 */
126 	if (argc > 1)
127 		usage();
128 	if (argc == 1) {
129 		num_servers = atoi(argv[0]);
130 		if (num_servers < 1 || num_servers > MAXNFSDCNT) {
131 			warnx("nfsiod count %d; reset to %d", DEFNFSDCNT);
132 			num_servers = DEFNFSDCNT;
133 		}
134 	}
135 
136 	if (debug == 0) {
137 		daemon(0, 0);
138 		(void)signal(SIGHUP, SIG_IGN);
139 		(void)signal(SIGINT, SIG_IGN);
140 		(void)signal(SIGQUIT, SIG_IGN);
141 		(void)signal(SIGSYS, nonfs);
142 	}
143 	(void)signal(SIGCHLD, reapchild);
144 
145 	openlog("nfsiod:", LOG_PID, LOG_DAEMON);
146 
147 	while (num_servers--)
148 		switch (fork()) {
149 		case -1:
150 			syslog(LOG_ERR, "fork: %m");
151 			exit (1);
152 		case 0:
153 			if (nfssvc(NFSSVC_BIOD, NULL) < 0) {
154 				syslog(LOG_ERR, "nfssvc: %m");
155 				exit (1);
156 			}
157 			exit(0);
158 		}
159 	exit (0);
160 }
161 
162 void
163 nonfs(signo)
164 	int signo;
165 {
166 	syslog(LOG_ERR, "missing system call: NFS not available.");
167 }
168 
169 void
170 reapchild(signo)
171 	int signo;
172 {
173 
174 	while (wait3(NULL, WNOHANG, NULL) > 0) {
175 		/* nothing */
176 	};
177 }
178 
179 void
180 usage()
181 {
182 	(void)fprintf(stderr, "usage: nfsiod [-n num_servers]\n");
183 	exit(1);
184 }
185