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 #define _RPCSVC_CLOSEDOWN 120
48 (void) signal(sig, closedown);
50 if (_rpcsvcdirty == 0) {
54 if (_rpcfdtype == SOCK_DGRAM)
58 size = getdtablesize();
60 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
61 if (FD_ISSET(i, &svc_fdset))
67 (void) alarm(_RPCSVC_CLOSEDOWN);
71 * Create listener socket for a given port
73 * Return an open network socket on success; otherwise return -1
74 * if some error occurs.
77 makesock(int port, int proto)
79 struct sockaddr_in sin;
80 int sock, sock_type, val;
82 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
83 sock = socket(AF_INET, sock_type, proto);
85 xlog(L_FATAL, "Could not make a socket: %s",
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);
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",
101 if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
102 xlog(L_FATAL, "Could not bind name to socket: %s",
111 rpc_init(char *name, int prog, int vers,
112 void (*dispatch)(struct svc_req *, register SVCXPRT *),
115 struct sockaddr_in saddr;
120 asize = sizeof(saddr);
122 if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
123 && saddr.sin_family == AF_INET) {
124 socklen_t ssize = sizeof(int);
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).
132 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
138 pmap_unset(prog, vers);
142 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
143 static SVCXPRT *last_transp = NULL;
145 if (_rpcpmstart == 0) {
147 && (!defport || defport == last_transp->xp_port)) {
148 transp = last_transp;
154 sock = makesock(defport, IPPROTO_UDP);
156 if (sock == RPC_ANYSOCK)
157 sock = svcudp_socket (prog);
158 transp = svcudp_create(sock);
159 if (transp == NULL) {
160 xlog(L_FATAL, "cannot create udp service.");
163 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
164 xlog(L_FATAL, "unable to register (%s, %d, udp).",
167 last_transp = transp;
170 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
171 static SVCXPRT *last_transp = NULL;
173 if (_rpcpmstart == 0) {
175 && (!defport || defport == last_transp->xp_port)) {
176 transp = last_transp;
182 sock = makesock(defport, IPPROTO_TCP);
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.");
191 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
192 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
195 last_transp = transp;
199 signal(SIGALRM, closedown);
200 alarm(_RPCSVC_CLOSEDOWN);