errno.cpp 2.57 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2010 Baidu.com, Inc. All Rights Reserved
//
// Implement base/errno.h
// 
// Author: Ge,Jun (gejun@baidu.com)
// Date: Fri Sep 10 13:34:25 CST 2010

#include <errno.h>                                     // errno
#include <string.h>                                    // strerror_r
#include <stdlib.h>                                    // EXIT_FAILURE
#include <stdio.h>                                     // snprintf
#include <pthread.h>                                   // pthread_mutex_t
#include <error.h>                                     // error
#include "base/scoped_lock.h"                         // BAIDU_SCOPED_LOCK

namespace base {

18 19 20 21
const int ERRNO_BEGIN = -32768;
const int ERRNO_END = 32768;
static const char* errno_desc[ERRNO_END - ERRNO_BEGIN] = {};
static pthread_mutex_t modify_desc_mutex = PTHREAD_MUTEX_INITIALIZER;
gejun's avatar
gejun committed
22

23 24
const size_t ERROR_BUFSIZE = 64;
__thread char tls_error_buf[ERROR_BUFSIZE];
gejun's avatar
gejun committed
25 26 27 28 29 30 31 32 33 34 35 36 37

int DescribeCustomizedErrno(
    int error_code, const char* error_name, const char* description) {
    BAIDU_SCOPED_LOCK(modify_desc_mutex);
    if (error_code < ERRNO_BEGIN || error_code >= ERRNO_END) {
        error(EXIT_FAILURE, 0,
              "Fail to define %s(%d) which is out of range, abort.",
              error_name, error_code);
    }
    const char* desc = errno_desc[error_code - ERRNO_BEGIN];
    if (desc) {
        if (strcmp(desc, description) == 0) {
            fprintf(stderr, "WARNING: Detected shared library loading\n");
38 39 40 41 42 43 44 45
            return -1;
        }
    } else {
        desc = strerror_r(error_code, tls_error_buf, ERROR_BUFSIZE);
        if (desc && strncmp(desc, "Unknown error", 13) != 0) {
            error(EXIT_FAILURE, 0,
                    "Fail to define %s(%d) which is already defined as `%s', abort.",
                    error_name, error_code, desc);
gejun's avatar
gejun committed
46 47 48 49 50 51 52 53 54 55
        }
    }
    errno_desc[error_code - ERRNO_BEGIN] = description;
    return 0;  // must
}

}  // namespace base

const char* berror(int error_code) {
    if (error_code == -1) {
gejun's avatar
gejun committed
56
        return "General error -1";
gejun's avatar
gejun committed
57 58 59 60 61 62
    }
    if (error_code >= base::ERRNO_BEGIN && error_code < base::ERRNO_END) {
        const char* s = base::errno_desc[error_code - base::ERRNO_BEGIN];
        if (s) {
            return s;
        }
63
        s = strerror_r(error_code, base::tls_error_buf, base::ERROR_BUFSIZE);
gejun's avatar
gejun committed
64 65 66 67
        if (s) {  // strerror_r returns NULL if error_code is unknown
            return s;
        }
    }
68
    snprintf(base::tls_error_buf, base::ERROR_BUFSIZE,
gejun's avatar
gejun committed
69
             "Unknown error %d", error_code);
70
    return base::tls_error_buf;
gejun's avatar
gejun committed
71 72 73 74 75 76
}

const char* berror() {
    return berror(errno);
}