2 * Miscellaneous functions for RPC service startup and shutdown.
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.
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.
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
21 #include <sys/socket.h>
23 #include <rpc/pmap_clnt.h>
24 #include <netinet/in.h>
36 #if SIZEOF_SOCKLEN_T - 0 == 0
40 static void closedown(int sig);
41 static int makesock(int port, int proto);
43 #define _RPCSVC_CLOSEDOWN 120
49 rpc_init(char *name, int prog, int vers,
50 void (*dispatch)(struct svc_req *, register SVCXPRT *),
53 struct sockaddr_in saddr;
58 asize = sizeof(saddr);
60 if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
61 && saddr.sin_family == AF_INET) {
62 socklen_t ssize = sizeof(int);
64 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
65 (char *)&fdtype, &ssize) == -1)
66 xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
67 /* inetd passes a UDP socket or a listening TCP socket.
68 * listen will fail on a connected TCP socket(passed by rsh).
70 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
76 pmap_unset(prog, vers);
80 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
81 static SVCXPRT *last_transp = NULL;
83 if (_rpcpmstart == 0) {
85 && (!defport || defport == last_transp->xp_port)) {
92 sock = makesock(defport, IPPROTO_UDP);
94 if (sock == RPC_ANYSOCK)
95 sock = svcudp_socket (prog, 1);
96 transp = svcudp_create(sock);
98 xlog(L_FATAL, "cannot create udp service.");
101 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
102 xlog(L_FATAL, "unable to register (%s, %d, udp).",
105 last_transp = transp;
108 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
109 static SVCXPRT *last_transp = NULL;
111 if (_rpcpmstart == 0) {
113 && (!defport || defport == last_transp->xp_port)) {
114 transp = last_transp;
120 sock = makesock(defport, IPPROTO_TCP);
122 if (sock == RPC_ANYSOCK)
123 sock = svctcp_socket (prog, 1);
124 transp = svctcp_create(sock, 0, 0);
125 if (transp == NULL) {
126 xlog(L_FATAL, "cannot create tcp service.");
129 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
130 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
133 last_transp = transp;
137 signal(SIGALRM, closedown);
138 alarm(_RPCSVC_CLOSEDOWN);
142 static void closedown(sig)
145 (void) signal(sig, closedown);
146 if (_rpcsvcdirty == 0) {
150 if (_rpcfdtype == SOCK_DGRAM)
153 size = getdtablesize();
155 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
156 if (FD_ISSET(i, &svc_fdset))
161 (void) alarm(_RPCSVC_CLOSEDOWN);
165 * Create listener socket for a given port
167 * Return an open network socket on success; otherwise return -1
168 * if some error occurs.
171 makesock(int port, int proto)
173 struct sockaddr_in sin;
174 int sock, sock_type, val;
176 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
177 sock = socket(AF_INET, sock_type, proto);
179 xlog(L_FATAL, "Could not make a socket: %s",
183 memset((char *) &sin, 0, sizeof(sin));
184 sin.sin_family = AF_INET;
185 sin.sin_addr.s_addr = htonl(INADDR_ANY);
186 sin.sin_port = htons(port);
189 if (proto == IPPROTO_TCP)
190 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
191 &val, sizeof(val)) < 0)
192 xlog(L_ERROR, "setsockopt failed: %s",
195 if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
196 xlog(L_FATAL, "Could not bind name to socket: %s",