1 /*
2     clsync - file tree sync utility based on inotify
3 
4     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
5 
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef __CLSYNC_CTX_H
22 #define __CLSYNC_CTX_H
23 
24 #include <regex.h>
25 
26 #define OPTION_FLAGS		(1<<10)
27 #define OPTION_LONGOPTONLY	(1<<9)
28 #define OPTION_CONFIGONLY	(1<<8)
29 #define NOTOPTION		(3<<8)
30 enum flags_enum {
31 	WATCHDIR	= 'W',
32 	SYNCHANDLER	= 'S',
33 	RULESFILE	= 'R',
34 	DESTDIR		= 'D',
35 	SOCKETPATH	= 's',
36 
37 	HELP		= 'h',
38 	CONFIGFILE	= 'H',
39 	CONFIGBLOCK	= 'K',
40 	BACKGROUND	= 'b',
41 	UID		= 'u',
42 	GID		= 'g',
43 	CAP_PRESERVE_FILEACCESS = 'C',
44 	THREADING	= 'p',
45 	RETRIES		= 'r',
46 	OUTPUT_METHOD	= 'Y',
47 	EXCLUDEMOUNTPOINTS= 'X',
48 	PIDFILE		= 'z',
49 #ifdef CLUSTER_SUPPORT
50 	CLUSTERIFACE	= 'c',
51 	CLUSTERMCASTIPADDR='m',
52 	CLUSTERMCASTIPPORT='P',
53 	CLUSTERTIMEOUT	= 'G',
54 	CLUSTERNODENAME = 'n',
55 	CLUSTERHDLMIN	= 'o',
56 	CLUSTERHDLMAX	= 'O',
57 	CLUSTERSDLMAX	= 11|OPTION_LONGOPTONLY,
58 #endif
59 	DELAY		= 't',
60 	BFILEDELAY	= 'T',
61 	SYNCDELAY	= 'w',
62 	BFILETHRESHOLD	= 'B',
63 	DEBUG		= 'd',
64 	QUIET		= 'q',
65 	VERBOSE		= 'v',
66 	OUTLISTSDIR	= 'L',
67 	AUTORULESW	= 'A',
68 	MODE		= 'M',
69 	IGNOREEXITCODE	= 'x',
70 	DONTUNLINK	= 'U',
71 	INITFULL	= 'F',
72 	SYNCTIMEOUT	= 'k',
73 	LABEL		= 'l',
74 	SHOW_VERSION	= 'V',
75 
76 	HAVERECURSIVESYNC 	=  0|OPTION_LONGOPTONLY,
77 	RSYNCINCLIMIT		=  1|OPTION_LONGOPTONLY,
78 	RSYNCPREFERINCLUDE	=  2|OPTION_LONGOPTONLY,
79 	SYNCLISTSIMPLIFY	=  3|OPTION_LONGOPTONLY,
80 	ONEFILESYSTEM		=  4|OPTION_LONGOPTONLY,
81 	STATUSFILE		=  5|OPTION_LONGOPTONLY,
82 	SKIPINITSYNC		=  6|OPTION_LONGOPTONLY,
83 	ONLYINITSYNC		=  7|OPTION_LONGOPTONLY,
84 	EXITONNOEVENTS		=  8|OPTION_LONGOPTONLY,
85 	STANDBYFILE		=  9|OPTION_LONGOPTONLY,
86 	EXITHOOK		= 10|OPTION_LONGOPTONLY,
87 
88 	SOCKETAUTH		= 12|OPTION_LONGOPTONLY,
89 	SOCKETMOD		= 13|OPTION_LONGOPTONLY,
90 	SOCKETOWN		= 14|OPTION_LONGOPTONLY,
91 
92 	MAXITERATIONS		= 15|OPTION_LONGOPTONLY,
93 
94 	IGNOREFAILURES		= 16|OPTION_LONGOPTONLY,
95 
96 	DUMPDIR			= 17|OPTION_LONGOPTONLY,
97 
98 	CONFIGBLOCKINHERITS	= 18|OPTION_LONGOPTONLY,
99 
100 	MONITOR			= 19|OPTION_LONGOPTONLY,
101 };
102 typedef enum flags_enum flags_t;
103 
104 enum mode_id {
105 	MODE_UNSET	= 0,
106 	MODE_SIMPLE,
107 	MODE_SHELL,
108 	MODE_RSYNCSHELL,
109 	MODE_RSYNCDIRECT,
110 	MODE_RSYNCSO,
111 	MODE_SO,
112 };
113 typedef enum mode_id mode_id_t;
114 
115 enum queue_id {
116 	QUEUE_NORMAL,
117 	QUEUE_BIGFILE,
118 	QUEUE_INSTANT,
119 	QUEUE_LOCKWAIT,
120 
121 	QUEUE_MAX,
122 	QUEUE_AUTO
123 };
124 typedef enum queue_id queue_id_t;
125 
126 enum ruleactionsign_enum {
127 	RS_REJECT	= 0,
128 	RS_PERMIT	= 1
129 };
130 typedef enum ruleactionsign_enum ruleactionsign_t;
131 
132 enum ruleaction_enum {
133 	RA_NONE		 = 0x00,
134 	RA_MONITOR	 = 0x01,
135 	RA_WALK		 = 0x02,
136 	RA_ALL		 = 0xff
137 };
138 typedef enum ruleaction_enum ruleaction_t;
139 
140 // signals (man 7 signal)
141 enum sigusr_enum {
142 	SIGUSR_THREAD_GC	= 10,
143 	SIGUSR_INITSYNC  	= 12,
144 	SIGUSR_BLOPINT		= 16,
145 	SIGUSR_DUMP		= 29,
146 };
147 
148 struct rule {
149 	int		num;
150 	regex_t		expr;
151 	mode_t		objtype;
152 	ruleaction_t	perm;
153 	ruleaction_t	mask;
154 };
155 typedef struct rule rule_t;
156 
157 struct queueinfo {
158 	unsigned int 	collectdelay;
159 	time_t		stime;
160 };
161 typedef struct queueinfo queueinfo_t;
162 
163 struct api_functs {
164 	api_funct_init   init;
165 	api_funct_sync   sync;
166 	api_funct_rsync  rsync;
167 	api_funct_deinit deinit;
168 };
169 typedef struct api_functs api_functs_t;
170 
171 struct notifyenginefuncts {
172 	int (*wait)(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p);
173 	int (*handle)(struct ctx *ctx_p, struct indexes *indexes_p);
174 	int (*add_watch_dir)(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath);
175 };
176 
177 struct ctx {
178 #ifndef LIBCLSYNC
179 	uid_t uid;
180 	gid_t gid;
181 	pid_t child_pid[MAXCHILDREN];	// Used only for non-pthread mode
182 	int   children;			// Used only for non-pthread mode
183 	uint32_t iteration_num;
184 	rule_t rules[MAXRULES];
185 	dev_t st_dev;
186 #endif
187 	char *flags_values_raw[OPTION_FLAGS];
188 	int flags[OPTION_FLAGS];
189 	int flags_set[OPTION_FLAGS];
190 #ifndef LIBCLSYNC
191 	char *config_path;
192 	char *config_block;
193 	char *label;
194 	char *watchdir;
195 	char *pidfile;
196 	char *standbyfile;
197 	char *exithookfile;
198 	char *destdir;
199 	char *destproto;
200 	char *watchdirwslash;
201 	char *destdirwslash;
202 	char *statusfile;
203 	char *socketpath;
204 	char *dump_path;
205 	int socket;
206 	mode_t socketmod;
207 	uid_t  socketuid;
208 	gid_t  socketgid;
209 #ifdef CLUSTER_SUPPORT
210 	char *cluster_iface;
211 	char *cluster_mcastipaddr;
212 	char *cluster_nodename;
213 	uint32_t cluster_nodename_len;
214 	uint16_t cluster_mcastipport;
215 	uint16_t cluster_hash_dl_min;
216 	uint16_t cluster_hash_dl_max;
217 	uint16_t cluster_scan_dl_max;
218 	unsigned int cluster_timeout;
219 #endif
220 	size_t watchdirlen;
221 	size_t destdirlen;
222 	size_t watchdirsize;
223 	size_t destdirsize;
224 	size_t watchdirwslashsize;
225 	size_t destdirwslashsize;
226 	short int watchdir_dirlevel;
227 	char *handlerfpath;
228 	void *handler_handle;
229 	api_functs_t handler_funct;
230 	char *rulfpath;
231 	char *listoutdir;
232 	struct notifyenginefuncts notifyenginefunct;
233 	int retries;
234 	size_t bfilethreshold;
235 	unsigned int syncdelay;
236 	queueinfo_t _queues[QUEUE_MAX];	// TODO: remove this from here
237 	unsigned int rsyncinclimit;
238 	time_t synctime;
239 	unsigned int synctimeout;
240 	sigset_t *sigset;
241 	char isignoredexitcode[(1<<8)];
242 #endif
243 	void *indexes_p;
244 	void *fsmondata;
245 };
246 typedef struct ctx ctx_t;
247 
248 #endif
249 
250