clsync
libclsync.c
Go to the documentation of this file.
1 /*
2  libclsyncmgr - clsync control socket API
3 
4  Copyright (C) 2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #define LIBCLSYNC
21 
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h> // for memset
25 #include <sys/un.h> // for "struct sockaddr_un"
26 
27 #include "configuration.h"
28 #include "error.h"
29 #include "libclsync.h"
30 #include "malloc.h"
31 #include "socket.h"
32 
34 {
35  clsyncproc_t *proc_p = arg->arg;
36  clsyncsock_t *clsyncsock_p = proc_p->sock_p;
37  clsyncsock_procfunct_t procfunct = proc_p->procfunct;
38 #ifdef PARANOID
39 
40  if ( procfunct == NULL ) {
41  error ( "procfunct == NULL" );
42  return 0;
43  }
44 
45 #endif
46 
47  if ( procfunct ( arg, sockcmd_p ) )
48  switch ( sockcmd_p->cmd_id ) {
49  default:
50  socket_sendinvalid ( clsyncsock_p, sockcmd_p );
51  break;
52  }
53 
54  return 0;
55 }
56 
57 static inline int _clsync_connect_setthreaddata ( socket_sockthreaddata_t *threaddata_p, clsyncproc_t *proc_p, sockprocflags_t flags )
58 {
59  threaddata_p->procfunct = libproc_procclsyncsock;
60  threaddata_p->clsyncsock_p = proc_p->sock_p;
61  threaddata_p->arg = proc_p;
62  threaddata_p->running = NULL;
63  threaddata_p->authtype = SOCKAUTH_NULL;
64  threaddata_p->flags = flags;
65  threaddata_p->freefunct_arg = free;
66  return 0;
67 }
68 
69 static inline clsyncproc_t *_clsync_x_unix (
70  const char *const socket_path,
71  clsyncsock_procfunct_t procfunct,
72  sockprocflags_t flags,
73  const char *const action,
74  clsyncsock_t * ( *socket_x_unix ) ( const char *const )
75 )
76 {
77  if ( procfunct == NULL ) {
78  errno = EINVAL;
79  return NULL;
80  }
81 
82  clsyncproc_t *proc_p = xmalloc ( sizeof ( *proc_p ) );
83  memset ( proc_p, 0, sizeof ( *proc_p ) );
84  proc_p->sock_p = socket_x_unix ( socket_path );
85 
86  if ( proc_p->sock_p == NULL ) {
87  free ( proc_p );
88 
89  if ( errno == EUSERS ) {
90  error ( "clsync_%s_unix(): Too many connections.", action );
91  return NULL;
92  }
93 
94  error ( "clsync_%s_unix(): Cannot socket_%s_unix()",
95  action, action );
96  return NULL;
97  }
98 
99  socket_sockthreaddata_t *threaddata_p = socket_thread_attach ( proc_p->sock_p );
100 
101  if ( threaddata_p == NULL ) {
102  socket_close ( proc_p->sock_p );
103  free ( proc_p );
104  return NULL;
105  }
106 
107  _clsync_connect_setthreaddata ( threaddata_p, proc_p, flags );
108  proc_p->procfunct = procfunct;
109  socket_thread_start ( threaddata_p );
110  return proc_p;
111 }
112 
113 clsyncproc_t *clsync_listen_unix ( const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags )
114 {
115  return _clsync_x_unix ( socket_path, procfunct, flags, "listen", socket_listen_unix );
116 }
117 
118 
119 clsyncproc_t *clsync_connect_unix ( const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags )
120 {
121  return _clsync_x_unix ( socket_path, procfunct, flags, "connect", socket_connect_unix );
122 }
123 
124 
125 
socket_sockthreaddata
Definition: socket.h:209
clsyncsock_procfunct_t
int(* clsyncsock_procfunct_t)(struct socket_sockthreaddata *, sockcmd_t *)
Definition: socket.h:207
socket_sockthreaddata::clsyncsock_p
clsyncsock_t * clsyncsock_p
Definition: socket.h:213
socket_sockthreaddata::arg
void * arg
Definition: socket.h:214
socket_thread_start
int socket_thread_start(socket_sockthreaddata_t *threaddata_p)
Definition: socket.c:808
_clsync_connect_setthreaddata
static int _clsync_connect_setthreaddata(socket_sockthreaddata_t *threaddata_p, clsyncproc_t *proc_p, sockprocflags_t flags)
Definition: libclsync.c:57
socket_listen_unix
clsyncsock_t * socket_listen_unix(const char *const socket_path)
Definition: socket.c:192
socket_sockthreaddata::flags
sockprocflags_t flags
Definition: socket.h:218
_clsync_x_unix
static 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))
Definition: libclsync.c:69
clsyncproc::sock_p
clsyncsock_t * sock_p
Definition: libclsync.h:23
socket_sockthreaddata::running
int * running
Definition: socket.h:217
libproc_procclsyncsock
int libproc_procclsyncsock(socket_sockthreaddata_t *arg, sockcmd_t *sockcmd_p)
Definition: libclsync.c:33
SOCKAUTH_NULL
@ SOCKAUTH_NULL
Definition: socket.h:201
sockcmd::cmd_id
uint16_t cmd_id
Definition: socket.h:188
error
#define error(...)
Definition: error.h:36
clsyncproc
Definition: libclsync.h:22
error.h
socket_sendinvalid
int socket_sendinvalid(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p)
Definition: socket.c:621
malloc.h
socket_close
int socket_close(clsyncsock_t *clsyncsock_p)
Definition: socket.c:150
libclsync.h
clsync_connect_unix
clsyncproc_t * clsync_connect_unix(const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags)
Definition: libclsync.c:119
socket_sockthreaddata::authtype
sockauth_id_t authtype
Definition: socket.h:216
socket_sockthreaddata::procfunct
clsyncsock_procfunct_t procfunct
Definition: socket.h:211
configuration.h
sockcmd
Definition: socket.h:186
socket.h
sockprocflags_t
enum sockprocflags sockprocflags_t
Definition: socket.h:197
clsyncproc::procfunct
clsyncsock_procfunct_t procfunct
Definition: libclsync.h:24
socket_sockthreaddata::freefunct_arg
freefunct_t freefunct_arg
Definition: socket.h:212
clsync_listen_unix
clsyncproc_t * clsync_listen_unix(const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags)
Definition: libclsync.c:113
clsyncsock
Definition: socket.h:65
socket_thread_attach
socket_sockthreaddata_t * socket_thread_attach(clsyncsock_t *clsyncsock_p)
Definition: socket.c:797
socket_connect_unix
clsyncsock_t * socket_connect_unix(const char *const socket_path)