2 * support/nfs/rpcmisc.c
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.
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <sys/socket.h>
24 #include <rpc/pmap_clnt.h>
25 #include <netinet/in.h>
37 #if SIZEOF_SOCKLEN_T - 0 == 0
41 static void closedown(int sig);
42 static int makesock(int port, int proto);
44 #define _RPCSVC_CLOSEDOWN 120
50 rpc_init(char *name, int prog, int vers,
51 void (*dispatch)(struct svc_req *, register SVCXPRT *),
54 struct sockaddr_in saddr;
59 asize = sizeof(saddr);
61 if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
62 && saddr.sin_family == AF_INET) {
63 socklen_t ssize = sizeof (int);
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).
71 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
77 pmap_unset(prog, vers);
81 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
82 static SVCXPRT *last_transp = NULL;
84 if (_rpcpmstart == 0) {
86 && (!defport || defport == last_transp->xp_port)) {
92 else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
93 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
97 if (sock == RPC_ANYSOCK)
98 sock = svcudp_socket (prog, 1);
99 transp = svcudp_create(sock);
100 if (transp == NULL) {
101 xlog(L_FATAL, "cannot create udp service.");
104 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
105 xlog(L_FATAL, "unable to register (%s, %d, udp).",
108 last_transp = transp;
111 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
112 static SVCXPRT *last_transp = NULL;
114 if (_rpcpmstart == 0) {
116 && (!defport || defport == last_transp->xp_port)) {
117 transp = last_transp;
122 else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
123 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
127 if (sock == RPC_ANYSOCK)
128 sock = svctcp_socket (prog, 1);
129 transp = svctcp_create(sock, 0, 0);
130 if (transp == NULL) {
131 xlog(L_FATAL, "cannot create tcp service.");
134 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
135 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
138 last_transp = transp;
142 signal (SIGALRM, closedown);
143 alarm (_RPCSVC_CLOSEDOWN);
147 static void closedown(sig)
150 (void) signal(sig, closedown);
151 if (_rpcsvcdirty == 0) {
155 if (_rpcfdtype == SOCK_DGRAM)
158 size = getdtablesize();
160 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
161 if (FD_ISSET(i, &svc_fdset))
166 (void) alarm(_RPCSVC_CLOSEDOWN);
170 * Create listener socket for a given port
172 * Return an open network socket on success; otherwise return -1
173 * if some error occurs.
176 makesock(int port, int proto)
178 struct sockaddr_in sin;
179 int sock, sock_type, val;
181 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
182 sock = socket(AF_INET, sock_type, proto);
184 xlog(L_FATAL, "Could not make a socket: %s",
188 memset((char *) &sin, 0, sizeof(sin));
189 sin.sin_family = AF_INET;
190 sin.sin_addr.s_addr = htonl(INADDR_ANY);
191 sin.sin_port = htons(port);
194 if (proto == IPPROTO_TCP)
195 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
196 &val, sizeof(val)) < 0)
197 xlog(L_ERROR, "setsockopt failed: %s",
200 if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
201 xlog(L_FATAL, "Could not bind name to socket: %s",