Fix potential memory leak.
[pwmd.git] / src / mutex.h
blob8e61e5a7ffc9da029e6a8332a29a301673dd7640
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
3 2016, 2017
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef MUTEX_H
22 #define MUTEX_H
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #ifdef HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
32 #ifdef HAVE_TIME_H
33 #include <time.h>
34 #endif
36 #ifdef HAVE_SYS_SELECT_H
37 #include <sys/select.h>
38 #else
39 #include <sys/types.h>
40 #include <unistd.h>
41 #endif
43 #ifdef HAVE_SYS_FILE_H
44 #include <sys/file.h>
45 #endif
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
51 #include <pthread.h>
52 #include <assert.h>
53 #include <errno.h>
54 #include "rcfile.h"
56 #ifndef HAVE_PTHREAD_CANCEL
57 pthread_key_t signal_thread_key;
58 #define TEST_CANCEL() do { \
59 int *cancel = (int *) pthread_getspecific (signal_thread_key); \
60 if (cancel && *cancel) \
61 pthread_exit (NULL); \
62 } while (0)
63 #else
64 #define TEST_CANCEL()
65 #endif
67 #define INIT_TIMESPEC(t, ts) do { \
68 long s = (t*100000000)/1000000000; \
69 long l = (t*100000000)%1000000000; \
70 clock_gettime(CLOCK_REALTIME, &ts); \
71 ts.tv_sec += s; \
72 if (ts.tv_nsec + l >= 1000000000) { \
73 ts.tv_sec++; \
74 ts.tv_nsec = 0; \
75 } \
76 else \
77 ts.tv_nsec += l; \
78 } while (0)
80 #ifdef MUTEX_DEBUG
81 #define MUTEX_LOCK_DEBUG(m) do { \
82 log_write("%s(%i): %s(): LOCK %p", __FILE__, __LINE__, \
83 __FUNCTION__, m); \
84 } while (0)
86 #define MUTEX_UNLOCK_DEBUG(m) do { \
87 log_write("%s(%i): %s(): UNLOCK %p", __FILE__, __LINE__, \
88 __FUNCTION__, m); \
89 } while (0)
90 #else
91 #define MUTEX_LOCK_DEBUG(m)
92 #define MUTEX_UNLOCK_DEBUG(m)
93 #endif
95 #define MUTEX_LOCK(m) do { \
96 MUTEX_LOCK_DEBUG(m); \
97 while (pthread_mutex_trylock (m) == EBUSY) { \
98 struct timeval tv = { 0, 3000 }; \
99 TEST_CANCEL (); \
100 select (0, NULL, NULL, NULL, &tv); \
101 }; \
102 } while (0)
104 #define MUTEX_UNLOCK(m) do { \
105 MUTEX_UNLOCK_DEBUG(m); \
106 pthread_mutex_unlock(m); \
107 } while (0)
109 #define TIMED_LOCK_DURATION 100000
110 #ifdef HAVE_FLOCK
111 #define TRY_FLOCK(ctx, fd, type, rc) do { \
112 long ka = (long)config_get_integer ("global", "keepalive_interval");\
113 long to = config_get_long ("global", "lock_timeout"); \
114 for (long elapsed = 0; ; elapsed++) { \
115 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
116 int n = flock (fd, type|LOCK_NB); \
117 if (n == -1 && errno == EWOULDBLOCK) { \
118 if (to == -1 || elapsed >= to) { \
119 rc = GPG_ERR_LOCKED; \
120 break; \
122 select (0, NULL, NULL, NULL, &tv); \
123 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
124 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
125 if (rc) \
126 break; \
128 TEST_CANCEL(); \
130 else if (n == -1) \
131 rc = gpg_error_from_errno (errno); \
132 else \
133 break; \
135 } while (0)
136 #else
137 #define TRY_FLOCK(ctx, fd, type, rc) do { \
138 rc = 0; \
139 } while (0)
140 #endif
142 #define MUTEX_TIMED_LOCK(ctx, m, timeout, rc) do { \
143 long ka = (long)config_get_integer ("global", "keepalive_interval");\
144 for (long elapsed = 0; ; elapsed++) { \
145 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
146 int n = pthread_mutex_trylock (m); \
147 if (n == EBUSY) { \
148 if (timeout == -1 || elapsed >= timeout) { \
149 rc = GPG_ERR_LOCKED; \
150 break; \
152 select (0, NULL, NULL, NULL, &tv); \
153 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
154 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
155 if (rc) \
156 break; \
158 TEST_CANCEL(); \
160 else if (n) { \
161 rc = gpg_error_from_errno (n); \
162 break; \
164 else { \
165 MUTEX_LOCK_DEBUG(m); \
166 break; \
169 } while (0)
171 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
172 int err = pthread_mutex_trylock (m); \
173 rc = 0; \
174 if (err == EBUSY) { \
175 if (t != -1) { \
176 if (ctx) \
177 rc = send_status(ctx, STATUS_LOCKED, NULL); \
178 if (!rc && t != 0) \
179 MUTEX_TIMED_LOCK(ctx, m, t, rc); \
180 else if (!rc) \
181 rc = GPG_ERR_LOCKED; \
183 else \
184 rc = GPG_ERR_LOCKED; \
186 else if (err) \
187 rc = gpg_error_from_errno (err); \
188 else \
189 MUTEX_LOCK_DEBUG(m); \
190 } while (0)
192 #define MUTEX_TIMED_RWLOCK(ctx, m, timeout, rc, w) do { \
193 long ka = (long)config_get_integer ("global", "keepalive_interval");\
194 for (long elapsed = 0; ; elapsed++) { \
195 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
196 int err; \
197 if (w) \
198 err = pthread_rwlock_trywrlock(m); \
199 else \
200 err = pthread_rwlock_tryrdlock(m); \
201 if (err == EBUSY) { \
202 if (timeout == -1 || elapsed >= timeout) { \
203 rc = GPG_ERR_LOCKED; \
204 break; \
206 select (0, NULL, NULL, NULL, &tv); \
207 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
208 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
209 if (rc) \
210 break; \
212 TEST_CANCEL(); \
214 else if (err) { \
215 rc = gpg_error_from_errno (err); \
216 break; \
218 else { \
219 MUTEX_LOCK_DEBUG(m); \
220 break; \
223 } while (0)
225 #endif