xref: /original-bsd/sbin/nfsiod/nfsiod.c (revision f737e041)
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char copyright[] =
13 "@(#) Copyright (c) 1989, 1993\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif not lint
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)nfsiod.c	8.3 (Berkeley) 02/22/94";
19 #endif not lint
20 
21 #include <sys/param.h>
22 #include <sys/ioctl.h>
23 #include <sys/syslog.h>
24 #include <sys/ucred.h>
25 #include <sys/wait.h>
26 
27 #include <nfs/nfsv2.h>
28 #include <nfs/nfs.h>
29 
30 #include <err.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 /* Global defs */
39 #ifdef DEBUG
40 int debug = 1;
41 #else
42 int debug = 0;
43 #endif
44 
45 void nonfs __P((int));
46 void reapchild __P((int));
47 void usage __P((void));
48 
49 /*
50  * Nfsiod does asynchronous buffered I/O on behalf of the NFS client.
51  * It does not have to be running for correct operation, but will
52  * improve throughput.
53  */
54 int
55 main(argc, argv)
56 	int argc;
57 	char *argv[];
58 {
59 	int ch, num_servers;
60 
61 #define	MAXNFSDCNT      20
62 #define	DEFNFSDCNT       1
63 	num_servers = DEFNFSDCNT;
64 	while ((ch = getopt(argc, argv, "n:")) != EOF)
65 		switch (ch) {
66 		case 'n':
67 			num_servers = atoi(optarg);
68 			if (num_servers < 1 || num_servers > MAXNFSDCNT) {
69 				warnx("nfsiod count %d; reset to %d",
70 				    DEFNFSDCNT);
71 				num_servers = DEFNFSDCNT;
72 			}
73 			break;
74 		case '?':
75 		default:
76 			usage();
77 		}
78 	argc -= optind;
79 	argv += optind;
80 
81 	/*
82 	 * XXX
83 	 * Backward compatibility, trailing number is the count of daemons.
84 	 */
85 	if (argc > 1)
86 		usage();
87 	if (argc == 1) {
88 		num_servers = atoi(argv[0]);
89 		if (num_servers < 1 || num_servers > MAXNFSDCNT) {
90 			warnx("nfsiod count %d; reset to %d", DEFNFSDCNT);
91 			num_servers = DEFNFSDCNT;
92 		}
93 	}
94 
95 	if (debug == 0) {
96 		daemon(0, 0);
97 		(void)signal(SIGHUP, SIG_IGN);
98 		(void)signal(SIGINT, SIG_IGN);
99 		(void)signal(SIGQUIT, SIG_IGN);
100 		(void)signal(SIGSYS, nonfs);
101 	}
102 	(void)signal(SIGCHLD, reapchild);
103 
104 	openlog("nfsiod:", LOG_PID, LOG_DAEMON);
105 
106 	while (num_servers--)
107 		switch (fork()) {
108 		case -1:
109 			syslog(LOG_ERR, "fork: %m");
110 			exit (1);
111 		case 0:
112 			if (nfssvc(NFSSVC_BIOD, NULL) < 0) {
113 				syslog(LOG_ERR, "nfssvc: %m");
114 				exit (1);
115 			}
116 			exit(0);
117 		}
118 	exit (0);
119 }
120 
121 void
122 nonfs(signo)
123 	int signo;
124 {
125 	syslog(LOG_ERR, "missing system call: NFS not available.");
126 }
127 
128 void
129 reapchild(signo)
130 	int signo;
131 {
132 
133 	while (wait3(NULL, WNOHANG, NULL));
134 }
135 
136 void
137 usage()
138 {
139 	(void)fprintf(stderr, "usage: nfsiod [-n num_servers]\n");
140 	exit(1);
141 }
142