1da2e3ebchin/***********************************************************************
2da2e3ebchin*                                                                      *
3da2e3ebchin*               This software is part of the ast package               *
43e14f97Roger A. Faulkner*          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebchin*                      and is licensed under the                       *
6da2e3ebchin*                  Common Public License, Version 1.0                  *
77c2fbfbApril Chin*                    by AT&T Intellectual Property                     *
8da2e3ebchin*                                                                      *
9da2e3ebchin*                A copy of the License is available at                 *
10da2e3ebchin*            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebchin*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebchin*                                                                      *
13da2e3ebchin*              Information and Software Systems Research               *
14da2e3ebchin*                            AT&T Research                             *
15da2e3ebchin*                           Florham Park NJ                            *
16da2e3ebchin*                                                                      *
17da2e3ebchin*                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebchin*                  David Korn <dgk@research.att.com>                   *
19da2e3ebchin*                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebchin*                                                                      *
21da2e3ebchin***********************************************************************/
22da2e3ebchin#pragma prototyped
23da2e3ebchin
24da2e3ebchin/*
25da2e3ebchin * signal that disables syscall restart on interrupt with clear signal mask
26da2e3ebchin * fun==SIG_DFL also unblocks signal
27da2e3ebchin */
28da2e3ebchin
29da2e3ebchin#if !_UWIN
30da2e3ebchin
31da2e3ebchin#undef	signal
32da2e3ebchin#define signal		______signal
33da2e3ebchin
34da2e3ebchin#endif
35da2e3ebchin
36da2e3ebchin#include <ast.h>
37da2e3ebchin#include <sig.h>
38da2e3ebchin
39da2e3ebchin#if !_UWIN
40da2e3ebchin
41da2e3ebchin#undef	signal
42da2e3ebchin
43da2e3ebchin#undef	_def_map_ast
44da2e3ebchin#include <ast_map.h>
45da2e3ebchin
46da2e3ebchin#if defined(__EXPORT__)
47da2e3ebchin#define extern	__EXPORT__
48da2e3ebchin#endif
49da2e3ebchin
50da2e3ebchin#endif
51da2e3ebchin
52da2e3ebchin#if defined(SV_ABORT)
53da2e3ebchin#undef	SV_INTERRUPT
54da2e3ebchin#define SV_INTERRUPT	SV_ABORT
55da2e3ebchin#endif
56da2e3ebchin
57da2e3ebchin#if !_std_signal && (_lib_sigaction && defined(SA_NOCLDSTOP) || _lib_sigvec && defined(SV_INTERRUPT))
58da2e3ebchin
59da2e3ebchin#if !defined(SA_NOCLDSTOP) || !defined(SA_INTERRUPT) && defined(SV_INTERRUPT)
60da2e3ebchin#undef	SA_INTERRUPT
61da2e3ebchin#define SA_INTERRUPT	SV_INTERRUPT
62da2e3ebchin#undef	sigaction
63da2e3ebchin#define sigaction	sigvec
64da2e3ebchin#undef	sigemptyset
65da2e3ebchin#define sigemptyset(p)	(*(p)=0)
66da2e3ebchin#undef	sa_flags
67da2e3ebchin#define sa_flags	sv_flags
68da2e3ebchin#undef	sa_handler
69da2e3ebchin#define sa_handler	sv_handler
70da2e3ebchin#undef	sa_mask
71da2e3ebchin#define	sa_mask		sv_mask
72da2e3ebchin#endif
73da2e3ebchin
74da2e3ebchinextern Sig_handler_t
75da2e3ebchinsignal(int sig, Sig_handler_t fun)
76da2e3ebchin{
77da2e3ebchin	struct sigaction	na;
78da2e3ebchin	struct sigaction	oa;
797c2fbfbApril Chin	int			unblock;
80da2e3ebchin#ifdef SIGNO_MASK
81da2e3ebchin	unsigned int		flags;
82da2e3ebchin#endif
83da2e3ebchin
847c2fbfbApril Chin	if (sig < 0)
857c2fbfbApril Chin	{
867c2fbfbApril Chin		sig = -sig;
877c2fbfbApril Chin		unblock = 0;
887c2fbfbApril Chin	}
897c2fbfbApril Chin	else
907c2fbfbApril Chin		unblock = fun == SIG_DFL;
91da2e3ebchin#ifdef SIGNO_MASK
92da2e3ebchin	flags = sig & ~SIGNO_MASK;
93da2e3ebchin	sig &= SIGNO_MASK;
94da2e3ebchin#endif
95da2e3ebchin	memzero(&na, sizeof(na));
96da2e3ebchin	na.sa_handler = fun;
97da2e3ebchin#if defined(SA_INTERRUPT) || defined(SA_RESTART)
98da2e3ebchin	switch (sig)
99da2e3ebchin	{
100da2e3ebchin#if defined(SIGIO) || defined(SIGTSTP) || defined(SIGTTIN) || defined(SIGTTOU)
101da2e3ebchin#if defined(SIGIO)
102da2e3ebchin	case SIGIO:
103da2e3ebchin#endif
104da2e3ebchin#if defined(SIGTSTP)
105da2e3ebchin	case SIGTSTP:
106da2e3ebchin#endif
107da2e3ebchin#if defined(SIGTTIN)
108da2e3ebchin	case SIGTTIN:
109da2e3ebchin#endif
110da2e3ebchin#if defined(SIGTTOU)
111da2e3ebchin	case SIGTTOU:
112da2e3ebchin#endif
113da2e3ebchin#if defined(SA_RESTART)
114da2e3ebchin		na.sa_flags = SA_RESTART;
115da2e3ebchin#endif
116da2e3ebchin		break;
117da2e3ebchin#endif
118da2e3ebchin	default:
119da2e3ebchin#if defined(SA_INTERRUPT)
120da2e3ebchin		na.sa_flags = SA_INTERRUPT;
121da2e3ebchin#endif
122da2e3ebchin		break;
123da2e3ebchin	}
124da2e3ebchin#endif
1257c2fbfbApril Chin	if (sigaction(sig, &na, &oa))
1267c2fbfbApril Chin		return 0;
1277c2fbfbApril Chin	if (unblock)
1287c2fbfbApril Chin		sigunblock(sig);
1297c2fbfbApril Chin	return oa.sa_handler;
130da2e3ebchin}
131da2e3ebchin
132da2e3ebchin#else
133da2e3ebchin
134da2e3ebchinNoN(signal)
135da2e3ebchin
136da2e3ebchin#endif
137