1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <libtest/common.h>
40 #include <semaphore.h>
43 #include <libtest/signal.h>
45 using namespace libtest
;
54 sigaddset(&set
, SIGQUIT
);
55 sigaddset(&set
, SIGINT
);
57 sem_init(&lock
, 0, 0);
62 assert(sigismember(&set
, SIGQUIT
));
63 assert(sigismember(&set
, SIGINT
));
68 return sigwait(&set
, &sig
);
77 static volatile shutdown_t __shutdown
;
78 static pthread_mutex_t shutdown_mutex
;
79 static pthread_t thread
;
84 pthread_mutex_lock(&shutdown_mutex
);
85 ret
= bool(__shutdown
!= SHUTDOWN_RUNNING
);
86 pthread_mutex_unlock(&shutdown_mutex
);
91 void set_shutdown(shutdown_t arg
)
93 pthread_mutex_lock(&shutdown_mutex
);
95 pthread_mutex_unlock(&shutdown_mutex
);
97 if (arg
== SHUTDOWN_GRACEFUL
)
99 pthread_kill(thread
, SIGQUIT
);
102 pthread_join(thread
, &retval
);
106 shutdown_t
get_shutdown()
109 pthread_mutex_lock(&shutdown_mutex
);
111 pthread_mutex_unlock(&shutdown_mutex
);
118 static void *sig_thread(void *arg
)
120 context_st
*context
= (context_st
*)arg
;
124 sem_post(&context
->lock
);
126 while (get_shutdown() == SHUTDOWN_RUNNING
)
130 if (context
->wait(sig
) == -1)
132 Error
<< "sigwait() returned errno:" << strerror(errno
);
140 if (is_shutdown() == false)
142 Error
<< "Signal handling thread got signal " << strsignal(sig
);
143 set_shutdown(SHUTDOWN_FORCED
);
148 Error
<< "Signal handling thread got unexpected signal " << strsignal(sig
);
162 pthread_mutex_init(&shutdown_mutex
, NULL
);
163 set_shutdown(SHUTDOWN_RUNNING
);
165 context_st
*context
= new context_st
;
170 sigemptyset(&old_set
);
171 pthread_sigmask(SIG_BLOCK
, NULL
, &old_set
);
173 if (sigismember(&old_set
, SIGQUIT
))
175 Error
<< strsignal(SIGQUIT
) << " has been previously set.";
177 if (sigismember(&old_set
, SIGINT
))
179 Error
<< strsignal(SIGINT
) << " has been previously set.";
183 if ((error
= pthread_sigmask(SIG_BLOCK
, &context
->set
, NULL
)) != 0)
185 Error
<< "pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")";
189 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, (void *) &context
->set
)) != 0)
191 Error
<< "pthread_create() died during pthread_create(" << strerror(error
) << ")";
195 sem_wait(&context
->lock
);