OpenDNSSEC-signer  1.4.10
locks.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
32 #include "config.h"
33 #include "shared/locks.h"
34 #include "shared/log.h"
35 
36 #include <errno.h>
37 #include <signal.h> /* sigfillset(), sigprocmask() */
38 #include <string.h> /* strerror() */
39 #ifdef HAVE_SYS_TIME_H
40 #include <sys/time.h> /* gettimeofday() */
41 #endif
42 #ifdef HAVE_TIME_H
43 #include <time.h> /* gettimeofday() */
44 #endif
45 
46 static const char* lock_str = "lock";
47 
48 #if !defined(HAVE_PTHREAD)
49 #include <sys/wait.h> /* waitpid() */
50 #include <sys/types.h> /* getpid(), waitpid() */
51 #include <unistd.h> /* fork(), getpid() */
52 
53 
62 void
63 ods_thr_fork_create(ods_thread_type* thr, void* (*func)(void*), void* arg)
64 {
65  pid_t pid = fork();
66 
67  switch (pid) {
68  case 0: /* child */
69  *thr = (ods_thread_type)getpid();
70  (void)(*func)(arg);
71  exit(0);
72  case -1: /* error */
73  ods_fatal_exit("[%s] unable to fork thread: %s", lock_str,
74  strerror(errno));
75  default: /* main */
76  *thr = (ods_thread_type)pid;
77  return;
78  }
79  return;
80 }
81 
82 
89 {
90  int status = 0;
91 
92  if (waitpid((pid_t)thread, &status, 0) == -1) {
93  ods_log_error("[%s] waitpid(%d): %s", lock_str, (int)thread,
94  strerror(errno));
95  }
96  if (status != 0) {
97  ods_log_warning("[%s] process %d abnormal exit with status %d",
98  lock_str, (int)thread, status);
99  }
100  return;
101 }
102 #else /* defined(HAVE_PTHREAD) */
103 
104 int
105 ods_thread_create(pthread_t *thr, void *(*func)(void *), void *arg)
106 {
107  int ret, attr_set;
108  pthread_attr_t attr;
109  size_t stacksize;
110 
111  attr_set = (
112  !pthread_attr_init(&attr)
113  && !pthread_attr_getstacksize(&attr, &stacksize)
114  && stacksize < ODS_MINIMUM_STACKSIZE
115  && !pthread_attr_setstacksize(&attr, ODS_MINIMUM_STACKSIZE)
116  );
117 
118  ret = pthread_create(thr, attr_set?&attr:NULL, func, arg);
119  if (attr_set)
120  (void) pthread_attr_destroy(&attr);
121 
122  if ( ret != 0) {
123  ods_log_error("%s at %d could not pthread_create(thr, &attr, func, arg): %s",
124  __FILE__, __LINE__, strerror(ret));
125  }
126 
127  return ret;
128 }
129 
130 int
131 ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait)
132 {
133  struct timespec ts;
134  int ret = 0;
135 
136  /* If timeshift is enabled, we don't care about threads. No need
137  * to take the timeshift into account here */
138 
139 #ifndef HAVE_CLOCK_GETTIME
140  struct timeval tv;
141  if (gettimeofday(&tv, NULL) != 0) {
142  ods_log_error("[%s] clock_gettime() error: %s", lock_str,
143  strerror(errno));
144  return 1;
145  }
146  ts.tv_sec = tv.tv_sec;
147  ts.tv_nsec = (tv.tv_usec/1000);
148 #else /* HAVE_CLOCK_GETTIME */
149  if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
150  ods_log_error("[%s] clock_gettime() error: %s", lock_str,
151  strerror(errno));
152  return 1;
153  }
154 #endif /* !HAVE_CLOCK_GETTIME */
155 
156  if (wait > 0) {
157  ts.tv_sec = ts.tv_sec + wait;
158  ret = pthread_cond_timedwait(cond, lock, &ts);
159  } else {
160  ret = pthread_cond_wait(cond, lock);
161  }
162 
163  if (ret == ETIMEDOUT) {
164  return 0;
165  }
166  return ret;
167 }
168 
169 #endif /* defined(HAVE_PTHREAD) */
170 
171 
172 void
174 {
175 #ifdef HAVE_PTHREAD
176  int err = 0;
177 #endif
178  sigset_t sigset;
179  sigfillset(&sigset);
180 
181 #ifdef HAVE_PTHREAD
182  if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL)))
183  ods_fatal_exit("[%s] pthread_sigmask: %s", lock_str, strerror(err));
184 #else /* !HAVE_PTHREAD */
185  /* have nothing, do single process signal mask */
186  if(sigprocmask(SIG_SETMASK, &sigset, NULL) != 0)
187  ods_fatal_exit("[%s] sigprocmask: %s", lock_str, strerror(errno));
188 #endif /* HAVE_PTHREAD */
189 }
void ods_thread_blocksigs(void)
Definition: locks.c:173
void ods_thr_fork_create(ods_thread_type *thr, void *(*func)(void *), void *arg)
Definition: locks.c:63
void ods_fatal_exit(const char *format,...)
Definition: log.c:382
void ods_log_error(const char *format,...)
Definition: log.c:334
pid_t ods_thread_type
Definition: locks.h:103
int lock_basic_type
Definition: locks.h:91
#define ods_thread_create(thr, func, arg)
Definition: locks.h:104
void ods_log_warning(const char *format,...)
Definition: log.c:318
void ods_thr_fork_wait(ods_thread_type thread)
Definition: locks.c:88