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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
|
/* Quagga Pthreads support -- thread safe versions of standard functions
* Copyright (C) 2009 Chris Hall (GMCH), Highwayman
*
* This file is part of GNU Zebra.
*
* GNU Zebra 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, or (at your
* option) any later version.
*
* GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* We use thread specific storage to provide buffers for the _r versions
* of standard functions, so that the callers don't need to provide
* their own. The contents of a buffer will remain intact until another
* safe_ function is called on the same thread
*/
#include "misc.h"
#include "pthread_safe.h"
#include "qpthreads.h"
#include "memory.h"
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include "qstring.h"
#include "qfstring.h"
#include "errno_names.h"
/*==============================================================================
* Initialisation, close down and local variables.
*
* Note that the "thread_safe" mutex is recursive, so one thread safe function
* can call another, if required.
*/
static pthread_key_t tsd_key;
static const int buff_size = 1024;
static qpt_mutex thread_safe = NULL ;
static const char ellipsis[] = "..." ;
static void destructor(void* data);
static char * thread_buff(void);
#define THREAD_SAFE_LOCK() qpt_mutex_lock(thread_safe)
#define THREAD_SAFE_UNLOCK() qpt_mutex_unlock(thread_safe)
/*------------------------------------------------------------------------------
* Module initialization, before any threads have been created
*/
void
safe_init_r(void)
{
if (qpthreads_enabled)
{
int status;
status = pthread_key_create(&tsd_key, destructor);
if (status != 0)
zabort("Can't create thread specific data key");
qassert(thread_safe == NULL) ;
thread_safe = qpt_mutex_new(qpt_mutex_recursive, "pthread safe") ;
} ;
}
/*------------------------------------------------------------------------------
* Clean up
*/
void
safe_finish(void)
{
if (qpthreads_enabled)
{
pthread_key_delete(tsd_key) ;
thread_safe = qpt_mutex_destroy(thread_safe) ;
} ;
} ;
/*------------------------------------------------------------------------------
* called when thread terminates, clean up
*/
static void
destructor(void* data)
{
XFREE(MTYPE_TSD, data);
}
/*==============================================================================
* Alternative error number handling.
*
* The descriptive strings for error numbers are all very well, but for some
* purposes knowing the name for the error is more useful -- the name, not the
* number, as the number is system dependent.
*
* The following provides:
*
* * errtoa() -- which maps error number to: ENAME '<strerror>'
*
* * errtoname() -- which maps error number to: ENAME
*
* * errtostr() -- which maps error number to: <strerror>
*
* where:
*
* * if name is not known gives: ERRNO=999
*
* * if strerror rejects the number gives: *unknown error*
*
* * err == 0 gives: EOK -- for the name
* 'no error' -- for the <strerror>
*
* These functions take a 'len' argument, and truncates the result to the given
* len (0 => no truncation) -- silently imposing the maximum length allowed by
* the strerror_t.
*
* If has to truncate the <strerror>, places "..." at the end of the message
* to show this has happened.
*/
static strerror_t errtox(int err, ulen len, uint want) ;
static const char* qstrerror_r(int err, char *buf, size_t n) ;
static const char* qstrerror(int err) ;
/*------------------------------------------------------------------------------
* Construct string to describe the given error of the form:
*
* ENAME '<strerror>'
*
* Thread safe extension to strerror. Never returns NULL.
*/
extern strerror_t
errtoa(int err, ulen len)
{
return errtox(err, len, 3) ;
} ;
/*------------------------------------------------------------------------------
* Convert error number to its name
*
* Thread-safe. Never returns NULL.
*/
extern strerror_t
errtoname(int err, ulen len)
{
return errtox(err, len, 1) ;
} ;
/*------------------------------------------------------------------------------
* Alternative thread-safe safe_strerror()
*
* Thread safe replacement for strerror. Never returns NULL.
*/
extern strerror_t
errtostr(int err, ulen len)
{
return errtox(err, len, 2) ;
} ;
/*-----------------------------------------------------------------------------
* Common code for errto<x> above.
*
* len > 0 -- limit length (unless longer than maximum for strerror_t)
* len <= 0 -- allow up to the maximum for strerror_t
*
* want == 1 -- return just name
* want == 2 -- return just the strerror()
* want == 3 -- return both, with the strerror() in single quotes.
*
* NB: this is not async-signal-safe !
*/
static strerror_t
errtox(int err, ulen len, uint want)
{
strerror_t QFB_QFS(st, qfs) ;
int errno_saved ;
const char* q ;
ulen ql ;
/* Prepare. */
errno_saved = errno ;
if ((len > 0) && (len < sizeof(st.str)))
qfs_init(qfs, st.str, len + 1) ; /* limit the size */
q = "" ;
ql = 0 ;
/* If want the error name, do that now. */
if (want & 1)
{
const char* name = errno_name_lookup(err) ;
if (name != NULL)
qfs_append(qfs, name) ;
else
qfs_printf(qfs, "ERRNO=%d", err) ;
} ;
/* name and string ? */
if (want == 3)
{
qfs_append(qfs, " ") ;
q = "'" ;
ql = 2 ;
} ;
/* If want the error string, do that now */
if (want & 2)
{
char buf[400] ; /* impossibly vast */
const char* errm ;
if (err == 0)
errm = "no error" ;
else
{
if (qpthreads_enabled)
errm = qstrerror_r(err, buf, sizeof(buf)) ;
else
errm = qstrerror(err) ;
/* Deal with errors, however exotic. */
if (errm == NULL)
{
int ret = errno ;
qf_str_t qfs_b ;
qfs_init(qfs_b, buf, sizeof(buf)) ;
if (ret == EINVAL)
qfs_printf(qfs_b, "unknown error number %d", err) ;
else if (ret == ERANGE)
qfs_printf(qfs_b, "strerror%s(%d) returned impossibly large "
" error message (> %d bytes)",
qpthreads_enabled ? "_r" : "", err, (int)sizeof(buf)) ;
else
qfs_printf(qfs_b, "strerror%s(%d) returned error %d",
qpthreads_enabled ? "_r" : "", err, ret) ;
qfs_term(qfs_b) ;
q = "*" ;
ql = 2 ; /* force "*" "quotes" */
errm = buf ;
} ;
} ;
/* Add strerror to the result... with quotes as rquired */
if (ql != 0)
qfs_append(qfs, q) ;
qfs_append(qfs, errm) ;
if (ql != 0)
qfs_append(qfs, q) ;
} ;
/* '\0' terminate -- if has overflowed, replace last few characters
* by "..." -- noting that sizeof("...") includes the '\0'.
*/
if (qfs_term(qfs) != 0)
qfs_term_string(qfs, ellipsis, sizeof(ellipsis)) ;
/* Put back errno and we are done
*/
errno = errno_saved ;
return st ;
} ;
/*==============================================================================
* getaddrinfo() and getnameinfo() "EAI_XXXX" error number handling.
*
* This is similar to the above for errno.
*
* The following provides:
*
* * eaitoa() -- which maps error number to: EAI_XXX '<gai_strerror>'
* or: as errtoa()
*
* * eaitoname() -- which maps error number to: EAI_XXX
* or: as errtoname()
*
* * eaitostr() -- which maps error number to: <gai_strerror>
* or: as errtostr()
*
* where:
*
* * if given EAI_SYSTEM, and given a non-zero errno type error number,
* produce the errno string.
*
* * if name is not known gives: EAI=999
*
* * gai_strerror returns a string saying the error is not known if that is
* the case.
*
* * eai == 0 gives: EAI_OK -- for the name
* 'no error' -- for the <sgai_strerror>
*
* NB: EAI_SYSTEM is an invitation to look at errno to discover the true
* error.
*/
static strerror_t eaitox(int eai, int err, ulen len, uint want) ;
/*------------------------------------------------------------------------------
* Construct string to describe the given EAI_XXX error of the form:
*
* EAI_XXX '<gai_strerror>'
* or: ENAME '<strerror>' -- if EAI_SYSTEM and err != 0
*
* Thread safe. Never returns NULL.
*/
extern strerror_t
eaitoa(int eai, int err, ulen len)
{
return eaitox(eai, err, len, 3) ;
} ;
/*------------------------------------------------------------------------------
* Convert EAI_XXX error number to its name...
*
* ...or, if EAI_SYSTEM and err != 0, convert err to its name.
*
* Thread-safe. Never returns NULL.
*/
extern strerror_t
eaitoname(int eai, int err, ulen len)
{
return eaitox(eai, err, len, 1) ;
} ;
/*------------------------------------------------------------------------------
* Alternative to gai_strerror()...
*
* ...or, if EAI_SYSTEM and err != 0, do strerror(err) or strerror_r(err).
*
* Thread-safe. Never returns NULL.
*/
extern strerror_t
eaitostr(int eai, int err, ulen len)
{
return eaitox(eai, err, len, 2) ;
} ;
/*-----------------------------------------------------------------------------
* Common code for eaito<x> above.
*
* want == 1 -- return just name
* want == 2 -- return just the gai_strerror()
* want == 3 -- return both, with the gai_strerror() in single quotes.
*
* err != 0 => if EAI_SYSTEM, return result for errno == err instead.
*/
static strerror_t
eaitox(int eai, int err, ulen len, uint want)
{
strerror_t QFB_QFS(st, qfs) ;
int errno_saved ;
const char* q ;
ulen ql ;
/* Look out for mapping EAI_SYSTEM
*/
if ((eai == EAI_SYSTEM) && (err != 0))
return errtox(err, len, want) ;
/* Prepare.
*/
errno_saved = errno ;
if ((len > 0) && (len < sizeof(st.str)))
qfs_init(qfs, st.str, len + 1) ; /* limit the size */
q = "" ;
ql = 0 ;
/* If want the error name, do that now.
*/
if (want & 1)
{
const char* name = eaino_name_lookup(eai) ;
if (name != NULL)
qfs_append(qfs, name) ;
else
qfs_printf(qfs, "EAI=%d", eai) ;
} ;
/* name and string ?
*/
if (want == 3)
{
qfs_append(qfs, " ") ;
q = "'" ;
ql = 2 ;
} ;
/* If want the error string, do that now
*/
if (want & 2)
{
const char* eaim ;
if (eai == 0)
eaim = "no error" ;
else
eaim = gai_strerror(eai) ;
/* Add strerror to the result... with quotes as rquired */
if (ql != 0)
qfs_append(qfs, q) ;
qfs_append(qfs, eaim) ;
if (ql != 0)
qfs_append(qfs, q) ;
/* '\0' terminate -- if has overflowed, replace last few characters
* by "..." -- noting that sizeof("...") includes the '\0'.
*/
if (qfs_term(qfs) != 0)
qfs_term_string(qfs, ellipsis, sizeof(ellipsis)) ;
} ;
/* Put back errno and we are done
*/
errno = errno_saved ;
return st ;
} ;
/*==============================================================================
* Miscellaneous thread-safe functions
*/
/*------------------------------------------------------------------------------
* getenv_r -- fetch environment variable into the given buffer.
*
* If buffer is not long enough, fetches as much as can and '\0' terminates.
*
* Returns: -1 => not found -- buffer set empty
* >= 0 == length of environment variable
*
* NB: this is NOT signal safe. If need value of environment variable in
* a signal action -- make OTHER arrangements !!
*/
extern int
getenv_r(const char* name, char* buf, int buf_len)
{
char* val ;
int len ;
int cl ;
THREAD_SAFE_LOCK() ;
val = getenv(name) ;
if (val == NULL)
{
len = -1 ;
cl = 0 ;
}
else
{
len = strlen(val) ;
cl = (len < buf_len) ? len : buf_len - 1 ;
} ;
if (buf_len > 0)
{
if (cl > 0)
memcpy(buf, val, cl) ;
buf[cl] = '\0' ;
} ;
THREAD_SAFE_UNLOCK() ;
return len ;
} ;
/*------------------------------------------------------------------------------
* Thread safe version of strerror. Never returns NULL.
* Contents of result remains intact until another call of
* a safe_ function.
*/
const char *
safe_strerror(int errnum)
{
int errno_saved ;
const char* errm ;
errno_saved = errno ;
if (qpthreads_enabled)
errm = qstrerror_r(errnum, thread_buff(), buff_size) ;
else
errm = qstrerror(errnum);
errno = errno_saved ;
return (errm != NULL) ? errm : "Unknown error" ;
}
/*------------------------------------------------------------------------------
* Thread safe version of inet_ntoa. Never returns NULL.
* Contents of result remains intact until another call of
* a safe_ function.
*/
const char *
safe_inet_ntoa (struct in_addr in)
{
static const char * unknown = "Unknown address";
const char * buff;
buff = (qpthreads_enabled)
? inet_ntop(AF_INET, &in, thread_buff(), buff_size)
: inet_ntoa(in);
return buff != NULL
? buff
: unknown;
}
/*------------------------------------------------------------------------------
* Construct string for given IP family/address.
*
* Returns struct with embedded string.
*/
extern str_iptoa_t
siptoa(sa_family_t family, const void* address)
{
str_iptoa_t QFB_QFS(ipa, qfs) ;
switch (family)
{
case AF_INET:
confirm(sizeof(ipa.str) >= INET_ADDRSTRLEN) ;
#ifdef HAVE_IPV6
case AF_INET6:
confirm(sizeof(ipa.str) >= INET6_ADDRSTRLEN) ;
#endif
inet_ntop(family, address, ipa.str, sizeof(ipa.str));
break;
default:
qfs_printf(qfs, "?unknown address family=%d?", family) ;
qfs_term(qfs) ;
break ;
} ;
return ipa;
} ;
/*------------------------------------------------------------------------------
* Return the thread's buffer, create it if necessary.
* (pthread Thread Specific Data)
*/
static char *
thread_buff(void)
{
int ret;
char * buff = pthread_getspecific(tsd_key);
if (buff == NULL)
{
buff = XMALLOC(MTYPE_TSD, buff_size);
ret = pthread_setspecific(tsd_key, buff);
if (ret != 0)
zabort("Can't set thread specific data");
}
return buff;
}
/*------------------------------------------------------------------------------
* Do a getpwnam_r() using the given buffer if at all possible.
*
* If given buffer is not big enough, malloc() a buffer until get one which
* is big enough.
*
* Returns: 0 => OK, setting p_pwd to the struct passwd
* if that is not the same as the incoming buf, then must
* XFREE(MTYPE_TMP, pwd) when finished.
* != 0 => failed, and this is the errno
* no memory to tidy up if failed
*
* Note: can pass size == 0 (and buf == NULL) to force malloc()
*/
extern int
safe_getpwnam(const char* name, struct passwd** p_pwd, void* buf, ulen size)
{
void* tmp ;
int err ;
if (size < (sizeof(struct passwd) + 100))
{
size = sizeof(struct passwd) + 200 ;
tmp = XMALLOC(MTYPE_TMP, size) ;
}
else
tmp = buf ;
while (1)
{
char* b ;
size_t bl ;
b = (char*)tmp + sizeof(struct passwd) ;
bl = size - sizeof(struct passwd) ;
err = getpwnam_r(name, tmp, b, bl, p_pwd) ;
if (err == EINTR)
continue ;
if (err != ERANGE)
break ;
size *= 2 ;
if (tmp == buf)
tmp = XMALLOC(MTYPE_TMP, size) ;
else
tmp = XREALLOC(MTYPE_TMP, tmp, size) ;
} ;
if (err != 0)
{
if (tmp != buf)
XFREE(MTYPE_TMP, tmp) ;
*p_pwd = buf ;
} ;
return err ;
} ;
/*------------------------------------------------------------------------------
* Do a getpwuid_r() using the given buffer if at all possible.
*
* If given buffer is not big enough, malloc() a buffer until get one which
* is big enough.
*
* Returns: 0 => OK, setting p_pwd to the struct passwd
* if that is not the same as the incoming buf, then must
* XFREE(MTYPE_TMP, pwd) when finished.
* != 0 => failed, and this is the errno
* no memory to tidy up if failed
*
* Note: can pass size == 0 (and buf == NULL) to force malloc()
*/
extern int
safe_getpwuid(uid_t id, struct passwd** p_pwd, void* buf, ulen size)
{
void* tmp ;
int err ;
if (size < (sizeof(struct passwd) + 100))
{
size = sizeof(struct passwd) + 200 ;
tmp = XMALLOC(MTYPE_TMP, size) ;
}
else
tmp = buf ;
while (1)
{
char* b ;
size_t bl ;
b = (char*)tmp + sizeof(struct passwd) ;
bl = size - sizeof(struct passwd) ;
err = getpwuid_r(id, tmp, b, bl, p_pwd) ;
if (err == EINTR)
continue ;
if (err != ERANGE)
break ;
size *= 2 ;
if (tmp == buf)
tmp = XMALLOC(MTYPE_TMP, size) ;
else
tmp = XREALLOC(MTYPE_TMP, tmp, size) ;
} ;
if (err != 0)
{
if (tmp != buf)
XFREE(MTYPE_TMP, tmp) ;
*p_pwd = buf ;
} ;
return err ;
} ;
/*------------------------------------------------------------------------------
* crypt() wrapper -- create a qstring with results of crypt()
*
* If the given salt is NULL, creates new, random salt.
*
* Returns: new qstring -- caller is responsible for freeing same.
*/
extern qstring
qcrypt(const char* text, const char* salt)
{
uint32_t r ;
char new_salt[3];
qstring cypher ;
static const unsigned char itoa64[] =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
extern char *crypt (const char *, const char *) ;
if (salt == NULL)
{
r = qt_random((uintptr_t)text) ;
new_salt[0] = itoa64[(r >> (32 - 5)) & 0x3F] ; /* ms 5 */
new_salt[1] = itoa64[(r >> (32 - 10)) & 0x3F] ; /* next ms 5 */
new_salt[2] = '\0';
salt = new_salt ;
} ;
/* Can only may this thread-safe by locking.
*
* Minimises the chance of having to allocate memory while holding the lock.
*/
cypher = qs_new(strlen(text) * 2) ;
THREAD_SAFE_LOCK() ;
qs_set_str(cypher, crypt(text, salt)) ;
THREAD_SAFE_UNLOCK() ;
return cypher ;
} ;
/*------------------------------------------------------------------------------
* Local wrapper for strerror_r().
*
* Two variants of strerror_r are known:
*
* (a) "GNU", which returns a char*
*
* (b) POSIX, which returns an int.
*
* GNU does not say whether it returns an error if the errno is not known or if
* the buffer is too small (though for buffer too small, suggests that silent
* truncation is the order of the day). We treat it like strerror().
*
* POSIX is not explicit about what happens if the buffer is not big enough to
* accommodate the message, except that an ERANGE error may be raised.
*
* By experiment: glibc-2.10.2-1.x86_64 returns -1, with errno set to ERANGE
* and no string at all if the buffer is too small.
*
* This function encapsulates the variants.
*
* Returns: string if one was returned, and was not empty.
* NULL otherwise
*
* In all cases: errno == 0 <=> OK
* errno != 0 <=> some sort of error
*
* EINVAL => unknown error, or NULL or empty string returned
* ERANGE => buffer too small !
*
* NB: sets errno to EINVAL if no other error is set, and the result is either
* NULL or '\0'.
*
* NB: POSIX recommends if the errno is not known, that errno be set to EINVAL
* *and* a message is returned saying "unknown", or something more helpful.
*
* Caller can accept what was provided (if anything) or act on EINVAL and
* make up their own error message.
*/
static const char*
qstrerror_r(int err, char *buf, size_t n)
{
const char* errm ;
buf[0] = '\0' ; /* in case nothing returned */
errno = 0 ; /* make sure */
#if STRERROR_R_CHAR_P
errm = strerror_r(err, buf, n) ;
#else
if (strerror_r(err, buf, n) >= 0)
errno = 0 ; /* Should be, in any case ! */
errm = buf ;
#endif
if ((errno == 0) && ((errm == NULL) || (*errm == '\0')))
{
errno = EINVAL ;
errm = NULL ;
} ;
return errm ;
} ;
/*------------------------------------------------------------------------------
* Local wrapper for strerror().
*
* The difficulty is that strerror() is underspecified, and may or may not
* return something or may return an error if the errno is not known.
*
* Returns: string if one was returned, and was not empty.
* NULL otherwise
*
* In all cases: errno == 0 <=> OK
* errno != 0 <=> some sort of error
*
* EINVAL => unknown error, or NULL or empty string returned
* ERANGE => buffer too small !
*
* NB: sets errno to EINVAL if no other error is set, and the result is either
* NULL or '\0'.
*
* NB: POSIX recommends if the errno is not known, that errno be set to EINVAL
* *and* a message is returned saying "unknown", or something more helpful.
*
* Caller can accept what was provided (if anything) or act on EINVAL and
* make up their own error message.
*/
static const char*
qstrerror(int err)
{
const char* errm ;
errno = 0 ;
errm = strerror(err) ;
if ((errno == 0) && ((errm == NULL) || (*errm == '\0')))
{
errno = EINVAL ;
errm = NULL ;
} ;
return errm ;
} ;
|