redmine

Added a hack to bypass FOPEN_MAX limit of Posix standart

https://lists.freebsd.org/pipermail/freebsd-arch/2008-February/007705.html
... ... @@ -6,10 +6,10 @@ bin_PROGRAMS = clsync
clsync_SOURCES = calc.c cluster.c error.c fileutils.c glibex.c \
indexes.c main.c malloc.c rules.c stringex.c sync.c \
privileged.c pthreadex.c calc.h cluster.h \
fileutils.h glibex.h main.h port-hacks.h pthreadex.h \
stringex.h sync.h common.h control.h privileged.h rules.h \
syscalls.h
posix-hacks.c privileged.c pthreadex.c calc.h cluster.h \
fileutils.h glibex.h main.h port-hacks.h posix-hacks.h \
pthreadex.h stringex.h sync.h common.h control.h privileged.h \
rules.h syscalls.h
clsync_CFLAGS = $(AM_CFLAGS)
clsync_LDFLAGS = $(AM_LDFLAGS)
... ...
... ... @@ -76,6 +76,7 @@
#include "clsync.h"
#include "port-hacks.h"
#include "posix-hacks.h"
#include "ctx.h"
#include "program.h"
... ... @@ -162,8 +163,8 @@ enum notifyengine_enum {
NE_KQUEUE,
NE_BSM,
NE_BSM_PREFETCH,
NE_GIO,
NE_DTRACEPIPE,
NE_GIO,
};
typedef enum notifyengine_enum notifyengine_t;
... ...
... ... @@ -51,6 +51,7 @@
#if CGROUP_SUPPORT
# include "cgroup.h"
#endif
#include "posix-hacks.h"
//#include "revision.h"
... ... @@ -2245,6 +2246,9 @@ int main(int _argc, char *_argv[]) {
argc = _argc;
int ret = 0, nret, rm_listoutdir = 0;
SAFE (posixhacks_init(), errno = ret = _SAFE_rc);
ctx_p->flags[MONITOR] = DEFAULT_NOTIFYENGINE;
ctx_p->syncdelay = DEFAULT_SYNCDELAY;
ctx_p->_queues[QUEUE_NORMAL].collectdelay = DEFAULT_COLLECTDELAY;
... ... @@ -2868,6 +2872,9 @@ int main(int _argc, char *_argv[]) {
ctx_cleanup(ctx_p);
debug(1, "finished, exitcode: %i: %s.", ret, strerror(ret));
free(ctx_p);
SAFE (posixhacks_deinit(), errno = ret = _SAFE_rc);
return ret;
}
... ...
/*
clsync - file tree sync utility based on inotify/kqueue/bsm/gio
Copyright (C) 2013-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/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define __POSIX_HACKS_C
int reserved_fd[FOPEN_MAX+1] = {-1};
int reserved_fd_used;
static inline int reserve_fdpair(int idx) {
int pipe_fds[2];
if (pipe2(pipe_fds, O_CLOEXEC|O_NONBLOCK))
return errno;
reserved_fd[ idx ] = pipe_fds[0];
reserved_fd[ idx + 1 ] = pipe_fds[1];
return 0;
}
int posixhacks_init() {
int i;
// Reserving file descriptors from start to bypass FOPEN_MAX limit on fopen()/fdopen()
i = 0;
while (i < (FOPEN_MAX+1)/2) {
if (reserve_fdpair (i<<1))
return errno;
i++;
}
reserved_fd_used = 0;
return 0;
}
FILE *posixhacks_fopen(const char *path, const char *mode) {
close(reserved_fd[reserved_fd_used++]);
return fopen(path, mode);
}
int posixhacks_fclose(FILE *fp) {
int rc;
int pipe_fds[2];
rc = fclose(fp);
// reserving the file descriptor
if (!(reserved_fd_used&1))
close(reserved_fd[reserved_fd_used++]);
reserved_fd_used -= 2;
if (reserve_fdpair (reserved_fd_used))
return errno;
return rc;
}
int posixhacks_deinit() {
int i;
i = 0;
while (i < (FOPEN_MAX+1)/2) {
close(reserved_fd[ (i<<1) ]);
close(reserved_fd[ (i<<1) + 1 ]);
}
return 0;
}
... ...
/*
clsync - file tree sync utility based on inotify/kqueue/bsm/gio
Copyright (C) 2013-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 __POSIX_HACKS_C
# define fopen posixhacks_fopen
# define fdopen posixhacks_fdopen
# define fclose posixhacks_fclose
#endif
extern int posixhacks_init();
extern FILE *posixhacks_fopen(const char *path, const char *mode);
extern int posixhacks_fclose(FILE *fp);
extern int posixhacks_deinit();
... ...