Blame view

libclsync.c 3.25 KB
redmine authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    libclsyncmgr - clsync control socket API
    
    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/>.
 */

redmine authored
20
#define LIBCLSYNC
redmine authored
21

Andrew Savchenko authored
22 23
#include <errno.h>
#include <stdlib.h>
redmine authored
24 25
#include <sys/un.h>	// for "struct sockaddr_un"

Andrew Savchenko authored
26 27
#include "configuration.h"
#include "error.h"
redmine authored
28
#include "libclsync.h"
redmine authored
29
#include "malloc.h"
Andrew Savchenko authored
30
#include "socket.h"
redmine authored
31

redmine authored
32 33 34 35
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;
redmine authored
36

redmine authored
37 38
#ifdef PARANOID
	if (procfunct == NULL) {
redmine authored
39
		error("procfunct == NULL");
redmine authored
40 41 42
		return 0;
	}
#endif
redmine authored
43

redmine authored
44 45 46
	if(procfunct(arg, sockcmd_p))
		switch(sockcmd_p->cmd_id) {
			default:
redmine authored
47
				socket_sendinvalid(clsyncsock_p, sockcmd_p);
redmine authored
48 49
				break;
		}
redmine authored
50

redmine authored
51
	return 0;
redmine authored
52 53
}

redmine authored
54 55 56 57
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;
redmine authored
58 59
	threaddata_p->running		=  NULL;
	threaddata_p->authtype		=  SOCKAUTH_NULL;
redmine authored
60
	threaddata_p->flags		=  flags;
redmine authored
61
	threaddata_p->freefunct_arg	=  free;
redmine authored
62

redmine authored
63
	return 0;
redmine authored
64 65
}

redmine authored
66 67 68 69 70 71 72
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)
) {
redmine authored
73 74 75 76
	if (procfunct == NULL) {
		errno = EINVAL;
		return NULL;
	}
redmine authored
77

redmine authored
78 79 80 81 82 83
	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);
redmine authored
84
		if(errno == EUSERS) {
redmine authored
85
			error("clsync_%s_unix(): Too many connections.", action);
redmine authored
86
			return NULL;
redmine authored
87 88
		}

redmine authored
89 90
		error("clsync_%s_unix(): Cannot socket_%s_unix()",
			action, action);
redmine authored
91
		return NULL;
redmine authored
92 93
	}

redmine authored
94
	socket_sockthreaddata_t *threaddata_p = socket_thread_attach(proc_p->sock_p);
redmine authored
95
	if (threaddata_p == NULL) {
redmine authored
96
		socket_close(proc_p->sock_p);
redmine authored
97
		free(proc_p);
redmine authored
98
		return NULL;
redmine authored
99 100
	}

redmine authored
101
	_clsync_connect_setthreaddata(threaddata_p, proc_p, flags);
redmine authored
102

redmine authored
103
	proc_p->procfunct = procfunct;
redmine authored
104
	socket_thread_start(threaddata_p);
redmine authored
105

redmine authored
106 107 108 109 110 111 112 113 114 115
	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);
redmine authored
116 117 118 119
}