2005-08-22 07:05:39 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* 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.
|
2005-08-22 07:05:39 +04:00
|
|
|
* 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.
|
2007-04-25 05:55:40 +04:00
|
|
|
* Copyright (c) 2007 Los Alamos National Security, LLC.
|
|
|
|
* All rights reserved.
|
2005-08-22 07:05:39 +04:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2005-08-22 07:05:39 +04:00
|
|
|
|
2005-09-05 00:54:19 +04:00
|
|
|
#ifdef HAVE_STRING_H
|
2005-08-22 07:05:39 +04:00
|
|
|
#include <string.h>
|
2005-09-05 00:54:19 +04:00
|
|
|
#endif
|
2005-08-22 07:05:39 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2005-09-05 00:54:19 +04:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2005-08-30 14:41:25 +04:00
|
|
|
#include <stdlib.h>
|
2005-09-05 00:54:19 +04:00
|
|
|
#endif
|
2005-08-22 07:05:39 +04:00
|
|
|
|
|
|
|
#include "opal/util/error.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2005-08-22 07:05:39 +04:00
|
|
|
|
|
|
|
#define MAX_CONVERTERS 5
|
2005-08-30 03:36:53 +04:00
|
|
|
#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;
|
|
|
|
|
2005-08-22 07:05:39 +04:00
|
|
|
/* all default to NULL */
|
2005-08-30 03:36:53 +04:00
|
|
|
converter_info_t converters[MAX_CONVERTERS];
|
2005-08-22 07:05:39 +04:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
opal_strerror_int(int errnum)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char *ret = NULL;
|
|
|
|
|
|
|
|
for (i = 0 ; i < MAX_CONVERTERS ; ++i) {
|
2005-08-30 03:36:53 +04:00
|
|
|
if (0 != converters[i].init) {
|
|
|
|
ret = converters[i].converter(errnum);
|
2005-08-22 07:05:39 +04:00
|
|
|
if (NULL != ret) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-30 03:36:53 +04:00
|
|
|
/* caller must free string */
|
|
|
|
static char*
|
|
|
|
opal_strerror_unknown(int errnum)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
for (i = 0 ; i < MAX_CONVERTERS ; ++i) {
|
|
|
|
if (0 != converters[i].init) {
|
|
|
|
if (errnum < converters[i].err_base &&
|
|
|
|
errnum > converters[i].err_max) {
|
|
|
|
asprintf(&ret, "Unknown error: %d (%s error %d)",
|
|
|
|
errnum, converters[i].project,
|
|
|
|
errnum - converters[i].err_base);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
asprintf(&ret, "Unknown error: %d", errnum);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-22 07:05:39 +04:00
|
|
|
void
|
|
|
|
opal_perror(int errnum, const char *msg)
|
|
|
|
{
|
|
|
|
const char* errmsg = opal_strerror_int(errnum);
|
|
|
|
|
|
|
|
if (NULL != msg && errnum != OPAL_ERR_IN_ERRNO) {
|
|
|
|
fprintf(stderr, "%s: ", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == errmsg) {
|
|
|
|
if (errnum == OPAL_ERR_IN_ERRNO) {
|
|
|
|
perror(msg);
|
|
|
|
} else {
|
2005-08-30 03:36:53 +04:00
|
|
|
char *ue_msg = opal_strerror_unknown(errnum);
|
|
|
|
fprintf(stderr, "%s\n", ue_msg);
|
|
|
|
free(ue_msg);
|
2005-08-22 07:05:39 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s\n", errmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
2005-08-30 03:36:53 +04:00
|
|
|
/* big enough to hold long version */
|
|
|
|
#define UNKNOWN_RETBUF_LEN 50
|
|
|
|
static char unknown_retbuf[UNKNOWN_RETBUF_LEN];
|
2005-08-22 07:05:39 +04:00
|
|
|
|
|
|
|
const char *
|
|
|
|
opal_strerror(int errnum)
|
|
|
|
{
|
2007-04-25 05:55:40 +04:00
|
|
|
const char* errmsg;
|
|
|
|
|
|
|
|
if (errnum == OPAL_ERR_IN_ERRNO) {
|
|
|
|
return strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
errmsg = opal_strerror_int(errnum);
|
2005-08-22 07:05:39 +04:00
|
|
|
|
|
|
|
if (NULL == errmsg) {
|
2007-04-25 05:55:40 +04:00
|
|
|
char *ue_msg = opal_strerror_unknown(errnum);
|
|
|
|
snprintf(unknown_retbuf, UNKNOWN_RETBUF_LEN, "%s", ue_msg);
|
|
|
|
free(ue_msg);
|
|
|
|
errno = EINVAL;
|
|
|
|
return (const char*) unknown_retbuf;
|
2005-08-22 07:05:39 +04:00
|
|
|
} else {
|
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
opal_strerror_r(int errnum, char *strerrbuf, size_t buflen)
|
|
|
|
{
|
|
|
|
const char* errmsg = opal_strerror_int(errnum);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (NULL == errmsg) {
|
|
|
|
if (errnum == OPAL_ERR_IN_ERRNO) {
|
2005-08-24 17:44:57 +04:00
|
|
|
char *tmp = strerror(errno);
|
|
|
|
strncpy(strerrbuf, tmp, buflen);
|
|
|
|
return OPAL_SUCCESS;
|
2005-08-22 07:05:39 +04:00
|
|
|
} else {
|
2005-08-30 03:36:53 +04:00
|
|
|
char *ue_msg = opal_strerror_unknown(errnum);
|
|
|
|
ret = snprintf(strerrbuf, buflen, "%s", ue_msg);
|
|
|
|
free(ue_msg);
|
2005-08-22 07:05:39 +04:00
|
|
|
if (ret > (int) buflen) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
} else {
|
2005-08-30 03:36:53 +04:00
|
|
|
errno = EINVAL;
|
2005-08-22 07:05:39 +04:00
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = snprintf(strerrbuf, buflen, "%s", errmsg);
|
|
|
|
if (ret > (int) buflen) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
} else {
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-08-30 03:36:53 +04:00
|
|
|
opal_error_register(const char *project, int err_base, int err_max,
|
|
|
|
opal_err2str_fn_t converter)
|
2005-08-22 07:05:39 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < MAX_CONVERTERS ; ++i) {
|
2005-08-30 03:36:53 +04:00
|
|
|
if (0 == converters[i].init) {
|
|
|
|
converters[i].init = 1;
|
|
|
|
strncpy(converters[i].project, project, MAX_CONVERTER_PROJECT_LEN);
|
2008-08-06 18:53:43 +04:00
|
|
|
converters[i].project[MAX_CONVERTER_PROJECT_LEN-1] = '\0';
|
2005-08-30 03:36:53 +04:00
|
|
|
converters[i].err_base = err_base;
|
|
|
|
converters[i].err_max = err_max;
|
|
|
|
converters[i].converter = converter;
|
2005-08-22 07:05:39 +04:00
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|