summaryrefslogtreecommitdiff
path: root/web/src/alpined.d/signal.c
blob: 52d8e600fa47a79bea46c73c7ebbf4fee30b6de2 (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
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: signal.c 91 2006-07-28 19:02:07Z mikes@u.washington.edu $";
#endif

/* ========================================================================
 * Copyright 2006-2008 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

#include <system.h>
#include <general.h>

#include "../../../c-client/c-client.h"

#include "../../../pith/conf.h"
#include "../../../pith/status.h"
#include "../../../pith/signal.h"
#include "../../../pith/debug.h"
#include "../../../pith/adrbklib.h"
#include "../../../pith/remote.h"
#include "../../../pith/imap.h"

#include "alpined.h"
#include "debug.h"


static  int cleanup_called_from_sig_handler;

static	RETSIGTYPE	hup_signal(int);
static	RETSIGTYPE	term_signal(int);
static	RETSIGTYPE	auger_in_signal(int);
int			fast_clean_up();
void			end_signals(int);
#if	defined(DEBUG) && defined(SIGUSR1) && defined(SIGUSR2)
static	RETSIGTYPE	usr1_signal(int);
static	RETSIGTYPE	usr2_signal(int);
#endif


/*----------------------------------------------------------------------
    Install handlers for all the signals we care to catch
  ----------------------------------------------------------------------*/
void
init_signals(void)
{
    /* prepare for unexpected exit */
    signal(SIGHUP,  hup_signal);
    signal(SIGTERM, term_signal);

    /* prepare for unforseen problems */
    signal(SIGILL,  auger_in_signal);
    signal(SIGTRAP, auger_in_signal);
#ifdef	SIGEMT
    signal(SIGEMT,  auger_in_signal);
#endif
    signal(SIGBUS,  auger_in_signal);
    signal(SIGSEGV, auger_in_signal);
    signal(SIGSYS,  auger_in_signal);

#if	defined(DEBUG) && defined(SIGUSR1) && defined(SIGUSR2)
    /* Set up SIGUSR2 to {in,de}crement debug level */
    signal(SIGUSR1, usr1_signal);
    signal(SIGUSR2, usr2_signal);
#endif
}


/*----------------------------------------------------------------------
      handle hang up signal -- SIGHUP

Not much to do. Rely on periodic mail file check pointing.
  ----------------------------------------------------------------------*/
static RETSIGTYPE
hup_signal(int sig)
{
    end_signals(1);
    cleanup_called_from_sig_handler = 1;

    while(!fast_clean_up())
      sleep(1);

    if(peSocketName)		/* clean up unix domain socket */
      (void) unlink(peSocketName);

    exceptional_exit("SIGHUP received", 0);
}


/*----------------------------------------------------------------------
      handle terminate signal -- SIGTERM

Not much to do. Rely on periodic mail file check pointing.
  ----------------------------------------------------------------------*/
static RETSIGTYPE
term_signal(int sig)
{
    end_signals(1);
    cleanup_called_from_sig_handler = 1;

    while(!fast_clean_up())
      sleep(1);

    if(peSocketName)		/* clean up unix domain socket */
      (void) unlink(peSocketName);

    exceptional_exit("SIGTERM received", 0);
}


/*----------------------------------------------------------------------
     Handle signals caused by aborts -- SIGSEGV, SIGILL, etc

Call panic which cleans up tty modes and then core dumps
  ----------------------------------------------------------------------*/
static RETSIGTYPE
auger_in_signal(int sig)
{
    end_signals(1);

    if(peSocketName)		/* clean up unix domain socket */
      (void) unlink(peSocketName);

    snprintf(tmp_20k_buf, SIZEOF_20KBUF, "Abort: signal %d", sig);
    alpine_panic(tmp_20k_buf);		/* clean up and get out */
    exit(-1);			/* in case panic doesn't kill us */
}


/*----------------------------------------------------------------------
     Handle cleaning up mail streams and tty modes...
Not much to do. Rely on periodic mail file check pointing.  Don't try
cleaning up screen or flushing output since stdout is likely already
gone.  To be safe, though, we'll at least restore the original tty mode.
Also delete any remnant _DATAFILE_ from sending-filters.
  ----------------------------------------------------------------------*/
int
fast_clean_up(void)
{
    int         i;
    MAILSTREAM *m;

    if(ps_global->expunge_in_progress)
      return(0);

    /*
     * This gets rid of temporary cache files for remote addrbooks.
     */
    completely_done_with_adrbks();

    /*
     * This flushes out deferred changes and gets rid of temporary cache
     * files for remote config files.
     */
    if(ps_global->prc){
	if(ps_global->prc->outstanding_pinerc_changes)
	  write_pinerc(ps_global, Main,
		       cleanup_called_from_sig_handler ? WRP_NOUSER : WRP_NONE);

	if(ps_global->prc->rd)
	  rd_close_remdata(&ps_global->prc->rd);
	
	free_pinerc_s(&ps_global->prc);
    }

    /* as does this */
    if(ps_global->post_prc){
	if(ps_global->post_prc->outstanding_pinerc_changes)
	  write_pinerc(ps_global, Post,
		       cleanup_called_from_sig_handler ? WRP_NOUSER : WRP_NONE);

	if(ps_global->post_prc->rd)
	  rd_close_remdata(&ps_global->post_prc->rd);
	
	free_pinerc_s(&ps_global->post_prc);
    }

    /*
     * Can't figure out why this section is inside the ifdef, but no
     * harm leaving it that way, I guess.
     */
#if !defined(DOS) && !defined(OS2)
    for(i = 0; i < ps_global->s_pool.nstream; i++){
	m = ps_global->s_pool.streams[i];
	if(m && !m->lock)
	  pine_mail_actually_close(m);
    }
#endif	/* !DOS */

    imap_flush_passwd_cache(TRUE);

    if(peSocketName)		/* clean up unix domain socket */
      (void) unlink(peSocketName);

    dprint((1, "done with fast_clean_up\n"));
    return(1);
}



/*----------------------------------------------------------------------
    Return all signal handling back to normal
  ----------------------------------------------------------------------*/
void
end_signals(int blockem)
{
#ifndef	SIG_ERR
#define	SIG_ERR	(RETSIGTYPE (*)())-1
#endif
    if(signal(SIGILL,  blockem ? SIG_IGN : SIG_DFL) != SIG_ERR){
	signal(SIGTRAP, blockem ? SIG_IGN : SIG_DFL);
#ifdef	SIGEMT
	signal(SIGEMT,  blockem ? SIG_IGN : SIG_DFL);
#endif
	signal(SIGBUS,  blockem ? SIG_IGN : SIG_DFL);
	signal(SIGSEGV, blockem ? SIG_IGN : SIG_DFL);
	signal(SIGSYS,  blockem ? SIG_IGN : SIG_DFL);
	signal(SIGHUP,  blockem ? SIG_IGN : SIG_DFL);
	signal(SIGTERM, blockem ? SIG_IGN : SIG_DFL);
	signal(SIGINT,  blockem ? SIG_IGN : SIG_DFL);
    }
}


#if	defined(DEBUG) && defined(SIGUSR1) && defined(SIGUSR2)
/*----------------------------------------------------------------------
      handle -- SIGUSR1

  Increment debug level
  ----------------------------------------------------------------------*/
static RETSIGTYPE
usr1_signal(int sig)
{
    if(debug < 11)
      debug++;

    setup_imap_debug();

    dprint((SYSDBG_INFO, "Debug level now %d", debug));
}

/*----------------------------------------------------------------------
      handle -- SIGUSR2

  Decrement debug level
  ----------------------------------------------------------------------*/
static RETSIGTYPE
usr2_signal(int sig)
{
    if(debug > 0)
      debug--;

    setup_imap_debug();

    dprint((SYSDBG_INFO, "Debug level now %d", debug));

}
#endif


/*
 * Command interrupt support.
 */
int
intr_handling_on(void)
{
    return 0;
}


void
intr_handling_off(void)
{
}