OpenDNSSEC-signer  1.3.9
signal.c
Go to the documentation of this file.
1 /*
2  * $Id: signal.c 4349 2011-02-01 10:42:53Z matthijs $
3  *
4  * Copyright (c) 2009 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "config.h"
35 #include "daemon/engine.h"
36 #include "daemon/signal.h"
37 #include "shared/locks.h"
38 #include "shared/log.h"
39 
40 #include <signal.h>
41 
42 static int signal_hup_recvd = 0;
43 static int signal_term_recvd = 0;
44 static engine_type* signal_engine = NULL;
45 static const char* signal_str = "signal";
46 
47 
52 void
54 {
55  signal_engine = (engine_type*) engine;
56 }
57 
58 
63 void
64 signal_handler(sig_atomic_t sig)
65 {
66  switch (sig) {
67  case SIGHUP:
68  ods_log_debug("[%s] SIGHUP received", signal_str);
69  signal_hup_recvd++;
70  if (signal_engine) {
71  lock_basic_lock(&signal_engine->signal_lock);
72  /* [LOCK] signal */
73  lock_basic_alarm(&signal_engine->signal_cond);
74  /* [UNLOCK] signal */
75  lock_basic_unlock(&signal_engine->signal_lock);
76  }
77  break;
78  case SIGTERM:
79  ods_log_debug("[%s] SIGTERM received", signal_str);
80  signal_term_recvd++;
81  if (signal_engine) {
82  lock_basic_lock(&signal_engine->signal_lock);
83  /* [LOCK] signal */
84  lock_basic_alarm(&signal_engine->signal_cond);
85  /* [UNLOCK] signal */
86  lock_basic_unlock(&signal_engine->signal_lock);
87  }
88  break;
89  default:
90  break;
91  }
92  return;
93 }
94 
95 
100 sig_atomic_t
101 signal_capture(sig_atomic_t dflsig)
102 {
103  if (signal_term_recvd) {
104  signal_term_recvd = 0;
105  return SIGNAL_SHUTDOWN;
106  } else if (signal_hup_recvd) {
107  signal_hup_recvd = 0;
108  return SIGNAL_RELOAD;
109  }
110  return dflsig;
111 }