0

I need to call a external program from mine.

However, I would like to make the loading only once; that is,



int proc(void *path) {

    void *p;

    // ...

    //ELF loader
    p = load_magic(path);

    do {

        register int t = fork();

        if (!t) {
            // push arguments and jump to entrypoint
            exec_magic(p, _ARGS_); // execlp(path, _ARGS_);

            _exit(-0x1);
        }

        // stuff

    } while(CONDITION);

    // ...

}



Is this possible?

imakak
  • 113
  • 6
  • Are you, in fact, looking for an application binary interface (ABI) ? – sjsam Apr 08 '19 at 12:36
  • What exactly do you mean by "loading only once"? Read from disk, into memory, and remaining there even after the child process exits, so it can be reused on the next iteration? That would be [the sticky bit](https://en.wikipedia.org/wiki/Sticky_bit), which most operating systems no longer implement. – Andrew Henle Apr 08 '19 at 12:42
  • Yes. I want to load ELF into the memory for reuse it in the iterations. It appears that it was possible with Position Independent Executables : https://stackoverflow.com/questions/13908276/loading-elf-file-in-c-in-user-space , and therefore I wonder that if there is a way to do same things on general images. – imakak Apr 08 '19 at 12:58

1 Answers1

1

Check out this page: https://linux.die.net/man/3/execvp - it looks like exec family of functions may be a solution to your needs.

Diodacus
  • 663
  • 3
  • 8
  • Thanks, but I would like to load and relocate ELF and keep it in the memory, then call it in the loop. The exec family do both loads and jump to the entry point of ELF. – imakak Apr 08 '19 at 12:42
  • @imakak I really do think the exact number of times it will "load" the program is firmly behind the wall of abstraction. I would expect subsequent loading to happen using page-mapping magic, code is usually read-only. This is either XY, or very some specific optimization attempt that is (IMO) a bit too poorly described in the question. – unwind Apr 08 '19 at 13:20
  • @unwind Sorry for the bad question-description. By the term "load" I mean something like load_elf_binary in kernel of linux-like OS. – imakak Apr 08 '19 at 14:05