Whamcloud - gitweb
LU-14199 sec: find policy version in use for sepol
[fs/lustre-release.git] / lustre / utils / ofd_access_log_reader.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  *
22  * Copyright 2020, DataDirect Networks Storage.
23  *
24  * This file is part of Lustre, http://www.lustre.org/
25  *
26  * Author: John L. Hammond <jhammond@whamcloud.com>
27  *
28  * lustre/utils/ofd_access_log_reader.c
29  *
30  * Sample utility to discover and read Lustre (ofd) access logs.
31  *
32  * This demonstrates the discovery and reading of Lustre access logs
33  * (see linux/lustre/lustre_access_log.h and
34  * lustre/ofd/ofd_access_log.c.). By default it opens the control
35  * device, discovers and opens all access log devices, and consumes
36  * all access log entries. If invoked with the --list option then it
37  * prints information about all available devices to stdout and exits.
38  *
39  * Structured trace points (when --trace is used) are added to permit
40  * testing of the access log functionality (see test_165* in
41  * lustre/tests/sanity.sh).
42  */
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <assert.h>
47 #include <dirent.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <getopt.h>
51 #include <inttypes.h>
52 #include <limits.h>
53 #include <malloc.h>
54 #include <pthread.h>
55 #include <signal.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <sys/epoll.h>
59 #include <sys/ioctl.h>
60 #include <sys/signalfd.h>
61 #include <sys/stat.h>
62 #include <sys/sysmacros.h>
63 #include <sys/timerfd.h>
64 #include <sys/types.h>
65 #include <linux/types.h>
66 #include <linux/lustre/lustre_user.h>
67 #include <linux/lustre/lustre_access_log.h>
68 #include "ofd_access_batch.h"
69 #include "lstddef.h"
70
71 /* TODO fsname filter */
72
73 static FILE *debug_file;
74 static FILE *trace_file;
75
76 #define DEBUG(fmt, args...)                                             \
77         do {                                                            \
78                 if (debug_file != NULL)                                 \
79                         fprintf(debug_file, "DEBUG %s:%d: "fmt, __func__, __LINE__, ##args); \
80         } while (0)
81
82 #define TRACE(fmt, args...)                                             \
83         do {                                                            \
84                 if (trace_file != NULL)                                 \
85                         fprintf(trace_file, "TRACE "fmt, ##args);       \
86         } while (0)
87
88 #define DEBUG_D(x) DEBUG("%s = %"PRIdMAX"\n", #x, (intmax_t)x)
89 #define DEBUG_P(x) DEBUG("%s = %p\n", #x, x)
90 #define DEBUG_S(x) DEBUG("%s = '%s'\n", #x, x)
91 #define DEBUG_U(x) DEBUG("%s = %"PRIuMAX"\n", #x, (uintmax_t)x)
92
93 #define ERROR(fmt, args...) \
94         fprintf(stderr, "%s: "fmt, program_invocation_short_name, ##args)
95
96 #define FATAL(fmt, args...)                     \
97         do {                                    \
98                 ERROR("FATAL: "fmt, ##args);    \
99                 exit(EXIT_FAILURE);             \
100         } while (0)
101
102 enum {
103         ALR_EXIT_SUCCESS = INT_MIN + EXIT_SUCCESS,
104         ALR_EXIT_FAILURE = INT_MIN + EXIT_FAILURE,
105         ALR_ERROR = -1,
106         ALR_EOF = 0,
107         ALR_OK = 1,
108 };
109
110 struct alr_dev {
111         char *alr_name;
112         int (*alr_io)(int /* epoll_fd */, struct alr_dev * /* this */, unsigned int /* mask */);
113         void (*alr_destroy)(struct alr_dev *);
114         int alr_fd;
115 };
116
117 struct alr_log {
118         struct alr_dev alr_dev;
119         char *alr_buf;
120         size_t alr_buf_size;
121         size_t alr_entry_size;
122         size_t alr_read_count;
123         dev_t alr_rdev;
124 };
125
126 static unsigned int alr_log_count;
127 static struct alr_log *alr_log[1 << 20]; /* 20 == MINORBITS */
128 static int oal_version; /* FIXME ... major version, minor version */
129 static __u32 alr_filter = 0xffffffff; /* no filter by default */
130 static unsigned int oal_log_major;
131 static unsigned int oal_log_minor_max;
132 static struct alr_batch *alr_batch;
133 static FILE *alr_batch_file;
134 static pthread_mutex_t alr_batch_file_mutex = PTHREAD_MUTEX_INITIALIZER;
135 static const char *alr_batch_file_path;
136 static const char *alr_stats_file_path;
137 static int alr_print_fraction = 100;
138
139 #define D_ALR_DEV "%s %d"
140 #define P_ALR_DEV(ad) \
141         (ad)->alr_name, (ad)->alr_fd
142
143 #define D_ALR_LOG D_ALR_DEV" %u:%u"
144 #define P_ALR_LOG(al) \
145         P_ALR_DEV(&(al)->alr_dev), major((al)->alr_rdev), minor((al)->alr_rdev)
146
147 static void alr_dev_free(int epoll_fd, struct alr_dev *ad)
148 {
149         TRACE("alr_dev_free %s\n", ad->alr_name);
150
151         if (!(ad->alr_fd < 0))
152                 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, ad->alr_fd, NULL);
153
154         if (ad->alr_destroy != NULL)
155                 (*ad->alr_destroy)(ad);
156
157         if (!(ad->alr_fd < 0))
158                 close(ad->alr_fd);
159
160         free(ad->alr_name);
161         free(ad);
162 }
163
164 static struct alr_log **alr_log_lookup(dev_t rdev)
165 {
166         assert(major(rdev) == oal_log_major);
167
168         if (!(minor(rdev) < ARRAY_SIZE(alr_log)))
169                 return NULL;
170
171         return &alr_log[minor(rdev)];
172 }
173
174 static const char *alr_flags_to_str(unsigned int flags)
175 {
176         switch (flags & (OFD_ACCESS_READ | OFD_ACCESS_WRITE)) {
177         default:
178                 return "0";
179         case OFD_ACCESS_READ:
180                 return "r";
181         case OFD_ACCESS_WRITE:
182                 return "w";
183         case OFD_ACCESS_READ | OFD_ACCESS_WRITE:
184                 return "rw";
185         }
186 }
187
188 /* /dev/lustre-access-log/scratch-OST0000 device poll callback: read entries
189  * from log and print. */
190 static int alr_log_io(int epoll_fd, struct alr_dev *ad, unsigned int mask)
191 {
192         struct alr_log *al = container_of(ad, struct alr_log, alr_dev);
193         ssize_t i, count;
194
195         TRACE("alr_log_io %s\n", ad->alr_name);
196         DEBUG_U(mask);
197
198         assert(al->alr_entry_size != 0);
199         assert(al->alr_buf_size != 0);
200         assert(al->alr_buf != NULL);
201
202         count = read(ad->alr_fd, al->alr_buf, al->alr_buf_size);
203         if (count < 0) {
204                 ERROR("cannot read events from '%s': %s\n", ad->alr_name, strerror(errno));
205                 return ALR_ERROR;
206         }
207
208         if (count == 0) {
209                 TRACE("alr_log_eof %s\n", ad->alr_name);
210                 return ALR_EOF;
211         }
212
213         if (count % al->alr_entry_size != 0) {
214                 ERROR("invalid read from "D_ALR_LOG": entry_size = %zu, count = %zd\n",
215                         P_ALR_LOG(al), al->alr_entry_size, count);
216                 return ALR_ERROR;
217         }
218
219         DEBUG("read "D_ALR_LOG", count = %zd\n", P_ALR_LOG(al), count);
220
221         al->alr_read_count += count / al->alr_entry_size;
222
223         for (i = 0; i < count; i += al->alr_entry_size) {
224                 struct ofd_access_entry_v1 *oae =
225                         (struct ofd_access_entry_v1 *)&al->alr_buf[i];
226
227                 TRACE("alr_log_entry %s "DFID" %lu %lu %lu %u %u %s\n",
228                         ad->alr_name,
229                         PFID(&oae->oae_parent_fid),
230                         (unsigned long)oae->oae_begin,
231                         (unsigned long)oae->oae_end,
232                         (unsigned long)oae->oae_time,
233                         (unsigned int)oae->oae_size,
234                         (unsigned int)oae->oae_segment_count,
235                         alr_flags_to_str(oae->oae_flags));
236
237                 alr_batch_add(alr_batch, ad->alr_name, &oae->oae_parent_fid,
238                         oae->oae_time, oae->oae_begin, oae->oae_end,
239                         oae->oae_size, oae->oae_segment_count, oae->oae_flags);
240         }
241
242         return ALR_OK;
243 }
244
245 static void alr_log_destroy(struct alr_dev *ad)
246 {
247         struct alr_log *al = container_of(ad, struct alr_log, alr_dev);
248         struct alr_log **pal;
249
250         TRACE("alr_log_free %s\n", ad->alr_name);
251         assert(major(al->alr_rdev) == oal_log_major);
252
253         pal = alr_log_lookup(al->alr_rdev);
254         if (pal != NULL && *pal == al)
255                 *pal = NULL;
256
257         free(al->alr_buf);
258         al->alr_buf = NULL;
259         al->alr_buf_size = 0;
260         alr_log_count--;
261 }
262
263 /* Add an access log (identified by path) to the epoll set. */
264 static int alr_log_add(int epoll_fd, const char *path)
265 {
266         struct alr_log **pal, *al = NULL;
267         struct stat st;
268         int fd = -1;
269         int rc;
270
271         DEBUG_S(path);
272
273         fd = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
274         if (fd < 0) {
275                 ERROR("cannot open device '%s': %s\n", path, strerror(errno));
276                 rc = (errno == ENOENT ? 0 : -1); /* Possible race. */
277                 goto out;
278         }
279
280         /* Revalidate rdev in case of race. */
281         rc = fstat(fd, &st);
282         if (rc < 0) {
283                 ERROR("cannot stat '%s': %s\n", path, strerror(errno));
284                 goto out;
285         }
286
287         if (major(st.st_rdev) != oal_log_major)
288                 goto out;
289
290         pal = alr_log_lookup(st.st_rdev);
291         if (pal == NULL) {
292                 ERROR("no device slot available for '%s' with minor %u\n",
293                         path, minor(st.st_rdev));
294                 goto out;
295         }
296
297         if (*pal != NULL)
298                 goto out; /* We already have this device. */
299
300         struct lustre_access_log_info_v1 lali;
301
302         memset(&lali, 0, sizeof(lali));
303
304         rc = ioctl(fd, LUSTRE_ACCESS_LOG_IOCTL_INFO, &lali);
305         if (rc < 0) {
306                 ERROR("cannot get info for device '%s': %s\n",
307                         path, strerror(errno));
308                 goto out;
309         }
310
311         if (lali.lali_type != LUSTRE_ACCESS_LOG_TYPE_OFD) {
312                 rc = 0;
313                 goto out;
314         }
315         rc = ioctl(fd, LUSTRE_ACCESS_LOG_IOCTL_FILTER, alr_filter);
316         if (rc < 0) {
317                 ERROR("cannot set filter '%s': %s\n",
318                         path, strerror(errno));
319                 goto out;
320         }
321
322         al = calloc(1, sizeof(*al));
323         if (al == NULL)
324                 FATAL("cannot allocate struct alr_dev of size %zu: %s\n",
325                         sizeof(*al), strerror(errno));
326
327         alr_log_count++;
328         al->alr_dev.alr_io = &alr_log_io;
329         al->alr_dev.alr_destroy = &alr_log_destroy;
330         al->alr_dev.alr_fd = fd;
331         fd = -1;
332
333         al->alr_rdev = st.st_rdev;
334
335         al->alr_dev.alr_name = strdup(lali.lali_name);
336         if (al->alr_dev.alr_name == NULL)
337                 FATAL("cannot copy name of size %zu: %s\n",
338                         strlen(lali.lali_name), strerror(errno));
339
340         al->alr_buf_size = lali.lali_log_size;
341         al->alr_entry_size = lali.lali_entry_size;
342
343         if (al->alr_entry_size == 0) {
344                 ERROR("device '%s' has zero entry size\n", path);
345                 rc = -1;
346                 goto out;
347         }
348
349         if (al->alr_buf_size == 0)
350                 al->alr_buf_size = 1048576;
351
352         al->alr_buf_size = roundup(al->alr_buf_size, al->alr_entry_size);
353
354         al->alr_buf = malloc(al->alr_buf_size);
355         if (al->alr_buf == NULL)
356                 FATAL("cannot allocate log buffer for '%s' of size %zu: %s\n",
357                         path, al->alr_buf_size, strerror(errno));
358
359         struct epoll_event ev = {
360                 .events = EPOLLIN | EPOLLHUP,
361                 .data.ptr = &al->alr_dev,
362         };
363
364         rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, al->alr_dev.alr_fd, &ev);
365         if (rc < 0) {
366                 ERROR("cannot add device '%s' to epoll set: %s\n",
367                         path, strerror(errno));
368                 goto out;
369         }
370
371         TRACE("alr_log_add %s\n", al->alr_dev.alr_name);
372
373         if (oal_log_minor_max < minor(al->alr_rdev))
374                 oal_log_minor_max = minor(al->alr_rdev);
375
376         assert(*pal == NULL);
377         *pal = al;
378         al = NULL;
379         rc = 0;
380 out:
381         if (al != NULL)
382                 alr_dev_free(epoll_fd, &al->alr_dev);
383
384         if (!(fd < 0))
385                 close(fd);
386
387         return rc;
388 }
389
390 /* Call LUSTRE_ACCESS_LOG_IOCTL_INFO to get access log info and print
391  * YAML formatted info to stdout. */
392 static int alr_log_info(struct alr_log *al)
393 {
394         struct lustre_access_log_info_v1 lali;
395         int rc;
396
397         rc = ioctl(al->alr_dev.alr_fd, LUSTRE_ACCESS_LOG_IOCTL_INFO, &lali);
398         if (rc < 0) {
399                 ERROR("cannot get info for device '%s': %s\n",
400                         al->alr_dev.alr_name, strerror(errno));
401                 return -1;
402         }
403
404         printf("- name: %s\n"
405                "  version: %#x\n"
406                "  type: %#x\n"
407                "  log_size: %u\n"
408                "  entry_size: %u\n",
409                lali.lali_name,
410                lali.lali_version,
411                lali.lali_type,
412                lali.lali_log_size,
413                lali.lali_entry_size);
414
415         return 0;
416 }
417
418 static int alr_log_stats(FILE *file, struct alr_log *al)
419 {
420         struct lustre_access_log_info_v1 lali;
421         int rc;
422
423         rc = ioctl(al->alr_dev.alr_fd, LUSTRE_ACCESS_LOG_IOCTL_INFO, &lali);
424         if (rc < 0) {
425                 ERROR("cannot get info for device '%s': %s\n",
426                         al->alr_dev.alr_name, strerror(errno));
427                 return -1;
428         }
429
430 #define X(m) \
431         fprintf(file, "STATS %s %s %u\n", lali.lali_name, #m, lali.m)
432
433         X(_lali_head);
434         X(_lali_tail);
435         X(_lali_entry_space);
436         X(_lali_entry_count);
437         X(_lali_drop_count);
438         X(_lali_is_closed);
439 #undef X
440
441         fprintf(file, "STATS %s %s %zu\n",
442                 lali.lali_name, "alr_read_count", al->alr_read_count);
443
444         return 0;
445 }
446
447 static void alr_log_stats_all(void)
448 {
449         FILE *stats_file;
450         int m;
451
452         if (alr_stats_file_path == NULL) {
453                 stats_file = stderr;
454         } else if (strcmp(alr_stats_file_path, "-") == 0) {
455                 stats_file = stdout;
456         } else {
457                 stats_file = fopen(alr_stats_file_path, "a");
458                 if (stats_file == NULL) {
459                         ERROR("cannot open '%s': %s\n",
460                               alr_stats_file_path, strerror(errno));
461                         return;
462                 }
463         }
464
465         for (m = 0; m <= oal_log_minor_max; m++) {
466                 if (alr_log[m] == NULL)
467                         continue;
468
469                 alr_log_stats(stats_file, alr_log[m]);
470         }
471
472         if (stats_file == stdout || stats_file == stderr)
473                 fflush(stats_file);
474         else
475                 fclose(stats_file);
476 }
477
478 /* Scan /dev/lustre-access-log/ for new access log devices and add to
479  * epoll set. */
480 static int alr_scan(int epoll_fd)
481 {
482         const char dir_path[] = "/dev/"LUSTRE_ACCESS_LOG_DIR_NAME;
483         DIR *dir;
484         int dir_fd;
485         struct dirent *d;
486         int rc;
487
488         dir = opendir(dir_path);
489         if (dir == NULL) {
490                 ERROR("cannot open '%s' for scanning: %s\n", dir_path, strerror(errno));
491                 return ALR_EXIT_FAILURE;
492         }
493
494         dir_fd = dirfd(dir);
495
496         /* Scan /dev for devices with major equal to oal_log_major and add
497          * any new devices. */
498         while ((d = readdir(dir)) != NULL) {
499                 char path[6 + PATH_MAX];
500                 struct alr_log **pal;
501                 struct stat st;
502
503                 if (d->d_type != DT_CHR)
504                         continue;
505
506                 rc = fstatat(dir_fd, d->d_name, &st, 0);
507                 if (rc < 0) {
508                         ERROR("cannot stat '%s/%s' while scanning: %s\n",
509                                 dir_path, d->d_name, strerror(errno));
510                         continue;
511                 }
512
513                 if (!S_ISCHR(st.st_mode))
514                         continue;
515
516                 if (major(st.st_rdev) != oal_log_major)
517                         continue;
518
519                 pal = alr_log_lookup(st.st_rdev);
520                 if (pal == NULL) {
521                         ERROR("no device slot available for '%s/%s' with minor %u\n",
522                                 dir_path, d->d_name, minor(st.st_rdev));
523                         continue;
524                 }
525
526                 if (*pal != NULL)
527                         continue; /* We already have this device. */
528
529                 snprintf(path, sizeof(path), "%s/%s", dir_path, d->d_name);
530
531                 alr_log_add(epoll_fd, path);
532         }
533
534         closedir(dir);
535
536         return ALR_OK;
537 }
538
539 /* /dev/lustre-access-log/control device poll callback: call prescan
540  * ioctl and scan /dev/lustre-access-log/ for new access log
541  * devices. */
542 static int alr_ctl_io(int epoll_fd, struct alr_dev *cd, unsigned int mask)
543 {
544         int rc;
545
546         TRACE("%s\n", __func__);
547         DEBUG_U(mask);
548
549         if (mask & EPOLLERR)
550                 return ALR_EXIT_FAILURE;
551
552         if (mask & EPOLLHUP)
553                 return ALR_EXIT_SUCCESS;
554
555         rc = ioctl(cd->alr_fd, LUSTRE_ACCESS_LOG_IOCTL_PRESCAN);
556         if (rc < 0) {
557                 ERROR("cannot start scanning: %s\n", strerror(errno));
558                 return ALR_EXIT_FAILURE;
559         }
560
561         return alr_scan(epoll_fd);
562 }
563
564 /* signalfd epoll callback. Handle SIGINT and SIGTERM by breaking from
565  * the epoll loop and exiting normally.*/
566 static int alr_signal_io(int epoll_fd, struct alr_dev *sd, unsigned int mask)
567 {
568         struct signalfd_siginfo ssi;
569         ssize_t rc;
570
571         TRACE("%s\n", __func__);
572         DEBUG_U(mask);
573
574         rc = read(sd->alr_fd, &ssi, sizeof(ssi));
575         if (rc <= 0)
576                 return ALR_OK;
577
578         DEBUG_U(ssi.ssi_signo);
579         switch (ssi.ssi_signo) {
580         case SIGINT:
581         case SIGTERM:
582                 return ALR_EXIT_SUCCESS;
583         case SIGUSR1:
584                 alr_log_stats_all();
585
586                 return ALR_OK;
587         case SIGUSR2:
588                 if (debug_file == NULL)
589                         debug_file = stderr;
590
591                 if (trace_file == NULL)
592                         trace_file = stderr;
593
594                 return ALR_OK;
595         default:
596                 return ALR_OK;
597         }
598 }
599
600 /* batching timerfd epoll callback. Print batched access entries to
601  * alr_batch_file. */
602 static int alr_batch_timer_io(int epoll_fd, struct alr_dev *td, unsigned int mask)
603 {
604         time_t now = time(NULL);
605         uint64_t expire_count;
606         ssize_t rc;
607
608         TRACE("%s\n", __func__);
609         DEBUG_D(now);
610         DEBUG_U(mask);
611
612         rc = read(td->alr_fd, &expire_count, sizeof(expire_count));
613         if (rc <= 0)
614                 return ALR_OK;
615
616         DEBUG_U(expire_count);
617
618         rc = alr_batch_print(alr_batch, alr_batch_file, &alr_batch_file_mutex,
619                              alr_print_fraction);
620         if (rc < 0) {
621                 ERROR("cannot write to '%s': %s\n",
622                         alr_batch_file_path, strerror(errno));
623                 goto out;
624         }
625 out:
626         /* Failed writes will leave alr_batch_file (pipe) in a
627          * weird state so make that fatal. */
628         return (rc < 0) ? ALR_EXIT_FAILURE : ALR_OK;
629 }
630
631 /* batch file (stdout) poll callback: detect remote pipe close and exit. */
632 static int alr_batch_file_io(int epoll_fd, struct alr_dev *ad, unsigned int mask)
633 {
634         TRACE("%s\n", __func__);
635         DEBUG_U(mask);
636
637         if (mask & EPOLLHUP)
638                 return ALR_EXIT_SUCCESS;
639
640         if (mask & EPOLLERR)
641                 return ALR_EXIT_FAILURE;
642
643         return ALR_OK;
644 }
645
646 static struct alr_dev *alr_dev_create(int epoll_fd, int fd, const char *name,
647                         uint32_t events,
648                         int (*io)(int, struct alr_dev *, unsigned int),
649                         void (*destroy)(struct alr_dev *))
650 {
651         struct alr_dev *alr;
652         int rc;
653
654         alr = calloc(1, sizeof(*alr));
655         if (alr == NULL)
656                 return NULL;
657
658         alr->alr_name = strdup(name);
659         if (alr->alr_name == NULL) {
660                 free(alr);
661                 return NULL;
662         }
663         alr->alr_io = io;
664         alr->alr_destroy = destroy;
665         alr->alr_fd = fd;
666
667         struct epoll_event event = {
668                 .events = events,
669                 .data.ptr = alr,
670         };
671
672         rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, alr->alr_fd, &event);
673         if (rc < 0) {
674                 free(alr);
675                 return NULL;
676         }
677
678         return alr;
679 }
680
681 void usage(void)
682 {
683         printf("Usage: %s: [OPTION]...\n"
684 "Discover, read, batch, and write Lustre access logs\n"
685 "\n"
686 "Mandatory arguments to long options are mandatory for short options too.\n"
687 "  -f, --batch-file=FILE          print batch to file (default stdout)\n"
688 "  -F, --batch-fraction=P         set batch printing fraction to P/100\n"
689 "  -i, --batch-interval=INTERVAL  print batch every INTERVAL seconds\n"
690 "  -o, --batch-offset=OFFSET      print batch at OFFSET seconds\n"
691 "  -e, --exit-on-close            exit on close of all log devices\n"
692 "  -I, --mdt-index-filter=INDEX   set log MDT index filter to INDEX\n"
693 "  -h, --help                     display this help and exit\n"
694 "  -l, --list                     print YAML list of available access logs\n"
695 "  -d, --debug[=FILE]             print debug messages to FILE (stderr)\n"
696 "  -s, --stats=FILE               print stats messages to FILE (stderr)\n"
697 "  -t, --trace[=FILE]             print trace messages to FILE (stderr)\n",
698                 program_invocation_short_name);
699 }
700
701 int main(int argc, char *argv[])
702 {
703         const char ctl_path[] = "/dev/"LUSTRE_ACCESS_LOG_DIR_NAME"/control";
704         struct alr_dev *alr_signal = NULL;
705         struct alr_dev *alr_batch_timer = NULL;
706         struct alr_dev *alr_batch_file_hup = NULL;
707         struct alr_dev *alr_ctl = NULL;
708         int exit_on_close = 0;
709         time_t batch_interval = 0;
710         time_t batch_offset = 0;
711         unsigned int m;
712         int list_info = 0;
713         int epoll_fd = -1;
714         int exit_status;
715         int rc;
716         int c;
717
718         static struct option options[] = {
719                 { .name = "batch-file", .has_arg = required_argument, .val = 'f', },
720                 { .name = "batch-fraction", .has_arg = required_argument, .val = 'F', },
721                 { .name = "batch-interval", .has_arg = required_argument, .val = 'i', },
722                 { .name = "batch-offset", .has_arg = required_argument, .val = 'o', },
723                 { .name = "exit-on-close", .has_arg = no_argument, .val = 'e', },
724                 { .name = "mdt-index-filter", .has_arg = required_argument, .val = 'I' },
725                 { .name = "debug", .has_arg = optional_argument, .val = 'd', },
726                 { .name = "help", .has_arg = no_argument, .val = 'h', },
727                 { .name = "list", .has_arg = no_argument, .val = 'l', },
728                 { .name = "stats", .has_arg = required_argument, .val = 's', },
729                 { .name = "trace", .has_arg = optional_argument, .val = 't', },
730                 { .name = NULL, },
731         };
732
733         while ((c = getopt_long(argc, argv, "d::ef:F:hi:I:ls:t::", options, NULL)) != -1) {
734                 switch (c) {
735                 case 'e':
736                         exit_on_close = 1;
737                         break;
738                 case 'f':
739                         alr_batch_file_path = optarg;
740                         break;
741                 case 'i':
742                         errno = 0;
743                         batch_interval = strtoll(optarg, NULL, 0);
744                         if (batch_interval < 0 || batch_interval >= 1048576 ||
745                             errno != 0)
746                                 FATAL("invalid batch interval '%s'\n", optarg);
747                         break;
748                 case 'o':
749                         errno = 0;
750                         batch_offset = strtoll(optarg, NULL, 0);
751                         if (batch_offset < 0 || batch_offset >= 1048576 ||
752                             errno != 0)
753                                 FATAL("invalid batch offset '%s'\n", optarg);
754                         break;
755                 case 'd':
756                         if (optarg == NULL) {
757                                 debug_file = stderr;
758                         } else if (strcmp(optarg, "-") == 0) {
759                                 debug_file = stdout;
760                         } else {
761                                 debug_file = fopen(optarg, "a");
762                                 if (debug_file == NULL)
763                                         FATAL("cannot open debug file '%s': %s\n",
764                                                 optarg, strerror(errno));
765                         }
766
767                         break;
768                 case 'h':
769                         usage();
770                         exit(EXIT_SUCCESS);
771                 case 'F':
772                         alr_print_fraction = strtoll(optarg, NULL, 0);
773                         if (alr_print_fraction < 1 || alr_print_fraction > 100)
774                                 FATAL("invalid batch offset '%s'\n", optarg);
775                         break;
776                 case 'I':
777                         alr_filter = strtoll(optarg, NULL, 0);
778                         break;
779                 case 'l':
780                         list_info = 1;
781                         break;
782                 case 's':
783                         alr_stats_file_path = optarg;
784                         break;
785                 case 't':
786                         if (optarg == NULL) {
787                                 trace_file = stderr;
788                         } else if (strcmp(optarg, "-") == 0) {
789                                 trace_file = stdout;
790                         } else {
791                                 trace_file = fopen(optarg, "a");
792                                 if (debug_file == NULL)
793                                         FATAL("cannot open debug file '%s': %s\n",
794                                                 optarg, strerror(errno));
795                         }
796
797                         break;
798                 case '?':
799                         fprintf(stderr, "Try '%s --help' for more information.\n",
800                                 program_invocation_short_name);
801                         exit(EXIT_FAILURE);
802                 }
803         }
804
805         if (batch_interval > 0) {
806                 alr_batch = alr_batch_create(-1);
807                 if (alr_batch == NULL)
808                         FATAL("cannot create batch struct: %s\n",
809                                 strerror(errno));
810         }
811
812         if (alr_batch_file_path != NULL) {
813                 alr_batch_file = fopen(alr_batch_file_path, "w");
814                 if (alr_batch_file == NULL)
815                         FATAL("cannot open batch file '%s': %s\n",
816                                 alr_batch_file_path, strerror(errno));
817         } else {
818                 alr_batch_file_path = "stdout";
819                 alr_batch_file = stdout;
820         }
821
822         epoll_fd = epoll_create1(EPOLL_CLOEXEC);
823         if (epoll_fd < 0)
824                 FATAL("cannot create epoll set: %s\n", strerror(errno));
825
826         /* Setup signal FD and add to epoll set. */
827         sigset_t signal_mask;
828         sigemptyset(&signal_mask);
829         sigaddset(&signal_mask, SIGINT);
830         sigaddset(&signal_mask, SIGTERM);
831         sigaddset(&signal_mask, SIGUSR1);
832         sigaddset(&signal_mask, SIGUSR2);
833         rc = sigprocmask(SIG_BLOCK, &signal_mask, NULL);
834         if (rc < 0)
835                 FATAL("cannot set process signal mask: %s\n", strerror(errno));
836
837         int signal_fd = signalfd(-1, &signal_mask, SFD_NONBLOCK|SFD_CLOEXEC);
838         if (signal_fd < 0)
839                 FATAL("cannot create signalfd: %s\n", strerror(errno));
840
841         alr_signal = alr_dev_create(epoll_fd, signal_fd, "signal", EPOLLIN,
842                                 &alr_signal_io, NULL);
843         if (alr_signal == NULL)
844                 FATAL("cannot register signalfd: %s\n", strerror(errno));
845
846         signal_fd = -1;
847
848         /* Setup batch timer FD and add to epoll set. */
849         struct timespec now;
850         rc = clock_gettime(CLOCK_REALTIME, &now);
851         if (rc < 0)
852                 FATAL("cannot read realtime clock: %s\n", strerror(errno));
853
854         int timer_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
855         if (timer_fd < 0)
856                 FATAL("cannot create batch timerfd: %s\n", strerror(errno));
857
858         struct itimerspec it = {
859                 .it_value.tv_sec = (batch_interval > 0) ?
860                                    roundup(now.tv_sec, batch_interval) +
861                                    (batch_offset % batch_interval) :
862                                    0,
863                 .it_interval.tv_sec = batch_interval,
864         };
865
866         DEBUG_D(it.it_value.tv_sec);
867
868         rc = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &it, NULL);
869         if (rc < 0)
870                 FATAL("cannot arm timerfd: %s\n", strerror(errno));
871
872         alr_batch_timer = alr_dev_create(epoll_fd, timer_fd, "batch_timer",
873                                         EPOLLIN, &alr_batch_timer_io, NULL);
874         if (alr_batch_timer == NULL)
875                 FATAL("cannot register batch timerfd: %s\n", strerror(errno));
876
877         timer_fd = -1;
878
879         int batch_fd = dup(fileno(alr_batch_file));
880         if (batch_fd < 0)
881                 FATAL("cannot duplicate batch file descriptor: %s\n",
882                       strerror(errno));
883
884         /* We pass events = 0 since we only care about EPOLLHUP. */
885         alr_batch_file_hup = alr_dev_create(epoll_fd, batch_fd, "batch_file", 0,
886                                         &alr_batch_file_io, NULL);
887         if (alr_batch_file_hup == NULL)
888                 FATAL("cannot register batch file HUP: %s\n", strerror(errno));
889
890         batch_fd = -1;
891
892         /* Open control device. */
893         int ctl_fd = open(ctl_path, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
894         if (ctl_fd < 0)
895                 FATAL("cannot open '%s': %s\n", ctl_path, strerror(errno));
896
897         /* Get and print interface version. */
898         oal_version = ioctl(ctl_fd, LUSTRE_ACCESS_LOG_IOCTL_VERSION);
899         if (oal_version < 0)
900                 FATAL("cannot get ofd access log interface version: %s\n", strerror(errno));
901
902         DEBUG_D(oal_version);
903
904         /* Get and print device major used for access log devices. */
905         oal_log_major = ioctl(ctl_fd, LUSTRE_ACCESS_LOG_IOCTL_MAJOR);
906         if (oal_log_major < 0)
907                 FATAL("cannot get ofd access log major: %s\n", strerror(errno));
908
909         DEBUG_D(oal_log_major);
910
911         /* Add control device to epoll set. */
912         alr_ctl = alr_dev_create(epoll_fd, ctl_fd, "control", EPOLLIN,
913                                 &alr_ctl_io, NULL);
914         if (alr_ctl == NULL)
915                 FATAL("cannot register control device: %s\n", strerror(errno));
916
917         ctl_fd = -1;
918
919         do {
920                 struct epoll_event ev[32];
921                 int timeout = (list_info ? 0 : -1);
922                 int i, ev_count;
923
924                 ev_count = epoll_wait(epoll_fd, ev, ARRAY_SIZE(ev), timeout);
925                 if (ev_count < 0) {
926                         if (errno == EINTR) /* Signal or timeout. */
927                                 continue;
928
929                         ERROR("cannot wait on epoll set: %s\n", strerror(errno));
930                         exit_status = EXIT_FAILURE;
931                         goto out;
932                 }
933
934                 DEBUG_D(ev_count);
935
936                 for (i = 0; i < ev_count; i++) {
937                         struct alr_dev *ad = ev[i].data.ptr;
938                         unsigned int mask = ev[i].events;
939
940                         rc = (*ad->alr_io)(epoll_fd, ad, mask);
941                         switch (rc) {
942                         case ALR_EXIT_FAILURE:
943                                 exit_status = EXIT_FAILURE;
944                                 goto out;
945                         case ALR_EXIT_SUCCESS:
946                                 exit_status = EXIT_SUCCESS;
947                                 goto out;
948                         case ALR_ERROR:
949                         case ALR_EOF:
950                                 alr_dev_free(epoll_fd, ad);
951                                 break;
952                         case ALR_OK:
953                         default:
954                                 break;
955                         }
956                 }
957
958                 if (exit_on_close && alr_log_count == 0) {
959                         DEBUG("no open logs devices, exiting\n");
960                         exit_status = EXIT_SUCCESS;
961                         goto out;
962                 }
963         } while (!list_info);
964
965         exit_status = EXIT_SUCCESS;
966 out:
967         assert(oal_log_minor_max < ARRAY_SIZE(alr_log));
968
969         for (m = 0; m <= oal_log_minor_max; m++) {
970                 if (alr_log[m] == NULL)
971                         continue;
972
973                 if (list_info) {
974                         rc = alr_log_info(alr_log[m]);
975                         if (rc < 0)
976                                 exit_status = EXIT_FAILURE;
977                 }
978
979                 alr_dev_free(epoll_fd, &alr_log[m]->alr_dev);
980         }
981
982         alr_dev_free(epoll_fd, alr_ctl);
983         alr_dev_free(epoll_fd, alr_signal);
984         alr_dev_free(epoll_fd, alr_batch_timer);
985         alr_dev_free(epoll_fd, alr_batch_file_hup);
986         close(epoll_fd);
987
988         alr_batch_destroy(alr_batch);
989
990         DEBUG_D(exit_status);
991
992         return exit_status;
993 }