diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-11-28 16:33:57 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-11-28 16:33:57 -0500 |
commit | bc25cc045d6d4fdb6b20707cdec4da14890ee9d1 (patch) | |
tree | 76914ec4cd7af6444892b51016853b8a480f8a2e | |
parent | d8c48a5aae8f3561cec14f7f08c35fc3619ef00a (diff) |
cleanup inotify helpers
-rw-r--r-- | common/inotify_helpers.c | 140 | ||||
-rw-r--r-- | common/inotify_helpers.h | 66 | ||||
-rw-r--r-- | nslcd/hackers_watch.c | 2 | ||||
-rw-r--r-- | nslcd/inotify_helper.c | 51 | ||||
-rw-r--r-- | nslcd/inotify_iterator.h | 78 |
5 files changed, 105 insertions, 232 deletions
diff --git a/common/inotify_helpers.c b/common/inotify_helpers.c index 0aac023..781843e 100644 --- a/common/inotify_helpers.c +++ b/common/inotify_helpers.c @@ -1,52 +1,9 @@ -#include <stdio.h> -#include <stdlib.h> - +#include <stdio.h> /* for printf(3) */ +#include <stdlib.h> /* for realloc(3), memcpy(3) */ +#include <unistd.h> /* for read(2) */ #include "inotify_helpers.h" -static -const char *in_bits[] = { - /* main */ - "IN_ACCESS", // 0 - "IN_MODIFY", // 1 - "IN_ATTRIB", // 2 - "IN_CLOSE_WRITE", // 3 - "IN_CLOSE_NOWRITE", // 4 - "IN_OPEN", // 5 - "IN_MOVED_FROM", // 6 - "IN_MOVED_TO", // 7 - "IN_CREATE", // 8 - "IN_DELETE", // 9 - "IN_DELETE_SELF", // 10 - "IN_MOVE_SELF", // 11 - /* gap */ - "<invalid-12>", // 12 - /* events sent by the kernel */ - "IN_UNMOUNT", // 13 - "IN_Q_OVERFLOW", // 14 - "IN_IGNORED", // 15 - /* gap */ - "<invalid-16>", // 16 - "<invalid-17>", // 17 - "<invalid-18>", // 18 - "<invalid-19>", // 19 - "<invalid-20>", // 20 - "<invalid-21>", // 21 - "<invalid-22>", // 22 - "<invalid-23>", // 23 - /* special flags */ - "IN_ONLYDIR", // 24 - "IN_DONT_FOLLOW", // 25 - "IN_EXCL_UNLINK", // 26 - "<invalid-27>", // 27 - "<invalid-28>", // 28 - "IN_MASK_ADD", // 29 - "IN_ISDIR", // 30 - "IN_ONESHOT", // 31 -}; - -char *inotify_mask2str(uint32 mask); -int inotify_print_event(FILE *file, struct inotify_event *event); - + struct strbuf { char *str; @@ -55,7 +12,8 @@ struct strbuf { }; static -void strbufcat(struct strbuf *a, const char *b) { +void +strbufcat(struct strbuf *a, const char *b) { size_t blen = strlen(b); while (a->cap <= (a->len + blen)) { a->cap += 256; @@ -65,20 +23,68 @@ void strbufcat(struct strbuf *a, const char *b) { a->len += blen; } + -char *inotify_mask2str(uint32_t mask) { - struct strbuf out = { NULL, 0, 0 }; - for (size_t i = 0; i < sizeof(in_bits)/sizeof(in_bits[0]); i++) { +static +const char *in_mask_bits[] = { + /* main */ + "IN_ACCESS", // 0 + "IN_MODIFY", // 1 + "IN_ATTRIB", // 2 + "IN_CLOSE_WRITE", // 3 + "IN_CLOSE_NOWRITE", // 4 + "IN_OPEN", // 5 + "IN_MOVED_FROM", // 6 + "IN_MOVED_TO", // 7 + "IN_CREATE", // 8 + "IN_DELETE", // 9 + "IN_DELETE_SELF", // 10 + "IN_MOVE_SELF", // 11 + /* gap */ + "<invalid-12>", // 12 + /* events sent by the kernel */ + "IN_UNMOUNT", // 13 + "IN_Q_OVERFLOW", // 14 + "IN_IGNORED", // 15 + /* gap */ + "<invalid-16>", // 16 + "<invalid-17>", // 17 + "<invalid-18>", // 18 + "<invalid-19>", // 19 + "<invalid-20>", // 20 + "<invalid-21>", // 21 + "<invalid-22>", // 22 + "<invalid-23>", // 23 + /* special flags */ + "IN_ONLYDIR", // 24 + "IN_DONT_FOLLOW", // 25 + "IN_EXCL_UNLINK", // 26 + "<invalid-27>", // 27 + "<invalid-28>", // 28 + "IN_MASK_ADD", // 29 + "IN_ISDIR", // 30 + "IN_ONESHOT", // 31 +}; + +char * +inotify_mask2str(uint32_t mask) { + struct strbuf out = { 0 }; + for (size_t i = 0; + i < sizeof(in_mask_bits)/sizeof(in_mask_bits[0]); + i++) { if (mask & (1 << i)) { if (out.len > 0) - strbufcat(&out, ", "); + strbufcat(&out, " | "); strbufcat(&out, in_bits[i]); } } return out.str; } -int inotify_print_event(struct inotify_event *event) { + + +int +inotify_print_event(struct inotify_event *event) { return printf("wd:%d\n" "\tmask:[%s]\n" "\tcookie:%u\n" @@ -90,3 +96,31 @@ int inotify_print_event(struct inotify_event *event) { event->len, event->len > 0 ? event->name : ""); } + + + +#define EVENT_PTR(BUF) ((struct inotify_event *)&((BUF)->dat[(BUF)->pos])) +#define EVENT_SIZE(BUF) (sizeof(struct inotify_event) + EVENT_PTR(BUF)->len) + +struct inotify_event * +inotify_next_event_r(int fd, struct inotify_buffer *buf) { + if (((ssize_t)(buf->len - buf->pos - sizeof(struct inotify_event)) > 0) + && ((ssize_t)(buf->len - buf->pos - EVENT_SIZE(buf)) > 0)) { + buf->pos += EVENT_SIZE(buf); + } else { + do { + buf->len = read(fd, buf->dat, sizeof(buf->dat)); + } while (buf->len == 0); + buf->pos = 0; + if (buf->len < 0) { + return NULL; + } + } + return EVENT_PTR(buf); +} + +struct inotify_event * +inotify_next_event(int fd) { + static struct inotify_buffer buf = { 0 }; + return inotify_next_event_r(fd, &buf); +} diff --git a/common/inotify_helpers.h b/common/inotify_helpers.h index 5bb7647..a0d884a 100644 --- a/common/inotify_helpers.h +++ b/common/inotify_helpers.h @@ -16,65 +16,33 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -/* The usage is pretty simple: - * - * int my_filedesc = inotify_init(); - * ... - * struct inotify_event *my_event; - * for (INOTIFY_ITR(my_filedesc, my_event)) { - * ... - * } - * - * Easy, right? - */ - #ifndef _INOTIFY_HELPER_H #define _INOTIFY_HELPER_H -#include <sys/inotify.h> -#include <limits.h> /* for NAME_MAX */ -#include <string.h> /* for memset(3) */ -#include <unistd.h> /* for read(3) */ +#include <sys/inotify.h> /* for 'struct inotify_event' */ +#include <sys/types.h> /* for ssize_t, uint32_t */ +#include <limits.h> /* for NAME_MAX */ + +char *inotify_mask2str(uint32_t mask); +int inotify_print_event(struct inotify_event *event); -struct _inotify_buffer { +struct inotify_buffer { ssize_t len; ssize_t pos; char dat[sizeof(struct inotify_event)+NAME_MAX+1]; }; -#define _INOTIFY_ITR_EVENT(BUF) \ - ((struct inotify_event *)&((BUF).dat[(BUF).pos])) -#define _INOTIFY_ITR_EVENT_SIZE(BUF) \ - (sizeof(struct inotify_event) + _INOTIFY_ITR_EVENT(BUF)->len) +struct inotify_event *inotify_next_event_r(int fd, struct inotify_buffer *buf); +struct inotify_event *inotify_next_event(int fd); -#define _INOTIFY_ITR_INIT(FD, BUF, EVENT) \ - struct _inotify_buffer BUF = ({ \ - struct _inotify_buffer tmp; \ - memset(&tmp, 0, sizeof(tmp)); \ - do { \ - tmp.len = read(FD, tmp.dat, sizeof(tmp.dat)); \ - } while (tmp.len == 0); \ - EVENT = (struct inotify_event *)&BUF; \ - tmp; \ - }) -#define _INOTIFY_ITR_CHECK(FD, BUF, EVENT) \ - BUF.len > -1 -#define _INOTIFY_ITR_UPDATE(FD, BUF, EVENT) \ - EVENT = ({ \ - if (BUF.len-(BUF.pos+_INOTIFY_ITR_EVENT_SIZE(BUF)) < 1) { \ - do { \ - BUF.len = read(FD, BUF.dat, sizeof(BUF.dat)); \ - } while (BUF.len == 0); \ - BUF.pos = 0; \ - } else { \ - BUF.pos += _INOTIFY_ITR_EVENT_SIZE(BUF); \ - } \ - _INOTIFY_ITR_EVENT(BUF); \ - }) +#define INOTIFY_ITERATOR(FD, EVENT) \ + struct inotify_event *EVENT = inotify_next_event(FD); \ + EVENT != NULL; \ + EVENT = inotify_next_event(FD) -#define INOTIFY_ITR(FD, EVENT) \ - _INOTIFY_ITR_INIT(FD, _buf, EVENT); \ - _INOTIFY_ITR_CHECK(FD, _buf, EVENT); \ - _INOTIFY_ITR_UPDATE(FD, _buf, EVENT) +#define INOTIFY_ITERATOR_R(FD, EVENT, BUF) \ + struct inotify_event *EVENT = inotify_next_event_r(FD, BUF); \ + EVENT != NULL; \ + EVENT = inotify_next_event_r(FD, BUF) #endif diff --git a/nslcd/hackers_watch.c b/nslcd/hackers_watch.c index 5217163..6687675 100644 --- a/nslcd/hackers_watch.c +++ b/nslcd/hackers_watch.c @@ -21,7 +21,7 @@ #include <stdio.h> /* for asprintf(3) */ #include <unistd.h> /* for chdir(3) */ -#include "inotify_iterator.h" +#include "inotify_helpers.h" #include "hackers_parse.h" #include "hackers_watch.h" diff --git a/nslcd/inotify_helper.c b/nslcd/inotify_helper.c deleted file mode 100644 index dc1ae63..0000000 --- a/nslcd/inotify_helper.c +++ /dev/null @@ -1,51 +0,0 @@ -/* helper.h */ -#include <sys/inotify.h> /* for 'struct inotify_event' */ -#include <limits.h> /* for 'NAME_MAX' */ - -struct inotify_buffer { - ssize_t len; - ssize_t pos; - char dat[sizeof(struct inotify_event)+NAME_MAX+1]; -}; - -struct inotify_event *inotify_next_event_r(int fd, struct inotify_buffer *buf); -struct inotify_event *inotify_next_event(int fd); - -#define INOTIFY_ITERATOR(FD, EVENT) \ - struct inotify_event *EVENT = inotify_next_event(FD); \ - EVENT != NULL; \ - EVENT = inotify_next_event(FD) - -#define INOTIFY_ITERATOR_R(FD, EVENT, BUF) \ - struct inotify_event *EVENT = inotify_next_event_r(FD, BUF); \ - EVENT != NULL; \ - EVENT = inotify_next_event_r(FD, BUF) - -/* helper.c */ -#include <unistd.h> /* for read(3) */ - -#define EVENT_PTR(BUF) ((struct inotify_event *)&((BUF)->dat[(BUF)->pos])) -#define EVENT_SIZE(BUF) (sizeof(struct inotify_event) + EVENT_PTR(BUF)->len) - -struct inotify_event * -inotify_next_event_r(int fd, struct inotify_buffer *buf) { - if ((buf->len - buf->pos > sizeof(struct inotify_event)) - && (buf->len - buf->pos > EVENT_SIZE(buf))) { - buf->pos += EVENT_SIZE(buf); - } else { - do { - buf->len = read(FD, buf->dat, sizeof(buf->dat)); - } while (buf->len == 0); - BUF.pos = 0; - if (buf->len < 0) { - return NULL; - } - } - return EVENT_PTR(buf); -} - -struct inotify_event * -inotify_next_event(int fd) { - static struct inotify_buffer buf = { 0 }; - return inotify_next_event_r(fd, &buf); -} diff --git a/nslcd/inotify_iterator.h b/nslcd/inotify_iterator.h deleted file mode 100644 index 0fa5829..0000000 --- a/nslcd/inotify_iterator.h +++ /dev/null @@ -1,78 +0,0 @@ -/* inotify_helper.h - Simple iteration for inotify events - * - * Copyright (C) 2014 Luke Shumaker - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -/* The usage is pretty simple: - * - * int my_filedesc = inotify_init(); - * ... - * struct inotify_event *my_event; - * for (INOTIFY_ITERATOR(my_filedesc, my_event)) { - * ... - * } - * - * Easy, right? - */ - -#ifndef _INOTIFY_ITERATOR_H -#define _INOTIFY_ITERATOR_H - -#include <sys/inotify.h> -#include <limits.h> /* for NAME_MAX */ -#include <string.h> /* for memset(3) */ -#include <unistd.h> /* for read(3) */ - -struct _inotify_buffer { - ssize_t len; - ssize_t pos; - char dat[sizeof(struct inotify_event)+NAME_MAX+1]; -}; - -#define _INOTIFY_ITERATOR_EVENT(BUF) \ - ((struct inotify_event *)&((BUF).dat[(BUF).pos])) -#define _INOTIFY_ITERATOR_EVENT_SIZE(BUF) \ - (sizeof(struct inotify_event) + _INOTIFY_ITERATOR_EVENT(BUF)->len) -#define _INOTIFY_ITERATOR_CHECK(FD, BUF, EVENT) \ - BUF.len > -1 -#define _INOTIFY_ITERATOR_UPDATE(FD, BUF, EVENT) \ - EVENT = ({ \ - if (BUF.len-(BUF.pos+_INOTIFY_ITERATOR_EVENT_SIZE(BUF)) < 1) { \ - do { \ - BUF.len = read(FD, BUF.dat, sizeof(BUF.dat)); \ - } while (BUF.len == 0); \ - BUF.pos = 0; \ - } else { \ - BUF.pos += _INOTIFY_ITERATOR_EVENT_SIZE(BUF); \ - } \ - _INOTIFY_ITERATOR_EVENT(BUF); \ - }) -#define _INOTIFY_ITERATOR_INIT(FD, BUF, EVENT) \ - struct _inotify_buffer BUF = ({ \ - struct _inotify_buffer tmp; \ - memset(&tmp, 0, sizeof(tmp)); \ - do { \ - tmp.len = read(FD, tmp.dat, sizeof(tmp.dat)); \ - } while (tmp.len == 0); \ - EVENT = (struct inotify_event *)&BUF; \ - tmp; \ - }) -#define INOTIFY_ITERATOR(FD, EVENT) \ - _INOTIFY_ITERATOR_INIT(FD, _buf, EVENT); \ - _INOTIFY_ITERATOR_CHECK(FD, _buf, EVENT); \ - _INOTIFY_ITERATOR_UPDATE(FD, _buf, EVENT) - -#endif |