Commit ff8021a2 authored by Steven Murray's avatar Steven Murray
Browse files

sstrerror_r prototype now matches that of the XSI strerror_r

parent bb329357
......@@ -41,15 +41,18 @@ Errnum::Errnum(int err, std::string what):Exception("") {
}
void Errnum::ErrnumConstructorBottomHalf(const std::string & what) {
const char *const errorStr = sstrerror(m_errnum);
if (!errorStr) {
int new_errno = errno;
char buf[100];
if(sstrerror_r(m_errnum, buf, sizeof(buf))) {
// sstrerror_r() failed
const int new_errno = errno;
std::stringstream w;
w << "Errno=" << m_errnum << ". In addition, failed to read the corresponding error string (strerror gave errno="
w << "Errno=" << m_errnum << ". In addition, failed to read the corresponding error string (sstrerror_r gave errno="
<< new_errno << ")";
m_strerror = w.str();
} else {
m_strerror = errorStr;
// sstrerror_r() succeeded
m_strerror = buf;
}
std::stringstream w2;
if (what.size())
......
......@@ -42,15 +42,18 @@ SErrnum::SErrnum(int err, std::string what):Exception("") {
}
void SErrnum::SErrnumConstructorBottomHalf(const std::string & what) {
const char *const errorStr = sstrerror(m_serrnum);
if (!errorStr) {
int new_errno = errno;
char buf[100];
if(sstrerror_r(m_serrnum, buf, sizeof(buf))) {
// sstrerror_r() failed
const int new_errno = errno;
std::stringstream w;
w << "SErrno=" << m_serrnum << ". In addition, failed to read the corresponding error string (sstrerror gave errno="
w << "SErrno=" << m_serrnum << ". In addition, failed to read the corresponding error string (sstrerror_r gave errno="
<< new_errno << ")";
m_sstrerror = w.str();
} else {
m_sstrerror = errorStr;
// sstrerror_r() succeeded
m_sstrerror = buf;
}
std::stringstream w2;
if (what.size())
......
......@@ -228,7 +228,11 @@ void castor::vdqm::SocketHelper::checkCupvPermissions(
if(Cupv_check(uid, gid, clientHostname.c_str(), localHostname.c_str(),
privilege)) {
char buf[80];
sstrerror_r(serrno, buf, 80);
if(sstrerror_r(serrno, buf, sizeof(buf))) {
// sstrerror_r failed
snprintf(buf, sizeof(buf), "Unknown error");
buf[sizeof(buf) - 1] = '\0';
}
castor::exception::PermissionDenied pe;
pe.getMessage() << "Failed Cupv_check call. messageType=" << messageType
......
......@@ -491,10 +491,16 @@ char *sys_secerrlist[ESECMAXERR-ESECBASEOFF+2] =
*------------------------------------------------------------------------
*/
char * sstrerror_r(const int n, char *const buf, const size_t buflen) {
int sstrerror_r(const int n, char *const buf, const size_t buflen) {
char *tmpstr;
char strerror_r_buf[100];
if ( buf == NULL || buflen <= 0 ) return(NULL);
if ( buf == NULL || buflen <= 0 ) {
errno = EINVAL;
serrno = EINVAL;
return -1;
}
memset(buf,'\0',buflen);
tmpstr = NULL;
......@@ -608,14 +614,20 @@ char * sstrerror_r(const int n, char *const buf, const size_t buflen) {
*/
sprintf(buf, "%*s: %10d", (int)buflen-14, sys_serrlist[SEMAXERR+1-SEBASEOFF], n);
}
return(buf);
return 0;
}
void sperror(char *msg)
{
char buf[80];
if (serrno) {
fprintf(stderr,"%s: %s\n",msg,sstrerror_r(serrno,buf,80));
if(sstrerror_r(serrno, buf, sizeof(buf))) {
/* sstrerror_r() failed so just print msg */
fprintf(stderr,"%s\n",msg);
} else {
/* sstrerror_r() succeeded so print both msg and buf */
fprintf(stderr,"%s: %s\n", msg, buf);
}
} else {
perror(msg);
}
......@@ -623,11 +635,20 @@ void sperror(char *msg)
static int sstrerror_key = -1;
char *sstrerror(int n)
{
char *sstrerror(const int n) {
void *buf = NULL;
int buflen = 80;
Cglobals_get(&sstrerror_key,&buf,buflen);
return(sstrerror_r(n,(char *)buf,buflen));
if(Cglobals_get(&sstrerror_key, &buf, buflen)) {
return "Unknown error"
": No thread specific memory to determine error string"
": Cglobals_get() failed";
}
if(sstrerror_r(n,(char *)buf,buflen)) {
return "Unknown error"
": sstrerror_r() failed";
} else {
return (char *)buf;
}
}
......@@ -485,8 +485,35 @@ EXTERN_C int *C__serrno (void);
*/
#define serrno (*C__serrno())
EXTERN_C char *sstrerror_r (const int n, char *const buf, const size_t buflen);
EXTERN_C char *sstrerror (int);
/**
* Writes the string representation of the specified error code into the
* specified buffer.
*
* The string representation is truncated if the specified buffer is too small.
* In case of such a truncation the string written to the specified buffer is
* null terminated.
*
* @param n The numeric error code.
* @param buf The buffer to which the string representation should be written.
* @param buflen The length of the buffer.
* @return 0 on success or -1 on failure. In the case of a failure both errno
* and serrno are set to indicate the type of error.
*/
EXTERN_C int sstrerror_r (const int n, char *const buf, const size_t buflen);
/**
* Returns the string representation of the specified error code.
*
* Allocates some thread specfic memory if needed using Cglobals_get() and then
* passes that memory to sstrerror_r along with the specified error code.
*
* A string is always returned. This function never returns NULL.
*
* @param n The numeric error code.
* @return The string representation.
*/
EXTERN_C char *sstrerror (const int n);
EXTERN_C void sperror (char *);
extern char *sys_serrlist[]; /* Error text array */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment