apl logo

A.P. Lawrence

Information and Resources for Unix and Linux Systems



Printer Friendly Version
Please read this disclaimer
(OLDER) <- More Stuff -> (NEWER)
Default Style - Plain - No Border

Numeric Unix Error Messages

More Articles

It's an unfortunate fact that many programmers are lazy about error messages. Very often, all you get is a cryptic "Error 5", and you may be lucky to get that: sometimes all you get is an error return that you have to examine yourself with "echo $?". You can't even depend on that being the actual Unix error, but even if it is, what does it mean?

Well, every Unix/Linux system includes various ".h" files that describe the numeric errors returned by kernel system calls. Unfortunately, those files are only a little bit more illuminating than the numeric errors themselves. For example, here's a couple of lines from a Linux system:

#define EPERM            1      /* Operation not permitted */
...
#define EACCES          13      /* Permission denied */

What's the difference? When would you get one versus the other? This article attempts to more fully explain what these errors mean and to give examples of what might cause them.

I'm only going to look at the first 32 of these; there are many more, but these are the more common. Understand that the numeric codes can vary from Unix to Unix- you really need to look in the /usr/include files to find the symbolic names, and even those are used in slightly different ways- certain system calls on a BSD system, for example, will return a different error result than they will on a SysV Unix. However, most of that kind of thing is esoteric detail of concern only to programmers working on multiple platforms.

Even where the error numbers and the symbolic constants are the same, the comments may vary. For example, while SCO Unix and Linux systems would look almost exactly alike for the first 30 or 40 errors, some of the comments are markedly different, and higher numbered errors are defined completely differently. So, the thing to keep in mind is that just because you've seen a particular error on a particular platform doesn't mean it is the same somewhere else. There's also nothing that prtevents a programmer from misusing these constants in their own error returns, either through ignorance or simple misunderstanding of the historical use of these.

And it also means that the descriptions of what might cause a specific error are heavily dependent on that word "might". Please keep that in mind as you read this.

On a Linux system with source installed, you can cd to /usr/src/linux*/kernel and do a grep -l for the symbolic constant you are interested in. For example, here's the places where EPERM is referenced on a 7.2 Red Hat system:

acct.c
capability.c
fork.c
kmod.c
module.c
printk.c
ptrace.c
sched.c
signal.c
sys.c
sysctl.c
time.c
uid16.c

For other Unix systems, pawing through documentation is the only way. For this article, I used:

Unix Internals by Steve Pate
Unix Network Programming by W. Richard Stevens
The Magic Garden by Berny Goodhart and James Cox
Advanced Programming in the Unix Environment by W. Richard Stevens

Again, keep in mind that this is all examples, and may not apply to your specific platform. The system calls shown as examples may not be the only functions that will return these errors; you really need access to the source to know that.

Here's some odd ones:

#define     ENOPKG              65
#define     EISNAM             139

See Bofcusm/2342.html

Comments /Unixart/errors.html

Add your comments
UnixartErrors :





Heres a program that will display the error acronyms and string messages for any platform its compiled on.



Tested on OSR5.0.{56} and RH 6.1 Linux



-- hops



/*

* Print out err nums and msgs up to sys_nerr or hit null

* return

* Note sys_nerr not defined on some systems..

* Hops 27-Apr-95

* add ENO* display as well (osr5) 30-Mar-99

* Build on Linux (RH 6.1) Sept 2002

*/



#include

#include

#include



char *enostr(int a);

int estrmatch(char *s);



#ifdef ERRNO_SCOMAX /* overide past sys_errno */

#define MAX_ERRNO 501

#else

#define MAX_ERRNO sys_nerr

#endif



int

main(int ac, char **av)

{

int a;

char *s;



if (ac != 2 )

{

printf("Errno\tAcronym Msg String\n");

for (a=1; a < MAX_ERRNO; a++ )

{

s = strerror(a);

if (s == NULL || strcmp(s, "Unknown error")

== 0 )

break;

printf("%d\t%-10.10s %s\n", a, enostr(a),

strerror(a) );

}

printf("sys_nerr=%d\n", sys_nerr);

exit(0);

}



if (*av[1] == '-' )

{

printf("errno: print errno values\n");

printf("usage: errno [n | EvalueStr]\n");

exit(1);

}



a = atoi(av[1]);

if (a != 0 )

printf("%d\t%-10.10s %s\n", a, enostr(a),

strerror(a) );

else

{

a = estrmatch(av[1]);

printf("%d\t%-10.10s %s\n", a, enostr(a),

strerror(a) );

}

}



#define ENT(n) {n, #n}



struct e_ent {

int eno;

char *estr;

};



/* Table of errno CODES

* taken from Osr5.0.5 /usr/include/sys/errno.h

*/

struct e_ent etbl[] =

{

/* Error codes */

ENT(EPERM),

ENT(ENOENT),

ENT(ESRCH),

ENT(EINTR),

ENT(EIO),

ENT(ENXIO),

ENT(E2BIG),

ENT(ENOEXEC),

ENT(EBADF),

ENT(ECHILD),

ENT(EAGAIN),

ENT(ENOMEM),

ENT(EACCES),

ENT(EFAULT),

ENT(ENOTBLK),

ENT(EBUSY),

ENT(EEXIST),

ENT(EXDEV),

ENT(ENODEV),

ENT(ENOTDIR),

ENT(EISDIR),

ENT(EINVAL),

ENT(ENFILE),

ENT(EMFILE),

ENT(ENOTTY),

ENT(ETXTBSY),

ENT(EFBIG),

ENT(ENOSPC),

ENT(ESPIPE),

ENT(EROFS),

ENT(EMLINK),

ENT(EPIPE),

ENT(EDOM),

ENT(ERANGE),

ENT(ENOMSG),

ENT(EIDRM),

ENT(ECHRNG),

ENT(EL2NSYNC),

ENT(EL3HLT),

ENT(EL3RST),

ENT(ELNRNG),

ENT(EUNATCH),

ENT(ENOCSI),

ENT(EL2HLT),

ENT(EDEADLK),

ENT(ENOLCK),

ENT(EBADE),

ENT(EBADR),

ENT(EXFULL),

ENT(ENOANO),

ENT(EBADRQC),

ENT(EBADSLT),

ENT(EDEADLOCK),

ENT(EBFONT),



/* stream problems */

ENT(ENOSTR),

ENT(ENODATA),

ENT(ETIME),

ENT(ENOSR),



ENT(ENONET),

ENT(ENOPKG),

ENT(EREMOTE),

ENT(ENOLINK),

ENT(EADV),

ENT(ESRMNT),

ENT(ECOMM),

ENT(EPROTO),

ENT(EMULTIHOP),

#ifdef ELBIN

ENT(ELBIN),

#endif

ENT(EDOTDOT),

ENT(EBADMSG),

ENT(ENAMETOOLONG),

ENT(EOVERFLOW),

ENT(ENOTUNIQ),

ENT(EBADFD),

ENT(EREMCHG),



/* shared library problems */

ENT(ELIBACC),

ENT(ELIBBAD),

ENT(ELIBSCN),

ENT(ELIBMAX),

ENT(ELIBEXEC),

ENT(EILSEQ),



ENT(ENOSYS),



/* non-blocking and interrupt i/o */

ENT(EWOULDBLOCK),

ENT(EINPROGRESS),

ENT(EALREADY),



/* ipc/network software arg errors */

ENT(ENOTSOCK),

ENT(EDESTADDRREQ),

ENT(EMSGSIZE),

ENT(EPROTOTYPE),

ENT(EPROTONOSUPPORT),

ENT(ESOCKTNOSUPPORT),

ENT(EOPNOTSUPP),

ENT(EPFNOSUPPORT),

ENT(EAFNOSUPPORT),

ENT(EADDRINUSE),

ENT(EADDRNOTAVAIL),



/* ipc/network software arg - operational errors */

ENT(ENETDOWN),

ENT(ENETUNREACH),

ENT(ENETRESET),

ENT(ECONNABORTED),

ENT(ECONNRESET),

ENT(ENOBUFS),

ENT(EISCONN),

ENT(ENOTCONN),

ENT(ESHUTDOWN),

ENT(ETOOMANYREFS),

ENT(ETIMEDOUT),

ENT(ECONNREFUSED),

ENT(EHOSTDOWN),

ENT(EHOSTUNREACH),

ENT(ENOPROTOOPT),

/* Xenix */

ENT(EUCLEAN),

ENT(ENOTNAM),

ENT(ENAVAIL),

ENT(EISNAM),

ENT(EREMOTEIO),

#ifdef EINIT

ENT(EINIT),

#endif

#ifdef EREMDEV

ENT(EREMDEV),

#endif

ENT(ENOTEMPTY),

ENT(ELOOP),

ENT(ESTALE),

ENT(ERESTART),

ENT(ESTRPIPE),

#ifdef EIORESID

ENT(EIORESID)

#endif





#ifdef __linux

ENT(EWOULDBLOCK), /* 41 mapped to EAGAIN */

ENT(EDEADLOCK), /* 58 mapped to EDEADLK */

ENT(EUSERS), /* 87 Too many users */

ENT(EDQUOT), /* 122 Quota exceeded */

ENT(ENOMEDIUM), /* 123 No medium found */

ENT(EMEDIUMTYPE), /* 124 Wrong medium type */

#endif

};



/* lookup estr given number */

char *enostr(a)

{

int i;



for (i = 0; i < sizeof(etbl)/sizeof(etbl[0]); i++)

{

if (a == etbl[i].eno)

return etbl[i].estr;

}

return "unknown";

}



char *strupper(char *s)

{

char *s1 = s ;

while (*s1)

{

*s1 = toupper(*s1);

s1++;

}

return s;

}





/* return errno given str */

int estrmatch(char *e)

{

int i;



e = strupper(e);

for (i = 0; i < sizeof(etbl)/sizeof(etbl[0]); i++)

{

if ( *e == *(etbl[i].estr)

&& strcmp(e, etbl[i].estr) == 0 )

return etbl[i].eno;

}

return 0;

}















http://www.pixelbeat.org/scripts/strerror

Add your comments
Google

Related Articles

Have you tried Searching this site?

Please read this disclaimer

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

/Unixart/errors.html copyright December 2001 Tony Lawrence All Rights Reserved

Views for this page
TodayThis WeekThis MonthThis YearOverall
51386323,17423,023