File: dcap_reconnect.c

package info (click to toggle)
dcap 2.47.12-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,372 kB
  • sloc: ansic: 14,608; makefile: 313; python: 75; sh: 58
file content (292 lines) | stat: -rw-r--r-- 5,359 bytes parent folder | download | duplicates (7)
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
/*
 *   DCAP - dCache Access Protocol client interface
 *
 *   Copyright (C) 2000,2004 DESY Hamburg DMG-Division.
 *
 *   AUTHOR: Tigran Mkrtchayn (tigran.mkrtchyan@desy.de)
 *
 *   This program can be distributed under the terms of the GNU LGPL.
 *   See the file COPYING.LIB
 *
 */


/*
 * $Id: dcap_reconnect.c,v 1.21 2004-11-01 19:33:29 tigran Exp $
 */
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>

#ifdef WIN32
#    include "dcap_unix2win.h"
#    include "dcap_win_poll.h"
#else
#    include <sys/poll.h>
#    include <unistd.h>
#endif

#include "dcap.h"
#include "dcap_functions.h"
#include "dcap_types.h"
#include "dcap_protocol.h"
#include "dcap_poll.h"
#include "socket_nio.h"
#include "sysdep.h"
#include "debug_level.h"
#include "dcap_reconnect.h"

/* Local function prototypes */
static int smart_reconnect(struct vsp_node *, int);
#ifndef WIN32
static void alarm_handler( int sig);
#endif

#ifdef _REENTRANT
static TKEY ioFailedKey;
static TKEY isAlarmKey;
static TKEY sa_alarmKey;

static int ioKeyOnce = 0;
static int alarmKeyOnce = 0;
static int saKeyOnce = 0;
static MUTEX(kLock);

#define isAlarm (*(__isAlarm()))
#define old_sa_alarm (*(__old_sa_alarm()))

/* Reentrant local function prototypes */
static int *__isAlarm();
static struct sigaction *__old_sa_alarm();

int *
__isIOFailed()
{
	int *io;

	m_lock(&kLock);
	if(!ioKeyOnce) {
		t_keycreate(&ioFailedKey, NULL);
		++ioKeyOnce;
	}
	m_unlock(&kLock);

	t_getspecific(ioFailedKey, &io);
	if( io == NULL ) {
		io = calloc(1, sizeof(int));
		t_setspecific(ioFailedKey, (void *)io);
	}

	return io;

}

int *
__isAlarm()
{
	int *al;

	m_lock(&kLock);
	if(!alarmKeyOnce) {
		t_keycreate(&isAlarmKey, NULL);
		++alarmKeyOnce;
	}
	m_unlock(&kLock);

	t_getspecific(isAlarmKey, &al);
	if( al == NULL ) {
		al = calloc(1, sizeof(int));
		t_setspecific(isAlarmKey, (void *)al);
	}

	return al;

}

struct sigaction *
__old_sa_alarm()
{
	struct sigaction *sa;

	m_lock(&kLock);
	if(!saKeyOnce) {
		t_keycreate(&sa_alarmKey, NULL);
		++saKeyOnce;
	}
	m_unlock(&kLock);

	t_getspecific(sa_alarmKey, &sa);
	if( sa == NULL ) {
		sa = calloc(1, sizeof(struct sigaction));
		t_setspecific(sa_alarmKey, (void *)sa);
	}

	return sa;

}

#else
int isIOFailed = 0;
static int isAlarm = 0;
#ifndef WIN32
    static struct sigaction old_sa_alarm;
#endif /* WIN32 */
#endif /* _REENTRANT */

int recover_connection(struct vsp_node * node, int mode)
{
	char fail_message[64];

	fail_message[0] = '\0';
	sprintf(fail_message, "%d 1 client fail\n", node->queueID);
	sendControlMessage(node->fd, fail_message, strlen(fail_message), node->tunnel);

	return smart_reconnect(node, mode);
}



int
smart_reconnect(struct vsp_node * node, int mode)
{

	int old_fd;

	if(node->flags != O_RDONLY) {
		return 1;
	}

	/* user uses data fd as file descriptor and we have to keep it unchanged */
	old_fd = node->dataFd;

	if(data_hello_conversation(node) < 0) {
		dc_debug(DC_ERROR, "[%d] Failed to make a new data connection.", node->dataFd);
		return 1;
	}

	/* try to get old fd in use */
	node->dataFd = dup2(node->dataFd, old_fd);
	if( node->dataFd != old_fd) {
		node->dataFd = old_fd;
		dc_debug(DC_ERROR, "dup2 failed. Reconnection impossible.");
		return 1;
	}

	if(mode && !dc_set_pos(node, mode, -1)) {
		dc_debug(DC_ERROR, "[%d] Failed to set correct position.", node->dataFd);
		return 1;
	}

	dc_debug(DC_INFO, "[%d] Broken connection recovered.", node->dataFd);
	return 0;
}

#ifndef WIN32
void alarm_handler( int sig)
{
	dc_debug(DC_ERROR, "dcap: Last IO operation timeout.");
	isIOFailed = 1;
	isAlarm = 0;
	/* set back old alarm handler */
	sigaction(SIGALRM, &old_sa_alarm, NULL);
}

int dcap_set_alarm(unsigned int t)
{
	struct sigaction sa_alarm;
	struct sigaction *optr;
	struct sigaction *ptr;

	if(t) {
		dc_debug(DC_TRACE, "Setting IO timeout to %d seconds.", t);
		sa_alarm.sa_handler = alarm_handler;
		sigemptyset(&sa_alarm.sa_mask);
		sa_alarm.sa_flags = 0;

		ptr = &sa_alarm;
		optr = &old_sa_alarm;

		/* reset the  error flag */
		isIOFailed = 0;
		/* make a note, that alarm is set */
		isAlarm = 1;

	}else{

		dc_debug(DC_TRACE, "Removing IO timeout handler.");
		/* set the old interrupt handler */

		/* do nothing if alarm not seted */
		if( !isAlarm ) return 0;
		ptr = &old_sa_alarm;
		optr = NULL;
		isAlarm = 0;
	}

	if (sigaction(SIGALRM, ptr, optr) < 0) {
		dc_debug(DC_ERROR,"Sigaction failed!");
		return 1;

	}
	alarm(t);

	return 0;
}

#else
int dcap_set_alarm(unsigned int t)
{
	return 0;
}
#endif /* WIN32 */

int ping_pong(struct vsp_node * node)
{


	char ping[64];
	int len;
	asciiMessage   *aM;
	struct pollfd  pfd;
	int rc;

	ping[0] = '\0';

	len = sprintf(ping, "%d 2 client ping\n", node->queueID);


	setNonBlocking(node->fd);
	rc = sendControlMessage(node->fd, ping, len, node->tunnel);
	clearNonBlocking(node->fd);


	if( rc < 0 ) {
		dc_debug(DC_ERROR, "Ping failed (control line down).");
		return 0;
	}

	pfd.fd = node->fd;
	pfd.events = POLLIN;
	rc = poll(&pfd, 1, 1000*10); /* 10 seconds */

	if((rc == 1) && (pfd.revents & POLLIN )) {

		dcap_set_alarm(10);
		aM = getControlMessage(HAVETO, node);
		dcap_set_alarm(0);

		if( (aM != NULL ) && (aM->type == ASCII_PING) ) {
			free(aM->msg);
			free(aM);
			return 1;
		}
	}
	dc_debug(DC_ERROR, "Ping failed.");
	return 0;

}