OpenDNSSEC-signer  1.4.1
locks.c
Go to the documentation of this file.
1 /*
2  * $Id: locks.c 7039 2013-02-15 08:10:15Z 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 "shared/locks.h"
36 #include "shared/log.h"
37 
38 #include <errno.h>
39 #include <signal.h> /* sigfillset(), sigprocmask() */
40 #include <string.h> /* strerror() */
41 #ifdef HAVE_SYS_TIME_H
42 #include <sys/time.h> /* gettimeofday() */
43 #endif
44 #ifdef HAVE_TIME_H
45 #include <time.h> /* gettimeofday() */
46 #endif
47 
48 static const char* lock_str = "lock";
49 
50 #if !defined(HAVE_PTHREAD)
51 #include <sys/wait.h> /* waitpid() */
52 #include <sys/types.h> /* getpid(), waitpid() */
53 #include <unistd.h> /* fork(), getpid() */
54 
55 
64 void
65 ods_thr_fork_create(ods_thread_type* thr, void* (*func)(void*), void* arg)
66 {
67  pid_t pid = fork();
68 
69  switch (pid) {
70  case 0: /* child */
71  *thr = (ods_thread_type)getpid();
72  (void)(*func)(arg);
73  exit(0);
74  case -1: /* error */
75  ods_fatal_exit("[%s] unable to fork thread: %s", lock_str,
76  strerror(errno));
77  default: /* main */
78  *thr = (ods_thread_type)pid;
79  return;
80  }
81  return;
82 }
83 
84 
91 {
92  int status = 0;
93 
94  if (waitpid((pid_t)thread, &status, 0) == -1) {
95  ods_log_error("[%s] waitpid(%d): %s", lock_str, (int)thread,
96  strerror(errno));
97  }
98  if (status != 0) {
99  ods_log_warning("[%s] process %d abnormal exit with status %d",
100  lock_str, (int)thread, status);
101  }
102  return;
103 }
104 #else /* defined(HAVE_PTHREAD) */
105 
106 
107 int
108 ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait)
109 {
110  struct timespec ts;
111  int ret = 0;
112 
113  /* If timeshift is enabled, we don't care about threads. No need
114  * to take the timeshift into account here */
115 
116 #ifndef HAVE_CLOCK_GETTIME
117  struct timeval tv;
118  if (gettimeofday(&tv, NULL) != 0) {
119  ods_log_error("[%s] clock_gettime() error: %s", lock_str,
120  strerror(errno));
121  return 1;
122  }
123  ts.tv_sec = tv.tv_sec;
124  ts.tv_nsec = (tv.tv_usec/1000);
125 #else /* HAVE_CLOCK_GETTIME */
126  if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
127  ods_log_error("[%s] clock_gettime() error: %s", lock_str,
128  strerror(errno));
129  return 1;
130  }
131 #endif /* !HAVE_CLOCK_GETTIME */
132 
133  if (wait > 0) {
134  ts.tv_sec = ts.tv_sec + wait;
135  ret = pthread_cond_timedwait(cond, lock, &ts);
136  } else {
137  ret = pthread_cond_wait(cond, lock);
138  }
139 
140  if (ret == ETIMEDOUT) {
141  return 0;
142  }
143  return ret;
144 }
145 
146 #endif /* defined(HAVE_PTHREAD) */
147 
148 
149 void
151 {
152 #ifndef HAVE_PTHREAD
153  int err = 0;
154 #endif
155  sigset_t sigset;
156  sigfillset(&sigset);
157 
158 #ifndef HAVE_PTHREAD
159  if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL)))
160  ods_fatal_exit("[%s] pthread_sigmask: %s", lock_str, strerror(err));
161 #else /* !HAVE_PTHREAD */
162  /* have nothing, do single process signal mask */
163  if(sigprocmask(SIG_SETMASK, &sigset, NULL) != 0)
164  ods_fatal_exit("[%s] sigprocmask: %s", lock_str, strerror(errno));
165 #endif /* HAVE_PTHREAD */
166 }
void ods_thread_blocksigs(void)
Definition: locks.c:150
void ods_thr_fork_create(ods_thread_type *thr, void *(*func)(void *), void *arg)
Definition: locks.c:65
void ods_fatal_exit(const char *format,...)
Definition: log.c:384
void ods_log_error(const char *format,...)
Definition: log.c:336
pid_t ods_thread_type
Definition: locks.h:102
int lock_basic_type
Definition: locks.h:90
void ods_log_warning(const char *format,...)
Definition: log.c:320
void ods_thr_fork_wait(ods_thread_type thread)
Definition: locks.c:90