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>
35 static void closedown(int sig);
36 int makesock(int port, int proto);
38 #define _RPCSVC_CLOSEDOWN 120
44 rpc_init(char *name, int prog, int vers, void (*dispatch)(), int defport)
46 struct sockaddr_in saddr;
51 asize = sizeof(saddr);
53 if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0) {
54 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) {
72 && (!defport || defport == last_transp->xp_port)) {
76 if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
77 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
81 transp = svcudp_create(sock);
83 xlog(L_FATAL, "cannot create udp service.");
86 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
87 xlog(L_FATAL, "unable to register (%s, %d, udp).",
93 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
94 static SVCXPRT *last_transp = NULL;
96 if (_rpcfdtype == 0) {
98 && (!defport || defport == last_transp->xp_port)) {
102 if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
103 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
107 transp = svctcp_create(sock, 0, 0);
108 if (transp == NULL) {
109 xlog(L_FATAL, "cannot create tcp service.");
112 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
113 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
116 last_transp = transp;
120 signal (SIGALRM, closedown);
121 alarm (_RPCSVC_CLOSEDOWN);
125 static void closedown(sig)
128 (void) signal(sig, closedown);
129 if (_rpcsvcdirty == 0) {
133 if (_rpcfdtype == SOCK_DGRAM)
136 size = getdtablesize();
138 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
139 if (FD_ISSET(i, &svc_fdset))
144 (void) alarm(_RPCSVC_CLOSEDOWN);
147 int makesock(int port, int proto)
149 struct sockaddr_in sin;
154 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
155 s = socket(AF_INET, sock_type, proto);
157 xlog(L_FATAL, "Could not make a socket: %s\n",
161 memset((char *) &sin, 0, sizeof(sin));
162 sin.sin_family = AF_INET;
163 sin.sin_addr.s_addr = INADDR_ANY;
164 sin.sin_port = htons(port);
167 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
168 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
171 /* I was told it didn't work with gigabit ethernet.
172 Don't bothet with it. H.J. */
177 /* 1024 for rpc & transport overheads */
178 sblen = rblen = socksz + 1024;
179 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
180 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
181 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
183 #endif /* SO_SNDBUF */
186 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
187 xlog(L_FATAL, "Could not bind name to socket: %s\n",
195 /* Log an incoming call. */
197 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
200 int buflen=sizeof(buff);
205 if (!xlog_enabled(D_CALL))
209 switch (rqstp->rq_cred.oa_flavor) {
214 struct authunix_parms *unix_cred;
217 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
218 tm = localtime(&unix_cred->aup_time);
219 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
220 tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
221 tm->tm_hour, tm->tm_min, tm->tm_sec,
222 unix_cred->aup_machname,
229 if ((int) unix_cred->aup_len > 0) {
230 snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
235 for (i = 1; i < unix_cred->aup_len; i++) {
236 snprintf(sp, buflen, ",%d",
237 unix_cred->aup_gids[i]);
247 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
249 xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);