0

I need to return 0 after execve. Always, its not matter is there successfull or error. Just return 0. But after i use execve the program return nothing at all (execve return only errors and in my case there is no error, so nothing to return).

Here is my code example:

execve(program,args,null);
return(0); <-- this does not work!
Supply
  • 51
  • 2
  • 13
  • 5
    If `execve()` succeeds, there is no *after*. There's just a whole new program running. – Shawn Feb 08 '19 at 03:18
  • 3
    To put it another way, on success, `execve` *does not return*. There is no control flow downstream of `execve` except when an error occurs. If you want the original program to continue running, then you `fork` a child process in which to call `execve`. – John Bollinger Feb 08 '19 at 03:28
  • 1
    Would you expect to be able to return 0 after `exit()`? Of course not: your program isn't there any more. It's the same with the `exec` family (although for a different reason). – Steve Summit Feb 08 '19 at 03:51
  • You may be getting `exec` confused with `system` or `spawn`. Those do spawn a new subprocess to run the other program, and do return. But `exec` is different: it overlays the calling process, without spawning a new one. – Steve Summit Feb 08 '19 at 03:53
  • You need to post more of your program. Are you using fork() ? – Bartleby T. Scrivener Feb 08 '19 at 04:47
  • If `execve()` does fail and does return, then make sure you call `_exit` instead of `exit` as any of the `atexit` or `constructor/destructors` set will have been invalidated. – David C. Rankin Feb 08 '19 at 05:15
  • @DavidC.Rankin Does this mean you shouldn’t simply `return errno`? – user8393252 Apr 20 '23 at 02:48
  • @user8393252 if `execve()` succeeds -- it never returns. The current process is replaced by the new one you just exec'ed. If it fails, it returns `-1` and sets `errno`, so it is fine to `return errno;` (just understand it is only reached in the case where `execve()` fails) It sounds like you had that pretty much figured out anyway... `:)` – David C. Rankin Apr 20 '23 at 04:13
  • Yup, what I was asking if it’s proper to `return errno` (which I thought is akin to `exit(errno)`)or if `_exit(errno)` is more proper because it won’t flush i/o buffers and do other `atexit()` functionality (i don’t really get why this is a bad thing) – user8393252 Apr 20 '23 at 06:19

0 Answers0