xref: /PHP-8.2/ext/pcntl/php_signal.c (revision 01b3fc03)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Jason Greene <jason@inetgurus.net>                           |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "TSRM.h"
18 #include "php_signal.h"
19 #include "Zend/zend.h"
20 #include "Zend/zend_signal.h"
21 
22 /* php_signal using sigaction is derived from Advanced Programming
23  * in the Unix Environment by W. Richard Stevens p 298. */
php_signal4(int signo,Sigfunc * func,int restart,int mask_all)24 Sigfunc *php_signal4(int signo, Sigfunc *func, int restart, int mask_all)
25 {
26 	struct sigaction act,oact;
27 
28 #ifdef HAVE_STRUCT_SIGINFO_T
29 	act.sa_sigaction = func;
30 #else
31 	act.sa_handler = func;
32 #endif
33 	if (mask_all) {
34 		sigfillset(&act.sa_mask);
35 	} else {
36 		sigemptyset(&act.sa_mask);
37 	}
38 	act.sa_flags = 0;
39 #ifdef HAVE_STRUCT_SIGINFO_T
40 	act.sa_flags |= SA_SIGINFO;
41 #endif
42 	if (!restart) {
43 #ifdef SA_INTERRUPT
44 		act.sa_flags |= SA_INTERRUPT; /* SunOS */
45 #endif
46 	} else {
47 #ifdef SA_RESTART
48 		act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
49 #endif
50 	}
51 	zend_sigaction(signo, &act, &oact);
52 
53 #ifdef HAVE_STRUCT_SIGINFO_T
54 	return oact.sa_sigaction;
55 #else
56 	return oact.sa_handler;
57 #endif
58 }
59 
php_signal(int signo,Sigfunc * func,int restart)60 Sigfunc *php_signal(int signo, Sigfunc *func, int restart)
61 {
62 	return php_signal4(signo, func, restart, 0);
63 }
64