Whamcloud - gitweb
Merge b1_4_bug3389 from b1_4 (20050729_0312)
[fs/lustre-release.git] / lnet / utils / acceptor.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/tcp.h>
8 #include <netdb.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <sys/ioctl.h>
13 #include <unistd.h>
14 #include <syslog.h>
15 #include <stdarg.h>
16 #include <signal.h>
17 #include <errno.h>
18 #ifdef HAVE_LIBWRAP
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21 #include <tcpd.h>
22 #endif
23
24 #include <libcfs/portals_utils.h>
25 #include <portals/api-support.h>
26 #include <portals/lib-types.h>
27 #include <portals/socknal.h>
28
29 /* should get this from autoconf somehow */
30 #ifndef PIDFILE_DIR
31 #define PIDFILE_DIR "/var/run"
32 #endif
33
34 char progname[] = "acceptor";
35 char name_port[40];             /* for signal handler */
36
37 #ifdef HAVE_LIBWRAP
38 /* needed because libwrap declares these as externs */
39 int allow_severity = LOG_INFO;
40 int deny_severity = LOG_WARNING;
41 #endif
42
43 void usage(char *progname)
44 {
45         fprintf(stderr, "usage: %s [-N nal_id] [-p] [-l] port\n\n"
46                 " -l\tKeep stdin/stdout open\n"
47                 " -p\tAllow connections from non-privileged ports\n", progname);
48         exit (1);
49 }
50
51 void errlog(int level, const char *fmt, ...)
52 {
53         va_list arg;
54         FILE *out;
55
56         switch (level) {
57         case LOG_DEBUG:
58         case LOG_INFO:
59         case LOG_NOTICE:
60                 out = stdout;
61                 break;
62         default:
63                 out = stderr;
64                 break;
65         }
66         va_start(arg, fmt);
67         fprintf(out, "%s: ", name_port);
68         vfprintf(out, fmt, arg);
69         va_end(arg);
70         va_start(arg, fmt);
71         vsyslog(level, fmt, arg);
72         va_end(arg);
73 }
74
75 char *pidfile_name(char *name_port)
76 {
77         static char pidfile[1024];
78
79         snprintf(pidfile, sizeof(pidfile), "%s/%s.pid", PIDFILE_DIR, name_port);
80
81         return pidfile;
82 }
83
84 void pidfile_create(char *name_port)
85 {
86         char *pidfile = pidfile_name(name_port);
87         FILE *fp;
88
89         if ((fp = fopen(pidfile, "w"))) {
90                 fprintf(fp, "%d\n", getpid());
91                 fclose(fp);
92         } else {
93                 errlog(LOG_ERR, " error creating %s: %s\n",
94                        pidfile, strerror(errno));
95         }
96 }
97
98 int pidfile_cleanup(char *name_port)
99 {
100         char *pidfile = pidfile_name(name_port);
101         int rc;
102
103         rc = unlink(pidfile);
104         if (rc && errno != -ENOENT)
105                 fprintf(stderr, "%s: error removing %s: %s\n",
106                         progname, pidfile, strerror(errno));
107
108         return errno;
109 }
110
111 int pidfile_exists(char *name_port)
112 {
113         char *pidfile = pidfile_name(name_port);
114         FILE *fpid;
115         int pid, rc;
116
117         fpid = fopen(pidfile, "r+");
118         if (fpid == NULL) {
119                 if (errno == ENOENT)
120                         return 0;
121
122                 fprintf(stderr, "%s: error opening %s: %s.\n",
123                         progname, pidfile, strerror(errno));
124                 return (1);
125         }
126
127         rc = fscanf(fpid, "%i", &pid);
128         fclose(fpid);
129         if (rc != 1) {
130                 fprintf(stderr,"%s: %s didn't contain a valid pid, removing.\n",
131                         progname, pidfile);
132                 goto stale;
133         }
134
135         if (kill(pid, 0) == 0) {
136                 fprintf(stderr, "%s: %s exists, acceptor pid %d running.\n",
137                         progname, pidfile, pid);
138                 return (1);
139         }
140
141         fprintf(stderr, "%s: stale %s exists, pid %d doesn't, removing.\n",
142                 progname, pidfile, pid);
143 stale:
144         pidfile_cleanup(name_port);
145         return (0);
146 }
147
148 void handler(int sig)
149 {
150         pidfile_cleanup(name_port);
151         exit(sig);
152 }
153
154 void show_connection(int fd, __u32 net_ip)
155 {
156         static long last_time;
157         static __u32 host_ip;
158         long now = time(0);
159         struct hostent *h;
160         int  len;
161         char host[1024];
162
163         /* Don't show repeats for same host, it adds no value */
164         if (host_ip == ntohl(net_ip) && (now - last_time) < 5)
165                 return;
166
167         h = gethostbyaddr((char *)&net_ip, sizeof(net_ip), AF_INET);
168         last_time = now;
169         host_ip = ntohl(net_ip);
170
171         if (h == NULL)
172                 snprintf(host, sizeof(host), "%d.%d.%d.%d",
173                          (host_ip >> 24) & 0xff, (host_ip >> 16) & 0xff,
174                          (host_ip >> 8)  & 0xff, host_ip & 0xff);
175         else
176                 snprintf(host, sizeof(host), "%s", h->h_name);
177
178         syslog(LOG_INFO, "accepted host: %s\n", host);
179 }
180
181 int main(int argc, char **argv)
182 {
183         int o, fd, rc, port, pfd;
184         struct sockaddr_in srvaddr;
185         int c;
186         int noclose = 0;
187         int nal = SOCKNAL;
188         int rport;
189         int require_privports = 1;
190
191         while ((c = getopt (argc, argv, "N:lp")) != -1) {
192                 switch (c) {
193                 case 'N':
194                         if (sscanf(optarg, "%d", &nal) != 1 ||
195                             nal < 0 || nal > NAL_MAX_NR)
196                                 usage(argv[0]);
197                         break;
198                 case 'l':
199                         noclose = 1;
200                         break;
201                 case 'p':
202                         require_privports = 0;
203                         break;
204                 default:
205                         usage (argv[0]);
206                         break;
207                 }
208         }
209
210         if (optind >= argc)
211                 usage (argv[0]);
212
213         port = atol(argv[optind++]);
214
215         snprintf(name_port, sizeof(name_port) - 1, "%s-%d", progname, port);
216         if (pidfile_exists(name_port))
217                 return(EEXIST);
218         openlog(name_port, LOG_PID, LOG_DAEMON);
219
220         memset(&srvaddr, 0, sizeof(srvaddr));
221         srvaddr.sin_family = AF_INET;
222         srvaddr.sin_port = htons(port);
223         srvaddr.sin_addr.s_addr = INADDR_ANY;
224
225         fd = socket(PF_INET, SOCK_STREAM, 0);
226         if (fd < 0) {
227                 rc = errno;
228                 errlog(LOG_ERR, "error opening socket: %s\n", strerror(errno));
229                 return(rc);
230         }
231
232         o = 1;
233         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &o, sizeof(o))) {
234                 rc = errno;
235                 errlog(LOG_ERR, "cannot set REUSEADDR socket opt: %s\n",
236                        strerror(errno));
237                 return(rc);
238         }
239
240         rc = bind(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
241         if (rc == -1) {
242                 rc = errno;
243                 errlog(LOG_ERR, "error binding to socket: %s\n",
244                        strerror(errno));
245                 return(rc);
246         }
247
248         if (listen(fd, 127)) {
249                 rc = errno;
250                 perror("listen: ");
251                 return(rc);
252         }
253         printf("listening on port %d\n", port);
254
255         pfd = open("/dev/portals", O_RDWR);
256         if (pfd < 0) {
257                 rc = errno;
258                 errlog(LOG_ERR, "opening portals device: %s\n",strerror(errno));
259                 return(rc);
260         }
261
262         rc = daemon(0, noclose);
263         if (rc < 0) {
264                 rc = errno;
265                 errlog(LOG_ERR, "error daemonizing: %s\n", strerror(errno));
266                 return(rc);
267         }
268
269         signal(SIGHUP, SIG_IGN);
270         signal(SIGINT, handler);
271         signal(SIGQUIT, handler);
272         signal(SIGTERM, handler);
273
274         errlog(LOG_NOTICE, "started, listening on port %d\n", port);
275         pidfile_create(name_port);
276
277         while (1) {
278                 struct sockaddr_in clntaddr;
279                 int len = sizeof(clntaddr);
280                 int cfd;
281                 struct portal_ioctl_data data;
282                 struct portals_cfg pcfg;
283 #ifdef HAVE_LIBWRAP
284                 struct request_info request;
285 #endif
286                 char addrstr[INET_ADDRSTRLEN];
287
288                 cfd = accept(fd, (struct sockaddr *)&clntaddr, &len);
289                 if (cfd < 0) {
290                         errlog(LOG_ERR, "error accepting connection: %s\n",
291                                strerror(errno));
292                         break;
293                         //continue;
294                 }
295
296 #ifdef HAVE_LIBWRAP
297                 /* libwrap access control */
298                 request_init(&request, RQ_DAEMON, "lustre", RQ_FILE, cfd, 0);
299                 sock_host(&request);
300                 if (!hosts_access(&request)) {
301                         inet_ntop(AF_INET, &clntaddr.sin_addr,
302                                   addrstr, INET_ADDRSTRLEN);
303                         errlog(LOG_WARNING, "unauthorized access from %s:%hd\n",
304                                addrstr, ntohs(clntaddr.sin_port));
305                         close (cfd);
306                         continue;
307                 }
308 #endif
309
310                 if (require_privports &&
311                     ntohs(clntaddr.sin_port) >= IPPORT_RESERVED) {
312                         inet_ntop(AF_INET, &clntaddr.sin_addr,
313                                   addrstr, INET_ADDRSTRLEN);
314                         errlog(LOG_ERR,
315                                "closing non-privileged connection from %s:%d\n",
316                                addrstr, ntohs(clntaddr.sin_port));
317                         rc = close(cfd);
318                         if (rc)
319                                 perror ("close un-privileged client failed");
320                         continue;
321                 }
322
323                 show_connection (cfd, clntaddr.sin_addr.s_addr);
324
325                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
326                 pcfg.pcfg_nal = nal;
327                 pcfg.pcfg_fd = cfd;
328                 pcfg.pcfg_misc = SOCKNAL_CONN_NONE; /* == incoming connection */
329
330                 PORTAL_IOC_INIT(data);
331                 data.ioc_pbuf1 = (char*)&pcfg;
332                 data.ioc_plen1 = sizeof(pcfg);
333
334                 if (ioctl(pfd, IOC_PORTAL_NAL_CMD, &data) < 0) {
335                         errlog(LOG_ERR,
336                                "portals ioctl failed: %s\n", strerror(errno));
337                 } else {
338                         errlog(LOG_DEBUG, "client registered\n");
339                 }
340                 rc = close(cfd);
341                 if (rc)
342                         perror ("close failed");
343         }
344
345         closelog();
346         pidfile_cleanup(name_port);
347
348         return (0);
349 }