0

I am writing a middleware application for Android Gingerbread.

Somewhere in my code, there is a line,

printf("--Executing command\n");
if (system ("rm -f /tmp/dump.txt")){}
printf("file deletion successful\n");

I get the prints till line, --Executing command , then it hangs.

When I did ps, I can see sh -c rm -f /tmp/dump.txt. The process is still there. Why my code is not moving past the system call or Why the system call hanged, as seen in the ps?

If there had been permission problems, I would have got an error.

EDIT

The call if (system ("rm -f /tmp/dump.txt")){} is actually deleting the file, as I verified through ls /tmp/ file is removed, but then its not coming out. It hangs.

EDIT

On searching few more, I found this. Can there be a similar issue? I am using Android Gingerbread, which is way too old.

Community
  • 1
  • 1
Naveen
  • 7,944
  • 12
  • 78
  • 165
  • So your question is "why doesn't the code I haven't shown you work, when the code I *have* shown you does?". That's not particularly easy to answer. What is *different* between the code you posted and the code that is displaying the unexpected behavior? – EOF Sep 18 '15 at 12:17
  • @EOF : Yes, this makes the matter more confusing. Let me edit the question. – Naveen Sep 18 '15 at 12:24
  • Did you try to `unlink()` the file? Or is there any specific reason to invoke a shell for this? – EOF Sep 18 '15 at 12:29
  • @EOF : `unlink()` can be a reason. Is there any way I can check the processes waiting on the file? Any system command? Because, it might be possible that first I have to close/kill the processes waiting on the file and then the delete command can proceed. – Naveen Sep 18 '15 at 12:45
  • `unlink()` doesn't delete the file. It decrements the file's reference count. If the reference count is low enough (no process has an open file-descriptor to the file and all hardlinks to the file are deleted), *then* the file is removed. – EOF Sep 18 '15 at 12:49
  • 1
    @EOF, `rm` doesn't delete the file either, in the sense of "delete" you are using. Indeed, what it does is `unlink()` it. Using `unlink()` directly is indeed the better alternative, but that just cuts out a few needless intermediaries; it does not fundamentally change what operation is performed. – John Bollinger Sep 18 '15 at 13:10
  • @InsaneCoder, the target file being open by other programs or even by the program unlinking it (whether directly or via `rm`) should not interfere with the removal, and *certainly* should not prevent `rm` from exiting and `system()` returning once the removal occurs. – John Bollinger Sep 18 '15 at 13:16
  • @InsaneCoder, is the process in which your error occurs multithreaded? Is there any chance that a different thread could simultaneously be blocked in `wait()`, `waitpid()`, or `waitid()`? – John Bollinger Sep 18 '15 at 13:19
  • @JohnBollinger : Yes my program is multithreaded and mutiple child processes are also involved. The strange thing is even if I delete some dummy file, say `rm -f dummy.txt`, which is nowhere created or used by the application, it is not able to delete that file alsp. It is hanging. – Naveen Sep 18 '15 at 13:25
  • @JohnBollinger: I am aware of what `rm` and `unlink` (and `remove()`) do. I was just suggesting a simpler, more debuggable way of removing the file. Shelling out is bad taste in my opinion. – EOF Sep 18 '15 at 13:38
  • @InsaneCoder, the `system()` function is implemented as a wrapper around a standard `fork` / `exec` / `wait` sequence. I was going to suggest that perhaps another thread was collecting the `fork()`ed child, thus leaving `system()` to hang, but in that case the `sh` process would not remain in the process list. Do take EOF's advice and just `unlink()` the file instead of launching `rm`. It may well be that that resolves the issue. – John Bollinger Sep 18 '15 at 13:47
  • @JohnBollinger : I will definitely try that but the main problem is hanging of `system()` call. Later I also have to do `system("killall myapp")`, which is also hanging. Once thing, I cannot understand is why doing `system()` is spawning a shell here. The calls are doing their task but simply not able to return, maybe the shell itself is not able to return and hence the call is getting hanged. Can somehow I make the calls non-blocking? – Naveen Sep 18 '15 at 14:02
  • @JohnBollinger : Using `unlink()` is deleting the file and proceeding ahead, but I am stuck at the next call, `system("killall myapp")`, as I expected in my previous comment. This problem has become really intersting now for me ;) Is there a similar workaround for `killall`? – Naveen Sep 18 '15 at 14:09
  • @InsaneCoder, `system()` spawns a shell in which to run the command because *that's what it is defined to do*. – John Bollinger Sep 18 '15 at 14:09
  • 1
    @InsaneCoder: `system("killall myapp")` is *probably* a horrible idea anyway. If your program requires it, is is **broken by design**. – EOF Sep 18 '15 at 14:19
  • I can't speak to why calls to `system()` are hanging, but this does not appear to be a known general issue. Most likely, then, this behavior is associated with something else your particular program is doing, or with some specific aspect of your development environment. This is the point where we request an MCVE. – John Bollinger Sep 18 '15 at 14:23

1 Answers1

0

Use unlink function instead of system:

http://man7.org/linux/man-pages/man2/unlink.2.html

For example (in that case): 

printf("--Executing command\n");
int result = unlink("/tmp/dump.txt");
if(result == 0)
{
    // unlink returns 0 on success
    printf("file deletion successful\n");
} else {
    perror("unlink"); // prints error to console (to stderr, you can skip this if you don't want to read detailed error");
    printf("file deletion failure");
}
krystian71115
  • 1,927
  • 17
  • 36