redmine

Porting to FreeBSD, chapter 1

/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ... @@ -17,8 +17,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#define _XOPEN_SOURCE 700
//#define _XOPEN_SOURCE 700
#define _LARGEFILE64_SOURCE
#include <stdio.h>
... ... @@ -37,12 +38,14 @@
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <wait.h>
#include <fts.h>
#ifdef FANOTIFY_SUPPORT
#include <sys/fanotify.h>
#endif
#include <sys/inotify.h>
#ifndef __FreeBSD__
# ifdef FANOTIFY_SUPPORT
# include <sys/fanotify.h>
# endif
#endif
#include <sys/wait.h>
#include <fts.h>
#include <sys/time.h>
#include <dirent.h>
#include <sys/utsname.h>
... ... @@ -53,13 +56,13 @@
#include <pthread.h>
#ifdef HAVE_CAPABILITIES
#include <sys/capability.h> // for capset()/capget() for --preserve-file-access
#include <sys/prctl.h> // for prctl() for --preserve-fil-access
# include <sys/capability.h> // for capset()/capget() for --preserve-file-access
# include <sys/prctl.h> // for prctl() for --preserve-fil-access
#endif
#include "configuration.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif
#include "clsync.h"
... ... @@ -70,19 +73,19 @@
#include <sys/param.h>
#ifndef IN_CREATE_SELF
#define IN_CREATE_SELF IN_CREATE
# define IN_CREATE_SELF IN_CREATE
#endif
#ifdef _DEBUG
#define DEBUGV(...) __VA_ARGS__
# define DEBUGV(...) __VA_ARGS__
#else
#define DEBUGV(...)
# define DEBUGV(...)
#endif
#ifdef PARANOID
#define PARANOIDV(...) __VA_ARGS__
# define PARANOIDV(...) __VA_ARGS__
#else
#define PARANOIDV(...)
# define PARANOIDV(...)
#endif
#ifdef _GNU_SOURCE
... ...
... ... @@ -7,10 +7,21 @@ AC_CONFIG_SRCDIR([sync.c])
AM_INIT_AUTOMAKE([1.11 foreign -Wall -Wno-portability])
AC_CONFIG_HEADERS([config.h])
AM_PROG_CC_C_O
AC_PROG_CC_STDC
AC_CONFIG_MACRO_DIR([m4])
LT_INIT
AC_CANONICAL_HOST
case $host_os in
*bsd*)
AC_SEARCH_LIBS([inotify_init], [inotify], [],
[AC_MSG_ERROR("Unable to find libinotify")])
AC_SEARCH_LIBS([backtrace], [execinfo], [],
[AC_MSG_ERROR("Unable to find libexecinfo")])
;;
esac
dnl --enable-clsync
AC_ARG_ENABLE(clsync,
AS_HELP_STRING(--disable-clsync,
... ... @@ -27,7 +38,7 @@ AC_ARG_ENABLE(cluster,
AS_HELP_STRING(--enable-cluster,
[enable clustering support (not yet implemented), default: no]))
AS_IF([test "x$enable_cluster" = "xyes"], [CPPFLAGS+=" -DCLUSTER_SUPPORT"
AS_IF([test "x$enable_cluster" = "xyes"], [CPPFLAGS="${CPPFLAGS} -DCLUSTER_SUPPORT"
dnl mhash check
AC_ARG_WITH(mhash,
... ... @@ -36,7 +47,7 @@ AS_IF([test "x$enable_cluster" = "xyes"], [CPPFLAGS+=" -DCLUSTER_SUPPORT"
AS_IF([test "x$with_mhash" = "xno"], [], [
AC_CHECK_HEADER([mhash.h], [], [AC_MSG_ERROR("Unable to find mhash.h")])
AC_SEARCH_LIBS([mhash_init], [mhash], [CPPFLAGS+=" -DHAVE_MHASH"],
AC_SEARCH_LIBS([mhash_init], [mhash], [CPPFLAGS="${CPPFLAGS} -DHAVE_MHASH"],
[AC_MSG_ERROR("Unable to find libmhash")])
])
])
... ... @@ -47,7 +58,7 @@ AS_HELP_STRING(--enable-socket,
[enable control socket support, default: no]))
AS_IF([test "x$enable_socket" = "xyes"],
[CPPFLAGS+=" -DENABLE_SOCKET"])
[CPPFLAGS="${CPPFLAGS} -DENABLE_SOCKET"])
AM_CONDITIONAL([SOCKET], [test "x$enable_socket" = "xyes"])
... ... @@ -63,7 +74,7 @@ AS_HELP_STRING(--enable-debug,
[enable debugging, default: no]))
AS_IF([test "x$enable_debug" = "xyes"],
[CFLAGS+=" -pipe -Wall -ggdb3" CPPFLAGS+=" -D_DEBUG"])
[CFLAGS="${CFLAGS} -pipe -Wall -ggdb3" CPPFLAGS="${CPPFLAGS} -D_DEBUG"])
dnl --paranoid
AC_ARG_ENABLE(paranoid,
... ... @@ -78,11 +89,11 @@ esac],
[paranoid=1])
AS_IF([test $paranoid -ge 1],[
CPPFLAGS+=" -D_FORTIFY_SOURCE=2 -DPARANOID"
CFLAGS+=" -fstack-protector-all -Wall --param ssp-buffer-size=4 -fstack-check"
LDFLAGS+=" -Xlinker -zrelro"
CPPFLAGS="${CPPFLAGS} -D_FORTIFY_SOURCE=2 -DPARANOID"
CFLAGS="${CFLAGS} -fstack-protector-all -Wall --param ssp-buffer-size=4 -fstack-check"
LDFLAGS="${LDFLAGS} -Xlinker -zrelro"
])
AS_IF([test $paranoid -eq 2], [CPPFLAGS+=" -DVERYPARANOID"])
AS_IF([test $paranoid -eq 2], [CPPFLAGS="${CPPFLAGS} -DVERYPARANOID"])
# Checks for programs.
AC_PROG_CC_C99
... ... @@ -92,17 +103,17 @@ PKG_PROG_PKG_CONFIG([0.20])
# Checks for libraries.
dnl libdl
AC_CHECK_HEADER([dlfcn.h], [], [AC_MSG_ERROR("Unable to find dlfcn.h")])
AC_SEARCH_LIBS([dlopen], [dl], [LDFLAGS+=' -rdynamic'],
AC_SEARCH_LIBS([dlopen], [dl], [LDFLAGS="${LDFLAGS} -rdynamic"],
[AC_MSG_ERROR("Unable to find libdl")])
dnl pthread
AC_CHECK_LIB([pthread], [pthread_create],
[CPPFLAGS+=" -pthread" LDFLAGS+=" -pthread"],
[AC_MSG_ERROR("Pthread support is mandatory")])
#AC_CHECK_LIB(pthread, pthread_create)
PTHREAD_LIBS=-pthread
AC_SUBST(PTHREAD_LIBS)
dnl -lrt is needed on < glibc-2.17
AC_SEARCH_LIBS([clock_getres], [rt], [],
[AC_MSG_ERROR("Unable to find librt; clock_getres() is needed")])
#AC_SEARCH_LIBS([clock_getres], [rt], [],
# [AC_MSG_ERROR("Unable to find librt; clock_getres() is needed")])
dnl capabilities check
AC_ARG_WITH(capabilities,
... ... @@ -113,7 +124,7 @@ AS_IF(
[test "x$with_capabilities" = "xyes"], [
AC_CHECK_HEADER(
[sys/capability.h],
[CPPFLAGS+=" -DHAVE_CAPABILITIES"],
[CPPFLAGS="${CPPFLAGS} -DHAVE_CAPABILITIES"],
[AC_MSG_ERROR("Unable to find sys/capability.h")])
])
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
#!/bin/bash
#!/bin/sh
IFACE="$1"
... ... @@ -15,7 +15,7 @@ if [ "$IPADDR" = "" ]; then
exit 2
fi
mkdir -m 700 -p testdir/{from,to,listdir}
mkdir -m 700 -p testdir/from testdir/to testdir/listdir
cat > rules <<EOF
-d^[Dd]ont[Ss]ync\$
... ...
#!/bin/bash
#!/bin/sh
mkdir -m 700 -p testdir/{from,to,listdir}
mkdir -m 700 -p testdir/from testdir/to testdir/listdir
cat > rules <<EOF
-d^[Dd]ont[Ss]ync\$
... ...
#!/bin/bash
#!/bin/sh
mkdir -m 700 -p testdir/{from,to,listdir}
mkdir -m 700 -p testdir/from testdir/to testdir/listdir
cat > rules <<EOF
-d^[Dd]ont[Ss]ync\$
... ...
#!/bin/bash
#!/bin/sh
mkdir -m 700 -p testdir/{from,to,listdir}
mkdir -m 700 -p testdir/from testdir/to testdir/listdir
cat > rules <<EOF
-d^[Dd]ont[Ss]ync\$
+*.*
EOF
gcc -ggdb3 -fPIC -shared -o clsync-synchandler-rsyncso.so clsync-synchandler-rsyncso.c &&
cc -ggdb3 -fPIC -shared -o clsync-synchandler-rsyncso.so clsync-synchandler-rsyncso.c &&
clsync -K example-rsyncso -M rsyncso -L /dev/shm/clsync -w2 -t5 -W ./testdir/from -S ./clsync-synchandler-rsyncso.so -R rules -D ./testdir/to $@
... ...
#!/bin/bash
#!/bin/sh
mkdir -m 700 -p testdir/{from,to,listdir}
mkdir -m 700 -p testdir/from testdir/to testdir/listdir
cat > rules <<EOF
-d^[Dd]ont[Ss]ync\$
+*.*
EOF
gcc -ggdb3 -fPIC -shared -o clsync-synchandler-so.so clsync-synchandler-so.c &&
cc -ggdb3 -fPIC -shared -o clsync-synchandler-so.so clsync-synchandler-so.c &&
clsync -K example-so -M so -w2 -t5 -W ./testdir/from -S ./clsync-synchandler-so.so -R rules -D ./testdir/to $@
... ...
#!/bin/bash -x
#!/bin/sh -x
FROM="./testdir/from"
TO="./testdir/to"
... ... @@ -8,7 +8,7 @@ LABEL="$2"
ARG0="$3"
ARG1="$4"
function rsynclist() {
rsynclist() {
LISTFILE="$1"
EXCLISTFILE="$2"
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ... @@ -18,12 +18,15 @@
*/
#include "common.h"
#include "port-hacks.h"
#include "error.h"
#include "malloc.h"
char *fd2fpath_malloc(int fd) {
struct stat64 lstat;
stat64_t st64;
if(fd <= 0) {
error("Invalid file descriptor supplied: fd2fpath_malloc(%i).", fd);
... ... @@ -35,12 +38,12 @@ char *fd2fpath_malloc(int fd) {
char *fpath = xmalloc((1<<8) + 2);
sprintf(fpath, "/proc/self/fd/%i", fd);
if(lstat64(fpath, &lstat)) {
error("Cannot lstat(\"%s\", lstat).", fpath);
if(lstat64(fpath, &st64)) {
error("Cannot lstat64(\"%s\", st64).", fpath);
return NULL;
}
ssize_t fpathlen = lstat.st_size;
ssize_t fpathlen = st64.st_size;
if(fpathlen > (1<<8))
fpath = xrealloc(fpath, fpathlen+2);
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ... @@ -20,6 +20,8 @@
#include "common.h"
#include "port-hacks.h"
#include <pwd.h> // For getpwnam()
#include <grp.h> // For getgrnam()
... ... @@ -519,7 +521,7 @@ int parse_parameter(ctx_t *ctx_p, uint16_t param_id, char *arg, paramsource_t pa
}
}
case SOCKETMOD:
if(!sscanf(arg, "%o", &ctx_p->socketmod)) {
if(!sscanf(arg, "%o", (unsigned int *)&ctx_p->socketmod)) {
error("Non octal value passed to --socket-mod: \"%s\"", arg);
return EINVAL;
}
... ... @@ -1309,7 +1311,7 @@ int main(int argc, char *argv[]) {
ret = errno;
}
struct stat64 stat64={0};
stat64_t stat64={0};
if(lstat64(ctx.watchdir, &stat64)) {
error("Cannot lstat64() on \"%s\"", ctx.watchdir);
if(!ret)
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
Copyright (C) 2014 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/>.
*/
#ifndef ETIME
#define ETIME ETIMEDOUT
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifndef __FreeBSD__
typedef struct stat64 stat64_t;
#endif
#ifdef __FreeBSD__
#define O_PATH 0
typedef struct stat stat64_t;
#include <pthread.h>
static inline int pthread_tryjoin_np(pthread_t thread, void **retval) {
struct timespec abstime;
int rc;
abstime.tv_sec = 0;
abstime.tv_nsec = 0;
extern int pthread_timedjoin_np(pthread_t thread, void **value_ptr, const struct timespec *abstime);
rc = pthread_timedjoin_np(thread, retval, &abstime);
if (rc == ETIMEDOUT)
rc = EBUSY;
return rc;
}
static inline int lstat64(const char *pathname, struct stat *buf) {
return lstat(pathname, buf);
}
#endif
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ... @@ -20,12 +20,14 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/un.h> // for "struct sockaddr_un"
#include <unistd.h>
#include "configuration.h"
#include "error.h"
#include "malloc.h"
... ... @@ -458,7 +460,7 @@ int socket_recv(clsyncsock_t *clsyncsock, sockcmd_t *sockcmd_p) {
case SUBPROT0_TEXT:
if((end=strchr(ptr, '\n'))!=NULL) {
if(sscanf(start, "%03u", (unsigned int *)&sockcmd_p->cmd_id) != 1)
return EBADRQC;
return ENOMSG;
char *str_args = &start[3+1];
parse_text_data(sockcmd_p, str_args, end-str_args);
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ... @@ -20,7 +20,10 @@
#ifndef __CLSYNC_SOCKET_H
#define __CLSYNC_SOCKET_H
#ifdef __linux__
#include <linux/limits.h>
#endif
#include <pthread.h>
#include <stdint.h>
... ...
/*
clsync - file tree sync utility based on fanotify and inotify
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
... ... @@ -19,6 +19,9 @@
#include "common.h"
#include "port-hacks.h"
#include "main.h"
#include "error.h"
#include "fileutils.h"
... ... @@ -338,7 +341,7 @@ static inline int indexes_outaggr_add(indexes_t *indexes_p, char *outline, event
}
static threadsinfo_t *thread_info() { // TODO: optimize this
static threadsinfo_t threadsinfo={{{{0}}},{{{0}}},0};
static threadsinfo_t threadsinfo={{0},{0},0};
if(!threadsinfo.mutex_init) {
int i=0;
while(i < PTHREAD_MUTEX_MAX) {
... ... @@ -1593,6 +1596,7 @@ int sync_notify_mark(int notify_d, ctx_t *ctx_p, const char *accpath, const char
case NE_INOTIFY: {
int inotify_d = notify_d;
debug(5, "inotify_add_watch(%i, \"%s\", 0x%x)", inotify_d, accpath, INOTIFY_MARKMASK);
if((wd = inotify_add_watch(inotify_d, accpath, INOTIFY_MARKMASK)) == -1) {
if(errno == ENOENT)
return -2;
... ... @@ -1601,6 +1605,7 @@ int sync_notify_mark(int notify_d, ctx_t *ctx_p, const char *accpath, const char
path);
return -1;
}
debug(6, "endof inotify_add_watch(%i, \"%s\", %x)", inotify_d, accpath, INOTIFY_MARKMASK);
break;
}
default: {
... ... @@ -1743,7 +1748,7 @@ int sync_notify_init(ctx_t *ctx_p) {
}
#endif
case NE_INOTIFY: {
#ifdef OLDSYSTEM
#if OLDSYSTEM || __FreeBSD__
int inotify_d = inotify_init();
#else
int inotify_d = inotify_init1(INOTIFY_FLAGS);
... ... @@ -2265,7 +2270,7 @@ int sync_idle_dosync_collectedevents_aggrqueue(queue_id_t queue_id, ctx_t *ctx_p
int sync_idle_dosync_collectedevents_uniqfname(ctx_t *ctx_p, char *fpath, char *name) {
pid_t pid = getpid();
time_t tm = time(NULL);
struct stat64 stat64;
stat64_t stat64;
int counter = 0;
do {
... ... @@ -3020,7 +3025,7 @@ int sync_inotify_handle(int inotify_d, ctx_t *ctx_p, indexes_t *indexes_p) {
// Getting infomation about file/dir/etc
struct stat64 lstat;
stat64_t lstat;
mode_t st_mode;
size_t st_size;
if (lstat64(path_full, &lstat)) {
... ...