lsyncd/lsyncd.h

177 lines
4.3 KiB
C
Raw Normal View History

2012-02-15 19:10:50 +00:00
/**
2010-11-22 14:54:50 +00:00
* lsyncd.h Live (Mirror) Syncing Demon
*
2012-02-15 19:10:50 +00:00
* Interface between the core modules.
2010-11-22 14:54:50 +00:00
*
2012-02-15 19:10:50 +00:00
* License: GPLv2 (see COPYING) or any later version
2010-11-22 14:54:50 +00:00
* Authors: Axel Kittenberger <axkibe@gmail.com>
*
2012-02-15 19:10:50 +00:00
**/
2010-11-22 14:54:50 +00:00
#ifndef LSYNCD_H
#define LSYNCD_H
2012-02-15 19:10:50 +00:00
// some older machines need this to see pselect
#define _DEFAULT_SOURCE 1
#define _BSD_SOURCE 1
#define _XOPEN_SOURCE 700
#define _DARWIN_C_SOURCE 1
2012-09-25 15:29:12 +00:00
#define LUA_COMPAT_ALL
2016-11-24 14:44:08 +00:00
#define LUA_COMPAT_5_1
2012-09-25 15:29:12 +00:00
2012-02-15 19:10:50 +00:00
// includes needed for headerfile
2010-11-22 14:54:50 +00:00
#include "config.h"
2010-11-22 21:06:02 +00:00
#include <signal.h>
#include <stdbool.h>
2010-11-22 14:54:50 +00:00
#include <stdlib.h>
#define LUA_USE_APICHECK 1
#include <lua.h>
#define LSYNCD_LIBNAME "lsyncd"
#define LSYNCD_INOTIFYLIBNAME "inotify"
2016-11-24 14:44:08 +00:00
/*
| Workaround to register a library for different lua versions.
*/
#if LUA_VERSION_NUM > 502
#define lua_compat_register( L, name, lib ) \
{ \
lua_newtable((L)); \
luaL_setfuncs((L), (lib), 0); \
}
#else
#define lua_compat_register( L, name, lib ) \
{luaL_register( (L), (name), (lib) );}
#endif
2012-02-15 19:10:50 +00:00
/**
2010-11-22 21:06:02 +00:00
* Lsyncd runtime configuration
*/
extern struct settings {
2012-02-15 19:10:50 +00:00
char * log_file; // If not NULL Lsyncd logs into this file.
bool log_syslog; // If true Lsyncd sends log messages to syslog
char * log_ident; // If not NULL the syslog identity (otherwise "Lsyncd")
int log_facility; // The syslog facility
int log_level; // -1 logs everything, 0 normal mode, LOG_ERROR errors only.
bool nodaemon; // True if Lsyncd shall not daemonize.
bool onepass; // True if Lsyncd should exit after first sync pass
2012-02-15 19:10:50 +00:00
char * pidfile; // If not NULL Lsyncd writes its pid into this file.
2010-11-22 21:06:02 +00:00
} settings;
2012-02-15 19:10:50 +00:00
/**
* time comparisons - wrap around safe
2010-11-22 21:06:02 +00:00
*/
2010-11-22 14:54:50 +00:00
#define time_after(a,b) ((long)(b) - (long)(a) < 0)
#define time_before(a,b) time_after(b,a)
#define time_after_eq(a,b) ((long)(a) - (long)(b) >= 0)
#define time_before_eq(a,b) time_after_eq(b,a)
2012-02-15 19:10:50 +00:00
// returns (on Lua stack) the current kernels * clock state (jiffies)
extern int l_now(lua_State *L);
2010-11-22 14:54:50 +00:00
2012-02-15 19:10:50 +00:00
// pushes a runner function and the runner error handler onto Lua stack
2010-11-22 21:06:02 +00:00
extern void load_runner_func(lua_State *L, const char *name);
2012-02-15 19:10:50 +00:00
// set to 1 on hup signal or term signal
2010-11-22 21:06:02 +00:00
extern volatile sig_atomic_t hup;
extern volatile sig_atomic_t term;
2012-02-15 19:10:50 +00:00
/**
* wrappers for heap management, they exit if out-of-memory.
2010-11-22 14:54:50 +00:00
*/
extern void * s_calloc(size_t nmemb, size_t size);
extern void * s_malloc(size_t size);
extern void * s_realloc(void *ptr, size_t size);
extern char * s_strdup(const char *src);
2010-11-22 21:06:02 +00:00
2012-02-15 19:10:50 +00:00
/**
2010-11-22 21:06:02 +00:00
* Logging
*/
2012-02-15 19:10:50 +00:00
// Returns the positive priority if name is configured to be logged, or -1
2010-11-22 21:06:02 +00:00
extern int check_logcat(const char *name);
2012-02-15 19:10:50 +00:00
// logs a string
2010-11-22 14:54:50 +00:00
#define logstring(cat, message) \
2011-11-21 13:31:34 +00:00
{int p; if ((p = check_logcat(cat)) <= settings.log_level) \
2010-11-22 14:54:50 +00:00
{logstring0(p, cat, message);}}
extern void logstring0(int priority, const char *cat, const char *message);
2012-02-15 19:10:50 +00:00
// logs a formated string
2010-11-22 14:54:50 +00:00
#define printlogf(L, cat, ...) \
2011-11-21 13:31:34 +00:00
{int p; if ((p = check_logcat(cat)) <= settings.log_level) \
2010-11-22 14:54:50 +00:00
{printlogf0(L, p, cat, __VA_ARGS__);}}
extern void
2012-02-15 19:10:50 +00:00
printlogf0(lua_State *L,
int priority,
2010-11-22 14:54:50 +00:00
const char *cat,
2012-02-15 19:10:50 +00:00
const char *fmt,
2010-11-22 14:54:50 +00:00
...)
__attribute__((format(printf, 4, 5)));
2012-02-15 19:10:50 +00:00
/**
2010-11-22 21:06:02 +00:00
* File-descriptor helpers
*/
2012-02-15 19:10:50 +00:00
// Sets the non-blocking flag for a file descriptor.
2010-11-22 14:54:50 +00:00
extern void non_block_fd(int fd);
2012-02-15 19:10:50 +00:00
// Sets the close-on-exit flag for a file descriptor.
2010-11-22 14:54:50 +00:00
extern void close_exec_fd(int fd);
/**
* An observance to be called when a file descritor becomes
* read-ready or write-ready.
*/
struct observance {
2012-02-15 19:10:50 +00:00
// The file descriptor to observe.
int fd;
2012-02-15 19:10:50 +00:00
// Function to call when read becomes ready.
void (*ready)(lua_State *, struct observance *);
2012-02-15 19:10:50 +00:00
// Function to call when write becomes ready.
void (*writey)(lua_State *, struct observance *);
2012-02-15 19:10:50 +00:00
// Function to call to clean up
void (*tidy)(struct observance *);
2012-02-15 19:10:50 +00:00
// Extra tokens to pass to the functions.
void *extra;
};
2012-02-15 19:10:50 +00:00
// makes the core observe a file descriptor
extern void observe_fd(
2012-02-15 19:10:50 +00:00
int fd,
void (*ready) (lua_State *, struct observance *),
void (*writey)(lua_State *, struct observance *),
void (*tidy) (struct observance *),
void *extra
);
2010-11-22 21:06:02 +00:00
2012-02-15 19:10:50 +00:00
// stops the core to observe a file descriptor
extern void nonobserve_fd(int fd);
2010-11-22 21:06:02 +00:00
2014-04-29 14:11:27 +00:00
/*
2010-11-22 21:06:02 +00:00
* inotify
*/
2014-04-29 14:11:27 +00:00
#ifdef WITH_INOTIFY
2010-11-22 21:06:02 +00:00
extern void register_inotify(lua_State *L);
extern void open_inotify(lua_State *L);
#endif
2014-04-29 14:11:27 +00:00
/*
* /dev/fsevents
*/
2014-04-29 14:11:27 +00:00
#ifdef WITH_FSEVENTS
extern void open_fsevents(lua_State *L);
#endif
2010-11-22 14:54:50 +00:00
#endif