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 static void closedown(int sig);
38 int makesock(int port, int proto);
40 #define _RPCSVC_CLOSEDOWN 120
46 rpc_init(char *name, int prog, int vers, void (*dispatch)(), int defport)
48 struct sockaddr_in saddr;
53 asize = sizeof(saddr);
55 if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
56 && saddr.sin_family == AF_INET) {
57 int ssize = sizeof (int);
59 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
60 (char *)&fdtype, &ssize) == -1)
61 xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
62 /* inetd passes a UDP socket or a listening TCP socket.
63 * listen will fail on a connected TCP socket(passed by rsh).
65 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
71 pmap_unset(prog, vers);
75 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
76 static SVCXPRT *last_transp = NULL;
78 if (_rpcpmstart == 0) {
80 && (!defport || defport == last_transp->xp_port)) {
86 else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
87 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
91 if (sock == RPC_ANYSOCK)
92 sock = svcudp_socket (prog, 1);
93 transp = svcudp_create(sock);
95 xlog(L_FATAL, "cannot create udp service.");
98 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
99 xlog(L_FATAL, "unable to register (%s, %d, udp).",
102 last_transp = transp;
105 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
106 static SVCXPRT *last_transp = NULL;
108 if (_rpcpmstart == 0) {
110 && (!defport || defport == last_transp->xp_port)) {
111 transp = last_transp;
116 else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
117 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
121 if (sock == RPC_ANYSOCK)
122 sock = svctcp_socket (prog, 1);
123 transp = svctcp_create(sock, 0, 0);
124 if (transp == NULL) {
125 xlog(L_FATAL, "cannot create tcp service.");
128 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
129 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
132 last_transp = transp;
136 signal (SIGALRM, closedown);
137 alarm (_RPCSVC_CLOSEDOWN);
141 static void closedown(sig)
144 (void) signal(sig, closedown);
145 if (_rpcsvcdirty == 0) {
149 if (_rpcfdtype == SOCK_DGRAM)
152 size = getdtablesize();
154 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
155 if (FD_ISSET(i, &svc_fdset))
160 (void) alarm(_RPCSVC_CLOSEDOWN);
163 int makesock(int port, int proto)
165 struct sockaddr_in sin;
170 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
171 s = socket(AF_INET, sock_type, proto);
173 xlog(L_FATAL, "Could not make a socket: %s\n",
177 memset((char *) &sin, 0, sizeof(sin));
178 sin.sin_family = AF_INET;
179 sin.sin_addr.s_addr = INADDR_ANY;
180 sin.sin_port = htons(port);
183 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
184 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
187 /* I was told it didn't work with gigabit ethernet.
188 Don't bothet with it. H.J. */
193 /* 1024 for rpc & transport overheads */
194 sblen = rblen = socksz + 1024;
195 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
196 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
197 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
199 #endif /* SO_SNDBUF */
202 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
203 xlog(L_FATAL, "Could not bind name to socket: %s\n",
211 /* Log an incoming call. */
213 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
216 int buflen=sizeof(buff);
221 if (!xlog_enabled(D_CALL))
225 switch (rqstp->rq_cred.oa_flavor) {
230 struct authunix_parms *unix_cred;
233 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
234 tm = localtime(&unix_cred->aup_time);
235 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
236 tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
237 tm->tm_hour, tm->tm_min, tm->tm_sec,
238 unix_cred->aup_machname,
245 if ((int) unix_cred->aup_len > 0) {
246 snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
251 for (i = 1; i < unix_cred->aup_len; i++) {
252 snprintf(sp, buflen, ",%d",
253 unix_cred->aup_gids[i]);
263 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
265 xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);