2 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <config/config.h>
30 #include <sys/types.h>
31 #include <sys/event.h>
41 #ifndef HAVE_ARC4RANDOM
42 #include <openssl/rand.h>
46 #include <compat/strlcat.h>
49 #include "auditdistd.h"
54 vsnprlcat(char *str, size_t size, const char *fmt, va_list ap)
59 return (vsnprintf(str + len, size - len, fmt, ap));
63 snprlcat(char *str, size_t size, const char *fmt, ...)
69 result = vsnprlcat(str, size, fmt, ap);
79 case ADIST_ROLE_SENDER:
81 case ADIST_ROLE_RECEIVER:
88 adist_errstr(int error)
92 case ADIST_ERROR_WRONG_ORDER:
93 return ("wrong operations order");
94 case ADIST_ERROR_INVALID_NAME:
95 return ("invalid trail file name");
96 case ADIST_ERROR_OPEN_OLD:
97 return ("attempt to open an old trail file");
98 case ADIST_ERROR_CREATE:
99 return ("creation of new trail file failed");
100 case ADIST_ERROR_OPEN:
101 return ("open of existing trail file failed");
102 case ADIST_ERROR_READ:
103 return ("read failed");
104 case ADIST_ERROR_WRITE:
105 return ("write failed");
106 case ADIST_ERROR_RENAME:
107 return ("rename of a trail file failed");
109 return ("unknown error");
114 adreq_log(int loglevel, int debuglevel, int error, struct adreq *adreq,
115 const char *fmt, ...)
121 (void)vsnprintf(msg, sizeof(msg), fmt, ap);
123 (void)snprlcat(msg, sizeof(msg), "(seq=%ju) ",
124 (uintmax_t)adreq->adr_seq);
125 switch (adreq->adr_cmd) {
127 (void)snprlcat(msg, sizeof(msg), "OPEN(%s)",
130 case ADIST_CMD_APPEND:
131 (void)snprlcat(msg, sizeof(msg), "APPEND(%ju)",
132 (uintmax_t)adreq->adr_datasize);
134 case ADIST_CMD_CLOSE:
135 (void)snprlcat(msg, sizeof(msg), "CLOSE(%s)",
138 case ADIST_CMD_KEEPALIVE:
139 (void)snprlcat(msg, sizeof(msg), "KEEPALIVE");
141 case ADIST_CMD_ERROR:
142 (void)snprlcat(msg, sizeof(msg), "ERROR");
145 (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%hhu)",
150 (void)snprlcat(msg, sizeof(msg), ": %s", adist_errstr(error));
151 (void)strlcat(msg, ".", sizeof(msg));
152 pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
156 adist_random(unsigned char *buf, size_t size)
158 #ifdef HAVE_ARC4RANDOM_BUF
159 arc4random_buf(buf, size);
161 #elif defined(HAVE_ARC4RANDOM)
164 PJDLOG_ASSERT(size > 0);
165 PJDLOG_ASSERT((size % sizeof(val)) == 0);
169 bcopy(&val, buf, sizeof(val));
176 if (RAND_bytes(buf, (int)size) == 0)
182 static int wait_for_dir_kq = -1;
183 static int wait_for_file_kq = -1;
186 wait_for_dir_init(int fd)
192 PJDLOG_ASSERT(wait_for_dir_kq == -1);
195 PJDLOG_ASSERT(fd != -1);
200 pjdlog_errno(LOG_WARNING, "kqueue() failed");
203 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
205 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
207 pjdlog_errno(LOG_WARNING, "kevent() failed");
212 wait_for_dir_kq = kq;
219 wait_for_file_init(int fd)
226 PJDLOG_ASSERT(fd != -1);
229 if (wait_for_file_kq != -1) {
230 close(wait_for_file_kq);
231 wait_for_file_kq = -1;
236 pjdlog_errno(LOG_WARNING, "kqueue() failed");
239 EV_SET(&ev[0], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
241 EV_SET(&ev[1], fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,
243 if (kevent(kq, ev, 2, NULL, 0, NULL) == -1) {
245 pjdlog_errno(LOG_WARNING, "kevent() failed");
250 wait_for_file_kq = kq;
257 * Wait for new file to appear in directory.
266 if (wait_for_dir_kq == -1) {
272 PJDLOG_ASSERT(wait_for_dir_kq != -1);
274 if (kevent(wait_for_dir_kq, NULL, 0, &ev, 1, NULL) == -1) {
275 pjdlog_errno(LOG_WARNING, "kevent() failed");
282 * Wait for file growth or rename.
291 if (wait_for_file_kq == -1) {
297 PJDLOG_ASSERT(wait_for_file_kq != -1);
299 if (kevent(wait_for_file_kq, NULL, 0, ev, 2, NULL) == -1) {
300 pjdlog_errno(LOG_WARNING, "kevent() failed");