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 int makesock(int port, int proto);
42 #define _RPCSVC_CLOSEDOWN 120
50 (void) signal(sig, closedown);
52 if (_rpcsvcdirty == 0) {
56 if (_rpcfdtype == SOCK_DGRAM)
60 size = getdtablesize();
62 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
63 if (FD_ISSET(i, &svc_fdset))
69 (void) alarm(_RPCSVC_CLOSEDOWN);
73 rpc_init(char *name, int prog, int vers,
74 void (*dispatch)(struct svc_req *, register SVCXPRT *),
77 struct sockaddr_in saddr;
82 asize = sizeof(saddr);
84 if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
85 && saddr.sin_family == AF_INET) {
86 socklen_t ssize = sizeof(int);
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).
94 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
100 pmap_unset(prog, vers);
104 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
105 static SVCXPRT *last_transp = NULL;
107 if (_rpcpmstart == 0) {
109 && (!defport || defport == last_transp->xp_port)) {
110 transp = last_transp;
116 sock = makesock(defport, IPPROTO_UDP);
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.");
125 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
126 xlog(L_FATAL, "unable to register (%s, %d, udp).",
129 last_transp = transp;
132 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
133 static SVCXPRT *last_transp = NULL;
135 if (_rpcpmstart == 0) {
137 && (!defport || defport == last_transp->xp_port)) {
138 transp = last_transp;
144 sock = makesock(defport, IPPROTO_TCP);
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.");
153 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
154 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
157 last_transp = transp;
161 signal(SIGALRM, closedown);
162 alarm(_RPCSVC_CLOSEDOWN);
167 * Create listener socket for a given port
169 * Return an open network socket on success; otherwise return -1
170 * if some error occurs.
173 makesock(int port, int proto)
175 struct sockaddr_in sin;
176 int sock, sock_type, val;
178 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
179 sock = socket(AF_INET, sock_type, proto);
181 xlog(L_FATAL, "Could not make a socket: %s",
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);
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",
197 if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
198 xlog(L_FATAL, "Could not bind name to socket: %s",