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