File: ftpsigs.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,248 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (246 lines) | stat: -rw-r--r-- 5,261 bytes parent folder | download | duplicates (5)
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
/* $Id: ftpsigs.c,v 1.5 2001/05/28 09:53:44 mhe Exp $
 *
 * ftpsigs.c -- handles signals
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * 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 2 of the License, or
 * (at your option) any later version. See COPYING for more details.
 */

#include "syshdr.h"
#include "ftp.h"
#include "ftpsigs.h"
#include "gvars.h"
#include "transfer.h"

/* in cmd.c */
void exit_yafc(void);

void ftp_set_signal_with_mask(int signum, sighandler_t handler, int onesig)
{
	int saved_errno = errno;
#ifdef HAVE_POSIX_SIGNALS
	struct sigaction sa;
	sigset_t ss;

	/* unblock this signal */
	sigemptyset(&ss);
	sigaddset(&ss, signum);
	sigprocmask(SIG_UNBLOCK, &ss, 0);

	/* set the signal handler */
	sa.sa_handler = handler;
	sigemptyset(&sa.sa_mask);
	if(onesig)
		sigaddset(&sa.sa_mask, onesig); /* a signal to exclude */
	sa.sa_flags = 0;
#ifdef SA_RESTART
	sa.sa_flags |= SA_RESTART;
#endif

# if 0 /* this causes segfaults!? */
	if(signum == SIGALRM) {
#  ifdef SA_INTERRUPT
		sa.sa_flags |= SA_INTERRUPT; /* old, obsolete flag? */
#  endif
	} else {
#  ifdef SA_RESTART
		sa.sa_flags |= SA_RESTART;
#  endif
	}
# endif

	/*	sa.sa_restorer = 0;*/

	if(sigaction(signum, &sa, 0) != 0)
		ftp_trace("couldn't set signalhandler for signal %d\n", signum);
#else
	signal(signum, handler);
#endif
	errno = saved_errno;
}

void ftp_set_signal(int signum, sighandler_t handler)
{
	ftp_set_signal_with_mask(signum, handler, 0);
}

static unsigned int sigints = 0;

RETSIGTYPE sigint_close_handler(int signum)
{
	if(gvSighupReceived)
		return;

	ftp_trace("Interrupt received\n");
	sigints++;

	gvInterrupted = true;

	/* close connection and restart command loop after 4 interrupts
	 *
	 * If we're in SSH mode, the interrupt signal is intercepted by
	 * the ssh program as well, which terminates directly. The ssh
	 * program is started by ssh_connect() in ssh_ftp.c by forking and
	 * execv'ing the program in the child process. I don't know if
	 * there is a way to prevent the interrupt signal to reach the ssh
	 * program?
	 */

	if(sigints >= 4 || ftp->ssh_pid) {
		ftp_close();
		sigints = 0;
		if(gvJmpBufSet) {
			ftp_trace("jumping to gvRestartJmp\n");
			ftp_longjmp(gvRestartJmp, 1);
		} else {
			exit(17);
		}
	}
	else if(sigints == 3) {
		if(gvJmpBufSet)
			ftp_err(_("OK, one more to close connection...          \n"));
		else
			ftp_err(_("OK, one more to exit program...          \n"));
	}

	/* disable any alarm set */
	alarm(0);
	ftp_set_signal(SIGALRM, SIG_DFL);

	/* re-install the signal handler */
/*	ftp_set_signal(SIGINT, sigint_close_handler);*/
}

RETSIGTYPE sigint_abort_handler(int signum)
{
	if(gvSighupReceived)
		return;

	ftp_trace("Interrupt received\n");
	sigints++;

	gvInterrupted = true;

	if(sigints >= 3 || ftp->ssh_pid) {
		sigints = 0;
		alarm(0);
		if(gvJmpBufSet) {
			ftp_trace("jumping to gvRestartJmp\n");
			ftp_flush_reply();
			ftp_longjmp(gvRestartJmp, 1);
		} else
			exit(17);
	} else if(sigints == 2)
		ftp_err(_("OK, one more to abort command...          \n"));

	/* re-install the signal handler */
/*	ftp_set_signal(SIGINT, sigint_abort_handler);*/
}

RETSIGTYPE sigint_jmp_handler(int signum)
{
	if(gvSighupReceived)
		return;

	ftp_trace("Interrupt received         \n");
	alarm(0);
	if(gvJmpBufSet) {
		ftp_trace("jumping to gvRestartJmp\n");
		if(ftp->ssh_pid)
			ftp_close();
		ftp_longjmp(gvRestartJmp, 1);
	} else {
		ftp_err(_("Interrupt received, exiting...         \n"));
		exit(17);
	}
}

#if 0
static RETSIGTYPE sighup_term_handler(int signum)
{
	printf(_("SIGTERM (terminate) received, exiting...\n"));
	ftp_close();
	ftp_destroy(gvFtp);
	gvars_destroy();
	exit(19);
}
#endif

RETSIGTYPE sighup_handler(int signum)
{
	ftp_set_signal(SIGINT, SIG_IGN);
	ftp_set_signal(SIGHUP, sighup_handler);
	if(gvSighupReceived)
		return;

	gvSighupReceived = true;
	if(gvInTransfer)
		ftp_err(_("Hangup received, continuing transfer in background...\n"));
	else {
		ftp_err(_("Hangup received, exiting...\n"));
		exit_yafc();
	}

	if(transfer_init_nohup(0) != 0) {
		if(transfer_init_nohup("/dev/null") != 0) {
			ftp_err(_("Can't redirect output, exiting...\n"));
			ftp_quit();
			exit(18);
		}
	}

	transfer_begin_nohup(0, 0);
	sigints = 0;
}

int ftp_longjmp(
#ifdef HAVE_POSIX_SIGSETJMP
				sigjmp_buf restart_jmp
#else
				jmp_buf restart_jmp
#endif
				, int arg)
{
#ifdef HAVE_POSIX_SIGSETJMP
	siglongjmp(restart_jmp, 1);
#else
	longjmp(restart_jmp, 1);
#endif
	return 0; /* make cc happy */
}

void ftp_initsigs(void)
{
	sigints = 0;
	ftp_set_signal(SIGPIPE, SIG_IGN);
	ftp_set_signal(SIGINT, gvSighupReceived ? SIG_IGN : sigint_jmp_handler);
	ftp_set_signal_with_mask(SIGHUP, sighup_handler, SIGINT);
}

void ftp_sigint_close_reset(void)
{
	sigints = 0;
	ftp_initsigs();
}

void ftp_set_close_handler(void)
{
	sigints = 0;
	ftp_set_signal(SIGINT, gvSighupReceived ? SIG_IGN : sigint_close_handler);
}

void ftp_set_abort_handler(void)
{
	sigints = 0;
	ftp_set_signal(SIGINT, gvSighupReceived ? SIG_IGN : sigint_abort_handler);
}

unsigned int ftp_sigints(void)
{
	return sigints;
}