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.
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
20 #include <sys/socket.h>
22 #include <rpc/pmap_clnt.h>
23 #include <netinet/in.h>
34 static void closedown(int sig);
35 static int makesock(int port, int proto, int socksz);
37 #define _RPCSVC_CLOSEDOWN 120
43 rpc_init(char *name, int prog, int vers, void (*dispatch)(), int defport,
46 struct sockaddr_in saddr;
51 asize = sizeof(saddr);
54 if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0) {
55 int ssize = sizeof (int);
56 if (saddr.sin_family != AF_INET)
57 xlog(L_FATAL, "init: stdin is bound to non-inet addr");
58 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
59 (char *)&_rpcfdtype, &ssize) == -1)
60 xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
63 pmap_unset(prog, vers);
67 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
68 static SVCXPRT *last_transp = NULL;
70 if (_rpcfdtype == 0) {
71 if (last_transp && last_transp->xp_port == defport) {
75 if ((sock = makesock(defport, IPPROTO_UDP, bufsiz)) < 0) {
76 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
80 transp = svcudp_create(sock);
82 xlog(L_FATAL, "cannot create udp service.");
85 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
86 xlog(L_FATAL, "unable to register (%s, %d, udp).",
92 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
93 static SVCXPRT *last_transp = NULL;
95 if (_rpcfdtype == 0) {
96 if (last_transp && last_transp->xp_port == defport) {
100 if ((sock = makesock(defport, IPPROTO_TCP, bufsiz)) < 0) {
101 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
105 transp = svctcp_create(sock, 0, 0);
106 if (transp == NULL) {
107 xlog(L_FATAL, "cannot create tcp service.");
110 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
111 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
114 last_transp = transp;
118 signal (SIGALRM, closedown);
119 alarm (_RPCSVC_CLOSEDOWN);
123 static void closedown(sig)
126 (void) signal(sig, closedown);
127 if (_rpcsvcdirty == 0) {
128 extern fd_set svc_fdset;
132 if (_rpcfdtype == SOCK_DGRAM)
135 size = getdtablesize();
137 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
138 if (FD_ISSET(i, &svc_fdset))
143 (void) alarm(_RPCSVC_CLOSEDOWN);
146 static int makesock(port, proto, socksz)
151 struct sockaddr_in sin;
156 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
157 s = socket(AF_INET, sock_type, proto);
159 xlog(L_FATAL, "Could not make a socket: %s\n",
163 memset((char *) &sin, 0, sizeof(sin));
164 sin.sin_family = AF_INET;
165 sin.sin_addr.s_addr = INADDR_ANY;
166 sin.sin_port = htons(port);
169 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
170 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
176 /* 1024 for rpc & transport overheads */
177 sblen = rblen = socksz + 1024;
178 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
179 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
180 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
182 #endif /* SO_SNDBUF */
184 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
185 xlog(L_FATAL, "Could not bind name to socket: %s\n",
193 /* Log an incoming call. */
195 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
198 int buflen=sizeof(buff);
203 if (!xlog_enabled(D_CALL))
207 switch (rqstp->rq_cred.oa_flavor) {
212 struct authunix_parms *unix_cred;
215 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
216 tm = localtime(&unix_cred->aup_time);
217 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
218 tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
219 tm->tm_hour, tm->tm_min, tm->tm_sec,
220 unix_cred->aup_machname,
227 if ((int) unix_cred->aup_len > 0) {
228 snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
233 for (i = 1; i < unix_cred->aup_len; i++) {
234 snprintf(sp, buflen, ",%d",
235 unix_cred->aup_gids[i]);
245 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
247 xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);