OpenDNSSEC-signer 1.3.0
|
00001 /* 00002 * $Id: locks.c 5320 2011-07-12 10:42:26Z jakob $ 00003 * 00004 * Copyright (c) 2009 NLNet Labs. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00034 #include "config.h" 00035 #include "shared/locks.h" 00036 #include "shared/log.h" 00037 00038 #include <errno.h> 00039 #include <signal.h> /* sigfillset(), sigprocmask() */ 00040 #include <string.h> /* strerror() */ 00041 #ifdef HAVE_SYS_TIME_H 00042 #include <sys/time.h> /* gettimeofday() */ 00043 #endif 00044 #ifdef HAVE_TIME_H 00045 #include <time.h> /* gettimeofday() */ 00046 #endif 00047 00048 static const char* lock_str = "lock"; 00049 00050 #if !defined(HAVE_PTHREAD) 00051 #include <sys/wait.h> /* waitpid() */ 00052 #include <sys/types.h> /* getpid(), waitpid() */ 00053 #include <unistd.h> /* fork(), getpid() */ 00054 00055 00064 void 00065 ods_thr_fork_create(ods_thread_type* thr, void* (*func)(void*), void* arg) 00066 { 00067 pid_t pid = fork(); 00068 00069 switch (pid) { 00070 case 0: /* child */ 00071 *thr = (ods_thread_type)getpid(); 00072 (void)(*func)(arg); 00073 exit(0); 00074 case -1: /* error */ 00075 ods_fatal_exit("[%s] unable to fork thread: %s", lock_str, 00076 strerror(errno)); 00077 default: /* main */ 00078 *thr = (ods_thread_type)pid; 00079 return; 00080 } 00081 return; 00082 } 00083 00084 00090 void ods_thr_fork_wait(ods_thread_type thread) 00091 { 00092 int status = 0; 00093 00094 if (waitpid((pid_t)thread, &status, 0) == -1) { 00095 ods_log_error("[%s] waitpid(%d): %s", lock_str, (int)thread, 00096 strerror(errno)); 00097 } 00098 if (status != 0) { 00099 ods_log_warning("[%s] process %d abnormal exit with status %d", 00100 lock_str, (int)thread, status); 00101 } 00102 return; 00103 } 00104 #else /* defined(HAVE_PTHREAD) */ 00105 00106 00107 int 00108 ods_thread_wait(cond_basic_type* cond, lock_basic_type* lock, time_t wait) 00109 { 00110 struct timespec ts; 00111 int ret = 0; 00112 00113 /* If timeshift is enabled, we don't care about threads. No need 00114 * to take the timeshift into account here */ 00115 00116 #ifndef HAVE_CLOCK_GETTIME 00117 struct timeval tv; 00118 if (gettimeofday(&tv, NULL) != 0) { 00119 ods_log_error("[%s] clock_gettime() error: %s", lock_str, 00120 strerror(errno)); 00121 return 1; 00122 } 00123 ts.tv_sec = tv.tv_sec; 00124 ts.tv_nsec = (tv.tv_usec/1000); 00125 #else /* HAVE_CLOCK_GETTIME */ 00126 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { 00127 ods_log_error("[%s] clock_gettime() error: %s", lock_str, 00128 strerror(errno)); 00129 return 1; 00130 } 00131 #endif /* !HAVE_CLOCK_GETTIME */ 00132 00133 if (wait > 0) { 00134 ts.tv_sec = ts.tv_sec + wait; 00135 ret = pthread_cond_timedwait(cond, lock, &ts); 00136 } else { 00137 ret = pthread_cond_wait(cond, lock); 00138 } 00139 00140 if (ret == ETIMEDOUT) { 00141 return 0; 00142 } 00143 return ret; 00144 } 00145 00146 #endif /* defined(HAVE_PTHREAD) */ 00147 00148 00149 void 00150 ods_thread_blocksigs(void) 00151 { 00152 int err = 0; 00153 sigset_t sigset; 00154 sigfillset(&sigset); 00155 00156 #ifndef HAVE_PTHREAD 00157 if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL))) 00158 ods_fatal_exit("[%s] pthread_sigmask: %s", lock_str, strerror(err)); 00159 #else /* !HAVE_PTHREAD */ 00160 /* have nothing, do single process signal mask */ 00161 if((err=sigprocmask(SIG_SETMASK, &sigset, NULL))) 00162 ods_fatal_exit("[%s] sigprocmask: %s", lock_str, strerror(errno)); 00163 #endif /* HAVE_PTHREAD */ 00164 }