summaryrefslogtreecommitdiffstats
path: root/lib/pthread_safe.c
blob: b6c429e49b4aefe984092546ea9f5d6be6b5bfcd (plain)
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
/* 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 storeage to provide buufers 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 "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 "qfstring.h"
#include "errno_names.h"

/* prototypes */
static void destructor(void* data);
static char * thread_buff(void);

static pthread_key_t tsd_key;
static const int buff_size = 1024;

/* 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");
    }
}

/* Clean up */
void
safe_finish(void)
{
  if (qpthreads_enabled)
    pthread_key_delete(tsd_key);
}

/* called when thread terminates, clean up */
static void
destructor(void* data)
{
  XFREE(MTYPE_TSD, data);
}

/* 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)
{
  static const char * unknown = "Unknown error";
  if (qpthreads_enabled)
    {
      char * buff = thread_buff();
      int ret = strerror_r(errnum, buff, buff_size);

      return (ret >= 0)
          ? buff
          : unknown;
    }
  else
    {
      const char *s = strerror(errnum);
      return (s != NULL)
          ? s
          : unknown;
    }
}

/*------------------------------------------------------------------------------
 * Construct string to describe the given error of the form:
 *
 *   ENAME '<strerror>'
 *
 * where <strerror> is given by strerror() or, if pthreads_enabled, strerror_r()
 *
 * Truncates the result to the given len (0 => no truncation).  But silently
 * imposes the maximum length allowed by the strerror_t.
 *
 * If has to truncate, places "..." at the end of the message to show this has
 * happened.
 *
 * If a name is not known then (assuming strerror() won't know it either):
 *
 *   ERRNO=999 *unknown error*
 *
 * For err==0 returns:
 *
 *   EOK 'no error'
 *
 * Thread safe replacement for strerror.  Never returns NULL.
 */
static void
errtox(strerror_t* st, int err, int len, int want)
{
  qf_str_t qfs ;

  const char* q ;
  int ql ;

  /* Prepare.                                                   */
  int errno_saved = errno ;

  if ((len <= 0) || (len >= (int)sizeof(st->str)))
    len = sizeof(st->str) - 1 ;
  qfs_init(&qfs, st->str, len + 1) ;

  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      */
      int   ret ;
      const char* errm ;

      if (err == 0)
        errm = "no error" ;
      else
        {
          if (qpthreads_enabled)
            {
              /* 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.
               *
               * A huge buffer is used to get the message, and that is later
               * truncated, if necessary, to fit in the strerror_t structure.
               */

              buf[0] = '\0' ;           /* make sure starts empty       */
              ret  = strerror_r(err, buf, sizeof(buf)) ;
              errm = buf ;
              if (ret != 0)
                ret = errno ;
            }
          else
            {
              /* POSIX says that strerror *will* return something, but it is
               * known that it may return NULL if the error number is not
               * recognised.
               */
              errno = 0 ;
              errm = strerror(err) ;
              ret  = errno ;
              if ((ret == 0) && ((errm == NULL) || (*errm == '\0')))
                ret = EINVAL ;
            } ;

          /* Deal with errors, however exotic.                          */
          if (ret != 0)
            {
              q  = "*" ;
              ql = 2 ;          /* force "*" "quotes"   */
              if      (ret == EINVAL)
                errm = "unknown error" ;
              else if (ret == ERANGE)
                {
                  if (*errm == '\0')
                    errm = "vast error message" ;
                }
              else
                {
                  qf_str_t qfs_b ;
                  qfs_init(&qfs_b, buf, sizeof(buf)) ;
                  qfs_printf(&qfs_b, "strerror%s(%d) returned error %d",
                                      qpthreads_enabled ? "_r" : "", err, ret) ;
                  errm = buf ;
                } ;
            } ;
        } ;

      /* Add strerror to the result... looking out for overflow.        */
      len = strlen(errm) ;

      if ((len + ql) <= qfs_left(&qfs)) /* accounting for "quotes"      */
        qfs_printf(&qfs, "%s%s%s", q, errm, q) ;
      else
        qfs_printf(&qfs, "%s%.*s...%s", q, qfs_left(&qfs) - ql - 3, errm, q) ;
                                        /* -ve precision is ignored !   */
    } ;

  /* Put back errno                                                     */
  errno = errno_saved ;
} ;

/*------------------------------------------------------------------------------
 * Construct string to describe the given error of the form:
 *
 *   ENAME '<strerror>'
 *
 * where <strerror> is given by strerror() or, if pthreads_enabled, strerror_r()
 *
 * Truncates the result to the given len (0 => no truncation).  But silently
 * imposes the maximum length allowed by the strerror_t.
 *
 * If has to truncate, places "..." at the end of the message to show this has
 * happened.
 *
 * If a name is not known then (assuming strerror() won't know it either):
 *
 *   ERRNO=999 *unknown error*
 *
 * For err==0 returns:
 *
 *   EOK 'no error'
 *
 * Thread safe replacement for strerror.  Never returns NULL.
 */
extern strerror_t
errtoa(int err, int len)
{
  strerror_t  st ;

  errtox(&st, err, len, 3) ;  /* name and message     */

  return st ;
} ;

/*------------------------------------------------------------------------------
 * Convert error number to its name

 * If a name is not known then:
 *
 *    ERRNO=999
 *
 * For err==0 returns:
 *
 *   EOK
 *
 * Truncates the result to the given len (0 => no truncation).  But silently
 * imposes the maximum length allowed by the strerror_t.
 *
 * This is thread-safe.
 */
extern strerror_t
errtoname(int err, int len)
{
  strerror_t  st ;

  errtox(&st, err, len, 1) ;  /* name                 */

  return st ;
} ;

/*------------------------------------------------------------------------------
 * Alternative thread-safe safe_strerror()
 *
 * Truncates the result to the given len (0 => no truncation).  But silently
 * imposes the maximum length allowed by the strerror_t.
 *
 * If the <strerror> will not fit in the strerror_t, it is truncated and
 * '...' placed at the end to show this has happened.
 *
 * Thread safe replacement for strerror.  Never returns NULL.
 */
extern strerror_t
errtostr(int err, int len)
{
  strerror_t  st ;

  errtox(&st, err, len, 2) ;  /* message              */

  return st ;
} ;

/* 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;
}

/* 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;
}