-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.c
More file actions
3528 lines (2850 loc) · 89 KB
/
main.c
File metadata and controls
3528 lines (2850 loc) · 89 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
clsync - file tree sync utility based on inotify/kqueue/bsm
Copyright (C) 2014 Dmitry Yu Okunev <[email protected]> 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/>.
*/
#include "common.h"
#ifdef CAPABILITIES_SUPPORT
# include <sys/capability.h> // for capset()/capget() for --preserve-file-access
#endif
#if defined(__linux__) | defined(CAPABILITIES_SUPPORT)
# include <sys/prctl.h> // for prctl() for --preserve-fil-access
#endif
#include <pwd.h> // getpwnam()
#include <grp.h> // getgrnam()
#include <glib.h> // gkf
#ifdef UNSHARE_SUPPORT
# include <sched.h> // unshare()
#endif
#ifdef GETMNTENT_SUPPORT
# include <mntent.h> // getmntent()
# include <sys/mount.h> // umount2()
#endif
#include "error.h"
#include "stringex.h"
#include "sync.h"
#include "malloc.h"
#include "cluster.h"
#include "fileutils.h"
#include "socket.h"
#include "syscalls.h"
#include "rules.h"
#if CGROUP_SUPPORT
# include "cgroup.h"
#endif
#include "posix-hacks.h"
//#include "revision.h"
static const struct option long_options[] = {
{"watch-dir", required_argument, NULL, WATCHDIR},
{"sync-handler", required_argument, NULL, SYNCHANDLER},
{"--", required_argument, NULL, SYNCHANDLERARGS0},
{"---", required_argument, NULL, SYNCHANDLERARGS1},
{"rules-file", required_argument, NULL, RULESFILE},
{"destination-dir", required_argument, NULL, DESTDIR},
{"mode", required_argument, NULL, MODE},
{"socket", required_argument, NULL, SOCKETPATH},
{"socket-auth", required_argument, NULL, SOCKETAUTH},
{"socket-mod", required_argument, NULL, SOCKETMOD},
{"socket-own", required_argument, NULL, SOCKETOWN},
{"status-file", required_argument, NULL, STATUSFILE},
{"background", optional_argument, NULL, BACKGROUND},
{"config-file", required_argument, NULL, CONFIGFILE},
{"config-block", required_argument, NULL, CONFIGBLOCK},
{"config-block-inherits", required_argument, NULL, CONFIGBLOCKINHERITS},
{"custom-signals", required_argument, NULL, CUSTOMSIGNALS},
{"pid-file", required_argument, NULL, PIDFILE},
{"uid", required_argument, NULL, UID},
{"gid", required_argument, NULL, GID},
{"privileged-uid", required_argument, NULL, PRIVILEGEDUID},
{"privileged-gid", required_argument, NULL, PRIVILEGEDGID},
{"sync-handler-uid", required_argument, NULL, SYNCHANDLERUID},
{"sync-handler-gid", required_argument, NULL, SYNCHANDLERGID},
{"chroot", required_argument, NULL, CHROOT},
#ifdef PIVOTROOT_OPT_SUPPORT
{"pivot-root", required_argument, NULL, PIVOT_ROOT},
#endif
#ifdef UNSHARE_SUPPORT
{"detach-network", required_argument, NULL, DETACH_NETWORK},
{"detach-ipc", required_argument, NULL, DETACH_IPC},
{"detach-miscellanea", optional_argument, NULL, DETACH_MISCELLANEA},
#endif
#ifdef CAPABILITIES_SUPPORT
# ifdef SECCOMP_SUPPORT
{"secure-splitting", no_argument, NULL, SECURESPLITTING},
# endif
{"splitting", required_argument, NULL, SPLITTING},
{"check-execvp-arguments", optional_argument, NULL, CHECK_EXECVP_ARGS},
{"add-permitted-hook-files", required_argument, NULL, ADDPERMITTEDHOOKFILES},
# ifdef SECCOMP_SUPPORT
{"seccomp-filter", optional_argument, NULL, SECCOMP_FILTER},
# endif
{"forget-privthread-info", optional_argument, NULL, FORGET_PRIVTHREAD_INFO},
{"permit-mprotect", optional_argument, NULL, PERMIT_MPROTECT},
{"shm-mprotect", optional_argument, NULL, SHM_MPROTECT},
#endif
#ifdef UNSHARE_SUPPORT
# ifdef GETMNTENT_SUPPORT
{"mountpoints", required_argument, NULL, MOUNTPOINTS},
# endif
#endif
#ifdef CAPABILITIES_SUPPORT
{"preserve-capabilities", required_argument, NULL, CAP_PRESERVE},
{"inherit-capabilities", optional_argument, NULL, CAPS_INHERIT},
#endif
#ifdef CGROUP_SUPPORT
{"forbid-devices", optional_argument, NULL, FORBIDDEVICES},
{"cgroup-group-name", required_argument, NULL, CG_GROUPNAME},
#endif
#ifdef THREADING_SUPPORT
{"threading", required_argument, NULL, THREADING},
#endif
{"retries", required_argument, NULL, RETRIES},
{"ignore-failures", optional_argument, NULL, IGNOREFAILURES},
{"exit-on-sync-skipping", optional_argument, NULL, EXITONSYNCSKIP},
{"output", required_argument, NULL, OUTPUT_METHOD},
{"one-file-system", optional_argument, NULL, ONEFILESYSTEM},
{"exclude-mount-points", optional_argument, NULL, EXCLUDEMOUNTPOINTS},
#ifdef CLUSTER_SUPPORT
{"cluster-iface", required_argument, NULL, CLUSTERIFACE}, // Not implemented, yet
{"cluster-ip", required_argument, NULL, CLUSTERMCASTIPADDR}, // Not implemented, yet
{"cluster-port", required_argument, NULL, CLUSTERMCASTIPPORT}, // Not implemented, yet
{"cluster-timeout", required_argument, NULL, CLUSTERTIMEOUT}, // Not implemented, yet
{"cluster-node-name", required_argument, NULL, CLUSTERNODENAME}, // Not implemented, yet
{"cluster-hash-dl-min", required_argument, NULL, CLUSTERHDLMIN},
{"cluster-hash-dl-max", required_argument, NULL, CLUSTERHDLMAX},
{"cluster-scan-dl-max", required_argument, NULL, CLUSTERSDLMAX},
#endif
{"max-iterations", required_argument, NULL, MAXITERATIONS},
{"standby-file", required_argument, NULL, STANDBYFILE},
{"modification-signature", required_argument, NULL, MODSIGN},
{"timeout-sync", required_argument, NULL, SYNCTIMEOUT},
{"delay-sync", required_argument, NULL, SYNCDELAY},
{"delay-collect", required_argument, NULL, DELAY},
{"delay-collect-bigfile", required_argument, NULL, BFILEDELAY},
{"threshold-bigfile", required_argument, NULL, BFILETHRESHOLD},
{"cancel-syscalls", required_argument, NULL, CANCEL_SYSCALLS},
{"lists-dir", required_argument, NULL, OUTLISTSDIR},
{"have-recursive-sync", optional_argument, NULL, HAVERECURSIVESYNC},
{"synclist-simplify", optional_argument, NULL, SYNCLISTSIMPLIFY},
#ifdef AUTORULESW
{"auto-add-rules-w", optional_argument, NULL, AUTORULESW},
#endif
{"rsync-inclimit", required_argument, NULL, RSYNCINCLIMIT},
{"rsync-prefer-include", optional_argument, NULL, RSYNCPREFERINCLUDE},
{"ignore-exitcode", required_argument, NULL, IGNOREEXITCODE},
{"dont-unlink-lists", optional_argument, NULL, DONTUNLINK},
{"fts-experimental-optimization", optional_argument, NULL, FTS_EXPERIMENTAL_OPTIMIZATION},
{"full-initialsync", optional_argument, NULL, INITFULL},
{"only-initialsync", optional_argument, NULL, ONLYINITSYNC},
{"skip-initialsync", optional_argument, NULL, SKIPINITSYNC},
{"exit-on-no-events", optional_argument, NULL, EXITONNOEVENTS},
{"exit-hook", required_argument, NULL, EXITHOOK},
{"pre-exit-hook", required_argument, NULL, PREEXITHOOK},
{"sync-on-quit", optional_argument, NULL, SOFTEXITSYNC},
{"verbose", optional_argument, NULL, VERBOSE},
{"debug", optional_argument, NULL, DEBUG},
{"dump-dir", required_argument, NULL, DUMPDIR},
{"quiet", optional_argument, NULL, QUIET},
{"monitor", required_argument, NULL, MONITOR},
{"label", required_argument, NULL, LABEL},
{"help", optional_argument, NULL, HELP},
{"version", optional_argument, NULL, SHOW_VERSION},
{NULL, 0, NULL, 0}
};
#ifdef UNSHARE_SUPPORT
static char *const detachnetworkways[] = {
[DN_OFF] = "off",
[DN_NONPRIVILEGED] = "non-privileged",
[DN_EVERYWHERE] = "everywhere",
NULL,
};
#endif
#ifdef PIVOTROOT_OPT_SUPPORT
static char *const pivotrootways[] = {
[PW_OFF] = "off",
[PW_DIRECT] = "direct",
[PW_AUTO] = "auto",
[PW_AUTORO] = "auto-ro",
NULL,
};
#endif
enum xstatfield {
X_STAT_FIELD_RESET = 0,
X_STAT_FIELD_DEV,
X_STAT_FIELD_INO,
X_STAT_FIELD_MODE,
X_STAT_FIELD_NLINK,
X_STAT_FIELD_UID,
X_STAT_FIELD_GID,
X_STAT_FIELD_RDEV,
X_STAT_FIELD_SIZE,
X_STAT_FIELD_BLKSIZE,
X_STAT_FIELD_BLOCKS,
X_STAT_FIELD_ATIME,
X_STAT_FIELD_MTIME,
X_STAT_FIELD_CTIME,
X_STAT_FIELD_ALL,
};
uint32_t xstatfield_to_statfield[] = {
[X_STAT_FIELD_RESET] = STAT_FIELD_RESET,
[X_STAT_FIELD_DEV] = STAT_FIELD_DEV,
[X_STAT_FIELD_INO] = STAT_FIELD_INO,
[X_STAT_FIELD_MODE] = STAT_FIELD_MODE,
[X_STAT_FIELD_NLINK] = STAT_FIELD_NLINK,
[X_STAT_FIELD_UID] = STAT_FIELD_UID,
[X_STAT_FIELD_GID] = STAT_FIELD_GID,
[X_STAT_FIELD_RDEV] = STAT_FIELD_RDEV,
[X_STAT_FIELD_SIZE] = STAT_FIELD_SIZE,
[X_STAT_FIELD_BLKSIZE] = STAT_FIELD_BLKSIZE,
[X_STAT_FIELD_BLOCKS] = STAT_FIELD_BLOCKS,
[X_STAT_FIELD_ATIME] = STAT_FIELD_ATIME,
[X_STAT_FIELD_MTIME] = STAT_FIELD_MTIME,
[X_STAT_FIELD_CTIME] = STAT_FIELD_CTIME,
[X_STAT_FIELD_ALL] = STAT_FIELD_ALL,
};
static char *const stat_fields[] = {
[X_STAT_FIELD_RESET] = "",
[X_STAT_FIELD_DEV] = "dev",
[X_STAT_FIELD_INO] = "ino",
[X_STAT_FIELD_MODE] = "mode",
[X_STAT_FIELD_NLINK] = "nlink",
[X_STAT_FIELD_UID] = "uid",
[X_STAT_FIELD_GID] = "gid",
[X_STAT_FIELD_RDEV] = "rdev",
[X_STAT_FIELD_SIZE] = "size",
[X_STAT_FIELD_BLKSIZE] = "blksize",
[X_STAT_FIELD_BLOCKS] = "blocks",
[X_STAT_FIELD_ATIME] = "atime",
[X_STAT_FIELD_MTIME] = "mtime",
[X_STAT_FIELD_CTIME] = "ctime",
[X_STAT_FIELD_ALL] = "*",
NULL
};
enum x_csc_bm {
X_CSC_RESET = 0,
X_CSC_MON_STAT,
};
uint32_t xcsc_to_csc[] = {
[X_CSC_RESET] = CSC_RESET,
[X_CSC_MON_STAT] = CSC_MON_STAT,
};
static char *const syscalls_bitmask[] = {
[X_CSC_RESET] = "",
[X_CSC_MON_STAT] = "mon_stat", // disable {l,}stat{,64}()-s in mon_*.c
NULL
};
#ifdef CAPABILITIES_SUPPORT
enum x_capabilities {
X_CAP_RESET = 0,
X_CAP_DAC_READ_SEARCH,
X_CAP_SETUID,
X_CAP_SETGID,
X_CAP_KILL,
X_CAP_MAX
};
__u32 xcap_to_cap[] = {
[X_CAP_DAC_READ_SEARCH] = CAP_DAC_READ_SEARCH,
[X_CAP_SETUID] = CAP_SETUID,
[X_CAP_SETGID] = CAP_SETGID,
[X_CAP_KILL] = CAP_KILL,
};
static char *const capabilities[] = {
[X_CAP_RESET] = "",
[X_CAP_DAC_READ_SEARCH] = "CAP_DAC_READ_SEARCH",
[X_CAP_SETUID] = "CAP_SETUID",
[X_CAP_SETGID] = "CAP_SETGID",
[X_CAP_KILL] = "CAP_KILL",
NULL
};
#define XCAP_TO_CAP(x) (xcap_to_cap[x])
static char *const capsinherits[] = {
[CI_PERMITTED] = "permittied",
[CI_DONTTOUCH] = "dont-touch",
[CI_CLSYNC] = "clsync",
[CI_EMPTY] = "empty",
};
#endif
static char *const socketauth[] = {
[SOCKAUTH_UNSET] = "",
[SOCKAUTH_NULL] = "null",
// [SOCKAUTH_PAM] = "pam",
NULL
};
#ifdef THREADING_SUPPORT
static char *const threading_modes[] = {
[PM_OFF] = "off",
[PM_SAFE] = "safe",
[PM_FULL] = "full",
NULL
};
#endif
#ifdef CAPABILITIES_SUPPORT
static char *const splitting_modes[] = {
[SM_OFF] = "off",
[SM_THREAD] = "thread",
[SM_PROCESS] = "process",
NULL
};
#endif
static char *const notify_engines[] = {
[NE_UNDEFINED] = "",
[NE_INOTIFY] = "inotify",
[NE_KQUEUE] = "kqueue",
[NE_FANOTIFY] = "fanotify",
[NE_BSM] = "bsm",
[NE_BSM_PREFETCH] = "bsm_prefetch",
[NE_DTRACEPIPE] = "dtracepipe",
[NE_GIO] = "gio",
NULL
};
static char *const output_methods[] = {
[OM_STDERR] = "stderr",
[OM_STDOUT] = "stdout",
[OM_SYSLOG] = "syslog",
NULL
};
static char *const modes[] = {
[MODE_UNSET] = "",
[MODE_SIMPLE] = "simple",
[MODE_DIRECT] = "direct",
[MODE_SHELL] = "shell",
[MODE_RSYNCSHELL] = "rsyncshell",
[MODE_RSYNCDIRECT] = "rsyncdirect",
[MODE_RSYNCSO] = "rsyncso",
[MODE_SO] = "so",
NULL
};
int syntax()
{
info ( "possible options:" );
int i = -1;
while ( long_options[++i].name != NULL ) {
switch ( long_options[i].val ) {
case SYNCHANDLERARGS0:
case SYNCHANDLERARGS1:
continue;
}
if ( long_options[i].val & OPTION_CONFIGONLY )
continue;
info ( "\t--%-24s%c%c%s", long_options[i].name,
long_options[i].val & OPTION_LONGOPTONLY ? ' ' : '-',
long_options[i].val & OPTION_LONGOPTONLY ? ' ' : long_options[i].val,
( long_options[i].has_arg == required_argument ? " argument" : "" ) );
}
exit ( EINVAL );
}
int ncpus;
pid_t parent_pid = 0;
pid_t waitpid_timed ( pid_t child_pid, int *status_p, long sec, long nsec )
{
struct timespec ts;
int status;
ts.tv_sec = sec;
ts.tv_nsec = nsec;
while ( ts.tv_sec >= 0 ) {
if ( waitpid ( child_pid, &status, WNOHANG ) < 0 ) {
if ( errno == ECHILD )
return child_pid;
return -1;
} else if ( status_p != NULL )
*status_p = status;
ts.tv_nsec -= WAITPID_TIMED_GRANULARITY;
if ( ts.tv_nsec < 0 ) {
ts.tv_nsec += 1000 * 1000 * 1000;
ts.tv_sec--;
}
}
return 0;
}
int parent_isalive()
{
int rc;
debug ( 12, "parent_pid == %u", parent_pid );
if ( ( rc = kill ( parent_pid, 0 ) ) ) {
if ( errno == ESRCH ) {
debug ( 1, "kill(%u, 0) => %i; errno => %s", parent_pid, rc, strerror ( errno ) );
return 0;
}
}
return 1;
}
void child_sigchld()
{
if ( getppid() != 1 )
return;
debug ( 1, "Got SIGCHLD (parent ended). Exit." );
exit ( -1 );
return;
}
int sethandler_sigchld ( void ( *handler ) () )
{
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset ( &sa.sa_mask );
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
critical_on ( sigaction ( SIGCHLD, &sa, 0 ) == -1 );
return 0;
}
# ifndef __linux__
void *watchforparent ( void *parent_pid_p )
{
while ( 1 ) {
child_sigchld();
sleep ( SLEEP_SECONDS );
}
return NULL;
}
# endif
pthread_t pthread_watchforparent;
pid_t fork_helper()
{
pid_t pid = fork();
if ( !pid ) { // is child?
parent_pid = getppid();
// Anti-zombie:
# ifdef __linux__
// Linux have support of "prctl(PR_SET_PDEATHSIG, signal);"
sethandler_sigchld ( child_sigchld );
prctl ( PR_SET_PDEATHSIG, SIGCHLD );
# else
pthread_create ( &pthread_watchforparent, NULL, watchforparent, &parent_pid );
# endif
debug ( 20, "parent_pid == %u", parent_pid );
return 0;
}
return pid;
}
int version()
{
char flags[] =
#ifdef _DEBUG_SUPPORT
" -D_DEBUG_SUPPORT"
#endif
#ifdef _DEBUG_FORCE
" -D_DEBUG_FORCE"
#endif
#ifdef KQUEUE_SUPPORT
" -DKQUEUE_SUPPORT"
#endif
#ifdef INOTIFY_SUPPORT
" -DINOTIFY_SUPPORT"
#endif
#ifdef INOTIFY_OLD
" -DINOTIFY_OLD"
#endif
#ifdef FANOTIFY_SUPPORT
" -DFANOTIFY_SUPPORT"
#endif
#ifdef BSM_SUPPORT
" -DBSM_SUPPORT"
#endif
#ifdef GIO_SUPPORT
" -DGIO_SUPPORT"
#endif
#ifdef DTRACEPIPE_SUPPORT
" -DDTRACEPIPE_SUPPORT"
#endif
#ifdef BACKTRACE_SUPPORT
" -DBACKTRACE_SUPPORT"
#endif
#ifdef CAPABILITIES_SUPPORT
" -DCAPABILITIES_SUPPORT"
#endif
#ifdef SECCOMP_SUPPORT
" -DSECCOMP_SUPPORT"
#endif
#ifdef GETMNTENT_SUPPORT
" -DGETMNTENT_SUPPORT"
#endif
#ifdef UNSHARE_SUPPORT
" -DUNSHARE_SUPPORT"
#endif
#ifdef PIVOTROOT_OPT_SUPPORT
" -DPIVOTROOT_OPT_SUPPORT"
#endif
#ifdef CGROUP_SUPPORT
" -DCGROUP_SUPPORT"
#endif
#ifdef TRE_SUPPORT
" -DTRE_SUPPORT"
#endif
#ifdef THREADING_SUPPORT
" -DTHREADING_SUPPORT"
#endif
#ifdef HL_LOCKS
" -DHL_LOCKS"
#endif
;
info ( PROGRAM" v%i.%i.%i"REVISION"\n\t"AUTHOR"\n\t"URL"\n\nCompiled with options: %s"
, VERSION_MAJ, VERSION_MID, VERSION_MIN, flags );
exit ( 0 );
}
int clsyncapi_getapiversion()
{
return CLSYNC_API_VERSION;
}
/**
* @brief Gets raw (string) an option value by an option name
*
* @param[in] _ctx_p Context
* @param[in] variable_name The name of the option
*
* @retval char * Pointer to constant string, if successful
* @retval NULL On error
*
*/
const char *parameter_get ( const char *variable_name, void *_ctx_p )
{
const ctx_t *ctx_p = _ctx_p;
const struct option *long_option_p = long_options;
int param_id = -1;
debug ( 8, "(\"%s\", %p)", variable_name, ctx_p );
while ( long_option_p->name != NULL ) {
if ( !strcmp ( long_option_p->name, variable_name ) ) {
param_id = long_option_p->val;
break;
}
long_option_p++;
}
if ( param_id == -1 ) {
errno = ENOENT;
return NULL;
}
debug ( 9, "ctx_p->flags_values_raw[%i] == \"%s\"", param_id, ctx_p->flags_values_raw[param_id] );
return ctx_p->flags_values_raw[param_id];
}
/**
* @brief Gets the name of the parameter by it's id
*
* @param[in] param_id The id of the parameter
*
* @retval char * Pointer to a constant string, if successful
* @retval NULL On error
*
*/
const char *parameter_get_name_by_id ( const uint16_t param_id )
{
const struct option *long_option_p = long_options;
const char *param_name = NULL;
debug ( 8, "(%u)", param_id );
while ( long_option_p->name != NULL ) {
if ( long_option_p->val == param_id ) {
param_name = long_option_p->name;
break;
}
long_option_p++;
}
if ( param_name == NULL ) {
errno = ENOENT;
return NULL;
}
debug ( 9, "param: %u -> \"%s\"", param_id, param_name );
return param_name;
}
/**
* @brief Gets raw (string) an option value by an option name and
* updates ctx_p->synchandler_argf
*
* @param[in] _ctx_p Context
* @param[in] variable_name The name of the option
*
* @retval char * Pointer to newly allocated string, if successful
* @retval NULL On error
*
*/
const char *parameter_get_wmacro ( const char *variable_name, void *_ctx_p )
{
ctx_t *ctx_p = _ctx_p;
static struct dosync_arg dosync_arg = {0};
debug ( 9, "(\"%s\", %p)", variable_name, _ctx_p );
if ( *variable_name < 'A' || *variable_name > 'Z' )
return parameter_get ( variable_name, _ctx_p );
if ( !strcmp ( variable_name, "RSYNC-ARGS" ) ) {
ctx_p->synchandler_argf |= SHFL_RSYNC_ARGS;
return NULL;
}
if ( !strcmp ( variable_name, "INCLUDE-LIST" ) ) {
ctx_p->synchandler_argf |= SHFL_INCLUDE_LIST;
return NULL;
}
const char *r = sync_parameter_get ( variable_name, &dosync_arg );
if ( r == dosync_arg.outf_path ) {
ctx_p->synchandler_argf |= SHFL_INCLUDE_LIST_PATH;
return NULL;
}
if ( r == dosync_arg.excf_path ) {
ctx_p->synchandler_argf |= SHFL_EXCLUDE_LIST_PATH;
return NULL;
}
errno = ENOENT;
return NULL;
}
/* Parameter exception flags */
#define PEF_NONE 0
#define PEF_UNEXPECTED_END 1
#define PEF_UNSET_VARIABLE 2
#define PEF_LAZY_SUBSTITUTION 4
/**
* @brief Expands option values, e. g. "/var/log/clsync-%label%.pid" -> "/var/log/clsync-clone.pid"
*
* @param[in] ctx_p Context
* @param[in] arg An allocated string with unexpanded value. Will be free'd
* @param[in] exceptionflags A bit field of allowed exceptions during parameter expansion:
* - PEF_NONE No exceptions are allowed
* - PEF_UNEXPECTED_END Do not warn about unexpected end of macro-substitution
* - PEF_UNSET_VARIABLE Do not warn about unset variable
* - PEF_LAZY_SUBSTITUTION Perform lazy substitution preserving original value
* @param[out] macro_count_p A pointer to count of found macro-s
* @param[out] expand_count_p A pointer to count of expanded macro-s
* @param[in] parameter_get A function to resolve macro-s
* @param[in] parameter_get_arg An argument to the function
*
* @retval char * Pointer to newly allocated string, if successful
* @retval NULL On error
*
*/
char *parameter_expand (
ctx_t *ctx_p,
char *arg,
int exceptionflags,
int *macro_count_p,
int *expand_count_p,
const char * ( *parameter_get ) ( const char *variable_name, void *arg ),
void *parameter_get_arg
)
{
debug ( 9, "(ctx_p, \"%s\" [%p], ...)", arg, arg );
char *ret = NULL;
size_t ret_size = 0, ret_len = 0;
#ifdef PARANOID
if ( arg == NULL ) {
errno = EINVAL;
return NULL;
}
#endif
if ( macro_count_p != NULL )
*macro_count_p = 0;
if ( expand_count_p != NULL )
*expand_count_p = 0;
char *ptr = &arg[-1];
while ( 1 ) {
ptr++;
switch ( *ptr ) {
case 0:
if ( ret == NULL ) {
debug ( 3, "Expanding value \"%s\" to \"%s\" (case #1)", arg, arg );
return arg;
}
ret[ret_len] = 0;
debug ( 3, "Expanding value \"%s\" to \"%s\" (case #0)", arg, ret );
free ( arg );
return ret;
case '%': {
if ( ptr[1] == '%' ) {
if (likely( ret != NULL ))
ret[ret_len++] = * ( ptr++ );
else {
ret_size = ALLOC_PORTION + 2;
ret = xrealloc ( ret, ret_size );
ret[0] = '%';
ret_len = 1;
ptr++;
}
break;
}
debug ( 25, "A macro" );
char nest_searching = 1;
char *ptr_nest = ptr;
while ( nest_searching ) {
ptr_nest++;
switch ( *ptr_nest ) {
case 0:
if (likely( ret != NULL ))
ret[ret_len] = 0;
else
ret = strdup(arg);
if ( ! ( exceptionflags & PEF_UNEXPECTED_END ) )
warning ( "Unexpected end of macro-substitution \"%s\" in value \"%s\"; result value is \"%s\"", ptr, arg, ret );
free ( arg );
return ret;
case '%': {
char *variable_name;
const char *variable_value;
size_t variable_value_len;
if ( macro_count_p != NULL )
( *macro_count_p )++;
nest_searching = 0;
*ptr_nest = 0;
variable_name = &ptr[1];
debug ( 15, "The macro is \"%s\"", variable_name );
if ( !strcmp ( variable_name, "PID" ) ) {
debug ( 35, "\"PID\"", variable_name );
if ( !*ctx_p->pid_str ) {
snprintf ( ctx_p->pid_str, 64, "%u", ctx_p->pid );
ctx_p->pid_str_len = strlen ( ctx_p->pid_str );
}
variable_value = ctx_p->pid_str;
variable_value_len = ctx_p->pid_str_len;
} else if ( *variable_name >= 'A' && *variable_name <= 'Z' && ( exceptionflags & PEF_LAZY_SUBSTITUTION ) ) { // Lazy substitution, preserving the value
debug ( 35, "Lazy substitution", variable_name );
variable_value = ptr;
variable_value_len = ( ptr_nest - ptr + 1 );
parameter_get ( variable_name, parameter_get_arg );
} else { // Substituting
debug ( 35, "Substitution", variable_name );
errno = 0;
variable_value = parameter_get ( variable_name, parameter_get_arg );
if ( variable_value == NULL ) {
if ( ! ( exceptionflags & PEF_UNSET_VARIABLE ) && ( errno != ENOENT ) )
warning ( "Variable \"%s\" is not set (%s)", variable_name, strerror ( errno ) );
*ptr_nest = '%';
errno = 0;
break;
}
variable_value_len = strlen ( variable_value );
if ( expand_count_p != NULL )
( *expand_count_p )++;
}
*ptr_nest = '%';
if ( ret_len + variable_value_len + 1 >= ret_size ) {
ret_size = ret_len + variable_value_len + 1 + ALLOC_PORTION;
ret = xrealloc ( ret, ret_size );
}
memcpy ( &ret[ret_len], variable_value, variable_value_len );
ret_len += variable_value_len;
break;
}
}
}
ptr = ptr_nest;
break;
}
default: {
if ( ret_len + 2 >= ret_size ) {
ret_size += ALLOC_PORTION + 2;
ret = xrealloc ( ret, ret_size );
}
ret[ret_len++] = *ptr;
break;
}
}
}
error ( "Unknown internal error" );
return arg;
}
/**
* @brief Gets the name of the parameter source by it's id
*
* @param[in] paramsource The id of the parameter source
*
* @retval char * Pointer to a constant string, if successful
* @retval NULL On error
*
*/
const char *parametersource_get_name ( paramsource_t paramsource )
{
switch ( paramsource ) {
case PS_UNKNOWN:
return "unknown_case_0";
case PS_ARGUMENT:
return "cli_arguments";
case PS_CONFIG:
return "config";
case PS_CONTROL:
return "control";
case PS_DEFAULTS:
return "defaults";
case PS_CORRECTION:
return "correction";
}
return "unknown_case_1";
}
static inline int synchandler_arg ( char *arg, size_t arg_len, void *_ctx_p, enum shargsid shargsid )
{
ctx_t *ctx_p = _ctx_p;
debug ( 9, "(\"%s\" [%p], %u, %p, %u)", arg, arg, arg_len, _ctx_p, shargsid );
if ( !strcmp ( arg, "%RSYNC-ARGS%" ) ) {
char *args_e[] = RSYNC_ARGS_E, *args_i[] = RSYNC_ARGS_I, **args_p;
free ( arg );
args_p = ctx_p->flags[RSYNCPREFERINCLUDE] ? args_i : args_e;
while ( *args_p != NULL ) {
#ifdef VERYPARANOID
if ( !strcmp ( *args_p, "%RSYNC-ARGS%" ) ) {
errno = EINVAL;
critical ( "Infinite recursion detected" );
}
#endif
if ( synchandler_arg ( strdup ( *args_p ), strlen ( *args_p ), ctx_p, shargsid ) )
return errno;
args_p++;
}
return 0;
}
if ( ctx_p->synchandler_args[shargsid].c >= MAXARGUMENTS - 2 ) {
errno = E2BIG;
error ( "There're too many sync-handler arguments "
"(%u > "XTOSTR ( MAXARGUMENTS - 2 ) "; arg == \"%s\").",
arg );
return errno;
}
#ifdef _DEBUG_FORCE
debug ( 14, "ctx_p->synchandler_args[%u].v[%u] = %p", shargsid, ctx_p->synchandler_args[shargsid].c, arg );
#endif
ctx_p->synchandler_args[shargsid].v[ctx_p->synchandler_args[shargsid].c++] = arg;
return 0;
}
static int synchandler_arg0 ( char *arg, size_t arg_len, void *_ctx_p )
{
return synchandler_arg ( arg, arg_len, _ctx_p, SHARGS_PRIMARY );
}
static int synchandler_arg1 ( char *arg, size_t arg_len, void *_ctx_p )
{
return synchandler_arg ( arg, arg_len, _ctx_p, SHARGS_INITIAL );
}
/* strtol wrapper with error checks */
static inline long xstrtol ( const char *str, int *err )
{
long res;
char *endptr;
errno = 0;
res = strtol ( str, &endptr, 0 );
if ( errno || *endptr ) {
error ( "argument \"%s\" can't be parsed as a number", str );
*err = EINVAL;
}
return res;
}
// a wrapper for xstrtol with a trimming of leading and tailing whitespaces
static inline long xstrtol_trim ( char *str, int *err )
{
// Removing whitespace from the beginning
while ( *str == ' ' || *str == '\t' || *str == '\r' || *str == '\n' ) str++;
// Removing whitespaces from the ending
char *end = str;
while ( *end != '\0' ) end++; // find the end of the string
end--;
while ( *end == ' ' || *end == '\t' || *end == '\r' || *end == '\n' ) end--; // find the end of the string excluding whitespaces
end++;
*end = '\0';
// Calling xstrtol(), obviously :)
return xstrtol ( str, err );
}
__extension__ static inline int parse_customsignals ( ctx_t *ctx_p, char *arg )
{
char *ptr = arg, *start = arg;
int ret = 0;
unsigned int signal;
do {
switch ( *ptr ) {
case 0:
case ',':
case ':':
// TODO: use xstrtol() instead of atoi()
//signal = (unsigned int)xstrtol(start, &ret);
signal = ( unsigned int ) atoi ( start );
if ( ret ) {
errno = ret;
return errno;
}
if ( signal == 0 ) {
// flushing the setting