errno.cpp 3.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
gejun's avatar
gejun committed
17

gejun's avatar
gejun committed
18 19 20
// Author: Ge,Jun (gejun@baidu.com)
// Date: Fri Sep 10 13:34:25 CST 2010

gejun's avatar
gejun committed
21
#include "butil/build_config.h"                        // OS_MACOSX
gejun's avatar
gejun committed
22 23 24 25 26
#include <errno.h>                                     // errno
#include <string.h>                                    // strerror_r
#include <stdlib.h>                                    // EXIT_FAILURE
#include <stdio.h>                                     // snprintf
#include <pthread.h>                                   // pthread_mutex_t
gejun's avatar
gejun committed
27
#include <unistd.h>                                    // _exit
28
#include "butil/scoped_lock.h"                         // BAIDU_SCOPED_LOCK
gejun's avatar
gejun committed
29

30
namespace butil {
gejun's avatar
gejun committed
31

32 33 34 35
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
36

37 38
const size_t ERROR_BUFSIZE = 64;
__thread char tls_error_buf[ERROR_BUFSIZE];
gejun's avatar
gejun committed
39 40 41 42 43

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) {
gejun's avatar
gejun committed
44 45
        // error() is a non-portable GNU extension that should not be used.
        fprintf(stderr, "Fail to define %s(%d) which is out of range, abort.",
gejun's avatar
gejun committed
46
              error_name, error_code);
gejun's avatar
gejun committed
47
        _exit(1);
gejun's avatar
gejun committed
48 49 50 51 52
    }
    const char* desc = errno_desc[error_code - ERRNO_BEGIN];
    if (desc) {
        if (strcmp(desc, description) == 0) {
            fprintf(stderr, "WARNING: Detected shared library loading\n");
53 54 55
            return -1;
        }
    } else {
gejun's avatar
gejun committed
56 57 58 59
#if defined(OS_MACOSX)
        const int rc = strerror_r(error_code, tls_error_buf, ERROR_BUFSIZE);
        if (rc != EINVAL)
#else
60
        desc = strerror_r(error_code, tls_error_buf, ERROR_BUFSIZE);
gejun's avatar
gejun committed
61 62 63 64
        if (desc && strncmp(desc, "Unknown error", 13) != 0)
#endif
        {
            fprintf(stderr, "Fail to define %s(%d) which is already defined as `%s', abort.",
65
                    error_name, error_code, desc);
gejun's avatar
gejun committed
66
            _exit(1);
gejun's avatar
gejun committed
67 68 69 70 71 72
        }
    }
    errno_desc[error_code - ERRNO_BEGIN] = description;
    return 0;  // must
}

73
}  // namespace butil
gejun's avatar
gejun committed
74 75 76

const char* berror(int error_code) {
    if (error_code == -1) {
gejun's avatar
gejun committed
77
        return "General error -1";
gejun's avatar
gejun committed
78
    }
79 80
    if (error_code >= butil::ERRNO_BEGIN && error_code < butil::ERRNO_END) {
        const char* s = butil::errno_desc[error_code - butil::ERRNO_BEGIN];
gejun's avatar
gejun committed
81 82 83
        if (s) {
            return s;
        }
gejun's avatar
gejun committed
84 85 86 87 88 89
#if defined(OS_MACOSX)
        const int rc = strerror_r(error_code, butil::tls_error_buf, butil::ERROR_BUFSIZE);
        if (rc == 0 || rc == ERANGE/*bufsize is not long enough*/) {
            return butil::tls_error_buf;
        }
#else
90
        s = strerror_r(error_code, butil::tls_error_buf, butil::ERROR_BUFSIZE);
gejun's avatar
gejun committed
91
        if (s) {
gejun's avatar
gejun committed
92 93
            return s;
        }
gejun's avatar
gejun committed
94
#endif
gejun's avatar
gejun committed
95
    }
96
    snprintf(butil::tls_error_buf, butil::ERROR_BUFSIZE,
gejun's avatar
gejun committed
97
             "Unknown error %d", error_code);
98
    return butil::tls_error_buf;
gejun's avatar
gejun committed
99 100 101 102 103
}

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