]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/rpcmisc.c
2001-04-01 Chip Salzenberg <chip@valinux.com>
[nfs-utils.git] / support / nfs / rpcmisc.c
1 /*
2  * support/nfs/rpcmisc.c
3  *
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.
9  *
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.
13  */
14
15 #include "config.h"
16
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <rpc/rpc.h>
22 #include <rpc/pmap_clnt.h>
23 #include <netinet/in.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <fcntl.h>
29 #include <memory.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include "nfslib.h"
34
35 static void     closedown(int sig);
36 int     makesock(int port, int proto);
37
38 #define _RPCSVC_CLOSEDOWN       120
39 int     _rpcpmstart = 0;
40 int     _rpcfdtype = 0;
41 int     _rpcsvcdirty = 0;
42
43 void
44 rpc_init(char *name, int prog, int vers, void (*dispatch)(), int defport)
45 {
46         struct sockaddr_in saddr;
47         SVCXPRT *transp;
48         int     sock;
49         int     asize;
50
51         asize = sizeof(saddr);
52         sock = 0;
53         if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0) {
54                 int ssize = sizeof (int);
55                 _rpcfdtype = 0;
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));
61                 _rpcpmstart = 1;
62         } else {
63                 pmap_unset(prog, vers);
64                 sock = RPC_ANYSOCK;
65         }
66
67         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
68                 static SVCXPRT *last_transp = NULL;
69  
70                 if (_rpcfdtype == 0) {
71                         if (last_transp
72                             && (!defport || defport == last_transp->xp_port)) {
73                                 transp = last_transp;
74                                 goto udp_transport;
75                         }
76                         if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
77                                 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
78                                                 name);
79                         }
80                 }
81                 transp = svcudp_create(sock);
82                 if (transp == NULL) {
83                         xlog(L_FATAL, "cannot create udp service.");
84                 }
85       udp_transport:
86                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
87                         xlog(L_FATAL, "unable to register (%s, %d, udp).",
88                                         name, vers);
89                 }
90                 last_transp = transp;
91         }
92
93         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
94                 static SVCXPRT *last_transp = NULL;
95
96                 if (_rpcfdtype == 0) {
97                         if (last_transp
98                             && (!defport || defport == last_transp->xp_port)) {
99                                 transp = last_transp;
100                                 goto tcp_transport;
101                         }
102                         if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
103                                 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
104                                                 name);
105                         }
106                 }
107                 transp = svctcp_create(sock, 0, 0);
108                 if (transp == NULL) {
109                         xlog(L_FATAL, "cannot create tcp service.");
110                 }
111       tcp_transport:
112                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
113                         xlog(L_FATAL, "unable to register (%s, %d, tcp).",
114                                         name, vers);
115                 }
116                 last_transp = transp;
117         }
118
119         if (_rpcpmstart) {
120                 signal (SIGALRM, closedown);
121                 alarm (_RPCSVC_CLOSEDOWN);
122         }
123 }
124
125 static void closedown(sig)
126 int sig;
127 {
128         (void) signal(sig, closedown);
129         if (_rpcsvcdirty == 0) {
130                 static int size;
131                 int i, openfd;
132
133                 if (_rpcfdtype == SOCK_DGRAM)
134                         exit(0);
135                 if (size == 0) {
136                         size = getdtablesize();
137                 }
138                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
139                         if (FD_ISSET(i, &svc_fdset))
140                                 openfd++;
141                 if (openfd <= 1)
142                         exit(0);
143         }
144         (void) alarm(_RPCSVC_CLOSEDOWN);
145 }
146
147 int makesock(int port, int proto)
148 {
149         struct sockaddr_in sin;
150         int     s;
151         int     sock_type;
152         int     val;
153
154         sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
155         s = socket(AF_INET, sock_type, proto);
156         if (s < 0) {
157                 xlog(L_FATAL, "Could not make a socket: %s\n",
158                                         strerror(errno));
159                 return (-1);
160         }
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);
165
166         val = 1;
167         if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
168                 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
169
170 #if 0
171         /* I was told it didn't work with gigabit ethernet.
172            Don't bothet with it.  H.J. */
173 #ifdef SO_SNDBUF
174         {
175                 int sblen, rblen;
176
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));
182         }
183 #endif                          /* SO_SNDBUF */
184 #endif
185
186         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
187                 xlog(L_FATAL, "Could not bind name to socket: %s\n",
188                                         strerror(errno));
189                 return (-1);
190         }
191         return (s);
192 }
193
194
195 /* Log an incoming call. */
196 void
197 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
198 {
199         char            buff[1024];
200         int             buflen=sizeof(buff);
201         int             len;
202         char            *sp;
203         int             i;
204
205         if (!xlog_enabled(D_CALL))
206                 return;
207
208         sp = buff;
209         switch (rqstp->rq_cred.oa_flavor) {
210         case AUTH_NULL:
211                 sprintf(sp, "NULL");
212                 break;
213         case AUTH_UNIX: {
214                 struct authunix_parms *unix_cred;
215                 struct tm *tm;
216
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,
223                         unix_cred->aup_uid,
224                         unix_cred->aup_gid);
225                 sp[buflen-1] = 0;
226                 len = strlen(sp);
227                 sp += buflen;
228                 buflen -= len;
229                 if ((int) unix_cred->aup_len > 0) {
230                         snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
231                         sp[buflen-1] = 0;
232                         len = strlen(sp);
233                         sp += buflen;
234                         buflen -= len;
235                         for (i = 1; i < unix_cred->aup_len; i++) {
236                                 snprintf(sp, buflen, ",%d", 
237                                         unix_cred->aup_gids[i]);
238                                 sp[buflen-1] = 0;
239                                 len = strlen(sp);
240                                 sp += buflen;
241                                 buflen -= len;
242                         }
243                 }
244                 }
245                 break;
246         default:
247                 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
248         }
249         xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);
250 }