transmission/libtransmission/watchdir-inotify.c

200 lines
5.1 KiB
C
Raw Normal View History

/*
* This file Copyright (C) 2015-2016 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
#include <errno.h>
#include <limits.h> /* NAME_MAX */
#include <stdlib.h> /* realloc() */
#include <unistd.h> /* close() */
#include <sys/inotify.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#define __LIBTRANSMISSION_WATCHDIR_MODULE__
#include "transmission.h"
#include "log.h"
#include "tr-assert.h"
#include "utils.h"
#include "watchdir.h"
#include "watchdir-common.h"
/***
****
***/
#define log_error(...) (!tr_logLevelIsActive(TR_LOG_ERROR) ? (void)0 : \
tr_logAddMessage(__FILE__, __LINE__, TR_LOG_ERROR, "watchdir:inotify", __VA_ARGS__))
/***
****
***/
typedef struct tr_watchdir_inotify
{
tr_watchdir_backend base;
int infd;
int inwd;
struct bufferevent* event;
}
tr_watchdir_inotify;
#define BACKEND_UPCAST(b) ((tr_watchdir_inotify*)(b))
#define INOTIFY_WATCH_MASK (IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE)
/***
****
***/
static void tr_watchdir_inotify_on_first_scan(evutil_socket_t fd UNUSED, short type UNUSED, void* context)
{
tr_watchdir_t const handle = context;
tr_watchdir_scan(handle, NULL);
}
static void tr_watchdir_inotify_on_event(struct bufferevent* event, void* context)
{
TR_ASSERT(context != NULL);
tr_watchdir_t const handle = context;
fix: gcc warnings in libtransmission/ and utils/ (#843) * fix: __attribute__(__printf__) warnings * fix: implicit fallthrough warning * fixup! fix: implicit fallthrough warning * fix: disable warnings for 3rd party code Since we want to leave upstream code as-is * fixup! fix: disable warnings for 3rd party code * fixup! fix: disable warnings for 3rd party code * silence spurious alignment warning Xrefs Discussion: https://stackoverflow.com/a/35554349 Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35 * fixup! fix: disable warnings for 3rd party code * fixup! fix: implicit fallthrough warning * make uncrustify happy * remove uncrustify-test.sh that's probably off-topic for this PR * fixup! fix: __attribute__(__printf__) warnings * Update libtransmission/CMakeLists.txt Co-Authored-By: ckerr <ckerr@github.com> * fixup! silence spurious alignment warning * use -w for DISABLE_WARNINGS in Clang * refactor: fix libtransmission deprecation warnings * fix: pthread_create's start_routine's return value This was defined as `void` on non-Windows but should have been `void*` * chore: uncrustify * fix: add DISABLE_WARNINGS option for SunPro Studio * fix "unused in lambda capture" warnings by clang++ * fix 'increases required alignment' warning Caused from storing int16_t's in a char array. * fix net.c 'increases required alignment' warning The code passes in a `struct sockaddr_storage*` which is a padded struct large enough for the necessary alignment. Unfortunately it was recast as a `struct sockaddr*` which has less padding and a smaller alignment. The warning occrred because of these differing alignments. * make building quieter so warnings are more visible * fixup! fix 'increases required alignment' warning * Fix -Wcast-function-type warnings in GTK+ app code https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both the issue and its solution. GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib applications because it's idiomatic there to recast function signatures, e.g. `g_slist_free(list, (GFunc)g_free, NULL);`. Disabling the warning with pragmas causes "unrecognized pragma" warnings on clang and older versions of gcc, and disabling the warning could miss actual bugs. GCC defines `void (*)(void)` as a special case that matches anything so we can silence warnings by double-casting through GCallback. In the previous example, the warning is silenced by changing the code to read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`). * fixup! fix "unused in lambda capture" warnings by clang++ * fixup! fix "unused in lambda capture" warnings by clang++ * fix two more libtransmission compiler warnings * fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
#ifdef TR_ENABLE_ASSERTS
tr_watchdir_inotify* const backend = BACKEND_UPCAST(tr_watchdir_get_backend(handle));
fix: gcc warnings in libtransmission/ and utils/ (#843) * fix: __attribute__(__printf__) warnings * fix: implicit fallthrough warning * fixup! fix: implicit fallthrough warning * fix: disable warnings for 3rd party code Since we want to leave upstream code as-is * fixup! fix: disable warnings for 3rd party code * fixup! fix: disable warnings for 3rd party code * silence spurious alignment warning Xrefs Discussion: https://stackoverflow.com/a/35554349 Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35 * fixup! fix: disable warnings for 3rd party code * fixup! fix: implicit fallthrough warning * make uncrustify happy * remove uncrustify-test.sh that's probably off-topic for this PR * fixup! fix: __attribute__(__printf__) warnings * Update libtransmission/CMakeLists.txt Co-Authored-By: ckerr <ckerr@github.com> * fixup! silence spurious alignment warning * use -w for DISABLE_WARNINGS in Clang * refactor: fix libtransmission deprecation warnings * fix: pthread_create's start_routine's return value This was defined as `void` on non-Windows but should have been `void*` * chore: uncrustify * fix: add DISABLE_WARNINGS option for SunPro Studio * fix "unused in lambda capture" warnings by clang++ * fix 'increases required alignment' warning Caused from storing int16_t's in a char array. * fix net.c 'increases required alignment' warning The code passes in a `struct sockaddr_storage*` which is a padded struct large enough for the necessary alignment. Unfortunately it was recast as a `struct sockaddr*` which has less padding and a smaller alignment. The warning occrred because of these differing alignments. * make building quieter so warnings are more visible * fixup! fix 'increases required alignment' warning * Fix -Wcast-function-type warnings in GTK+ app code https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both the issue and its solution. GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib applications because it's idiomatic there to recast function signatures, e.g. `g_slist_free(list, (GFunc)g_free, NULL);`. Disabling the warning with pragmas causes "unrecognized pragma" warnings on clang and older versions of gcc, and disabling the warning could miss actual bugs. GCC defines `void (*)(void)` as a special case that matches anything so we can silence warnings by double-casting through GCallback. In the previous example, the warning is silenced by changing the code to read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`). * fixup! fix "unused in lambda capture" warnings by clang++ * fixup! fix "unused in lambda capture" warnings by clang++ * fix two more libtransmission compiler warnings * fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
#endif
struct inotify_event ev;
size_t nread;
size_t name_size = NAME_MAX + 1;
char* name = tr_new(char, name_size);
/* Read the size of the struct excluding name into buf. Guaranteed to have at
least sizeof(ev) available */
while ((nread = bufferevent_read(event, &ev, sizeof(ev))) != 0)
{
if (nread == (size_t)-1)
{
log_error("Failed to read inotify event: %s", tr_strerror(errno));
break;
}
if (nread != sizeof(ev))
{
log_error("Failed to read inotify event: expected %zu, got %zu bytes.", sizeof(ev), nread);
break;
}
TR_ASSERT(ev.wd == backend->inwd);
TR_ASSERT((ev.mask & INOTIFY_WATCH_MASK) != 0);
TR_ASSERT(ev.len > 0);
if (ev.len > name_size)
{
name_size = ev.len;
name = tr_renew(char, name, name_size);
}
/* Consume entire name into buffer */
if ((nread = bufferevent_read(event, name, ev.len)) == (size_t)-1)
{
log_error("Failed to read inotify name: %s", tr_strerror(errno));
break;
}
if (nread != ev.len)
{
log_error("Failed to read inotify name: expected %" PRIu32 ", got %zu bytes.", ev.len, nread);
break;
}
tr_watchdir_process(handle, name);
}
tr_free(name);
}
static void tr_watchdir_inotify_free(tr_watchdir_backend* backend_base)
{
tr_watchdir_inotify* const backend = BACKEND_UPCAST(backend_base);
if (backend == NULL)
{
return;
}
TR_ASSERT(backend->base.free_func == &tr_watchdir_inotify_free);
if (backend->event != NULL)
{
bufferevent_disable(backend->event, EV_READ);
bufferevent_free(backend->event);
}
if (backend->infd != -1)
{
if (backend->inwd != -1)
{
inotify_rm_watch(backend->infd, backend->inwd);
}
close(backend->infd);
}
tr_free(backend);
}
tr_watchdir_backend* tr_watchdir_inotify_new(tr_watchdir_t handle)
{
char const* const path = tr_watchdir_get_path(handle);
tr_watchdir_inotify* backend;
backend = tr_new0(tr_watchdir_inotify, 1);
backend->base.free_func = &tr_watchdir_inotify_free;
backend->infd = -1;
backend->inwd = -1;
if ((backend->infd = inotify_init()) == -1)
{
log_error("Unable to inotify_init: %s", tr_strerror(errno));
goto fail;
}
if ((backend->inwd = inotify_add_watch(backend->infd, path, INOTIFY_WATCH_MASK | IN_ONLYDIR)) == -1)
{
log_error("Failed to setup watchdir \"%s\": %s (%d)", path, tr_strerror(errno), errno);
goto fail;
}
if ((backend->event = bufferevent_socket_new(tr_watchdir_get_event_base(handle), backend->infd, 0)) == NULL)
{
log_error("Failed to create event buffer: %s", tr_strerror(errno));
goto fail;
}
/* Guarantees at least the sizeof an inotify event will be available in the
event buffer */
bufferevent_setwatermark(backend->event, EV_READ, sizeof(struct inotify_event), 0);
bufferevent_setcb(backend->event, &tr_watchdir_inotify_on_event, NULL, NULL, handle);
bufferevent_enable(backend->event, EV_READ);
/* Perform an initial scan on the directory */
if (event_base_once(tr_watchdir_get_event_base(handle), -1, EV_TIMEOUT, &tr_watchdir_inotify_on_first_scan, handle,
NULL) == -1)
{
log_error("Failed to perform initial scan: %s", tr_strerror(errno));
}
return BACKEND_DOWNCAST(backend);
fail:
tr_watchdir_inotify_free(BACKEND_DOWNCAST(backend));
return NULL;
}