summaryrefslogtreecommitdiffstats
path: root/minislang/slsignal.c
blob: 23863024d6a272bea7d6b7a7039ac3ff90a9ced1 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Copyright (c) 1998 John E. Davis
 * This file is part of the S-Lang library.
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */
#include "config.h"
#include "sl-feat.h"

#include <stdio.h>
#include <signal.h>

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif

#include <errno.h>

#include "slang.h"
#include "_slang.h"

/* Do not trust these environments */
#if defined(__CYGWIN32__) || defined(__MINGW32__)
# ifdef SLANG_POSIX_SIGNALS
#  undef SLANG_POSIX_SIGNALS
# endif
#endif

/* This function will cause system calls to be restarted after signal if possible */
SLSig_Fun_Type *SLsignal (int sig, SLSig_Fun_Type *f)
{
#if defined(SLANG_POSIX_SIGNALS)
   struct sigaction old_sa, new_sa;

# ifdef SIGALRM
   /* We want system calls to be interrupted by SIGALRM. */
   if (sig == SIGALRM) return SLsignal_intr (sig, f);
# endif

   sigemptyset (&new_sa.sa_mask);
   new_sa.sa_handler = f;

   new_sa.sa_flags = 0;
# ifdef SA_RESTART
   new_sa.sa_flags |= SA_RESTART;
# endif

   if (-1 == sigaction (sig, &new_sa, &old_sa))
     return (SLSig_Fun_Type *) SIG_ERR;

   return old_sa.sa_handler;
#else
   /* Not POSIX. */
   return signal (sig, f);
#endif
}

/* This function will NOT cause system calls to be restarted after
 * signal if possible
 */
SLSig_Fun_Type *SLsignal_intr (int sig, SLSig_Fun_Type *f)
{
#ifdef SLANG_POSIX_SIGNALS
   struct sigaction old_sa, new_sa;

   sigemptyset (&new_sa.sa_mask);
   new_sa.sa_handler = f;

   new_sa.sa_flags = 0;
# ifdef SA_INTERRUPT
   new_sa.sa_flags |= SA_INTERRUPT;
# endif

   if (-1 == sigaction (sig, &new_sa, &old_sa))
     return (SLSig_Fun_Type *) SIG_ERR;

   return old_sa.sa_handler;
#else
   /* Not POSIX. */
   return signal (sig, f);
#endif
}

/* We are primarily interested in blocking signals that would cause the
 * application to reset the tty.  These include suspend signals and
 * possibly interrupt signals.
 */
#ifdef SLANG_POSIX_SIGNALS
static sigset_t Old_Signal_Mask;
#endif

static volatile unsigned int Blocked_Depth;

int SLsig_block_signals (void)
{
#ifdef SLANG_POSIX_SIGNALS
   sigset_t new_mask;
#endif

   Blocked_Depth++;
   if (Blocked_Depth != 1)
     {
	return 0;
     }

#ifdef SLANG_POSIX_SIGNALS
   sigemptyset (&new_mask);
# ifdef SIGQUIT
   sigaddset (&new_mask, SIGQUIT);
# endif
# ifdef SIGTSTP
   sigaddset (&new_mask, SIGTSTP);
# endif
# ifdef SIGINT
   sigaddset (&new_mask, SIGINT);
# endif
# ifdef SIGTTIN
   sigaddset (&new_mask, SIGTTIN);
# endif
# ifdef SIGTTOU
   sigaddset (&new_mask, SIGTTOU);
# endif

   (void) sigprocmask (SIG_BLOCK, &new_mask, &Old_Signal_Mask);
   return 0;
#else
   /* Not implemented. */
   return -1;
#endif
}

int SLsig_unblock_signals (void)
{
   if (Blocked_Depth == 0)
     return -1;

   Blocked_Depth--;

   if (Blocked_Depth != 0)
     return 0;

#ifdef SLANG_POSIX_SIGNALS
   (void) sigprocmask (SIG_SETMASK, &Old_Signal_Mask, NULL);
   return 0;
#else
   return -1;
#endif
}


#ifdef MSWINDOWS 
int SLsystem (char *cmd)
{
   SLang_verror (SL_NOT_IMPLEMENTED, "system not implemented");
   return -1;
}

#else
int SLsystem (char *cmd)
{
#ifdef SLANG_POSIX_SIGNALS
   pid_t pid;
   int status;
   struct sigaction ignore;
# ifdef SIGINT
   struct sigaction save_intr;
# endif
# ifdef SIGQUIT
   struct sigaction save_quit;
# endif
# ifdef SIGCHLD
   sigset_t child_mask, save_mask;
# endif

   if (cmd == NULL) return 1;

   ignore.sa_handler = SIG_IGN;
   sigemptyset (&ignore.sa_mask);
   ignore.sa_flags = 0;

# ifdef SIGINT
   if (-1 == sigaction (SIGINT, &ignore, &save_intr))
     return -1;
# endif

# ifdef SIGQUIT
   if (-1 == sigaction (SIGQUIT, &ignore, &save_quit))
     {
	(void) sigaction (SIGINT, &save_intr, NULL);
	return -1;
     }
# endif

# ifdef SIGCHLD
   sigemptyset (&child_mask);
   sigaddset (&child_mask, SIGCHLD);
   if (-1 == sigprocmask (SIG_BLOCK, &child_mask, &save_mask))
     {
#  ifdef SIGINT
	(void) sigaction (SIGINT, &save_intr, NULL);
#  endif
#  ifdef SIGQUIT
	(void) sigaction (SIGQUIT, &save_quit, NULL);
#  endif
	return -1;
     }
# endif

   pid = fork();

   if (pid == -1)
     status = -1;
   else if (pid == 0)
     {
	/* Child */
# ifdef SIGINT
	(void) sigaction (SIGINT, &save_intr, NULL);
# endif
# ifdef SIGQUIT
	(void) sigaction (SIGQUIT, &save_quit, NULL);
# endif
# ifdef SIGCHLD
	(void) sigprocmask (SIG_SETMASK, &save_mask, NULL);
# endif

	execl ("/bin/sh", "sh", "-c", cmd, NULL);
	_exit (127);
     }
   else
     {
	/* parent */
	while (-1 == waitpid (pid, &status, 0))
	  {
# ifdef EINTR
	     if (errno == EINTR)
	       continue;
# endif
# ifdef ERESTARTSYS
	     if (errno == ERESTARTSYS)
	       continue;
# endif
	     status = -1;
	     break;
	  }
     }
# ifdef SIGINT
   if (-1 == sigaction (SIGINT, &save_intr, NULL))
     status = -1;
# endif
# ifdef SIGQUIT
   if (-1 == sigaction (SIGQUIT, &save_quit, NULL))
     status = -1;
# endif
# ifdef SIGCHLD
   if (-1 == sigprocmask (SIG_SETMASK, &save_mask, NULL))
     status = -1;
# endif

   return status;

#else				       /* No POSIX Signals */
# ifdef SIGINT
   void (*sint)(int);
# endif
# ifdef SIGQUIT
   void (*squit)(int);
# endif
   int status;

# ifdef SIGQUIT
   squit = SLsignal (SIGQUIT, SIG_IGN);
# endif
# ifdef SIGINT
   sint = SLsignal (SIGINT, SIG_IGN);
# endif
   status = system (cmd);
# ifdef SIGINT
   SLsignal (SIGINT, sint);
# endif
# ifdef SIGQUIT
   SLsignal (SIGQUIT, squit);
# endif
   return status;
#endif				       /* POSIX_SIGNALS */
}
#endif


#if 0
#include <windows.h>
static int msw_system (char *cmd)
{
   STARTUPINFO startup_info;
   PROCESS_INFORMATION process_info;
   int status;

   if (cmd == NULL) return -1;

   memset ((char *) &startup_info, 0, sizeof (STARTUPINFO));
   startup_info.cb = sizeof(STARTUPINFO);
   startup_info.dwFlags = STARTF_USESHOWWINDOW;
   startup_info.wShowWindow = SW_SHOWDEFAULT;

   if (FALSE == CreateProcess (NULL, 
			       cmd, 
			       NULL,
			       NULL,
			       FALSE,
			       NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE,
			       NULL,
			       NULL,
			       &startup_info,
			       &process_info))
     {
	SLang_verror (0, "%s: CreateProcess failed.", cmd);
	return -1;
     }
   
   
   status = -1;

   if (0xFFFFFFFFUL != WaitForSingleObject (process_info.hProcess, INFINITE))
     {
	DWORD exit_code;
	
	if (TRUE == GetExitCodeProcess (process_info.hProcess, &exit_code))
	  status = (int) exit_code;
     }

   CloseHandle (process_info.hThread);
   CloseHandle (process_info.hProcess);
   
   return status;
}
#endif