xref: /dragonfly/sbin/nfsiod/nfsiod.c (revision 0212bfce)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, 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  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
33  * @(#)nfsiod.c	8.4 (Berkeley) 5/3/95
34  * $FreeBSD: src/sbin/nfsiod/nfsiod.c,v 1.9 1999/08/28 00:13:55 peter Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/syslog.h>
39 #include <sys/wait.h>
40 #include <sys/mount.h>
41 
42 #include <vfs/nfs/rpcv2.h>
43 #include <vfs/nfs/nfs.h>
44 
45 #include <err.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 
50 /* Global defs */
51 #ifdef DEBUG
52 int debug = 1;
53 #else
54 int debug = 0;
55 #endif
56 
57 static void	nonfs(int);
58 static void	reapchild(int);
59 static void	usage(void);
60 
61 /*
62  * Nfsiod does asynchronous buffered I/O on behalf of the NFS client.
63  * It does not have to be running for correct operation, but will
64  * improve throughput.
65  */
66 int
67 main(int argc, char **argv)
68 {
69 	int ch, num_servers;
70 	struct vfsconf vfc;
71 	int error;
72 
73 	error = getvfsbyname("nfs", &vfc);
74 	if (error && vfsisloadable("nfs")) {
75 		if (vfsload("nfs"))
76 			err(1, "vfsload(nfs)");
77 		endvfsent();	/* flush cache */
78 		error = getvfsbyname("nfs", &vfc);
79 	}
80 	if(error)
81 		errx(1, "NFS support is not available in the running kernel");
82 
83 #define	MAXNFSDCNT      20
84 #define	DEFNFSDCNT       1
85 	num_servers = DEFNFSDCNT;
86 	while ((ch = getopt(argc, argv, "n:")) != -1)
87 		switch (ch) {
88 		case 'n':
89 			num_servers = atoi(optarg);
90 			if (num_servers < 1 || num_servers > MAXNFSDCNT) {
91 				warnx("nfsiod count %d; reset to %d",
92 				    num_servers, DEFNFSDCNT);
93 				num_servers = DEFNFSDCNT;
94 			}
95 			break;
96 		default:
97 			usage();
98 		}
99 	argc -= optind;
100 	argv += optind;
101 
102 	/*
103 	 * XXX
104 	 * Backward compatibility, trailing number is the count of daemons.
105 	 */
106 	if (argc > 1)
107 		usage();
108 	if (argc == 1) {
109 		num_servers = atoi(argv[0]);
110 		if (num_servers < 1 || num_servers > MAXNFSDCNT) {
111 			warnx("nfsiod count %d; reset to %d", num_servers,
112 			    DEFNFSDCNT);
113 			num_servers = DEFNFSDCNT;
114 		}
115 	}
116 
117 	if (debug == 0) {
118 		daemon(0, 0);
119 		signal(SIGHUP, SIG_IGN);
120 		signal(SIGINT, SIG_IGN);
121 		signal(SIGQUIT, SIG_IGN);
122 		signal(SIGSYS, nonfs);
123 	}
124 	signal(SIGCHLD, reapchild);
125 
126 	openlog("nfsiod:", LOG_PID, LOG_DAEMON);
127 
128 	while (num_servers--)
129 		switch (fork()) {
130 		case -1:
131 			syslog(LOG_ERR, "fork: %m");
132 			exit (1);
133 		case 0:
134 			if (nfssvc(NFSSVC_BIOD, NULL) < 0) {
135 				syslog(LOG_ERR, "nfssvc: %m");
136 				exit (1);
137 			}
138 			exit(0);
139 		}
140 	exit (0);
141 }
142 
143 static void
144 nonfs(int signo __unused)
145 {
146 	syslog(LOG_ERR, "missing system call: NFS not available");
147 }
148 
149 static void
150 reapchild(int signo __unused)
151 {
152 
153 	while (wait3(NULL, WNOHANG, NULL) > 0)
154 		;
155 }
156 
157 static void
158 usage(void)
159 {
160 	fprintf(stderr, "usage: nfsiod [-n num_servers]\n");
161 	exit(1);
162 }
163