OpenDNSSEC-signer  1.4.5
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 
105 int
106 ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait)
107 {
108  struct timespec ts;
109  int ret = 0;
110 
111  /* If timeshift is enabled, we don't care about threads. No need
112  * to take the timeshift into account here */
113 
114 #ifndef HAVE_CLOCK_GETTIME
115  struct timeval tv;
116  if (gettimeofday(&tv, NULL) != 0) {
117  ods_log_error("[%s] clock_gettime() error: %s", lock_str,
118  strerror(errno));
119  return 1;
120  }
121  ts.tv_sec = tv.tv_sec;
122  ts.tv_nsec = (tv.tv_usec/1000);
123 #else /* HAVE_CLOCK_GETTIME */
124  if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
125  ods_log_error("[%s] clock_gettime() error: %s", lock_str,
126  strerror(errno));
127  return 1;
128  }
129 #endif /* !HAVE_CLOCK_GETTIME */
130 
131  if (wait > 0) {
132  ts.tv_sec = ts.tv_sec + wait;
133  ret = pthread_cond_timedwait(cond, lock, &ts);
134  } else {
135  ret = pthread_cond_wait(cond, lock);
136  }
137 
138  if (ret == ETIMEDOUT) {
139  return 0;
140  }
141  return ret;
142 }
143 
144 #endif /* defined(HAVE_PTHREAD) */
145 
146 
147 void
149 {
150 #ifndef HAVE_PTHREAD
151  int err = 0;
152 #endif
153  sigset_t sigset;
154  sigfillset(&sigset);
155 
156 #ifndef HAVE_PTHREAD
157  if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL)))
158  ods_fatal_exit("[%s] pthread_sigmask: %s", lock_str, strerror(err));
159 #else /* !HAVE_PTHREAD */
160  /* have nothing, do single process signal mask */
161  if(sigprocmask(SIG_SETMASK, &sigset, NULL) != 0)
162  ods_fatal_exit("[%s] sigprocmask: %s", lock_str, strerror(errno));
163 #endif /* HAVE_PTHREAD */
164 }