redmine

Continued the work on clsync-api

... ... @@ -41,7 +41,8 @@ clsync_include_HEADERS = \
indexes.h \
options.h \
output.h \
socket.h
socket.h \
libclsync.h
doc:
doxygen .doxygen
... ...
... ... @@ -28,13 +28,13 @@
static pthread_t pthread_control;
int control_procclsyncconn(socket_connthreaddata_t *arg, sockcmd_t *sockcmd_p) {
clsyncconn_t *clsyncconn_p = arg->clsyncconn_p;
int control_procclsyncsock(socket_sockthreaddata_t *arg, sockcmd_t *sockcmd_p) {
clsyncsock_t *clsyncsock_p = arg->clsyncsock_p;
options_t *options_p = (options_t *)arg->arg;
switch(sockcmd_p->cmd_id) {
case SOCKCMD_REQUEST_INFO: {
socket_send(clsyncconn_p, SOCKCMD_REPLY_INFO, options_p->config_block, options_p->label, options_p->flags, options_p->flags_set);
socket_send(clsyncsock_p, SOCKCMD_REPLY_INFO, options_p->config_block, options_p->label, options_p->flags, options_p->flags_set);
break;
}
case SOCKCMD_REQUEST_DIE: {
... ... @@ -101,10 +101,10 @@ int control_loop(options_t *options_p) {
continue;
}
// Processing the events: accepting new clsyncconn
// Processing the events: accepting new clsyncsock
clsyncconn_t *clsyncconn_p = socket_accept(s);
if(clsyncconn_p == NULL) {
clsyncsock_t *clsyncsock_p = socket_accept(s);
if(clsyncsock_p == NULL) {
if(errno == EUSERS) // Too many connections. Just ignoring the new one.
continue;
... ... @@ -116,7 +116,7 @@ int control_loop(options_t *options_p) {
}
printf_dd("Debug2: control_loop(): Starting new thread for new connection.\n");
socket_connthreaddata_t *threaddata_p = socket_thread_attach(clsyncconn_p);
socket_sockthreaddata_t *threaddata_p = socket_thread_attach(clsyncsock_p);
if (threaddata_p == NULL) {
printf_e("Error: control_loop(): Cannot create a thread for connection: %s (errno: %i)\n", strerror(errno), errno);
... ... @@ -124,8 +124,8 @@ int control_loop(options_t *options_p) {
continue;
}
threaddata_p->procfunct = control_procclsyncconn;
threaddata_p->clsyncconn_p = clsyncconn_p;
threaddata_p->procfunct = control_procclsyncsock;
threaddata_p->clsyncsock_p = clsyncsock_p;
threaddata_p->arg = options_p;
threaddata_p->running = &options_p->socket;
threaddata_p->authtype = options_p->flags[SOCKETAUTH];
... ... @@ -159,9 +159,13 @@ int control_run(options_t *options_p) {
if (!ret) {
s = socket_listen_unix(options_p->socketpath);
if (s == -1)
clsyncsock_t *clsyncsock = socket_listen_unix(options_p->socketpath);
if (clsyncsock == NULL) {
ret = errno;
} else {
s = clsyncsock->sock;
socket_cleanup(clsyncsock);
}
}
// fixing privileges
... ...
usr/include/clsync/*.h
usr/include/clsync/configuration.h
usr/include/clsync/clsync.h
usr/include/clsync/malloc.h
usr/include/clsync/indexes.h
usr/include/clsync/options.h
usr/include/clsync/output.h
... ...
... ... @@ -4,7 +4,7 @@ Priority: optional
Maintainer: Artyom A Anikeev <anikeev@ut.mephi.ru>
Uploaders: Barak A. Pearlmutter <bap@debian.org>
Build-Depends: debhelper (>= 9), libglib2.0-dev (>= 2.0.0),
dh-autoreconf, libmhash-dev
dh-autoreconf
Standards-Version: 3.9.4
Homepage: http://ut.mephi.ru/oss
Vcs-Git: git://anonscm.debian.org/collab-maint/clsync.git
... ...
usr/include/libclsync/*.h
usr/include/clsync/socket.h
usr/include/clsync/libclsync.h
... ...
usr/lib/libclsync*
usr/lib/x86_64-linux-gnu/libclsync*
... ...
... ... @@ -22,6 +22,7 @@
#include <sys/un.h> // for "struct sockaddr_un"
#include "socket.h"
#include "libclsync.h"
#include "malloc.h"
#include "output.h"
... ... @@ -36,82 +37,95 @@ write_funct _write_d=NULL;
write_funct _write_v=NULL;
write_funct write_e=NULL;
struct clsync {
clsyncconn_t *conn_p;
clsyncconn_procfunct_t procfunct;
};
typedef struct clsync clsync_t;
int libclsync_procclsyncconn(socket_connthreaddata_t *arg, sockcmd_t *sockcmd_p) {
clsync_t *clsync_p = arg->arg;
clsyncconn_t *clsyncconn_p = clsync_p->conn_p;
clsyncconn_procfunct_t procfunct = clsync_p->procfunct;
int libproc_procclsyncsock(socket_sockthreaddata_t *arg, sockcmd_t *sockcmd_p) {
clsyncproc_t *proc_p = arg->arg;
clsyncsock_t *clsyncsock_p = proc_p->sock_p;
clsyncsock_procfunct_t procfunct = proc_p->procfunct;
#ifdef PARANOID
if (procfunct == NULL) {
printf_e("Error: libclsync_procclsyncconn(): procfunct == NULL\n");
printf_e("Error: libproc_procclsyncsock(): procfunct == NULL\n");
return 0;
}
#endif
if(arg->flags&SOCKPROCFLAG_OVERRIDE_API)
goto l_libproc_procclsyncsock_sw_default;
switch(sockcmd_p->cmd_id) {
default:
l_libproc_procclsyncsock_sw_default:
if(procfunct(arg, sockcmd_p))
socket_sendinvalid(clsyncconn_p, sockcmd_p);
socket_sendinvalid(clsyncsock_p, sockcmd_p);
break;
}
return 0;
}
static inline int _clsync_connect_setthreaddata(socket_connthreaddata_t *threaddata_p, clsync_t *clsync_p) {
threaddata_p->procfunct = libclsync_procclsyncconn;
threaddata_p->clsyncconn_p = clsync_p->conn_p;
threaddata_p->arg = clsync_p;
static inline int _clsync_connect_setthreaddata(socket_sockthreaddata_t *threaddata_p, clsyncproc_t *proc_p, sockprocflags_t flags) {
threaddata_p->procfunct = libproc_procclsyncsock;
threaddata_p->clsyncsock_p = proc_p->sock_p;
threaddata_p->arg = proc_p;
threaddata_p->running = NULL;
threaddata_p->authtype = SOCKAUTH_NULL;
threaddata_p->flags = 0;
threaddata_p->flags = flags;
threaddata_p->freefunct_arg = free;
return 0;
}
clsync_t *clsync_connect_unix(const char *const socket_path, clsyncconn_procfunct_t procfunct) {
clsync_t *clsync_p = xmalloc(sizeof(*clsync_p));
memset(clsync_p, 0, sizeof(*clsync_p));
static inline clsyncproc_t *_clsync_x_unix(
const char *const socket_path,
clsyncsock_procfunct_t procfunct,
sockprocflags_t flags,
const char *const action,
clsyncsock_t *(*socket_x_unix)(const char *const)
) {
if (procfunct == NULL) {
errno = EINVAL;
return NULL;
}
clsync_p->conn_p = socket_connect_unix(socket_path);
if(clsync_p->conn_p == NULL) {
free(clsync_p);
clsyncproc_t *proc_p = xmalloc(sizeof(*proc_p));
memset(proc_p, 0, sizeof(*proc_p));
proc_p->sock_p = socket_x_unix(socket_path);
if(proc_p->sock_p == NULL) {
free(proc_p);
if(errno == EUSERS) {
printf_e("Error: clsync_connect_unix(): Too many connections.\n");
printf_e("Error: clsync_%s_unix(): Too many connections.\n", action);
return NULL;
}
// Got unknown error. Closing control socket just in case.
printf_e("Error: clsync_connect_unix(): Cannot socket_accept(): %s (errno: %i)\n", strerror(errno), errno);
printf_e("Error: clsync_%s_unix(): Cannot socket_%s_unix(): %s (errno: %i)\n",
action, action, strerror(errno), errno);
return NULL;
}
socket_connthreaddata_t *threaddata_p = socket_thread_attach(clsync_p->conn_p);
socket_sockthreaddata_t *threaddata_p = socket_thread_attach(proc_p->sock_p);
if (threaddata_p == NULL) {
socket_cleanup(clsync_p->conn_p);
free(clsync_p);
socket_cleanup(proc_p->sock_p);
free(proc_p);
return NULL;
}
_clsync_connect_setthreaddata(threaddata_p, clsync_p);
_clsync_connect_setthreaddata(threaddata_p, proc_p, flags);
clsync_p->procfunct = procfunct;
proc_p->procfunct = procfunct;
socket_thread_start(threaddata_p);
return clsync_p;
return proc_p;
}
clsyncproc_t *clsync_listen_unix(const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags) {
return _clsync_x_unix(socket_path, procfunct, flags, "listen", socket_listen_unix);
}
clsyncproc_t *clsync_connect_unix(const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags) {
return _clsync_x_unix(socket_path, procfunct, flags, "connect", socket_connect_unix);
}
... ...
/*
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/>.
*/
#include "socket.h"
struct clsyncproc {
clsyncsock_t *sock_p;
clsyncsock_procfunct_t procfunct;
};
typedef struct clsyncproc clsyncproc_t;
extern clsyncproc_t *clsync_listen_unix (const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags);
extern clsyncproc_t *clsync_connect_unix(const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags);
... ...
This diff is collapsed. Click to expand it.
... ... @@ -17,22 +17,24 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CLSYNC_SOCKET_H
#define __CLSYNC_SOCKET_H
#define SOCKET_DEFAULT_PROT 0
#define SOCKET_DEFAULT_SUBPROT SUBPROT0_TEXT
// buffer size
#define SOCKET_BUFSIZ (1<<12)
struct clsyncconn {
struct clsyncsock {
int sock;
int num;
uint16_t prot;
uint16_t subprot;
};
typedef struct clsyncconn clsyncconn_t;
typedef struct clsyncsock clsyncsock_t;
struct clsyncthread {
clsyncconn_t *clsyncconn_p;
clsyncsock_t *clsyncsock_p;
void *arg;
void *funct_arg_free;
};
... ... @@ -44,14 +46,14 @@ enum subprot0 {
};
typedef enum subprot0 subprot0_t;
enum clsyncconn_state {
enum clsyncsock_state {
CLSTATE_NONE = 0,
CLSTATE_AUTH,
CLSTATE_MAIN,
CLSTATE_DYING,
CLSTATE_DIED,
};
typedef enum clsyncconn_state clsyncconn_state_t;
typedef enum clsyncsock_state clsyncsock_state_t;
enum sockcmd_id {
SOCKCMD_REQUEST_NEGOTIATION = 000,
... ... @@ -121,8 +123,10 @@ struct sockcmd {
typedef struct sockcmd sockcmd_t;
enum sockprocflags {
SOCKPROCFLAG_NONE = 0,
SOCKPROCFLAG_OVERRIDECOMMON,
SOCKPROCFLAG_NONE = 0x00,
SOCKPROCFLAG_OVERRIDE_COMMON = 0x01,
SOCKPROCFLAG_OVERRIDE_API = 0x02,
SOCKPROCFLAG_OVERRIDE_ALL = 0x0f,
};
typedef enum sockprocflags sockprocflags_t;
... ... @@ -133,42 +137,44 @@ enum sockauth_id {
};
typedef enum sockauth_id sockauth_id_t;
struct socket_connthreaddata;
typedef int (*clsyncconn_procfunct_t)(struct socket_connthreaddata *, sockcmd_t *);
struct socket_sockthreaddata;
typedef int (*clsyncsock_procfunct_t)(struct socket_sockthreaddata *, sockcmd_t *);
typedef void (*freefunct_t)(void *);
struct socket_connthreaddata {
struct socket_sockthreaddata {
int id;
clsyncconn_procfunct_t procfunct;
clsyncsock_procfunct_t procfunct;
freefunct_t freefunct_arg;
clsyncconn_t *clsyncconn_p;
clsyncsock_t *clsyncsock_p;
void *arg;
clsyncconn_state_t state;
clsyncsock_state_t state;
sockauth_id_t authtype;
int *running; // Pointer to interger with non-zero value to continue running
sockprocflags_t flags;
pthread_t thread;
};
typedef struct socket_connthreaddata socket_connthreaddata_t;
typedef struct socket_sockthreaddata socket_sockthreaddata_t;
extern int socket_send(clsyncconn_t *clsyncconn, sockcmd_id_t cmd_id, ...);
extern int socket_sendinvalid(clsyncconn_t *clsyncconn_p, sockcmd_t *sockcmd_p);
extern int socket_recv(clsyncconn_t *clsyncconn, sockcmd_t *sockcmd);
extern int socket_send(clsyncsock_t *clsyncsock, sockcmd_id_t cmd_id, ...);
extern int socket_sendinvalid(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p);
extern int socket_recv(clsyncsock_t *clsyncsock, sockcmd_t *sockcmd);
extern int socket_check_bysock(int sock);
extern clsyncconn_t *socket_accept(int sock);
extern int socket_cleanup(clsyncconn_t *clsyncconn_p);
extern clsyncsock_t *socket_accept(int sock);
extern int socket_cleanup(clsyncsock_t *clsyncsock_p);
extern int socket_init();
extern int socket_deinit();
extern int socket_procclsyncconn(socket_connthreaddata_t *arg);
extern clsyncconn_t *socket_connect_unix(const char *const socket_path);
extern int socket_listen_unix(const char *const socket_path);
extern int socket_procclsyncsock(socket_sockthreaddata_t *arg);
extern clsyncsock_t *socket_connect_unix(const char *const socket_path);
extern clsyncsock_t *socket_listen_unix (const char *const socket_path);
extern socket_connthreaddata_t *socket_thread_attach(clsyncconn_t *clsyncconn_p);
extern int socket_thread_start(socket_connthreaddata_t *threaddata_p);
extern socket_sockthreaddata_t *socket_thread_attach(clsyncsock_t *clsyncsock_p);
extern int socket_thread_start(socket_sockthreaddata_t *threaddata_p);
extern int clsyncconns_num;
extern int clsyncconns_count;
extern int clsyncconns_last;
extern int clsyncsocks_num;
extern int clsyncsocks_count;
extern int clsyncsocks_last;
extern const char *const textmessage_args[];
extern const char *const textmessage_descr[];
#endif
... ...