summaryrefslogtreecommitdiffstats
path: root/src/rccexternal.c
blob: 3d1f12fddd34e585c4a3c224ac74c9a672fcd3c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
  LibRCC - module comunicating with rcc-external helper application

  Copyright (C) 2005-2008 Suren A. Chilingaryan <csa@dside.dyndns.org>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License version 2.1 or later
  as published by the Free Software Foundation.

  This library 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 Lesser General Public License 
  for more details.

  You should have received a copy of the GNU Lesser General Public License 
  along with this program; if not, write to the Free Software Foundation, Inc.,
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>

#include "../config.h"

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif /* HAVE_FCNTL_H */
#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif /* HAVE_SIGNAL_H */

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif /* HAVE_SYS_SOCKET_H */
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif /* HAVE_SYS_STAT_H */
#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#endif /* HAVE_SYS_UN_H */
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif /* HAVE_SYS_SELECT_H */
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif /* HAVE_SYS_WAIT_H */

#include "rcchome.h"
#include "rccexternal.h"
#include "internal.h"

#define RCC_EXT_PROG_NAME "rccexternal"
#define RCC_EXTERNAL_TIMEOUT			250 /* 100us */

#ifdef HAVE_UNISTD_H
static pid_t pid = (pid_t)-1;
static char *addr = NULL;
#endif /* HAVE_UNISTD_H */

int rccExternalInit() {
#ifdef HAVE_UNISTD_H
# ifdef HAVE_SIGNAL_H
    struct sigaction act;
# endif /* HAVE_SIGNAL_H */
# ifdef HAVE_SYS_STAT_H
    struct stat st;
# endif /* HAVE_SYS_STAT_H */

    if (pid != (pid_t)-1) return 0;

    if (!addr) {
	addr = (char*)malloc(strlen(rcc_home_dir)+32);
	if (!addr) return -1;
    }

    pid = fork();
    if (pid) {
	if (pid == (pid_t)-1) return -1;
	sprintf(addr,"%s/.rcc/comm/%lu.sock", rcc_home_dir, (unsigned long)pid);

# ifdef HAVE_SIGNAL_H
	act.sa_handler = SIG_IGN;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	sigaction(SIGPIPE,&act,NULL);
# endif /* HAVE_SIGNAL_H */

	return 0;
    }

    /*if ((!stat("../external/" RCC_EXT_PROG_NAME, &st))&&(st.st_mode&S_IXOTH)) {
	execl ("../external/" RCC_EXT_PROG_NAME, RCC_EXT_PROG_NAME, NULL);
    } else*/ 
# ifdef HAVE_SYS_STAT_H
    if ((!stat(LIBRCC_DATA_DIR "/" RCC_EXT_PROG_NAME, &st))&&(st.st_mode&S_IXOTH)) {
# endif /* HAVE_SYS_STAT_H */
	execl(LIBRCC_DATA_DIR "/" RCC_EXT_PROG_NAME, RCC_EXT_PROG_NAME, NULL);
# ifdef HAVE_SYS_STAT_H
    }
# endif /* HAVE_SYS_STAT_H */
#endif /* HAVE_UNISTD_H */
    _exit(1);
}

void rccExternalFree() {
#ifdef HAVE_UNISTD_H
    int retry;
    pid_t res;
    struct timespec timeout = { 0, 5000000 };

    if (pid == (pid_t)-1) return;

    for (retry = 0; retry < 3; retry++) {
	rccExternalConnect(0);    
	
	nanosleep(&timeout, NULL);
	
	res = waitpid(pid, NULL, WNOHANG);
	if (res) break;
	else timeout.tv_nsec*=10;
    }

    pid = (pid_t)-1;
    if (addr) free(addr);
#endif /* HAVE_UNISTD_H */
}

#ifdef HAVE_SYS_SELECT_H
static int rccExternalSetDeadline(struct timeval *tv, unsigned long timeout) {
/*
    gettimeofday(tv, NULL);
    tv->tv_sec += (tv->tv_usec + timeout + RCC_EXTERNAL_TIMEOUT) / 1000000;
    tv->tv_usec = (tv->tv_usec + timeout + RCC_EXTERNAL_TIMEOUT) % 1000000;
*/
    tv->tv_sec = (timeout + RCC_EXTERNAL_TIMEOUT) / 1000000;
    tv->tv_usec = (timeout + RCC_EXTERNAL_TIMEOUT) % 1000000;
    return 0;
}
#endif /* HAVE_SYS_SELECT_H */

size_t rccExternalWrite(int s, const char *buffer, ssize_t size, unsigned long timeout) {
#ifdef HAVE_SYS_SELECT_H
    int err;
    unsigned char connected = 1;
    ssize_t writed, res = 0;
    struct timeval tv;
    fd_set fdcon;

    if (s == -1) return -1;
    
    for (writed = 0; ((writed < size)&&(connected)); writed += connected?res:0) {
	FD_ZERO(&fdcon);
	FD_SET(s, &fdcon);
	rccExternalSetDeadline(&tv, timeout);
	err = select(s+1,NULL,&fdcon,NULL,&tv);
	if (err<=0) connected = 0;
	else {
	    res = write(s, buffer + writed, size - writed);
	    if (res<=0) connected = 0;
	}
    }

    return size - writed;
#else /* HAVE_SYS_SELECT_H */
    return -1;
#endif /* HAVE_SYS_SELECT_H */
}

size_t rccExternalRead(int s, char *buffer, ssize_t size, unsigned long timeout) {
#ifdef HAVE_SYS_SELECT_H
    int err;
    unsigned char connected = 1;
    ssize_t readed, res = 0;
    struct timeval tv;
    fd_set fdcon;
    
    if (s == -1) return -1;
    
    for (readed = 0; ((readed < size)&&(connected)); readed += connected?res:0) {
	FD_ZERO(&fdcon);
	FD_SET(s, &fdcon);
	rccExternalSetDeadline(&tv, timeout);
	err = select(s+1,&fdcon,NULL,NULL,&tv);
	if (err<=0) connected = 0;
	else {
	    res = read(s, buffer + readed, size - readed);
	    if (res<=0) connected = 0;
	}
    }

    return size - readed;
#else /* HAVE_SYS_SELECT_H */
    return -1;
#endif /* HAVE_SYS_SELECT_H */
}

int rccExternalConnect(unsigned char module) {
#ifdef HAVE_SYS_SOCKET_H
    int err;
    int retries = 10;
    int sock;
    int flags;
    struct sockaddr_un mysock;
    struct timeval tv;
    struct timespec ts;
    fd_set fdcon;
    
    if (pid == (pid_t)-1) return -1;
    
    sock = socket(PF_UNIX, SOCK_STREAM, 0);
    if (sock<=0) return -1;

    flags = fcntl(sock,F_GETFL,0);
    if (flags<0) flags = 0;
    if (fcntl(sock,F_SETFL,flags|O_NONBLOCK)<0) {
	close(sock);
	return -1;
    }
        
    memset(&mysock, 0, sizeof(mysock));
    mysock.sun_family=AF_UNIX;
    strncpy(mysock.sun_path,addr,sizeof(mysock.sun_path));
    mysock.sun_path[sizeof(mysock.sun_path)-1]=0;

again:
    if (connect(sock,(struct sockaddr*)&mysock,sizeof(mysock))<0) {
        if (errno == EINPROGRESS) {
	    FD_ZERO(&fdcon);
	    FD_SET(sock, &fdcon);

	    rccExternalSetDeadline(&tv, 0);
	    err = select(sock+1,&fdcon,NULL,NULL,&tv);
	    if (err<=0) {
		close(sock);
		return -1;
	    }
	} else if ((errno == ENOENT)&&(retries)) {
	    ts.tv_sec = (RCC_EXTERNAL_TIMEOUT/10) / 1000000;
	    ts.tv_nsec = ((RCC_EXTERNAL_TIMEOUT/10) % 1000000)*1000;
	    nanosleep(&ts, NULL);
	    retries--;
	    goto again;
	} else {
	    close(sock);
	    return -1;
	}
    }
    
    if (rccExternalWrite(sock, (char*)&module, 1, 0)) {
	close(sock);
	return -1;
    }

    return sock;
#else /* HAVE_SYS_SOCKET_H */
    return -1;
#endif /* HAVE_SYS_SOCKET_H */
}

void rccExternalClose(int s) {
#ifdef HAVE_SYS_SOCKET_H
    unsigned char cmd = 0;
    if (s != -1) {
	write(s, &cmd, 1);
	close(s);
    }
#endif /* HAVE_SYS_SOCKET_H */
}

int rccExternalAllowOfflineMode() {
    int sock;
    int err;
    rcc_external_option opt = RCC_EXTERNAL_OPTION_OFFLINE; 
    unsigned long opt_value = 1;
    
    sock = rccExternalConnect(RCC_EXTERNAL_MODULE_OPTIONS);
    if (sock) {
	err = rccExternalWrite(sock, (char*)&opt, sizeof(rcc_external_option), 0);
	if (!err) err = rccExternalWrite(sock, (char*)&opt_value, sizeof(unsigned long), 0);
	rccExternalClose(sock);
	return err;
    }
    return -1;
}