]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/rpcmisc.c
nfs-utils: Remove excess log reporting
[nfs-utils.git] / support / nfs / rpcmisc.c
1 /*
2  * support/nfs/rpcmisc.c
3  *
4  * Miscellaneous functions for RPC startup and shutdown.
5  * This code is partially snarfed from rpcgen -s tcp -s udp,
6  * partly written by Mark Shand, Donald Becker, and Rick 
7  * Sladkey. It was tweaked slightly by Olaf Kirch to be
8  * usable by both unfsd and mountd.
9  *
10  * This software may be used for any purpose provided
11  * the above copyright notice is retained.  It is supplied
12  * as is, with no warranty expressed or implied.
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <rpc/rpc.h>
24 #include <rpc/pmap_clnt.h>
25 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <fcntl.h>
31 #include <memory.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <time.h>
35 #include "nfslib.h"
36
37 #if SIZEOF_SOCKLEN_T - 0 == 0
38 #define socklen_t int
39 #endif
40
41 static void     closedown(int sig);
42 static int      makesock(int port, int proto);
43
44 #define _RPCSVC_CLOSEDOWN       120
45 int     _rpcpmstart = 0;
46 int     _rpcfdtype = 0;
47 int     _rpcsvcdirty = 0;
48
49 void
50 rpc_init(char *name, int prog, int vers,
51          void (*dispatch)(struct svc_req *, register SVCXPRT *),
52          int defport)
53 {
54         struct sockaddr_in saddr;
55         SVCXPRT *transp;
56         int     sock;
57         socklen_t asize;
58
59         asize = sizeof(saddr);
60         sock = 0;
61         if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
62             && saddr.sin_family == AF_INET) {
63                 socklen_t ssize = sizeof (int);
64                 int fdtype = 0;
65                 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
66                                 (char *)&fdtype, &ssize) == -1)
67                         xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
68                 /* inetd passes a UDP socket or a listening TCP socket.
69                  * listen will fail on a connected TCP socket(passed by rsh).
70                  */
71                 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
72                         _rpcfdtype = fdtype;
73                         _rpcpmstart = 1;
74                 }
75         }
76         if (!_rpcpmstart) {
77                 pmap_unset(prog, vers);
78                 sock = RPC_ANYSOCK;
79         }
80
81         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
82                 static SVCXPRT *last_transp = NULL;
83  
84                 if (_rpcpmstart == 0) {
85                         if (last_transp
86                             && (!defport || defport == last_transp->xp_port)) {
87                                 transp = last_transp;
88                                 goto udp_transport;
89                         }
90                         if (defport == 0)
91                                 sock = RPC_ANYSOCK;
92                         else
93                                 sock = makesock(defport, IPPROTO_UDP);
94                 }
95                 if (sock == RPC_ANYSOCK)
96                         sock = svcudp_socket (prog, 1);
97                 transp = svcudp_create(sock);
98                 if (transp == NULL) {
99                         xlog(L_FATAL, "cannot create udp service.");
100                 }
101       udp_transport:
102                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
103                         xlog(L_FATAL, "unable to register (%s, %d, udp).",
104                                         name, vers);
105                 }
106                 last_transp = transp;
107         }
108
109         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
110                 static SVCXPRT *last_transp = NULL;
111
112                 if (_rpcpmstart == 0) {
113                         if (last_transp
114                             && (!defport || defport == last_transp->xp_port)) {
115                                 transp = last_transp;
116                                 goto tcp_transport;
117                         }
118                         if (defport == 0)
119                                 sock = RPC_ANYSOCK;
120                         else
121                                 sock = makesock(defport, IPPROTO_TCP);
122                 }
123                 if (sock == RPC_ANYSOCK)
124                         sock = svctcp_socket (prog, 1);
125                 transp = svctcp_create(sock, 0, 0);
126                 if (transp == NULL) {
127                         xlog(L_FATAL, "cannot create tcp service.");
128                 }
129       tcp_transport:
130                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
131                         xlog(L_FATAL, "unable to register (%s, %d, tcp).",
132                                         name, vers);
133                 }
134                 last_transp = transp;
135         }
136
137         if (_rpcpmstart) {
138                 signal (SIGALRM, closedown);
139                 alarm (_RPCSVC_CLOSEDOWN);
140         }
141 }
142
143 static void closedown(sig)
144 int sig;
145 {
146         (void) signal(sig, closedown);
147         if (_rpcsvcdirty == 0) {
148                 static int size;
149                 int i, openfd;
150
151                 if (_rpcfdtype == SOCK_DGRAM)
152                         exit(0);
153                 if (size == 0) {
154                         size = getdtablesize();
155                 }
156                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
157                         if (FD_ISSET(i, &svc_fdset))
158                                 openfd++;
159                 if (openfd <= 1)
160                         exit(0);
161         }
162         (void) alarm(_RPCSVC_CLOSEDOWN);
163 }
164
165 /*
166  * Create listener socket for a given port
167  *
168  * Return an open network socket on success; otherwise return -1
169  * if some error occurs.
170  */
171 static int
172 makesock(int port, int proto)
173 {
174         struct sockaddr_in sin;
175         int     sock, sock_type, val;
176
177         sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
178         sock = socket(AF_INET, sock_type, proto);
179         if (sock < 0) {
180                 xlog(L_FATAL, "Could not make a socket: %s",
181                                         strerror(errno));
182                 return -1;
183         }
184         memset((char *) &sin, 0, sizeof(sin));
185         sin.sin_family = AF_INET;
186         sin.sin_addr.s_addr = htonl(INADDR_ANY);
187         sin.sin_port = htons(port);
188
189         val = 1;
190         if (proto == IPPROTO_TCP)
191                 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
192                                &val, sizeof(val)) < 0)
193                         xlog(L_ERROR, "setsockopt failed: %s",
194                              strerror(errno));
195
196         if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
197                 xlog(L_FATAL, "Could not bind name to socket: %s",
198                                         strerror(errno));
199                 return -1;
200         }
201
202         return sock;
203 }