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