xref: /csrg-svn/sbin/nfsiod/nfsiod.c (revision 69179)
138870Smckusick /*
261527Sbostic  * Copyright (c) 1989, 1993
361527Sbostic  *	The Regents of the University of California.  All rights reserved.
438870Smckusick  *
538870Smckusick  * This code is derived from software contributed to Berkeley by
638870Smckusick  * Rick Macklem at The University of Guelph.
738870Smckusick  *
842705Sbostic  * %sccs.include.redist.c%
938870Smckusick  */
1038870Smckusick 
1138870Smckusick #ifndef lint
1261527Sbostic static char copyright[] =
1361527Sbostic "@(#) Copyright (c) 1989, 1993\n\
1461527Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1538870Smckusick #endif not lint
1638870Smckusick 
1738870Smckusick #ifndef lint
18*69179Smckusick static char sccsid[] = "@(#)nfsiod.c	8.4 (Berkeley) 05/03/95";
1938870Smckusick #endif not lint
2038870Smckusick 
2152511Smckusick #include <sys/param.h>
2238870Smckusick #include <sys/ioctl.h>
2366245Sbostic #include <sys/syslog.h>
2466245Sbostic #include <sys/ucred.h>
2552511Smckusick #include <sys/wait.h>
2666245Sbostic 
27*69179Smckusick #include <sys/mount.h>
28*69179Smckusick #include <sys/time.h>
29*69179Smckusick #include <nfs/rpcv2.h>
30*69179Smckusick #include <nfs/nfsproto.h>
3152511Smckusick #include <nfs/nfs.h>
3238870Smckusick 
3366245Sbostic #include <err.h>
3466245Sbostic #include <errno.h>
3566245Sbostic #include <fcntl.h>
3666245Sbostic #include <signal.h>
3766245Sbostic #include <stdio.h>
3866245Sbostic #include <stdlib.h>
3966245Sbostic #include <unistd.h>
4066245Sbostic 
4138870Smckusick /* Global defs */
4238870Smckusick #ifdef DEBUG
4338870Smckusick int debug = 1;
4438870Smckusick #else
4538870Smckusick int debug = 0;
4638870Smckusick #endif
4738870Smckusick 
4866245Sbostic void nonfs __P((int));
4966245Sbostic void reapchild __P((int));
5066245Sbostic void usage __P((void));
5166245Sbostic 
5238870Smckusick /*
5339865Smckusick  * Nfsiod does asynchronous buffered I/O on behalf of the NFS client.
5466245Sbostic  * It does not have to be running for correct operation, but will
5566245Sbostic  * improve throughput.
5638870Smckusick  */
5766245Sbostic int
main(argc,argv)5838870Smckusick main(argc, argv)
5938870Smckusick 	int argc;
6038870Smckusick 	char *argv[];
6138870Smckusick {
6266245Sbostic 	int ch, num_servers;
6338870Smckusick 
6466245Sbostic #define	MAXNFSDCNT      20
6566246Sbostic #define	DEFNFSDCNT       1
6666245Sbostic 	num_servers = DEFNFSDCNT;
6766245Sbostic 	while ((ch = getopt(argc, argv, "n:")) != EOF)
6866245Sbostic 		switch (ch) {
6966245Sbostic 		case 'n':
7066245Sbostic 			num_servers = atoi(optarg);
7166245Sbostic 			if (num_servers < 1 || num_servers > MAXNFSDCNT) {
7266245Sbostic 				warnx("nfsiod count %d; reset to %d",
7366245Sbostic 				    DEFNFSDCNT);
7466245Sbostic 				num_servers = DEFNFSDCNT;
7566245Sbostic 			}
7666245Sbostic 			break;
7766245Sbostic 		case '?':
7866245Sbostic 		default:
7966245Sbostic 			usage();
8066245Sbostic 		}
8166245Sbostic 	argc -= optind;
8266245Sbostic 	argv += optind;
8366245Sbostic 
8466245Sbostic 	/*
8566245Sbostic 	 * XXX
8666245Sbostic 	 * Backward compatibility, trailing number is the count of daemons.
8766245Sbostic 	 */
8866245Sbostic 	if (argc > 1)
8966245Sbostic 		usage();
9066245Sbostic 	if (argc == 1) {
9166245Sbostic 		num_servers = atoi(argv[0]);
9266245Sbostic 		if (num_servers < 1 || num_servers > MAXNFSDCNT) {
9366245Sbostic 			warnx("nfsiod count %d; reset to %d", DEFNFSDCNT);
9466245Sbostic 			num_servers = DEFNFSDCNT;
9566245Sbostic 		}
9666245Sbostic 	}
9766245Sbostic 
9838870Smckusick 	if (debug == 0) {
9944694Skarels 		daemon(0, 0);
10066245Sbostic 		(void)signal(SIGHUP, SIG_IGN);
10166245Sbostic 		(void)signal(SIGINT, SIG_IGN);
10266245Sbostic 		(void)signal(SIGQUIT, SIG_IGN);
10366245Sbostic 		(void)signal(SIGSYS, nonfs);
10438870Smckusick 	}
10566245Sbostic 	(void)signal(SIGCHLD, reapchild);
10666245Sbostic 
10752511Smckusick 	openlog("nfsiod:", LOG_PID, LOG_DAEMON);
10866245Sbostic 
10966245Sbostic 	while (num_servers--)
11066245Sbostic 		switch (fork()) {
11166245Sbostic 		case -1:
11266245Sbostic 			syslog(LOG_ERR, "fork: %m");
11366245Sbostic 			exit (1);
11466245Sbostic 		case 0:
11566245Sbostic 			if (nfssvc(NFSSVC_BIOD, NULL) < 0) {
11666245Sbostic 				syslog(LOG_ERR, "nfssvc: %m");
11766245Sbostic 				exit (1);
11866245Sbostic 			}
11966245Sbostic 			exit(0);
12066245Sbostic 		}
12166245Sbostic 	exit (0);
12238870Smckusick }
12352511Smckusick 
12452511Smckusick void
nonfs(signo)12566245Sbostic nonfs(signo)
12666245Sbostic 	int signo;
12752511Smckusick {
12866245Sbostic 	syslog(LOG_ERR, "missing system call: NFS not available.");
12966245Sbostic }
13052511Smckusick 
13166245Sbostic void
reapchild(signo)13266245Sbostic reapchild(signo)
13366245Sbostic 	int signo;
13466245Sbostic {
13566245Sbostic 
13666245Sbostic 	while (wait3(NULL, WNOHANG, NULL));
13752511Smckusick }
13866245Sbostic 
13966245Sbostic void
usage()14066245Sbostic usage()
14166245Sbostic {
14266245Sbostic 	(void)fprintf(stderr, "usage: nfsiod [-n num_servers]\n");
14366245Sbostic 	exit(1);
14466245Sbostic }
145