system() fails

Hi,

I have the following code which fails with return value 1.

sprintf(tmp, "rm -rf %s/* 2>/dev/null\n", dest);
rc = system( tmp );
rc is 1

The files exist and the paths are correct. I can do a manual copy but the application fails. All the calls to system() function fail with the same return code. If I restart the application, this works fine.

On running truss, following is the output.
ioctl is failing with EIO error (Some Physical I/O error).

What could be the reason for this failure?

Thanks!

8287: 14.1724 vfork() = 16381
16381: 14.1724 vfork() (returning as child ...) = 8287
16381: 14.1777 lwp_sigmask(SIG_SETMASK, 0x00020000, 0x00000000) = 0xFFBFFEFF [0x0000FFFF]
16381: 14.1778 lwp_sigmask(SIG_SETMASK, 0x00020000, 0x00000000) = 0x00020000 [0x00000000]
8287: 14.1783 lwp_sigmask(SIG_SETMASK, 0x00020000, 0x00000000) = 0xFFBFFEFF [0x0000FFFF]
16381: 14.1789 execve("/bin/sh", 0xFFBFAA84, 0xFFBFDDAC) argc = 3
16381: rm -rf /usr/Dir_cp/* 2>>/usr/logs/audit080725
.
.
.
16381: 14.1881 getuid() = 110 [110]
16381: 14.1882 getuid() = 110 [110]
16381: 14.1882 getgid() = 110 [110]
16381: 14.1882 getgid() = 110 [110]
16381: 14.1884 ioctl(2, TCGETA, 0xFFBFDB8C) Err#5 EIO
16381: 14.1884 Received signal #1, SIGHUP [caught][/b]
16381: 14.1886 schedctl() = 0xFF26C000
16381: 14.1886 sigfillset(0xFF36FB20) = 0
16381: 14.1887 lwp_sigmask(SIG_SETMASK, 0x00020001, 0x00000000) = 0xFFBFFEFF [0x0000FFFF]
16381: 14.1887 lwp_sigmask(SIG_SETMASK, 0x00020000, 0x00000000) = 0xFFBFFEFF [0x0000FFFF]
16381: 14.1888 sigaction(SIGHUP, 0xFFBFD5C0, 0xFFBFD660) = 0
16381: 14.1888 kill(16381, SIGHUP) = 0
16381: 14.1889 Received signal #1, SIGHUP [default]
16381: siginfo: SIGHUP pid=16381 uid=110
8287: 14.1893 waitid(P_PID, 16381, 0xFFBFA928, WEXITED|WTRAPPED) = 0
8287: 14.1894 sigaction(SIGINT, 0xFFBFA968, 0x00000000) = 0
8287: 14.1895 sigaction(SIGQUIT, 0xFFBFA968, 0x00000000) = 0
8287: 14.1895 schedctl() = 0xFED14000
8287: 14.1896 lwp_sigmask(SIG_SETMASK, 0x00020000, 0x00000000) = 0xFFBFFEFF [0x0000FFFF]

Do you have permission to remove those files?
I don't know how your code looks like, but you can restrict your code to some lines to try it out, something like:

#include <stdio.h>

int main()
{
  char tmp[128];
  char *dest = "/usr/Dir_cp";
  int rc;

  sprintf(tmp, "rm -rf %s/* 2>/dev/null\n", dest);
  printf("Command : %s\n", tmp);

  rc = system(tmp);
  printf("Exit code system command: %d\n", rc);

}

Regards

what is the value of dest ?

This ioctl is failing with a control terminal error as to be expected from a redirection inside the child without a controlling terminal with normal attributes for the exec'd shell.
If I may ask: Why would you want to use system for this and not unlink()?

Franklin52,

Sufficient permissions are present to remove the files, copy etc.
I did capture, tmp and rc,
tmp is
rm -rf /usr/Dir_cp/* 2>>/usr/logs/audit080725
rc is 1.

This occurs intermittently and application restart solves the issue.

Sivaswami,
Dest is "/usr/Dir_cp"

ramen_noodle,

I think unlink is used for removing linked files.
system() is used not only for remove operation but for copy etc in this application.

ramen is telling you about 3-4 things.

The command run system() does not have stderr to redirect to without causing an error.
That is your error.

unlink() and remove() are how you delete files in UNIX/C. Not system(). try man unlink before you decide it is for removing what you seem to think a link might be. A link is a directory entry that points to an inode, by the way. That is usually an actual file.

Finally he is telling you that system() is a bad choice. Don't use it for things you can do in C. There are a lot of reasons for this: from security to performance, to 'why not just use shell instead of C?'

And next to last, you should be using the macros in sys/wait.h to test the return code of system() - you should consider: WIFEXITED(), WEXITSTATUS(), and so on. There are reasons for this, like the LSB of the return code, only, has meaning. You need to ignore the other bytes.

And last - don't you imagine that maybe all of the commands like: cp, mv, rm -- are written in C to start with? And that some of them are system calls, so that running them in C is an order of magnitude more efficient. By the way what system call do suppose rm uses? Hint: ramen named it.

jim mcnamara,

I didn't know much about this, so couldn't get what ramen_noodle was pointing to, thanks for elaborating.
So rm uses unlink.

Thanks.