]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/rpcmisc.c
Add -Wstrict-prototypes to compiler args, and fix warnings caused.
[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 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <rpc/rpc.h>
24 #include <rpc/pmap_clnt.h>
25 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <fcntl.h>
31 #include <memory.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <time.h>
35 #include "nfslib.h"
36
37 #if SIZEOF_SOCKLEN_T - 0 == 0
38 #define socklen_t int
39 #endif
40
41 static void     closedown(int sig);
42 int     makesock(int port, int proto);
43
44 #define _RPCSVC_CLOSEDOWN       120
45 int     _rpcpmstart = 0;
46 int     _rpcfdtype = 0;
47 int     _rpcsvcdirty = 0;
48
49 void
50 rpc_init(char *name, int prog, int vers,
51          void (*dispatch)(struct svc_req *, register SVCXPRT *),
52          int defport)
53 {
54         struct sockaddr_in saddr;
55         SVCXPRT *transp;
56         int     sock;
57         socklen_t asize;
58
59         asize = sizeof(saddr);
60         sock = 0;
61         if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
62             && saddr.sin_family == AF_INET) {
63                 socklen_t ssize = sizeof (int);
64                 int fdtype = 0;
65                 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
66                                 (char *)&fdtype, &ssize) == -1)
67                         xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
68                 /* inetd passes a UDP socket or a listening TCP socket.
69                  * listen will fail on a connected TCP socket(passed by rsh).
70                  */
71                 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
72                         _rpcfdtype = fdtype;
73                         _rpcpmstart = 1;
74                 }
75         }
76         if (!_rpcpmstart) {
77                 pmap_unset(prog, vers);
78                 sock = RPC_ANYSOCK;
79         }
80
81         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
82                 static SVCXPRT *last_transp = NULL;
83  
84                 if (_rpcpmstart == 0) {
85                         if (last_transp
86                             && (!defport || defport == last_transp->xp_port)) {
87                                 transp = last_transp;
88                                 goto udp_transport;
89                         }
90                         if (defport == 0)
91                                 sock = RPC_ANYSOCK;
92                         else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
93                                 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
94                                                 name);
95                         }
96                 }
97                 if (sock == RPC_ANYSOCK)
98                         sock = svcudp_socket (prog, 1);
99                 transp = svcudp_create(sock);
100                 if (transp == NULL) {
101                         xlog(L_FATAL, "cannot create udp service.");
102                 }
103       udp_transport:
104                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
105                         xlog(L_FATAL, "unable to register (%s, %d, udp).",
106                                         name, vers);
107                 }
108                 last_transp = transp;
109         }
110
111         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
112                 static SVCXPRT *last_transp = NULL;
113
114                 if (_rpcpmstart == 0) {
115                         if (last_transp
116                             && (!defport || defport == last_transp->xp_port)) {
117                                 transp = last_transp;
118                                 goto tcp_transport;
119                         }
120                         if (defport == 0)
121                                 sock = RPC_ANYSOCK;
122                         else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
123                                 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
124                                                 name);
125                         }
126                 }
127                 if (sock == RPC_ANYSOCK)
128                         sock = svctcp_socket (prog, 1);
129                 transp = svctcp_create(sock, 0, 0);
130                 if (transp == NULL) {
131                         xlog(L_FATAL, "cannot create tcp service.");
132                 }
133       tcp_transport:
134                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
135                         xlog(L_FATAL, "unable to register (%s, %d, tcp).",
136                                         name, vers);
137                 }
138                 last_transp = transp;
139         }
140
141         if (_rpcpmstart) {
142                 signal (SIGALRM, closedown);
143                 alarm (_RPCSVC_CLOSEDOWN);
144         }
145 }
146
147 static void closedown(sig)
148 int sig;
149 {
150         (void) signal(sig, closedown);
151         if (_rpcsvcdirty == 0) {
152                 static int size;
153                 int i, openfd;
154
155                 if (_rpcfdtype == SOCK_DGRAM)
156                         exit(0);
157                 if (size == 0) {
158                         size = getdtablesize();
159                 }
160                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
161                         if (FD_ISSET(i, &svc_fdset))
162                                 openfd++;
163                 if (openfd <= 1)
164                         exit(0);
165         }
166         (void) alarm(_RPCSVC_CLOSEDOWN);
167 }
168
169 int makesock(int port, int proto)
170 {
171         struct sockaddr_in sin;
172         int     s;
173         int     sock_type;
174         int     val;
175
176         sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
177         s = socket(AF_INET, sock_type, proto);
178         if (s < 0) {
179                 xlog(L_FATAL, "Could not make a socket: %s\n",
180                                         strerror(errno));
181                 return (-1);
182         }
183         memset((char *) &sin, 0, sizeof(sin));
184         sin.sin_family = AF_INET;
185         sin.sin_addr.s_addr = INADDR_ANY;
186         sin.sin_port = htons(port);
187
188         val = 1;
189         if (proto == IPPROTO_TCP)
190                 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
191                                &val, sizeof(val)) < 0)
192                         xlog(L_ERROR, "setsockopt failed: %s\n",
193                              strerror(errno));
194
195 #if 0
196         /* I was told it didn't work with gigabit ethernet.
197            Don't bothet with it.  H.J. */
198 #ifdef SO_SNDBUF
199         {
200                 int sblen, rblen;
201
202                 /* 1024 for rpc & transport overheads */
203                 sblen = rblen = socksz + 1024;
204                 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
205                     setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
206                         xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
207         }
208 #endif                          /* SO_SNDBUF */
209 #endif
210
211         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
212                 xlog(L_FATAL, "Could not bind name to socket: %s\n",
213                                         strerror(errno));
214                 return (-1);
215         }
216         return (s);
217 }
218
219
220 /* Log an incoming call. */
221 void
222 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
223 {
224         char            buff[1024];
225         int             buflen=sizeof(buff);
226         int             len;
227         char            *sp;
228         int             i;
229
230         if (!xlog_enabled(D_CALL))
231                 return;
232
233         sp = buff;
234         switch (rqstp->rq_cred.oa_flavor) {
235         case AUTH_NULL:
236                 sprintf(sp, "NULL");
237                 break;
238         case AUTH_UNIX: {
239                 struct authunix_parms *unix_cred;
240                 time_t time;
241                 struct tm *tm;
242
243                 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
244                 time = unix_cred->aup_time;
245                 tm = localtime(&time);
246                 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
247                         tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
248                         tm->tm_hour, tm->tm_min, tm->tm_sec,
249                         unix_cred->aup_machname,
250                         unix_cred->aup_uid,
251                         unix_cred->aup_gid);
252                 sp[buflen-1] = 0;
253                 len = strlen(sp);
254                 sp += buflen;
255                 buflen -= len;
256                 if ((int) unix_cred->aup_len > 0) {
257                         snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
258                         sp[buflen-1] = 0;
259                         len = strlen(sp);
260                         sp += buflen;
261                         buflen -= len;
262                         for (i = 1; i < unix_cred->aup_len; i++) {
263                                 snprintf(sp, buflen, ",%d", 
264                                         unix_cred->aup_gids[i]);
265                                 sp[buflen-1] = 0;
266                                 len = strlen(sp);
267                                 sp += buflen;
268                                 buflen -= len;
269                         }
270                 }
271                 }
272                 break;
273         default:
274                 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
275         }
276         xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);
277 }