2

I am looking for a way to execute a Linux executable from a separate Linux Executable that was compiled from C or C++. However, I have looked at numerous Stack Overflow posts which all direct the user asking to use the system() function or a wrapper of the system function and I do not want a program that relies on the shell, because it could easily fall apart if it was transferred to a different operating system with a different shell.

In the post How do I execute an external program within C in Linux with arguments, the second answer states that execve() is a wrapper for the system() function, and this makes me wary of the other functions in the exec() family.

I have also looked at the following articles:

All help is appreciated!

5
  • 9
    "execve() is a wrapper for the system function". It's not the case. It's the other way around - system is a wrapper for execve. The system manaul gives that info: "The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3)". So using the exec functions might actually be what you need? Commented Mar 2, 2022 at 0:31
  • system is really, really dumb. Many consider it to be a security hole because of how stupid it is. Commented Mar 2, 2022 at 0:34
  • 3
    The second answer doesn't say that execve() is a wrapper for the system function. It says system uses exec* functions. Commented Mar 2, 2022 at 0:40
  • 1
    If you're worried about portability, then system() is the portable way to do the job; it is required by the Standard C library so, for most practical purposes, it will be available anywhere C is available. However, there are some security risks associated with it, and it doesn't necessarily handle all the scenarios you want. In that case, you almost invariably end up using fork() and the exec() family of functions, possibly with pipe() and dup2(), or maybe some more esoteric functions for handling pseudo-terminals (aka pseudo-ttys or ptys). Commented Mar 2, 2022 at 1:28
  • 4
    On POSIX systems, an alternative to the fork() and exec*() functions is the posix_spawn() function and its relatives (findable from pubs.opengroup.org/onlinepubs/9699919799/toc.htm). Commented Mar 2, 2022 at 1:34

1 Answer 1

3

execve() is not a wrapper for system(); it is a wrapper for the execve syscall itself.

execve() replaces the current process, so you’ll probably need to fork() and then execute execve() in the child process, thereby emulating the behaviour of system().

Sign up to request clarification or add additional context in comments.

6 Comments

when the current process is replaced, and the program executed by execve() finishes, is control return to the previous process, or is the process eliminated entirely?
@ShortsKing, on success, execve() does not return. The binary executing in the calling process is completely replaced by the one designated by execve()'s arguments. Note also that it is more common to use one of the various other exec-family functions than to use execve() directly.
System call numbers are only meaningful for a specific OS and architecture and the only time you should have to worry about the actual numbers is when you're adding new system calls to the kernel. Saying (#11) just confuses people. Please edit that out.
@zwol You're right; of course they're unhelpful. Thanks for pointing that out. I've been writing a lot of assembly recently, so the first thing I thought of when I saw execve() was "11".
Even in assembly you ought to be able to use the SYS_* constants from sys/syscall.h, can't you?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.