xref: /csrg-svn/sbin/nfsd/nfsd.c (revision 38458)
1*38458Smckusick /*
2*38458Smckusick  * Copyright (c) 1989 The Regents of the University of California.
3*38458Smckusick  * All rights reserved.
4*38458Smckusick  *
5*38458Smckusick  * This code is derived from software contributed to Berkeley by
6*38458Smckusick  * Rick Macklem at The University of Guelph.
7*38458Smckusick  *
8*38458Smckusick  * Redistribution and use in source and binary forms are permitted
9*38458Smckusick  * provided that the above copyright notice and this paragraph are
10*38458Smckusick  * duplicated in all such forms and that any documentation,
11*38458Smckusick  * advertising materials, and other materials related to such
12*38458Smckusick  * distribution and use acknowledge that the software was developed
13*38458Smckusick  * by the University of California, Berkeley.  The name of the
14*38458Smckusick  * University may not be used to endorse or promote products derived
15*38458Smckusick  * from this software without specific prior written permission.
16*38458Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17*38458Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18*38458Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19*38458Smckusick  */
20*38458Smckusick 
21*38458Smckusick #ifndef lint
22*38458Smckusick char copyright[] =
23*38458Smckusick "@(#) Copyright (c) 1989 Regents of the University of California.\n\
24*38458Smckusick  All rights reserved.\n";
25*38458Smckusick #endif not lint
26*38458Smckusick 
27*38458Smckusick #ifndef lint
28*38458Smckusick static char sccsid[] = "@(#)nfsd.c	5.1 (Berkeley) 07/16/89";
29*38458Smckusick #endif not lint
30*38458Smckusick 
31*38458Smckusick #include <stdio.h>
32*38458Smckusick #include <syslog.h>
33*38458Smckusick #include <signal.h>
34*38458Smckusick #include <fcntl.h>
35*38458Smckusick #include <sys/types.h>
36*38458Smckusick #include <sys/ioctl.h>
37*38458Smckusick #include <sys/stat.h>
38*38458Smckusick #include <sys/dir.h>
39*38458Smckusick #include <sys/uio.h>
40*38458Smckusick #include <sys/namei.h>
41*38458Smckusick #include <sys/mount.h>
42*38458Smckusick #include <sys/socket.h>
43*38458Smckusick #include <sys/socketvar.h>
44*38458Smckusick #include <netdb.h>
45*38458Smckusick #include <rpc/rpc.h>
46*38458Smckusick #include <rpc/pmap_clnt.h>
47*38458Smckusick #include <rpc/pmap_prot.h>
48*38458Smckusick #include <nfs/rpcv2.h>
49*38458Smckusick #include <nfs/nfsv2.h>
50*38458Smckusick 
51*38458Smckusick /* Global defs */
52*38458Smckusick #ifdef DEBUG
53*38458Smckusick #define	syslog(e, s)	fprintf(stderr,(s))
54*38458Smckusick int debug = 1;
55*38458Smckusick #else
56*38458Smckusick int debug = 0;
57*38458Smckusick #endif
58*38458Smckusick 
59*38458Smckusick /*
60*38458Smckusick  * Nfs server daemon mostly just a user context for nfssvc()
61*38458Smckusick  * 1 - do file descriptor and signal cleanup
62*38458Smckusick  * 2 - create server socket
63*38458Smckusick  * 3 - register socket with portmap
64*38458Smckusick  * 4 - nfssvc(sock)
65*38458Smckusick  */
66*38458Smckusick main(argc, argv)
67*38458Smckusick 	int argc;
68*38458Smckusick 	char *argv[];
69*38458Smckusick {
70*38458Smckusick 	register int i;
71*38458Smckusick 	int cnt, sock;
72*38458Smckusick 	struct sockaddr_in saddr;
73*38458Smckusick 
74*38458Smckusick 	if (debug == 0) {
75*38458Smckusick 		if (fork())
76*38458Smckusick 			exit(0);
77*38458Smckusick 		{ int s;
78*38458Smckusick 		for (s = 0; s < 10; s++)
79*38458Smckusick 			(void) close(s);
80*38458Smckusick 		}
81*38458Smckusick 		(void) open("/", O_RDONLY);
82*38458Smckusick 		(void) dup2(0, 1);
83*38458Smckusick 		(void) dup2(0, 2);
84*38458Smckusick 		{ int tt = open("/dev/tty", O_RDWR);
85*38458Smckusick 		  if (tt > 0) {
86*38458Smckusick 			ioctl(tt, TIOCNOTTY, (char *)0);
87*38458Smckusick 			close(tt);
88*38458Smckusick 		  }
89*38458Smckusick 		}
90*38458Smckusick 		(void) setpgrp(0, 0);
91*38458Smckusick 		signal(SIGTSTP, SIG_IGN);
92*38458Smckusick 		signal(SIGTTIN, SIG_IGN);
93*38458Smckusick 		signal(SIGTTOU, SIG_IGN);
94*38458Smckusick 		signal(SIGINT, SIG_IGN);
95*38458Smckusick 		signal(SIGQUIT, SIG_IGN);
96*38458Smckusick 		signal(SIGTERM, SIG_IGN);
97*38458Smckusick 		signal(SIGHUP, SIG_IGN);
98*38458Smckusick 	}
99*38458Smckusick 	openlog("nfsd:", LOG_PID, LOG_DAEMON);
100*38458Smckusick 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
101*38458Smckusick 		syslog(LOG_ERR, "Can't create socket");
102*38458Smckusick 		exit(1);
103*38458Smckusick 	}
104*38458Smckusick 	saddr.sin_family = AF_INET;
105*38458Smckusick 	saddr.sin_addr.s_addr = INADDR_ANY;
106*38458Smckusick 	saddr.sin_port = htons(NFS_PORT);
107*38458Smckusick 	if (bind(sock, &saddr, sizeof(saddr)) < 0) {
108*38458Smckusick 		syslog(LOG_ERR, "Can't bind addr");
109*38458Smckusick 		exit(1);
110*38458Smckusick 	}
111*38458Smckusick 	pmap_unset(RPCPROG_NFS, NFS_VER2);
112*38458Smckusick 	if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
113*38458Smckusick 		syslog(LOG_ERR, "Can't register with portmap");
114*38458Smckusick 		exit(1);
115*38458Smckusick 	}
116*38458Smckusick 	if (argc != 2 || (cnt = atoi(argv[1])) <= 0 || cnt > 20)
117*38458Smckusick 		cnt = 1;
118*38458Smckusick 	for (i = 1; i < cnt; i++)
119*38458Smckusick 		if (fork() == 0)
120*38458Smckusick 			break;
121*38458Smckusick 	if (nfssvc(sock) < 0)		/* Only returns on error */
122*38458Smckusick 		syslog(LOG_ERR, "nfssvc() failed %m");
123*38458Smckusick 	exit();
124*38458Smckusick }
125