Blame view

redmine authored
1
/*
redmine authored
2
    clsync - file tree sync utility based on inotify
redmine authored
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C

    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

redmine authored
21 22
#ifndef __CLSYNC_CTX_H
#define __CLSYNC_CTX_H
redmine authored
23

redmine authored
24
#include <regex.h>
redmine authored
25 26 27
#ifdef CAPABILITIES_SUPPORT
#	include <sys/capability.h>	// __u32
#endif
redmine authored
28

redmine authored
29 30 31 32 33 34 35 36
#define	MAX_BLOCKTHREADS	(1<<4)

#define register_blockthread(thread) {\
	critical_on (ctx_p->blockthread_count >= MAX_BLOCKTHREADS);\
	ctx_p->blockthread[ctx_p->blockthread_count++] = pthread_self();\
	debug(3, "register_blockthread(): ctx_p->blockthread_count -> %i", ctx_p->blockthread_count);\
}

redmine authored
37 38 39
#define OPTION_FLAGS		(1<<10)
#define OPTION_LONGOPTONLY	(1<<9)
#define OPTION_CONFIGONLY	(1<<8)
redmine authored
40
#define NOTOPTION		(3<<8)
redmine authored
41
enum flags_enum {
redmine authored
42 43 44 45
	WATCHDIR	= 'W',
	SYNCHANDLER	= 'S',
	RULESFILE	= 'R',
	DESTDIR		= 'D',
redmine authored
46
	SOCKETPATH	= 's',
redmine authored
47

redmine authored
48
	HELP		= 'h',
redmine authored
49
	CONFIGFILE	= 'H',
redmine authored
50 51 52 53
	CONFIGBLOCK	= 'K',
	BACKGROUND	= 'b',
	UID		= 'u',
	GID		= 'g',
redmine authored
54
	CAP_PRESERVE	= 'C',
redmine authored
55
	THREADING	= 'p',
redmine authored
56
	RETRIES		= 'r',
redmine authored
57
	OUTPUT_METHOD	= 'Y',
redmine authored
58
	EXCLUDEMOUNTPOINTS= 'X',
redmine authored
59 60
	PIDFILE		= 'z',
	CLUSTERIFACE	= 'c',
redmine authored
61 62 63
	CLUSTERMCASTIPADDR='m',
	CLUSTERMCASTIPPORT='P',
	CLUSTERTIMEOUT	= 'G',
redmine authored
64 65 66 67 68 69 70
	CLUSTERNODENAME = 'n',
	CLUSTERHDLMIN	= 'o',
	CLUSTERHDLMAX	= 'O',
	DELAY		= 't',
	BFILEDELAY	= 'T',
	SYNCDELAY	= 'w',
	BFILETHRESHOLD	= 'B',
redmine authored
71
	DEBUG		= 'd',
redmine authored
72 73
	QUIET		= 'q',
	VERBOSE		= 'v',
redmine authored
74
	OUTLISTSDIR	= 'L',
redmine authored
75
	AUTORULESW	= 'A',
redmine authored
76
	MODE		= 'M',
redmine authored
77 78 79 80 81 82 83
	IGNOREEXITCODE	= 'x',
	DONTUNLINK	= 'U',
	INITFULL	= 'F',
	SYNCTIMEOUT	= 'k',
	LABEL		= 'l',
	SHOW_VERSION	= 'V',

redmine authored
84 85 86 87 88 89 90 91 92 93 94
	HAVERECURSIVESYNC 	=  0|OPTION_LONGOPTONLY,
	RSYNCINCLIMIT		=  1|OPTION_LONGOPTONLY,
	RSYNCPREFERINCLUDE	=  2|OPTION_LONGOPTONLY,
	SYNCLISTSIMPLIFY	=  3|OPTION_LONGOPTONLY,
	ONEFILESYSTEM		=  4|OPTION_LONGOPTONLY,
	STATUSFILE		=  5|OPTION_LONGOPTONLY,
	SKIPINITSYNC		=  6|OPTION_LONGOPTONLY,
	ONLYINITSYNC		=  7|OPTION_LONGOPTONLY,
	EXITONNOEVENTS		=  8|OPTION_LONGOPTONLY,
	STANDBYFILE		=  9|OPTION_LONGOPTONLY,
	EXITHOOK		= 10|OPTION_LONGOPTONLY,
redmine authored
95
	CLUSTERSDLMAX		= 11|OPTION_LONGOPTONLY,
redmine authored
96 97 98 99 100 101 102 103 104 105 106
	PREEXITHOOK		= 12|OPTION_LONGOPTONLY,
	SOCKETAUTH		= 13|OPTION_LONGOPTONLY,
	SOCKETMOD		= 14|OPTION_LONGOPTONLY,
	SOCKETOWN		= 15|OPTION_LONGOPTONLY,
	MAXITERATIONS		= 16|OPTION_LONGOPTONLY,
	IGNOREFAILURES		= 17|OPTION_LONGOPTONLY,
	DUMPDIR			= 18|OPTION_LONGOPTONLY,
	CONFIGBLOCKINHERITS	= 19|OPTION_LONGOPTONLY,
	MONITOR			= 20|OPTION_LONGOPTONLY,
	SYNCHANDLERARGS0	= 21|OPTION_LONGOPTONLY,
	SYNCHANDLERARGS1	= 22|OPTION_LONGOPTONLY,
redmine authored
107
	CUSTOMSIGNALS		= 23|OPTION_LONGOPTONLY,
redmine authored
108 109
	CHROOT			= 24|OPTION_LONGOPTONLY,
	MOUNTPOINTS		= 25|OPTION_LONGOPTONLY,
redmine authored
110
	SPLITTING		= 26|OPTION_LONGOPTONLY,
redmine authored
111 112
	SYNCHANDLERUID		= 27|OPTION_LONGOPTONLY,
	SYNCHANDLERGID		= 28|OPTION_LONGOPTONLY,
redmine authored
113
	CAPS_INHERIT		= 29|OPTION_LONGOPTONLY,
redmine authored
114
	CHECK_EXECVP_ARGS	= 30|OPTION_LONGOPTONLY,
redmine authored
115 116 117
	PIVOT_ROOT		= 31|OPTION_LONGOPTONLY,
	DETACH_NETWORK		= 32|OPTION_LONGOPTONLY,
	DETACH_MISCELLANEA	= 33|OPTION_LONGOPTONLY,
redmine authored
118
	ADDPERMITTEDHOOKFILES	= 34|OPTION_LONGOPTONLY,
redmine authored
119
	SECCOMP_FILTER		= 35|OPTION_LONGOPTONLY,
redmine authored
120 121
	FORGET_PRIVTHREAD_INFO	= 36|OPTION_LONGOPTONLY,
	SECURESPLITTING		= 37|OPTION_LONGOPTONLY,
redmine authored
122
	FTS_EXPERIMENTAL_OPTIMIZATION = 38|OPTION_LONGOPTONLY,
redmine authored
123
	FORBIDDEVICES		= 39|OPTION_LONGOPTONLY,
redmine authored
124
	CG_GROUPNAME		= 40|OPTION_LONGOPTONLY,
redmine authored
125
	PERMIT_MPROTECT		= 41|OPTION_LONGOPTONLY,
redmine authored
126
	SHM_MPROTECT		= 42|OPTION_LONGOPTONLY,
redmine authored
127
	MODSIGN			= 43|OPTION_LONGOPTONLY,
redmine authored
128
	CANCEL_SYSCALLS		= 44|OPTION_LONGOPTONLY,
redmine authored
129
	EXITONSYNCSKIP		= 45|OPTION_LONGOPTONLY,
redmine authored
130
	DETACH_IPC		= 46|OPTION_LONGOPTONLY,
redmine authored
131 132
	PRIVILEGEDUID		= 47|OPTION_LONGOPTONLY,
	PRIVILEGEDGID		= 48|OPTION_LONGOPTONLY,
redmine authored
133 134 135
};
typedef enum flags_enum flags_t;

redmine authored
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
enum detachnetwork_way {
	DN_OFF = 0,
	DN_NONPRIVILEGED,
	DN_EVERYWHERE,
};
typedef enum detachnetwork_way detachnetwork_way_t;

enum pivotroot_way {
	PW_OFF    = 0,
	PW_DIRECT,
	PW_AUTO,
	PW_AUTORO,
};
typedef enum pivotroot_way pivotroot_way_t;

redmine authored
151 152 153 154 155 156 157 158
enum capsinherit {
	CI_DONTTOUCH = 0,
	CI_PERMITTED,
	CI_CLSYNC,
	CI_EMPTY,
};
typedef enum capsinherit capsinherit_t;

redmine authored
159 160 161
enum mode_id {
	MODE_UNSET	= 0,
	MODE_SIMPLE,
redmine authored
162
	MODE_DIRECT,
redmine authored
163 164 165
	MODE_SHELL,
	MODE_RSYNCSHELL,
	MODE_RSYNCDIRECT,
redmine authored
166
	MODE_RSYNCSO,
redmine authored
167 168 169 170 171
	MODE_SO,
};
typedef enum mode_id mode_id_t;

enum queue_id {
redmine authored
172 173 174
	QUEUE_NORMAL,
	QUEUE_BIGFILE,
	QUEUE_INSTANT,
redmine authored
175 176
	QUEUE_LOCKWAIT,

redmine authored
177 178 179
	QUEUE_MAX,
	QUEUE_AUTO
};
redmine authored
180
typedef enum queue_id queue_id_t;
redmine authored
181 182 183 184 185 186 187 188

enum ruleactionsign_enum {
	RS_REJECT	= 0,
	RS_PERMIT	= 1
};
typedef enum ruleactionsign_enum ruleactionsign_t;

enum ruleaction_enum {
redmine authored
189 190 191 192
	RA_NONE			 = 0x00,
	RA_MONITOR		 = 0x01,
	RA_WALK			 = 0x02,
	RA_ALL			 = 0x0f,
redmine authored
193 194 195 196 197
};
typedef enum ruleaction_enum ruleaction_t;

// signals (man 7 signal)
enum sigusr_enum {
redmine authored
198
	SIGUSR_THREAD_GC	= 10,
redmine authored
199
	SIGUSR_INITSYNC  	= 12,
redmine authored
200 201
	SIGUSR_BLOPINT		= 16,
	SIGUSR_DUMP		= 29,
redmine authored
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
};

struct rule {
	int		num;
	regex_t		expr;
	mode_t		objtype;
	ruleaction_t	perm;
	ruleaction_t	mask;
};
typedef struct rule rule_t;

struct queueinfo {
	unsigned int 	collectdelay;
	time_t		stime;
};
typedef struct queueinfo queueinfo_t;

struct api_functs {
	api_funct_init   init;
	api_funct_sync   sync;
redmine authored
222
	api_funct_rsync  rsync;
redmine authored
223 224 225 226
	api_funct_deinit deinit;
};
typedef struct api_functs api_functs_t;

redmine authored
227
struct notifyenginefuncts {
redmine authored
228
	int (*wait)(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p);
redmine authored
229 230
	int (*handle)(struct ctx *ctx_p, struct indexes *indexes_p);
	int (*add_watch_dir)(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath);
redmine authored
231 232
};

redmine authored
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
enum shflags {
	SHFL_NONE		= 0x00,
	SHFL_RSYNC_ARGS		= 0x01,
	SHFL_INCLUDE_LIST	= 0x02,
	SHFL_INCLUDE_LIST_PATH	= 0x04,
	SHFL_EXCLUDE_LIST_PATH	= 0x08,
};
typedef enum shflags shflags_t;

enum shargsid {
	SHARGS_PRIMARY = 0,
	SHARGS_INITIAL,
	SHARGS_MAX,
};

struct synchandler_args {
	char	*v[MAXARGUMENTS];
	int	 c;
	char	 isexpanded[MAXARGUMENTS];
};
typedef struct synchandler_args synchandler_args_t;

redmine authored
255 256 257 258 259
#define STATE_STARTING(state_p) (state_p == NULL)
enum state_enum {
	STATE_EXIT 	= 0,
	STATE_STARTING,
	STATE_RUNNING,
redmine authored
260
	STATE_SYNCHANDLER_ERR,
redmine authored
261 262 263 264 265
	STATE_REHASH,
	STATE_PREEXIT,
	STATE_TERM,
	STATE_THREAD_GC,
	STATE_INITSYNC,
redmine authored
266
	STATE_HOLDON,
redmine authored
267 268 269 270
	STATE_UNKNOWN
};
typedef enum state_enum state_t;

redmine authored
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
enum stat_fields {
	STAT_FIELD_RESET		= 0x0000,
	STAT_FIELD_DEV			= 0x0001,
	STAT_FIELD_INO			= 0x0002,
	STAT_FIELD_MODE			= 0x0004,
	STAT_FIELD_NLINK		= 0x0008,
	STAT_FIELD_UID			= 0x0010,
	STAT_FIELD_GID			= 0x0020,
	STAT_FIELD_RDEV			= 0x0040,
	STAT_FIELD_SIZE			= 0x0080,
	STAT_FIELD_BLKSIZE		= 0x0100,
	STAT_FIELD_BLOCKS		= 0x0200,
	STAT_FIELD_ATIME		= 0x0400,
	STAT_FIELD_MTIME		= 0x0800,
	STAT_FIELD_CTIME		= 0x1000,

	STAT_FIELD_ALL			= 0x1ff7,
};

enum syscall_bitmask {
	CSC_RESET	= 0x00,
	CSC_MON_STAT	= 0x01,
};

redmine authored
295 296
#define CAP_PRESERVE_TRY (1<<16)

redmine authored
297
struct ctx {
redmine authored
298
#ifndef LIBCLSYNC
redmine authored
299
	volatile state_t state;
redmine authored
300 301 302
	pid_t  pid;
	char   pid_str[65];
	size_t pid_str_len;
redmine authored
303 304
	uid_t uid;
	gid_t gid;
redmine authored
305 306
	uid_t privileged_uid;
	gid_t privileged_gid;
redmine authored
307 308
	uid_t synchandler_uid;
	gid_t synchandler_gid;
redmine authored
309
#ifdef CAPABILITIES_SUPPORT
redmine authored
310 311
	__u32 caps;
#endif
redmine authored
312 313
	pid_t child_pid[MAXCHILDREN];	// Used only for non-pthread mode
	int   children;			// Used only for non-pthread mode
Artyom A Anikeev authored
314
	uint32_t iteration_num;
redmine authored
315
	rule_t rules[MAXRULES];
redmine authored
316
	size_t rules_count;
redmine authored
317
	dev_t st_dev;
redmine authored
318
#endif
redmine authored
319
	char *flags_values_raw[OPTION_FLAGS];
redmine authored
320 321
	int flags[OPTION_FLAGS];
	int flags_set[OPTION_FLAGS];
redmine authored
322
#ifndef LIBCLSYNC
redmine authored
323
	char *config_path;
redmine authored
324
	const char *config_block;
redmine authored
325
	char *customsignal[MAXSIGNALNUM+1];
redmine authored
326 327 328
	char *label;
	char *watchdir;
	char *pidfile;
redmine authored
329
	char *standbyfile;
redmine authored
330
	char *exithookfile;
redmine authored
331
	char *preexithookfile;
redmine authored
332
	char *destdir;
redmine authored
333
	char *destproto;
redmine authored
334 335
	char *watchdirwslash;
	char *destdirwslash;
redmine authored
336
	char *statusfile;
redmine authored
337
	char *socketpath;
redmine authored
338
	char *dump_path;
redmine authored
339 340 341
#ifdef CGROUP_SUPPORT
	char *cg_groupname;
#endif
redmine authored
342
	int socket;
redmine authored
343 344 345
	mode_t socketmod;
	uid_t  socketuid;
	gid_t  socketgid;
redmine authored
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
#ifdef CLUSTER_SUPPORT
	char *cluster_iface;
	char *cluster_mcastipaddr;
	char *cluster_nodename;
	uint32_t cluster_nodename_len;
	uint16_t cluster_mcastipport;
	uint16_t cluster_hash_dl_min;
	uint16_t cluster_hash_dl_max;
	uint16_t cluster_scan_dl_max;
	unsigned int cluster_timeout;
#endif
	size_t watchdirlen;
	size_t destdirlen;
	size_t watchdirsize;
	size_t destdirsize;
	size_t watchdirwslashsize;
	size_t destdirwslashsize;
	short int watchdir_dirlevel;
	char *handlerfpath;
	void *handler_handle;
	api_functs_t handler_funct;
redmine authored
367 368
	char  *rulfpath;
	size_t rulfpathsize;
redmine authored
369
	char *listoutdir;
redmine authored
370
	struct notifyenginefuncts notifyenginefunct;
redmine authored
371
	int retries;
redmine authored
372 373 374 375 376 377 378 379
	size_t bfilethreshold;
	unsigned int syncdelay;
	queueinfo_t _queues[QUEUE_MAX];	// TODO: remove this from here
	unsigned int rsyncinclimit;
	time_t synctime;
	unsigned int synctimeout;
	sigset_t *sigset;
	char isignoredexitcode[(1<<8)];
redmine authored
380 381
	pthread_t blockthread[MAX_BLOCKTHREADS];
	size_t    blockthread_count;
redmine authored
382

redmine authored
383 384
	char *chroot_dir;

redmine authored
385 386 387 388 389
#ifdef CAPABILITIES_SUPPORT
	char *permitted_hookfile[MAXPERMITTEDHOOKFILES+1];
	int   permitted_hookfiles;
#endif

redmine authored
390 391
#ifdef UNSHARE_SUPPORT
# ifdef GETMNTENT_SUPPORT
redmine authored
392
	char *mountpoint[MAXMOUNTPOINTS+1];
redmine authored
393
	int   mountpoints;
redmine authored
394
# endif
redmine authored
395 396
#endif

redmine authored
397 398
	synchandler_args_t synchandler_args[SHARGS_MAX];
	shflags_t synchandler_argf;
redmine authored
399 400 401
#endif // ifndef LIBCLSYNC
	void *indexes_p;
	void *fsmondata;
redmine authored
402
};
redmine authored
403
typedef struct ctx ctx_t;
redmine authored
404

redmine authored
405 406
#endif