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)) {
78 else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
79 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
83 if (sock == RPC_ANYSOCK)
84 sock = svcudp_socket (prog, 1);
85 transp = svcudp_create(sock);
87 xlog(L_FATAL, "cannot create udp service.");
90 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
91 xlog(L_FATAL, "unable to register (%s, %d, udp).",
97 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
98 static SVCXPRT *last_transp = NULL;
100 if (_rpcfdtype == 0) {
102 && (!defport || defport == last_transp->xp_port)) {
103 transp = last_transp;
108 else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
109 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
113 if (sock == RPC_ANYSOCK)
114 sock = svctcp_socket (prog, 1);
115 transp = svctcp_create(sock, 0, 0);
116 if (transp == NULL) {
117 xlog(L_FATAL, "cannot create tcp service.");
120 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
121 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
124 last_transp = transp;
128 signal (SIGALRM, closedown);
129 alarm (_RPCSVC_CLOSEDOWN);
133 static void closedown(sig)
136 (void) signal(sig, closedown);
137 if (_rpcsvcdirty == 0) {
141 if (_rpcfdtype == SOCK_DGRAM)
144 size = getdtablesize();
146 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
147 if (FD_ISSET(i, &svc_fdset))
152 (void) alarm(_RPCSVC_CLOSEDOWN);
155 int makesock(int port, int proto)
157 struct sockaddr_in sin;
162 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
163 s = socket(AF_INET, sock_type, proto);
165 xlog(L_FATAL, "Could not make a socket: %s\n",
169 memset((char *) &sin, 0, sizeof(sin));
170 sin.sin_family = AF_INET;
171 sin.sin_addr.s_addr = INADDR_ANY;
172 sin.sin_port = htons(port);
175 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
176 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
179 /* I was told it didn't work with gigabit ethernet.
180 Don't bothet with it. H.J. */
185 /* 1024 for rpc & transport overheads */
186 sblen = rblen = socksz + 1024;
187 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
188 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
189 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
191 #endif /* SO_SNDBUF */
194 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
195 xlog(L_FATAL, "Could not bind name to socket: %s\n",
203 /* Log an incoming call. */
205 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
208 int buflen=sizeof(buff);
213 if (!xlog_enabled(D_CALL))
217 switch (rqstp->rq_cred.oa_flavor) {
222 struct authunix_parms *unix_cred;
225 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
226 tm = localtime(&unix_cred->aup_time);
227 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
228 tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
229 tm->tm_hour, tm->tm_min, tm->tm_sec,
230 unix_cred->aup_machname,
237 if ((int) unix_cred->aup_len > 0) {
238 snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
243 for (i = 1; i < unix_cred->aup_len; i++) {
244 snprintf(sp, buflen, ",%d",
245 unix_cred->aup_gids[i]);
255 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
257 xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);