forked from Exhar/otpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xfunc.c
442 lines (387 loc) · 9.54 KB
/
xfunc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* For alternative licensing terms, contact licensing@tri-dsystems.com.
*
* Copyright 2005-2007 TRI-D Systems, Inc.
*/
#include "ident.h"
RCSID("$Id$")
#include <arpa/inet.h> /* inet_ntoa() */
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h> /* sockaddr_un */
#include <sys/types.h>
#include <unistd.h>
//#include <stropts.h>
#ifdef __sun__
#define BSD_COMP
#endif
#include <sys/ioctl.h>
#ifdef __linux__
#ifdef HAVE_CLOCK_GETTIME
#include <time.h>
#else
#include <sys/time.h>
#endif
#endif
#include "extern.h"
/* ascii to hex; returns HEXlen on success or -1 on error */
ssize_t
a2x(const char *s, unsigned char x[])
{
return a2nx(s, x, strlen(s) / 2);
}
/* bounded ascii to hex; returns HEXlen on success or -1 on error */
ssize_t
a2nx(const char *s, unsigned char x[], size_t l)
{
unsigned i;
/*
* We could just use sscanf, but we do this a lot, and have very
* specific needs, and it's easy to implement, so let's go for it!
*/
for (i = 0; i < l; ++i) {
unsigned int n[2];
int j;
/* extract 2 nibbles */
n[0] = *s++;
n[1] = *s++;
/* verify range */
for (j = 0; j < 2; ++j) {
if ((n[j] >= '0' && n[j] <= '9') ||
(n[j] >= 'A' && n[j] <= 'F') ||
(n[j] >= 'a' && n[j] <= 'f'))
continue;
return -1;
}
/* convert ASCII hex digits to numeric values */
n[0] -= '0';
n[1] -= '0';
if (n[0] > 9) {
if (n[0] > 'F' - '0')
n[0] -= 'a' - '9' - 1;
else
n[0] -= 'A' - '9' - 1;
}
if (n[1] > 9) {
if (n[1] > 'F' - '0')
n[1] -= 'a' - '9' - 1;
else
n[1] -= 'A' - '9' - 1;
}
/* store as octets */
x[i] = n[0] << 4;
x[i] += n[1];
} /* for (each octet) */
return i;
}
/* Character maps for generic hex and vendor specific decimal modes */
const char x2a_hex_conversion[] = "0123456789abcdef";
const char x2a_cc_dec_conversion[] = "0123456789012345";
const char x2a_snk_dec_conversion[] = "0123456789222333";
const char x2a_sc_friendly_conversion[] = "0123456789ahcpef";
/*
* hex to ascii
* Fills in s, which must point to at least len*2+1 bytes of space.
*/
char *
x2a(const unsigned char *x, size_t len, char *s, const char conversion[16])
{
unsigned i;
for (i = 0; i < len; ++i) {
unsigned n[2];
n[0] = (x[i] >> 4) & 0x0f;
n[1] = x[i] & 0x0f;
s[2 * i + 0] = conversion[n[0]];
s[2 * i + 1] = conversion[n[1]];
}
s[2 * len] = '\0';
return s;
}
/* guaranteed creation */
void
_xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg, const char *caller)
{
int rc;
if ((rc = pthread_create(thread, attr, start_routine, arg))) {
mlog(LOG_CRIT, "%s: pthread_create: %s", caller, strerror(rc));
exit(1);
}
}
/* guaranteed initialization */
void
_xpthread_mutex_init(pthread_mutex_t *mutexp, const pthread_mutexattr_t *attr,
const char *caller)
{
int rc;
if ((rc = pthread_mutex_init(mutexp, attr))) {
mlog(LOG_CRIT, "%s: pthread_mutex_init: %s", caller, strerror(rc));
exit(1);
}
}
/* guaranteed trylock */
int
_xpthread_mutex_trylock(pthread_mutex_t *mutexp, const char *caller)
{
int rc;
rc = pthread_mutex_trylock(mutexp);
if (rc && rc != EBUSY) {
mlog(LOG_CRIT, "%s: pthread_mutex_trylock: %s", caller, strerror(rc));
exit(1);
}
return rc;
}
/* guaranteed lock */
void
_xpthread_mutex_lock(pthread_mutex_t *mutexp, const char *caller)
{
int rc;
if ((rc = pthread_mutex_lock(mutexp))) {
mlog(LOG_CRIT, "%s: pthread_mutex_lock: %s", caller, strerror(rc));
exit(1);
}
}
/* guaranteed unlock */
void
_xpthread_mutex_unlock(pthread_mutex_t *mutexp, const char *caller)
{
int rc;
if ((rc = pthread_mutex_unlock(mutexp))) {
mlog(LOG_CRIT, "%s: pthread_mutex_unlock: %s", caller, strerror(rc));
exit(1);
}
}
/* guaranteed sempahore init */
void
_xsem_init(sem_t *sem, int pshared, unsigned value, const char *caller)
{
if (sem_init(sem, pshared, value) == -1) {
mlog(LOG_CRIT, "%s: sem_init: %s", caller, strerror(errno));
exit(1);
}
}
/* guaranteed socket allocation */
int
_xsocket(int domain, int type, int protocol, const char *caller)
{
int s;
if ((s = socket(domain, type, protocol)) == -1) {
mlog(LOG_CRIT, "%s: socket: %s", caller, strerror(errno));
exit(1);
}
return s;
}
/* guaranteed bind */
void
_xbind(int s, const struct sockaddr *addr, int namelen, const char *caller)
{
if (bind(s, addr, namelen) == -1) {
if (addr->sa_family == AF_UNIX)
mlog(LOG_CRIT, "%s: bind(%s): %s", caller,
((struct sockaddr_un *) addr)->sun_path, strerror(errno));
else
mlog(LOG_CRIT, "%s: bind(%d): %s", caller, s, strerror(errno));
exit(1);
}
}
/* guaranteed listen */
void
_xlisten(int s, int backlog, const char *caller)
{
if (listen(s, backlog) == -1) {
mlog(LOG_CRIT, "%s: listen: %s\n", caller, strerror(errno));
exit(1);
}
}
/* guaranteed connect */
void
_xconnect(int s, const struct sockaddr *name, int namelen, const char *caller)
{
if (connect(s, name, namelen) == -1) {
if (name->sa_family == AF_INET)
mlog(LOG_CRIT, "%s: connect(%s): %s", caller,
inet_ntoa(((struct sockaddr_in *) name)->sin_addr), strerror(errno));
else
mlog(LOG_CRIT, "%s: connect(%d): %s", caller, s, strerror(errno));
exit(1);
}
}
/* guaranteed ioctl with int arg 1 */
void
_xioctl1(int fildes, int request, const char *caller)
{
int one = 1;
if (ioctl(fildes, request, &one) == -1) {
mlog(LOG_CRIT, "%s: ioctl: %s", caller, strerror(errno));
exit(1);
}
}
/* guaranteed allocation */
void *
_xmalloc(size_t size)
{
void *p = malloc(size);
if (!p) {
/* sketchy, since mlog might need to malloc (but not xmalloc()!) */
mlog(LOG_CRIT, "malloc");
exit(1);
}
return p;
}
/* guaranteed reallocation */
void *
_xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
if (!p) {
/* sketchy, since mlog might need to malloc (but not xmalloc()!) */
mlog(LOG_CRIT, "realloc");
exit(1);
}
return p;
}
/* guaranteed unlink */
void
_xunlink(const char *path, const char *caller)
{
if (unlink(path) == -1) {
if (errno != ENOENT) {
mlog(LOG_CRIT, "%s: unlink(%s): %s",
caller, path, strerror(errno));
exit(1);
}
}
}
/*
* guaranteed full write
* returns 1 on success, -1 on failure
*/
ssize_t
_xwrite(int fd, const char *buf, size_t len, const char *caller)
{
size_t nwrote = 0;
ssize_t n;
while (nwrote < len) {
if ((n = write(fd, &buf[nwrote], len - nwrote)) == -1) {
mlog(LOG_ERR, "%s: write(%d): %s", caller, fd, strerror(errno));
return -1;
}
nwrote += n;
}
return 1;
}
/* guaranteed chdir */
void
_xchdir(const char *path, const char *caller)
{
if (chdir(path) == -1) {
mlog(LOG_CRIT, "%s: can't chdir to %s: %s", caller, path, strerror(errno));
exit(1);
}
}
/* guaranteed setuid */
void
_xsetuid(uid_t uid, const char *caller)
{
if (setuid(uid) == -1) {
if (errno == EPERM)
mlog(LOG_CRIT, "%s: Permission denied", caller);
else
mlog(LOG_CRIT, "%s: setuid: %s", caller, strerror(errno));
exit(1);
}
}
/* guaranteed setgid */
void
_xsetgid(gid_t gid, const char *caller)
{
if (setgid(gid) == -1) {
if (errno == EPERM)
mlog(LOG_CRIT, "%s: Permission denied", caller);
else
mlog(LOG_CRIT, "%s: setgid: %s", caller, strerror(errno));
exit(1);
}
}
/* guaranteed setsid */
void
_xsetsid(const char *caller)
{
if (setsid() == (pid_t) -1) {
mlog(LOG_CRIT, "%s: setsid: %s", caller, strerror(errno));
exit(1);
}
}
/* guaranteed strdup */
char *
_xstrdup(const char *s, const char *caller)
{
char *t;
if ((t = strdup(s)) == NULL) {
mlog(LOG_CRIT, "%s: strdup failed", caller);
exit(1);
}
return t;
}
#ifndef HAVE_CLOSEFROM
/* guaranteed getrlimit */
void
_xgetrlimit(int resource, struct rlimit *rlp, const char *caller)
{
if (getrlimit(resource, rlp) == -1) {
mlog(LOG_CRIT, "%s: getrlimit: %s", caller, strerror(errno));
exit(1);
}
}
#endif /* !HAVE_CLOSEFROM */
#ifdef __linux__
/* guaranteed time */
hrtime_t
_xgethrtime(const char *caller)
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec tp;
#else
struct timeval tp;
#endif
hrtime_t now;
#ifdef HAVE_CLOCK_GETTIME
/* we try clock_gettime() because it may be faster */
if (clock_gettime(CLOCK_REALTIME, &tp) == -1) {
mlog(LOG_CRIT, "%s: clock_gettime: %s", caller, strerror(errno));
exit(1);
}
#else
if (gettimeofday(&tp, NULL) == -1) {
mlog(LOG_CRIT, "%s: gettimeofday: %s", caller, strerror(errno));
exit(1);
}
#endif
#ifdef HAVE_CLOCK_GETTIME
now = tp.tv_sec * 1000000000LL + tp.tv_nsec;
#else
now = tp.tv_sec * 1000000000LL + tp.tv_usec * 1000;
#endif
return now;
}
#endif /* __linux__ */