Errors From System Calls
- Errors are somewhat primitive but
- Most system calls return an error flag.
- Most system calls set a global variable
errno
- Read
man 2 open
- Look at the function prototype
- Look for RETURN VALUE it is quite a way down.
- a non-negative return value means success
- A value of -1 means error.
- And errno is set.
- Look at ERRORS
- EACCESS : opening a file that is not allowed.
- EINVAL: invalid flag, among others
- Look at the rest, wow quite a few.
- We will play with open now and investigate it fully soon.
- Explore further
- Look at
getpwent
- Look at
getpid
note the errors here.
- Look at
getpriority
: note -1 is a valid return value with no error, but could also be an error.
- The majority of calls return a -1 on error but you need to look.
- Even if they return other values through pass by pointer. (This is c)
- This violates one of the "EUP Programming Practices", which has sort of gone out of vogue with retirements.
- Our author warns us that we should check return values.
- These are indications that something has gone wrong.
- And we really shouldn't continue until it is fixed.
- Look at errorCode.cpp.
-
errno
- It is good to have :
#include <errno.h>
- Is a global variable.
- Is set only when a system error occurs.
- It is not reset when no error occurs.
- Reading and writing, ie cin, cout, invoke system calls.
-
void perror(const char *s);
- Needs:
#include <stdio.h>
- This will print an error message to standard err (cerr).
- It is based on the value of errno
- User Message: standard error description.
-
char *strerror(int errnum);
- Needs:
#include <string.h>
- Returns a pointer to a string.
- The string MUST NOT BE MODIFIED by the application.
- The string could be modified by future calls to strerror, so save it if you need it later.
- This is not your memory, don't free it.
- This is a common pattern.
-
const char *strerrordesc_np(int errnum);
- Needs:
#include <string.h>
- Returns a pointer to a string containing the name of the error.
- See restrictions above.