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"
|
2010-05-18 03:08:56 +04:00
|
|
|
#include "opal/util/opal_sos.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
|
|
|
|
2011-02-13 19:09:17 +03:00
|
|
|
static int
|
|
|
|
opal_strerror_int(int errnum, const char **str)
|
2005-08-22 07:05:39 +04:00
|
|
|
{
|
2011-02-24 06:02:48 +03:00
|
|
|
int i, ret = OPAL_SUCCESS;
|
2011-02-13 19:09:17 +03:00
|
|
|
*str = NULL;
|
2005-08-22 07:05:39 +04:00
|
|
|
|
|
|
|
for (i = 0 ; i < MAX_CONVERTERS ; ++i) {
|
2011-08-08 08:10:40 +04:00
|
|
|
if (0 != converters[i].init &&
|
|
|
|
errnum < converters[i].err_base &&
|
|
|
|
converters[i].err_max < errnum) {
|
2011-02-13 19:09:17 +03:00
|
|
|
ret = converters[i].converter(errnum, str);
|
2011-08-08 08:10:40 +04:00
|
|
|
break;
|
2005-08-22 07:05:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-30 03:36:53 +04:00
|
|
|
/* caller must free string */
|
2011-02-13 19:09:17 +03:00
|
|
|
static int
|
2011-02-14 22:29:09 +03:00
|
|
|
opal_strerror_unknown(int errnum, char **str)
|
2005-08-30 03:36:53 +04:00
|
|
|
{
|
|
|
|
int i;
|
2011-02-13 19:09:17 +03:00
|
|
|
*str = NULL;
|
2005-08-30 03:36:53 +04:00
|
|
|
|
|
|
|
for (i = 0 ; i < MAX_CONVERTERS ; ++i) {
|
|
|
|
if (0 != converters[i].init) {
|
|
|
|
if (errnum < converters[i].err_base &&
|
|
|
|
errnum > converters[i].err_max) {
|
2011-02-13 19:09:17 +03:00
|
|
|
asprintf(str, "Unknown error: %d (%s error %d)",
|
2005-08-30 03:36:53 +04:00
|
|
|
errnum, converters[i].project,
|
|
|
|
errnum - converters[i].err_base);
|
2011-02-13 19:09:17 +03:00
|
|
|
return OPAL_SUCCESS;
|
2005-08-30 03:36:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 19:09:17 +03:00
|
|
|
asprintf(str, "Unknown error: %d", errnum);
|
2005-08-30 03:36:53 +04:00
|
|
|
|
2011-02-13 19:09:17 +03:00
|
|
|
return OPAL_SUCCESS;
|
2005-08-30 03:36:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-22 07:05:39 +04:00
|
|
|
void
|
|
|
|
opal_perror(int errnum, const char *msg)
|
|
|
|
{
|
2011-02-13 19:09:17 +03:00
|
|
|
int ret;
|
|
|
|
const char* errmsg;
|
|
|
|
ret = opal_strerror_int(errnum, &errmsg);
|
2005-08-22 07:05:39 +04:00
|
|
|
|
2010-05-18 03:08:56 +04:00
|
|
|
if (NULL != msg && OPAL_SOS_GET_ERROR_CODE(errnum) != OPAL_ERR_IN_ERRNO) {
|
2005-08-22 07:05:39 +04:00
|
|
|
fprintf(stderr, "%s: ", msg);
|
|
|
|
}
|
|
|
|
|
2011-02-13 19:09:17 +03:00
|
|
|
if (OPAL_SUCCESS != ret) {
|
2010-05-18 03:08:56 +04:00
|
|
|
if (OPAL_SOS_GET_ERROR_CODE(errnum) == OPAL_ERR_IN_ERRNO) {
|
2005-08-22 07:05:39 +04:00
|
|
|
perror(msg);
|
|
|
|
} else {
|
2011-02-14 22:29:09 +03:00
|
|
|
char *ue_msg;
|
2011-02-13 19:09:17 +03:00
|
|
|
ret = opal_strerror_unknown(errnum, &ue_msg);
|
2005-08-30 03:36:53 +04:00
|
|
|
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)
|
|
|
|
{
|
2011-02-13 19:09:17 +03:00
|
|
|
int ret;
|
2007-04-25 05:55:40 +04:00
|
|
|
const char* errmsg;
|
|
|
|
|
2011-01-21 02:21:38 +03:00
|
|
|
if (OPAL_SOS_GET_ERROR_CODE(errnum) == OPAL_ERR_IN_ERRNO) {
|
2007-04-25 05:55:40 +04:00
|
|
|
return strerror(errno);
|
|
|
|
}
|
|
|
|
|
2011-02-13 19:09:17 +03:00
|
|
|
ret = opal_strerror_int(errnum, &errmsg);
|
2005-08-22 07:05:39 +04:00
|
|
|
|
2011-02-13 19:09:17 +03:00
|
|
|
if (OPAL_SUCCESS != ret) {
|
2011-02-14 22:29:09 +03:00
|
|
|
char *ue_msg;
|
2011-02-13 19:09:17 +03:00
|
|
|
ret = opal_strerror_unknown(errnum, &ue_msg);
|
2007-04-25 05:55:40 +04:00
|
|
|
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)
|
|
|
|
{
|
2011-02-13 19:09:17 +03:00
|
|
|
const char* errmsg;
|
|
|
|
int ret, len;
|
2005-08-22 07:05:39 +04:00
|
|
|
|
2011-02-13 19:09:17 +03:00
|
|
|
ret = opal_strerror_int(errnum, &errmsg);
|
|
|
|
if (OPAL_SUCCESS != ret) {
|
2010-05-18 03:08:56 +04:00
|
|
|
if (OPAL_SOS_GET_ERROR_CODE(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 {
|
2011-02-14 22:29:09 +03:00
|
|
|
char *ue_msg;
|
2011-02-13 19:09:17 +03:00
|
|
|
ret = opal_strerror_unknown(errnum, &ue_msg);
|
|
|
|
len = snprintf(strerrbuf, buflen, "%s", ue_msg);
|
2005-08-30 03:36:53 +04:00
|
|
|
free(ue_msg);
|
2011-02-13 19:09:17 +03:00
|
|
|
if (len > (int) buflen) {
|
2005-08-22 07:05:39 +04:00
|
|
|
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 {
|
2011-02-13 19:09:17 +03:00
|
|
|
len = snprintf(strerrbuf, buflen, "%s", errmsg);
|
|
|
|
if (len > (int) buflen) {
|
2005-08-22 07:05:39 +04:00
|
|
|
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;
|
|
|
|
}
|