summary refs log tree commit diff
path: root/src/minutils/s6-logwatch.c
blob: 28739af5b86e6cfa0f2c14353819a11ae3c94acc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* ISC license. */

#include <sys/uio.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/inotify.h>
#include <skalibs/types.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr2.h>
#include <skalibs/error.h>
#include <skalibs/buffer.h>
#include <skalibs/sig.h>
#include <skalibs/djbunix.h>
#include <skalibs/iopause.h>

#define USAGE "s6-logwatch [ logdir ]"
#define dieusage() strerr_dieusage(100, USAGE)

#define B_READING 0
#define B_BLOCKING 1
#define B_WAITING 2
static unsigned int state ;
static int fd ;
static int newcurrent = 0 ;

union inotify_event_u
{
  struct inotify_event event ;
  char buf[sizeof(struct inotify_event) + NAME_MAX + 1] ;
} ;

static void goteof (void)
{
  if (newcurrent)
  {
    fd_close(fd) ;
    fd = open_read("current") ;
    if (fd < 0) strerr_diefu1sys(111, "current") ;
    newcurrent = 0 ;
    state = B_READING ;
  }
  else state = B_BLOCKING ;
}

static int readit (int fd)
{
  struct iovec v[2] ;
  ssize_t r ;
  buffer_wpeek(buffer_1, v) ;
  r = fd_readv(fd, v, 2) ;
  switch (r)
  {
    case -1 : return 0 ;
    case 0 : goteof() ; break ;
    default : buffer_wseek(buffer_1, r) ;
  }
  return 1 ;
}

static void maketransition (unsigned int transition)
{
  static unsigned char const table[3][3] = {
    { 0x10, 0x00, 0x00 },
    { 0x60, 0x22, 0x00 },
    { 0x40, 0x03, 0x02 }
  } ;
  unsigned char c = table[state][transition] ;
  state = c & 0x0f ;
  if (state == 3) strerr_dief1x(101, "current moved twice without being recreated") ;
  if (c & 0x10) newcurrent = 1 ;
  if (c & 0x20) { fd_close(fd) ; fd = -1 ; }
  if (c & 0x40)
  {
    fd = open_read("current") ;
    if (fd < 0) strerr_diefu1sys(111, "current") ;
  }
}

static void handle_event (int ifd, int watch)
{
  ssize_t r ;
  size_t offset = 0 ;
  union inotify_event_u u ;
  r = read(ifd, u.buf, sizeof(u.buf)) ;
  while (r > 0)
  {
    struct inotify_event *event = (struct inotify_event *)(u.buf + offset) ;
    offset += sizeof(struct inotify_event) + event->len ;
    r -= sizeof(struct inotify_event) + event->len ;
    if (event->wd == watch && !strcmp(event->name, "current"))
    {
      int transition = -1 ;
      if (event->mask & IN_CREATE) transition = 0 ;
      else if (event->mask & IN_MOVED_FROM) transition = 1 ;
      else if (event->mask & IN_MODIFY) transition = 2 ;
      if (transition >= 0) maketransition(transition) ;
    }
  }
}

int main (int argc, char const *const *argv)
{
  iopause_fd x[2] = { { .events = IOPAUSE_READ }, { .fd = 1 } } ;
  char const *dir = "." ;
  int watch ;
  unsigned int maxlen = 4096 ;
  PROG = "s6-logwatch" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "m:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'm' :
          if (!uint0_scan(l.arg, &maxlen)) dieusage() ;
          strerr_warnw1x("the -m option is deprecated") ;
          break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }

  if (argc) dir = *argv ;
  if (chdir(dir) < 0) strerr_diefu2sys(111, "chdir to ", dir) ;
  if (!fd_sanitize()) strerr_diefu1sys(111, "sanitize standard fds") ;

  x[0].fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC) ;
  if (x[0].fd < 0) strerr_diefu1sys(111, "inotify_init") ;
  watch = inotify_add_watch(x[0].fd, ".", IN_CREATE | IN_MOVED_FROM | IN_MODIFY) ;
  if (watch < 0) strerr_diefu1sys(111, "inotify_add_watch") ;
  fd = open_readb("current") ;
  if (fd < 0)
  {
    if (errno != ENOENT) strerr_diefu3sys(111, "open ", dir, "/current") ;
    state = B_WAITING ;
  }
  else state = B_READING ;
  if (sig_ignore(SIGPIPE) == -1) strerr_diefu1sys(111, "sig_ignore(SIGPIPE)") ;
  if (state == B_READING)
  {
    if (!readit(fd)) strerr_diefu3sys(111, "read from ", dir, "/current") ;
  }

  for (;;)
  {
    int r ;
    x[1].events = buffer_len(buffer_1) ? IOPAUSE_WRITE : 0 ;
    r = iopause(x, 2, 0, 0) ;
    if (r < 0) strerr_diefu1sys(111, "iopause") ;
    if (x[0].revents & IOPAUSE_EXCEPT) x[0].revents |= IOPAUSE_READ ;
    if (x[1].revents & IOPAUSE_EXCEPT) x[1].revents |= IOPAUSE_WRITE ;
    if (x[1].revents & IOPAUSE_WRITE)
    {
      if (!buffer_flush(buffer_1) && !error_isagain(errno))
        strerr_diefu1sys(111, "write to stdout") ;
      if (x[1].revents & IOPAUSE_EXCEPT) break ;
    }
    if (state == B_READING && buffer_available(buffer_1))
    {
      if (!readit(fd)) strerr_diefu3sys(111, "read from ", dir, "/current") ;
    }
    if (x[0].revents & IOPAUSE_READ) handle_event(x[0].fd, watch) ;
  }
  return 0 ;
}