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 #if SIZEOF_SOCKLEN_T - 0 == 0
41 static void closedown(int sig);
42 int makesock(int port, int proto);
44 #define _RPCSVC_CLOSEDOWN 120
50 rpc_init(char *name, int prog, int vers, void (*dispatch)(), int defport)
52 struct sockaddr_in saddr;
57 asize = sizeof(saddr);
59 if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
60 && saddr.sin_family == AF_INET) {
61 socklen_t ssize = sizeof (int);
63 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
64 (char *)&fdtype, &ssize) == -1)
65 xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
66 /* inetd passes a UDP socket or a listening TCP socket.
67 * listen will fail on a connected TCP socket(passed by rsh).
69 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
75 pmap_unset(prog, vers);
79 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
80 static SVCXPRT *last_transp = NULL;
82 if (_rpcpmstart == 0) {
84 && (!defport || defport == last_transp->xp_port)) {
90 else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
91 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
95 if (sock == RPC_ANYSOCK)
96 sock = svcudp_socket (prog, 1);
97 transp = svcudp_create(sock);
99 xlog(L_FATAL, "cannot create udp service.");
102 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
103 xlog(L_FATAL, "unable to register (%s, %d, udp).",
106 last_transp = transp;
109 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
110 static SVCXPRT *last_transp = NULL;
112 if (_rpcpmstart == 0) {
114 && (!defport || defport == last_transp->xp_port)) {
115 transp = last_transp;
120 else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
121 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
125 if (sock == RPC_ANYSOCK)
126 sock = svctcp_socket (prog, 1);
127 transp = svctcp_create(sock, 0, 0);
128 if (transp == NULL) {
129 xlog(L_FATAL, "cannot create tcp service.");
132 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
133 xlog(L_FATAL, "unable to register (%s, %d, tcp).",
136 last_transp = transp;
140 signal (SIGALRM, closedown);
141 alarm (_RPCSVC_CLOSEDOWN);
145 static void closedown(sig)
148 (void) signal(sig, closedown);
149 if (_rpcsvcdirty == 0) {
153 if (_rpcfdtype == SOCK_DGRAM)
156 size = getdtablesize();
158 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
159 if (FD_ISSET(i, &svc_fdset))
164 (void) alarm(_RPCSVC_CLOSEDOWN);
167 int makesock(int port, int proto)
169 struct sockaddr_in sin;
174 sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
175 s = socket(AF_INET, sock_type, proto);
177 xlog(L_FATAL, "Could not make a socket: %s\n",
181 memset((char *) &sin, 0, sizeof(sin));
182 sin.sin_family = AF_INET;
183 sin.sin_addr.s_addr = INADDR_ANY;
184 sin.sin_port = htons(port);
187 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
188 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
191 /* I was told it didn't work with gigabit ethernet.
192 Don't bothet with it. H.J. */
197 /* 1024 for rpc & transport overheads */
198 sblen = rblen = socksz + 1024;
199 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
200 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
201 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
203 #endif /* SO_SNDBUF */
206 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
207 xlog(L_FATAL, "Could not bind name to socket: %s\n",
215 /* Log an incoming call. */
217 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
220 int buflen=sizeof(buff);
225 if (!xlog_enabled(D_CALL))
229 switch (rqstp->rq_cred.oa_flavor) {
234 struct authunix_parms *unix_cred;
238 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
239 time = unix_cred->aup_time;
240 tm = localtime(&time);
241 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
242 tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
243 tm->tm_hour, tm->tm_min, tm->tm_sec,
244 unix_cred->aup_machname,
251 if ((int) unix_cred->aup_len > 0) {
252 snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
257 for (i = 1; i < unix_cred->aup_len; i++) {
258 snprintf(sp, buflen, ",%d",
259 unix_cred->aup_gids[i]);
269 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
271 xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);