forked from lsyncd/lsyncd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsyncd.h
176 lines (142 loc) · 4.28 KB
/
lsyncd.h
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
173
174
175
176
/**
* lsyncd.h Live (Mirror) Syncing Demon
*
* Interface between the core modules.
*
* License: GPLv2 (see COPYING) or any later version
* Authors: Axel Kittenberger <[email protected]>
*
**/
#ifndef LSYNCD_H
#define LSYNCD_H
// 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
#define LUA_COMPAT_ALL
#define LUA_COMPAT_5_1
// includes needed for headerfile
#include "config.h"
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#define LUA_USE_APICHECK 1
#include <lua.h>
#define LSYNCD_LIBNAME "lsyncd"
#define LSYNCD_INOTIFYLIBNAME "inotify"
/*
| 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
/**
* Lsyncd runtime configuration
*/
extern struct settings {
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
char * pidfile; // If not NULL Lsyncd writes its pid into this file.
} settings;
/**
* time comparisons - wrap around safe
*/
#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)
// returns (on Lua stack) the current kernels * clock state (jiffies)
extern int l_now(lua_State *L);
// pushes a runner function and the runner error handler onto Lua stack
extern void load_runner_func(lua_State *L, const char *name);
// set to 1 on hup signal or term signal
extern volatile sig_atomic_t hup;
extern volatile sig_atomic_t term;
/**
* wrappers for heap management, they exit if out-of-memory.
*/
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);
/**
* Logging
*/
// Returns the positive priority if name is configured to be logged, or -1
extern int check_logcat(const char *name);
// logs a string
#define logstring(cat, message) \
{int p; if ((p = check_logcat(cat)) <= settings.log_level) \
{logstring0(p, cat, message);}}
extern void logstring0(int priority, const char *cat, const char *message);
// logs a formated string
#define printlogf(L, cat, ...) \
{int p; if ((p = check_logcat(cat)) <= settings.log_level) \
{printlogf0(L, p, cat, __VA_ARGS__);}}
extern void
printlogf0(lua_State *L,
int priority,
const char *cat,
const char *fmt,
...)
__attribute__((format(printf, 4, 5)));
/**
* File-descriptor helpers
*/
// Sets the non-blocking flag for a file descriptor.
extern void non_block_fd(int fd);
// Sets the close-on-exit flag for a file descriptor.
extern void close_exec_fd(int fd);
/**
* An observance to be called when a file descritor becomes
* read-ready or write-ready.
*/
struct observance {
// The file descriptor to observe.
int fd;
// Function to call when read becomes ready.
void (*ready)(lua_State *, struct observance *);
// Function to call when write becomes ready.
void (*writey)(lua_State *, struct observance *);
// Function to call to clean up
void (*tidy)(struct observance *);
// Extra tokens to pass to the functions.
void *extra;
};
// makes the core observe a file descriptor
extern void observe_fd(
int fd,
void (*ready) (lua_State *, struct observance *),
void (*writey)(lua_State *, struct observance *),
void (*tidy) (struct observance *),
void *extra
);
// stops the core to observe a file descriptor
extern void nonobserve_fd(int fd);
/*
* inotify
*/
#ifdef WITH_INOTIFY
extern void register_inotify(lua_State *L);
extern void open_inotify(lua_State *L);
#endif
/*
* /dev/fsevents
*/
#ifdef WITH_FSEVENTS
extern void open_fsevents(lua_State *L);
#endif
#endif