~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/util.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include "util.h"
19
19
#include <errno.h>
20
20
#include <limits.h>
 
21
#include <pthread.h>
21
22
#include <stdarg.h>
22
23
#include <stdint.h>
23
24
#include <stdio.h>
27
28
#include <unistd.h>
28
29
#include "byte-order.h"
29
30
#include "coverage.h"
30
 
#include "openvswitch/types.h"
 
31
#include "ovs-thread.h"
31
32
#include "vlog.h"
32
33
 
33
34
VLOG_DEFINE_THIS_MODULE(util);
37
38
/* argv[0] without directory names. */
38
39
const char *program_name;
39
40
 
40
 
/* Ordinarily "" but set to "monitor" for a monitor process or "worker" for a
41
 
 * worker process. */
42
 
const char *subprogram_name = "";
 
41
/* Name for the currently running thread or process, for log messages, process
 
42
 * listings, and debuggers. */
 
43
DEFINE_PER_THREAD_MALLOCED_DATA(char *, subprogram_name);
43
44
 
44
45
/* --version option output. */
45
46
static char *program_version;
46
47
 
 
48
/* Buffer used by ovs_strerror(). */
 
49
DEFINE_PER_THREAD_DATA(struct { char s[128]; }, strerror_buffer, { "" });
 
50
 
47
51
void
48
52
ovs_assert_failure(const char *where, const char *function,
49
53
                   const char *condition)
277
281
void
278
282
ovs_error_valist(int err_no, const char *format, va_list args)
279
283
{
 
284
    const char *subprogram_name = get_subprogram_name();
280
285
    int save_errno = errno;
281
286
 
282
287
    if (subprogram_name[0]) {
306
311
const char *
307
312
ovs_retval_to_string(int retval)
308
313
{
309
 
    static char unknown[48];
310
 
 
311
 
    if (!retval) {
312
 
        return "";
313
 
    }
314
 
    if (retval > 0) {
315
 
        return strerror(retval);
316
 
    }
317
 
    if (retval == EOF) {
318
 
        return "End of file";
319
 
    }
320
 
    snprintf(unknown, sizeof unknown, "***unknown return value: %d***", retval);
321
 
    return unknown;
 
314
    return (!retval ? ""
 
315
            : retval == EOF ? "End of file"
 
316
            : ovs_strerror(retval));
 
317
}
 
318
 
 
319
const char *
 
320
ovs_strerror(int error)
 
321
{
 
322
    enum { BUFSIZE = sizeof strerror_buffer_get()->s };
 
323
    int save_errno;
 
324
    char *buffer;
 
325
    char *s;
 
326
 
 
327
    save_errno = errno;
 
328
    buffer = strerror_buffer_get()->s;
 
329
 
 
330
#if STRERROR_R_CHAR_P
 
331
    /* GNU style strerror_r() might return an immutable static string, or it
 
332
     * might write and return 'buffer', but in either case we can pass the
 
333
     * returned string directly to the caller. */
 
334
    s = strerror_r(error, buffer, BUFSIZE);
 
335
#else  /* strerror_r() returns an int. */
 
336
    s = buffer;
 
337
    if (strerror_r(error, buffer, BUFSIZE)) {
 
338
        /* strerror_r() is only allowed to fail on ERANGE (because the buffer
 
339
         * is too short).  We don't check the actual failure reason because
 
340
         * POSIX requires strerror_r() to return the error but old glibc
 
341
         * (before 2.13) returns -1 and sets errno. */
 
342
        snprintf(buffer, BUFSIZE, "Unknown error %d", error);
 
343
    }
 
344
#endif
 
345
 
 
346
    errno = save_errno;
 
347
 
 
348
    return s;
322
349
}
323
350
 
324
351
/* Sets global "program_name" and "program_version" variables.  Should
340
367
                   const char *time)
341
368
{
342
369
    const char *slash = strrchr(argv0, '/');
 
370
 
 
371
    assert_single_threaded();
 
372
 
343
373
    program_name = slash ? slash + 1 : argv0;
344
374
 
345
375
    free(program_version);
356
386
    }
357
387
}
358
388
 
 
389
/* Returns the name of the currently running thread or process. */
 
390
const char *
 
391
get_subprogram_name(void)
 
392
{
 
393
    const char *name = subprogram_name_get();
 
394
    return name ? name : "";
 
395
}
 
396
 
 
397
/* Sets 'name' as the name of the currently running thread or process.  (This
 
398
 * appears in log messages.) */
 
399
void
 
400
set_subprogram_name(const char *name)
 
401
{
 
402
    free(subprogram_name_set(xstrdup(name)));
 
403
}
 
404
 
359
405
/* Returns a pointer to a string describing the program version.  The
360
406
 * caller must not modify or free the returned string.
361
407
 */
587
633
            int error = errno;
588
634
            free(buf);
589
635
            if (error != ERANGE) {
590
 
                VLOG_WARN("getcwd failed (%s)", strerror(error));
 
636
                VLOG_WARN("getcwd failed (%s)", ovs_strerror(error));
591
637
                return NULL;
592
638
            }
593
639
            size *= 2;
725
771
 
726
772
        linkname = xreadlink(fn);
727
773
        if (!linkname) {
728
 
            VLOG_WARN("%s: readlink failed (%s)", filename, strerror(errno));
 
774
            VLOG_WARN("%s: readlink failed (%s)",
 
775
                      filename, ovs_strerror(errno));
729
776
            return fn;
730
777
        }
731
778
 
843
890
#endif
844
891
 
845
892
/* Returns the number of 1-bits in 'x', between 0 and 32 inclusive. */
846
 
int
 
893
unsigned int
847
894
popcount(uint32_t x)
848
895
{
849
896
    /* In my testing, this implementation is over twice as fast as any other