]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/rpcmisc.c
Now that only the Section names are case-insensitive
[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 #define _RPCSVC_CLOSEDOWN       120
41 int     _rpcpmstart = 0;
42 int     _rpcfdtype = 0;
43 int     _rpcsvcdirty = 0;
44
45 static void
46 closedown(int sig)
47 {
48         (void) signal(sig, closedown);
49
50         if (_rpcsvcdirty == 0) {
51                 static int size;
52                 int i, openfd;
53
54                 if (_rpcfdtype == SOCK_DGRAM)
55                         exit(0);
56
57                 if (size == 0)
58                         size = getdtablesize();
59
60                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
61                         if (FD_ISSET(i, &svc_fdset))
62                                 openfd++;
63                 if (openfd <= 1)
64                         exit(0);
65         }
66
67         (void) alarm(_RPCSVC_CLOSEDOWN);
68 }
69
70 /*
71  * Create listener socket for a given port
72  *
73  * Return an open network socket on success; otherwise return -1
74  * if some error occurs.
75  */
76 static int
77 makesock(int port, int proto)
78 {
79         struct sockaddr_in sin;
80         int     sock, sock_type, val;
81
82         sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
83         sock = socket(AF_INET, sock_type, proto);
84         if (sock < 0) {
85                 xlog(L_FATAL, "Could not make a socket: %s",
86                                         strerror(errno));
87                 return -1;
88         }
89         memset((char *) &sin, 0, sizeof(sin));
90         sin.sin_family = AF_INET;
91         sin.sin_addr.s_addr = htonl(INADDR_ANY);
92         sin.sin_port = htons(port);
93
94         val = 1;
95         if (proto == IPPROTO_TCP)
96                 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
97                                &val, sizeof(val)) < 0)
98                         xlog(L_ERROR, "setsockopt failed: %s",
99                              strerror(errno));
100
101         if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
102                 xlog(L_FATAL, "Could not bind name to socket: %s",
103                                         strerror(errno));
104                 return -1;
105         }
106
107         return sock;
108 }
109
110 void
111 rpc_init(char *name, int prog, int vers,
112          void (*dispatch)(struct svc_req *, register SVCXPRT *),
113          int defport)
114 {
115         struct sockaddr_in saddr;
116         SVCXPRT *transp;
117         int     sock;
118         socklen_t asize;
119
120         asize = sizeof(saddr);
121         sock = 0;
122         if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
123             && saddr.sin_family == AF_INET) {
124                 socklen_t ssize = sizeof(int);
125                 int fdtype = 0;
126                 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
127                                 (char *)&fdtype, &ssize) == -1)
128                         xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
129                 /* inetd passes a UDP socket or a listening TCP socket.
130                  * listen will fail on a connected TCP socket(passed by rsh).
131                  */
132                 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
133                         _rpcfdtype = fdtype;
134                         _rpcpmstart = 1;
135                 }
136         }
137         if (!_rpcpmstart) {
138                 pmap_unset(prog, vers);
139                 sock = RPC_ANYSOCK;
140         }
141
142         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
143                 static SVCXPRT *last_transp = NULL;
144
145                 if (_rpcpmstart == 0) {
146                         if (last_transp
147                             && (!defport || defport == last_transp->xp_port)) {
148                                 transp = last_transp;
149                                 goto udp_transport;
150                         }
151                         if (defport == 0)
152                                 sock = RPC_ANYSOCK;
153                         else
154                                 sock = makesock(defport, IPPROTO_UDP);
155                 }
156                 if (sock == RPC_ANYSOCK)
157                         sock = svcudp_socket (prog, 1);
158                 transp = svcudp_create(sock);
159                 if (transp == NULL) {
160                         xlog(L_FATAL, "cannot create udp service.");
161                 }
162       udp_transport:
163                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
164                         xlog(L_FATAL, "unable to register (%s, %d, udp).",
165                                         name, vers);
166                 }
167                 last_transp = transp;
168         }
169
170         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
171                 static SVCXPRT *last_transp = NULL;
172
173                 if (_rpcpmstart == 0) {
174                         if (last_transp
175                             && (!defport || defport == last_transp->xp_port)) {
176                                 transp = last_transp;
177                                 goto tcp_transport;
178                         }
179                         if (defport == 0)
180                                 sock = RPC_ANYSOCK;
181                         else
182                                 sock = makesock(defport, IPPROTO_TCP);
183                 }
184                 if (sock == RPC_ANYSOCK)
185                         sock = svctcp_socket (prog, 1);
186                 transp = svctcp_create(sock, 0, 0);
187                 if (transp == NULL) {
188                         xlog(L_FATAL, "cannot create tcp service.");
189                 }
190       tcp_transport:
191                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
192                         xlog(L_FATAL, "unable to register (%s, %d, tcp).",
193                                         name, vers);
194                 }
195                 last_transp = transp;
196         }
197
198         if (_rpcpmstart) {
199                 signal(SIGALRM, closedown);
200                 alarm(_RPCSVC_CLOSEDOWN);
201         }
202 }