add listen-timeout to function as an accept timeout
[socat/sam.git] / xiosignal.c
blobf41d24dea764301abf672efc87752a7615ec7e59
1 /* source: xiosignal.c */
2 /* Copyright Gerhard Rieger 2001-2003 */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* this file contains code for handling signals (except SIGCHLD) */
7 #include "config.h"
8 #include "xioconfig.h" /* what features are enabled */
10 #include "sysincludes.h"
12 #include "mytypes.h"
13 #include "compat.h"
14 #include "error.h"
16 #include "sycls.h"
19 #define SOCAT_MAXPIDS 4
21 struct socat_sig_desc {
22 int sig_use;
23 pid_t sig_pids[SOCAT_MAXPIDS];
24 } ;
26 #if 0
27 size_t socat_sigint_use; /* how many pids are set in following array */
28 static pid_t socat_sigint_pids[SOCAT_MAXPIDS];
29 size_t socat_sigquit_use; /* how many pids are set in following array */
30 static pid_t socat_sigquit_pids[SOCAT_MAXPIDS];
31 #else
32 static struct socat_sig_desc socat_sighup;
33 static struct socat_sig_desc socat_sigint;
34 static struct socat_sig_desc socat_sigquit;
35 #endif
38 static struct socat_sig_desc *socat_get_sig_desc(int signum) {
39 struct socat_sig_desc *sigdesc;
40 switch (signum) {
41 case SIGHUP: sigdesc = &socat_sighup; break;
42 case SIGINT: sigdesc = &socat_sigint; break;
43 case SIGQUIT: sigdesc = &socat_sigquit; break;
44 default: sigdesc = NULL; break;
46 return sigdesc;
49 /* a signal handler that eventually passes the signal to sub processes */
50 void socatsignalpass(int sig) {
51 int i;
52 struct socat_sig_desc *sigdesc;
54 Debug1("socatsignalpass(%d)", sig);
55 if ((sigdesc = socat_get_sig_desc(sig)) == NULL) {
56 return;
59 for (i=0; i<sigdesc->sig_use; ++i) {
60 if (sigdesc->sig_pids[i]) {
61 if (Kill(sigdesc->sig_pids[i], sig) < 0) {
62 Warn3("kill("F_pid", %d): %s",
63 sigdesc->sig_pids[i], sig, strerror(errno));
67 #if !HAVE_SIGACTION
68 Signal(sig, socatsignalpass);
69 #endif /* !HAVE_SIGACTION */
70 Debug("socatsignalpass() ->");
74 /* register the sub process pid for passing of signals of type signum.
75 Only for SIGHUP, SIGINT, and SIGQUIT!
76 returns 0 on success or <0 if an error occurred */
77 int xio_opt_signal(pid_t pid, int signum) {
78 struct socat_sig_desc *sigdesc;
80 if ((sigdesc = socat_get_sig_desc(signum)) == NULL) {
81 Error("sub process registered for unsupported signal");
82 return -1;
85 if (sigdesc->sig_use >= SOCAT_MAXPIDS) {
86 Error1("too many sub processes registered for signal %d", signum);
87 return -1;
89 if (sigdesc->sig_use == 0) {
90 /* the special signal handler has not been registered yet - do it now */
91 #if HAVE_SIGACTION
92 struct sigaction act;
93 memset(&act, 0, sizeof(struct sigaction));
94 act.sa_flags = SA_RESTART;
95 act.sa_handler = socatsignalpass;
96 if (Sigaction(signum, &act, NULL) < 0) {
97 /*! man does not say that errno is defined */
98 Warn3("sigaction(%d, %p, NULL): %s", signum, &act, strerror(errno));
100 #else
101 Signal(signum, socatsignalpass);
102 #endif /* !HAVE_SIGACTION */
104 sigdesc->sig_pids[sigdesc->sig_use++] = pid;
105 return 0;