Blame view

common.h 5.57 KB
redmine authored
1
/*
redmine authored
2
    clsync - file tree sync utility based on fanotify and inotify
redmine authored
3

redmine authored
4
    Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
redmine authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    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/>.
 */

redmine authored
20
#define _GNU_SOURCE
redmine authored
21
#define _XOPEN_SOURCE 700
redmine authored
22 23
#define _LARGEFILE64_SOURCE

redmine authored
24 25
#define PROGRAM "clsync"
#define VERSION_MAJ	0
redmine authored
26
#define VERSION_MIN	2
redmine authored
27
#define AUTHOR "Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C"
redmine authored
28

redmine authored
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <getopt.h>
#include <limits.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <wait.h>
redmine authored
46
#include <fts.h>
redmine authored
47
#ifdef FANOTIFY_SUPPORT
redmine authored
48
#include <sys/fanotify.h>
redmine authored
49
#endif
redmine authored
50
#include <sys/inotify.h>
redmine authored
51
#include <sys/time.h>
redmine authored
52
#include <dirent.h>
redmine authored
53
#include <sys/utsname.h>
redmine authored
54 55 56 57
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
redmine authored
58
#include <libgen.h>
redmine authored
59
#include <pthread.h>
redmine authored
60

redmine authored
61 62 63 64 65
#ifdef HAVE_CAPABILITIES
#include <sys/capability.h>	// for capset()/capget() for --preserve-file-access
#include <sys/prctl.h>		// for prctl() for --preserve-fil-access
#endif

Barak A. Pearlmutter authored
66
#include "configuration.h"
Barak A. Pearlmutter authored
67
#ifdef HAVE_CONFIG_H
Barak A. Pearlmutter authored
68
#include "config.h"
Barak A. Pearlmutter authored
69
#endif
redmine authored
70

redmine authored
71
#include "clsync.h"
redmine authored
72
#include "ctx.h"
redmine authored
73 74
#include "indexes.h"

redmine authored
75
#ifndef MIN
Barak A. Pearlmutter authored
76
#define MIN(a,b) ((a)>(b)?(b):(a))
redmine authored
77 78 79
#endif

#ifndef MAX
Barak A. Pearlmutter authored
80
#define MAX(a,b) ((a)>(b)?(a):(b))
redmine authored
81 82
#endif

redmine authored
83 84 85 86
#ifndef IN_CREATE_SELF
#define IN_CREATE_SELF IN_CREATE
#endif

redmine authored
87 88 89
#ifdef _DEBUG
#define DEBUGV(...) __VA_ARGS__
#else
redmine authored
90
#define DEBUGV(...)
redmine authored
91 92
#endif

redmine authored
93 94 95 96 97 98
#ifdef PARANOID
#define PARANOIDV(...) __VA_ARGS__
#else
#define PARANOIDV(...)
#endif

redmine authored
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#ifdef _GNU_SOURCE
#	ifndef likely
#		define likely(x)    __builtin_expect(!!(x), 1)
#	endif
#	ifndef unlikely
#		define unlikely(x)  __builtin_expect(!!(x), 0)
#	endif
#else
#	ifndef likely
#		define likely(x)   (x)
#	endif
#	ifndef unlikely
#		define unlikely(x) (x)
#	endif
#endif


redmine authored
116 117 118
#define TOSTR(a) # a
#define XTOSTR(a) TOSTR(a)

redmine authored
119 120
#define COLLECTDELAY_INSTANT ((unsigned int)~0)

redmine authored
121

redmine authored
122 123 124 125 126 127 128
enum paramsource_enum {
	PS_UNKNOWN	 = 0,
	PS_ARGUMENT,
	PS_CONFIG
};
typedef enum paramsource_enum paramsource_t;

redmine authored
129 130 131

enum notifyengine_enum {
	NE_UNDEFINED = 0,
redmine authored
132
#ifdef FANOTIFY_SUPPORT
redmine authored
133
	NE_FANOTIFY,
redmine authored
134
#endif
redmine authored
135 136 137 138
	NE_INOTIFY
};
typedef enum notifyengine_enum notifyenfine_t;

redmine authored
139
#define STATE_STARTING(state_p) (state_p == NULL)
redmine authored
140 141
enum state_enum {
	STATE_EXIT 	= 0,
redmine authored
142
	STATE_STARTING,
redmine authored
143 144
	STATE_RUNNING,
	STATE_REHASH,
redmine authored
145
	STATE_TERM,
redmine authored
146
	STATE_THREAD_GC,
redmine authored
147 148
	STATE_INITSYNC,
	STATE_UNKNOWN
redmine authored
149 150 151
};
typedef enum state_enum state_t;

redmine authored
152 153 154 155 156 157 158
enum threadingmode {
	PM_OFF	= 0,
	PM_SAFE,
	PM_FULL
};
typedef enum threadingmode threadingmode_t;

redmine authored
159
/*
redmine authored
160 161 162 163 164 165 166 167
struct excludeinfo {
	unsigned int	seqid_min;
	unsigned int	seqid_max;
	eventobjtype_t	objtype_old;
	eventobjtype_t	objtype_new;
	uint32_t	flags;
};
typedef struct eventinfo eventinfo_t;
redmine authored
168
*/
redmine authored
169 170
struct eventinfo {
	uint32_t	evmask;
redmine authored
171 172
	unsigned int	seqid_min;
	unsigned int	seqid_max;
redmine authored
173 174
	eventobjtype_t	objtype_old;
	eventobjtype_t	objtype_new;
redmine authored
175 176
	int		wd;
	size_t		fsize;
redmine authored
177
	uint32_t	flags;
redmine authored
178 179 180
};
typedef struct eventinfo eventinfo_t;

redmine authored
181

redmine authored
182
typedef int (*thread_callbackfunct_t)(ctx_t *ctx_p, char **argv);
redmine authored
183
struct threadinfo {
redmine authored
184
	int			  thread_num;
redmine authored
185
	uint32_t		  iteration;
redmine authored
186 187 188
	thread_callbackfunct_t 	  callback;
	char 			**argv;
	pthread_t		  pthread;
redmine authored
189
	int			  exitcode;
redmine authored
190
	int			  errcode;
redmine authored
191
	state_t			  state;
redmine authored
192
	ctx_t			 *ctx_p;
redmine authored
193 194
	time_t			  starttime;
	time_t			  expiretime;
redmine authored
195
	int			  child_pid;
redmine authored
196

redmine authored
197 198
	GHashTable		 *fpath2ei_ht;		// file path -> event information

redmine authored
199 200
	int			  try_n;

redmine authored
201 202 203
	// for so-synchandler
	int			  n;
	api_eventinfo_t		 *ei;
redmine authored
204 205 206
};
typedef struct threadinfo threadinfo_t;

redmine authored
207
enum pthread_mutex_id {
redmine authored
208
	PTHREAD_MUTEX_STATE,
redmine authored
209 210 211 212
	PTHREAD_MUTEX_MAX
};


redmine authored
213
struct threadsinfo {
redmine authored
214
	pthread_mutex_t		  mutex[PTHREAD_MUTEX_MAX];
redmine authored
215
	pthread_cond_t		  cond [PTHREAD_MUTEX_MAX];
redmine authored
216 217 218 219 220 221
	char			  mutex_init;
	int			  allocated;
	int			  used;
	threadinfo_t 		 *threads;
	threadinfo_t 		**threadsstack;	// stack of threadinfo_t to be used on thread_new()
	int			  stacklen;
redmine authored
222 223 224
};
typedef struct threadsinfo threadsinfo_t;

redmine authored
225 226
struct dosync_arg {
	int evcount;
redmine authored
227
	char excf_path[PATH_MAX+1];
redmine authored
228
	char outf_path[PATH_MAX+1];
redmine authored
229
	FILE *outf;
redmine authored
230
	ctx_t *ctx_p;
redmine authored
231
	indexes_t *indexes_p;
redmine authored
232
	void *data;
redmine authored
233
	int linescount;
redmine authored
234
	api_eventinfo_t *api_ei;
redmine authored
235
	int api_ei_count;
redmine authored
236
	char buf[BUFSIZ+1];
redmine authored
237 238
};

redmine authored
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
struct doubleentry {
	size_t  size0;
	size_t  size1;
	size_t  alloc0;
	size_t  alloc1;
	void   *dat0;
	void   *dat1;
};

struct pushdoubleentry_arg {
	int			 allocated;
	int			 total;
	size_t			 size;
	struct doubleentry	*entry;
};

struct entry {
	size_t  size;
	size_t  alloc;
	void   *dat;
};

struct pushentry_arg {
	int		 allocated;
	int		 total;
	size_t		 size;
	struct entry	*entry;
};

redmine authored
268 269
enum initsync {
	INITSYNC_UNKNOWN = 0,
redmine authored
270 271
	INITSYNC_FULL,
	INITSYNC_SUBDIR
redmine authored
272 273 274
};
typedef enum initsync initsync_t;

redmine authored
275
struct sighandler_arg {
redmine authored
276
	ctx_t     *ctx_p;
redmine authored
277 278 279 280 281 282 283
//	indexes_t *indexes_p;
	pthread_t  pthread_parent;
	int	  *exitcode_p;
	sigset_t  *sigset_p;
};
typedef struct sighandler_arg sighandler_arg_t;

redmine authored
284 285 286 287 288 289
enum unified_evetnmask {
	UEM_DIR		= 0x01,
	UEM_CREATED	= 0x02,
	UEM_DELETED	= 0x04,
};