File: error.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (238 lines) | stat: -rw-r--r-- 7,098 bytes parent folder | download | duplicates (5)
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
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2007-2015 Los Alamos National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2015      Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * Copyright (c) 2017      FUJITSU LIMITED.  All rights reserved.
 * Copyright (c) 2017      IBM Corporation. All rights reserved.
 * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "opal/constants.h"
#include "opal/runtime/opal_params.h"
#include "opal/util/error.h"
#include "opal/util/output.h"
#include "opal/util/printf.h"
#include "opal/util/proc.h"
#include "opal/util/string_copy.h"

#define MAX_CONVERTERS            5
#define MAX_CONVERTER_PROJECT_LEN 10

struct converter_info_t {
    int init;
    char project[MAX_CONVERTER_PROJECT_LEN];
    int err_base;
    int err_max;
    opal_err2str_fn_t converter;
};
typedef struct converter_info_t converter_info_t;

/* all default to NULL */
static converter_info_t converters[MAX_CONVERTERS] = {{0}};

static int opal_strerror_int(int errnum, const char **str)
{
    int i, ret = OPAL_SUCCESS;
    *str = NULL;

    for (i = 0; i < MAX_CONVERTERS; ++i) {
        if (0 != converters[i].init && errnum < converters[i].err_base
            && converters[i].err_max < errnum) {
            ret = converters[i].converter(errnum, str);
            break;
        }
    }

    return ret;
}

/* caller must free string */
static int opal_strerror_unknown(int errnum, char **str)
{
    int i;
    *str = NULL;

    for (i = 0; i < MAX_CONVERTERS; ++i) {
        if (0 != converters[i].init) {
            if (errnum < converters[i].err_base && errnum > converters[i].err_max) {
                opal_asprintf(str, "Unknown error: %d (%s error %d)", errnum, converters[i].project,
                              errnum - converters[i].err_base);
                return OPAL_SUCCESS;
            }
        }
    }

    opal_asprintf(str, "Unknown error: %d", errnum);

    return OPAL_SUCCESS;
}

void opal_perror(int errnum, const char *msg)
{
    int ret;
    const char *errmsg;
    ret = opal_strerror_int(errnum, &errmsg);

    if (NULL != msg && errnum != OPAL_ERR_IN_ERRNO) {
        fprintf(stderr, "%s: ", msg);
    }

    if (OPAL_SUCCESS != ret) {
        if (errnum == OPAL_ERR_IN_ERRNO) {
            perror(msg);
        } else {
            char *ue_msg;
            ret = opal_strerror_unknown(errnum, &ue_msg);
            fprintf(stderr, "%s\n", ue_msg);
            free(ue_msg);
        }
    } else {
        fprintf(stderr, "%s\n", errmsg);
    }

    fflush(stderr);
}

/* big enough to hold long version */
#define UNKNOWN_RETBUF_LEN 50
static char unknown_retbuf[UNKNOWN_RETBUF_LEN];

const char *opal_strerror(int errnum)
{
    int ret;
    const char *errmsg;

    if (errnum == OPAL_ERR_IN_ERRNO) {
        return strerror(errno);
    }

    ret = opal_strerror_int(errnum, &errmsg);

    if (OPAL_SUCCESS != ret) {
        char *ue_msg;
        ret = opal_strerror_unknown(errnum, &ue_msg);
        snprintf(unknown_retbuf, UNKNOWN_RETBUF_LEN, "%s", ue_msg);
        free(ue_msg);
        errno = EINVAL;
        return (const char *) unknown_retbuf;
    } else {
        return errmsg;
    }
}

int opal_strerror_r(int errnum, char *strerrbuf, size_t buflen)
{
    const char *errmsg;
    int ret, len;

    ret = opal_strerror_int(errnum, &errmsg);
    if (OPAL_SUCCESS != ret) {
        if (errnum == OPAL_ERR_IN_ERRNO) {
            char *tmp = strerror(errno);
            opal_string_copy(strerrbuf, tmp, buflen);
            return OPAL_SUCCESS;
        } else {
            char *ue_msg;
            ret = opal_strerror_unknown(errnum, &ue_msg);
            len = snprintf(strerrbuf, buflen, "%s", ue_msg);
            free(ue_msg);
            if (len > (int) buflen) {
                errno = ERANGE;
                return OPAL_ERR_OUT_OF_RESOURCE;
            } else {
                errno = EINVAL;
                return OPAL_SUCCESS;
            }
        }
    } else {
        len = snprintf(strerrbuf, buflen, "%s", errmsg);
        if (len > (int) buflen) {
            errno = ERANGE;
            return OPAL_ERR_OUT_OF_RESOURCE;
        } else {
            return OPAL_SUCCESS;
        }
    }
}

int opal_error_register(const char *project, int err_base, int err_max, opal_err2str_fn_t converter)
{
    int i;

    for (i = 0; i < MAX_CONVERTERS; ++i) {
        if (0 == converters[i].init) {
            converters[i].init = 1;
            opal_string_copy(converters[i].project, project, MAX_CONVERTER_PROJECT_LEN);
            converters[i].project[MAX_CONVERTER_PROJECT_LEN - 1] = '\0';
            converters[i].err_base = err_base;
            converters[i].err_max = err_max;
            converters[i].converter = converter;
            return OPAL_SUCCESS;
        } else if (converters[i].err_base == err_base && converters[i].err_max == err_max
                   && !strcmp(project, converters[i].project)) {
            converters[i].converter = converter;
            return OPAL_SUCCESS;
        }
    }

    return OPAL_ERR_OUT_OF_RESOURCE;
}

void opal_delay_abort(void)
{
    // Though snprintf and strlen are not guaranteed to be async-signal-safe
    // in POSIX, it is async-signal-safe on many implementations probably.

    if (0 != opal_abort_delay) {
        int delay = opal_abort_delay;
        pid_t pid = getpid();
        char msg[100 + OPAL_MAXHOSTNAMELEN];

        if (delay < 0) {
            snprintf(msg, sizeof(msg),
                     "[%s:%05d] Looping forever "
                     "(MCA parameter opal_abort_delay is < 0)\n",
                     opal_process_info.nodename, (int) pid);
        } else {
            snprintf(msg, sizeof(msg), "[%s:%05d] Delaying for %d seconds before aborting\n",
                     opal_process_info.nodename, (int) pid, delay);
        }

        opal_best_effort_write(STDERR_FILENO, msg, strlen(msg));

        if (delay < 0) {
            while (1) {
                sleep(5);
            }
        } else {
            do {
                sleep(1);
            } while (--delay > 0);
        }
    }
}