- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Getting error codes from system calls, in C
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-06-2007 06:15 AM
тАО07-06-2007 06:15 AM
...
mt -t $TAPE | grep 'BOT ONLINE'
errCode=$?
... other stuff
return $errCode
I'd like to recode it in C, but I'm not sure how to get the error code.
I can do
...
system("mt -t $TAPE | grep 'BOT ONLINE'");
...
. but then what?
I tried a small example:
...
#include
...
system("mt -t $TAPE status");
printf("err code=%d\n", errno)
...
. but it seems to return 0 no matter what the tape status is.
The script does a backup, first checking the tape, and then:
call system("dump -0 -f /dev/rmt/0h /important/files");
Solved! Go to Solution.
- Tags:
- system
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-06-2007 06:55 AM
тАО07-06-2007 06:55 AM
Solution#include
int call_system(char *cmd)
{
int status = 0;
status = system(cmd);
if (status != 0)
{
(void) printf(stderr,"Status %d\n",status);
}
return(status);
}
errno is only set when a system call (not the same as the system() function) fails.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-06-2007 06:57 AM
тАО07-06-2007 06:57 AM
Re: Getting error codes from system calls, in C
(void) fprintf(stderr,"Status %d\n",status);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-06-2007 08:07 AM
тАО07-06-2007 08:07 AM
Re: Getting error codes from system calls, in C
Calling system() is simply invoking the shell as a child process to perform some command. The return value from the system() call is essentially the exit status of the command as Clay clearly shows.
At most, you can also get the signal number, if any, (excepting SIGINT and SIGQUIT, which are ignored by system()), that caused the child to die.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-06-2007 01:50 PM
тАО07-06-2007 01:50 PM
Re: Getting error codes from system calls, in C
At least, I got 0 when the tape was ready, and 256 when it wasn't.
Unfortunately, I've uncovered a bigger problem: I'm trying to do this on a Red Hat Linux system that I'm doing backups for. For some odd reason, RHLinux doesn't seem to know about setuid. I set the ksh script to 4755 root:root, and when I run it as me, it says "effective UID=me". This is now a problem for the Red Hat SysAdmin.
It just might have to do with the way setuid is handled in different systems. There's a detailed writeup at
http://www.cs.berkeley.edu/~daw/papers/setuid-usenix02.pdf
"Setuid Demystified"
On the other hand, it may just be an unusual "safety feature" in RH Linux.
Anyway, it works the way you showed be here in HP-UX.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-06-2007 02:13 PM
тАО07-06-2007 02:13 PM
Re: Getting error codes from system calls, in C
#include
#include
#define assign_errno(x) ((errno != 0) ? errno : (x))
#define ROOT_USER 0
int call_system(char *cmd)
{
int status = 0;
status = setuid(ROOT_USER);
if (status == 0)
{
status = system(cmd);
if (status != 0) (void) fprintf(stderr,"Command '%s' failed; status %d\n",cmd,status);
}
else
{
status = assign_errno(-1);
(void) fprintf(stderr,"Setuid failed (%d)\n",status);
}
return(status);
} /* call_system */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-06-2007 02:15 PM
тАО07-06-2007 02:15 PM
Re: Getting error codes from system calls, in C
For a long time, 'setuid' shell scripts have been allowed on HP-UX. As you now know, this is not the case for all Unix variations. Fortunately, beginning with 11iv2, you have the ability to allow/disallow them with the kernel tunable 'secure_sid_scripts'.
If you must have a 'setuid' script, create a C-wrapper for it and set the 'setuid' bit for the wrapper.
http://www.docs.hp.com/en/B2355-60105/secure_sid_scripts.5.html
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2007 12:17 PM
тАО08-14-2007 12:17 PM
Re: Getting error codes from system calls, in C
The original problem was that the database did its backup to disk files, then I wanted to copy the files to tape, as root.
I fell back to a slightly less elegant solution.