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 && saddr.sin_family == AF_INET) {
55 int ssize = sizeof (int);
57 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
58 (char *)&_rpcfdtype, &ssize) == -1)
59 xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
62 pmap_unset(prog, vers);
66 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
67 static SVCXPRT *last_transp = NULL;
69 if (_rpcpmstart == 0) {
71 && (!defport || defport == last_transp->xp_port)) {
77 else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
78 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
82 if (sock == RPC_ANYSOCK)
83 sock = svcudp_socket (prog, 1);
84 transp = svcudp_create(sock);
86 xlog(L_FATAL, "cannot create udp service.");
89 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
90 xlog(L_FATAL, "unable to register (%s, %d, udp).",
96 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
97 static SVCXPRT *last_transp = NULL;
99 if (_rpcpmstart == 0) {
101 && (!defport || defport == last_transp->xp_port)) {
102 transp = last_transp;
107 else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
108 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
112 if (sock == RPC_ANYSOCK)
113 sock = svctcp_socket (prog, 1);
114 transp = svctcp_create(sock, 0, 0);
115 if (transp == NULL) {
116 xlog(L_FATAL, "cannot create tcp service.");
119 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
120 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
123 last_transp = transp;
127 signal (SIGALRM, closedown);
128 alarm (_RPCSVC_CLOSEDOWN);
132 static void closedown(sig)
135 (void) signal(sig, closedown);
136 if (_rpcsvcdirty == 0) {
140 if (_rpcfdtype == SOCK_DGRAM)
143 size = getdtablesize();
145 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
146 if (FD_ISSET(i, &svc_fdset))
151 (void) alarm(_RPCSVC_CLOSEDOWN);
154 int makesock(int port, int proto)
156 struct sockaddr_in sin;
161 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
162 s = socket(AF_INET, sock_type, proto);
164 xlog(L_FATAL, "Could not make a socket: %s\n",
168 memset((char *) &sin, 0, sizeof(sin));
169 sin.sin_family = AF_INET;
170 sin.sin_addr.s_addr = INADDR_ANY;
171 sin.sin_port = htons(port);
174 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
175 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
178 /* I was told it didn't work with gigabit ethernet.
179 Don't bothet with it. H.J. */
184 /* 1024 for rpc & transport overheads */
185 sblen = rblen = socksz + 1024;
186 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
187 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
188 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
190 #endif /* SO_SNDBUF */
193 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
194 xlog(L_FATAL, "Could not bind name to socket: %s\n",
202 /* Log an incoming call. */
204 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
207 int buflen=sizeof(buff);
212 if (!xlog_enabled(D_CALL))
216 switch (rqstp->rq_cred.oa_flavor) {
221 struct authunix_parms *unix_cred;
224 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
225 tm = localtime(&unix_cred->aup_time);
226 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
227 tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
228 tm->tm_hour, tm->tm_min, tm->tm_sec,
229 unix_cred->aup_machname,
236 if ((int) unix_cred->aup_len > 0) {
237 snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
242 for (i = 1; i < unix_cred->aup_len; i++) {
243 snprintf(sp, buflen, ",%d",
244 unix_cred->aup_gids[i]);
254 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
256 xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);