Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a C++ program inside another C++ program?

Tags:

c++

linux

shell

I will sketch the scenario I would like to get working below.

I have one main application. That application, based on user interactions, can load other applications inside a secure environment/shell. This means these child applications cannot interact with the OS anymore, nor with each other.

The parent program can at any time call functions of these child programs. The child program can at any time call functions of these parent programs.

Does anyone know how to implement this in C++? Preferably both parent and child should be written in C++.

The performance of loading the child applications inside the parent application doesn't matter. The only thing that matters is the performance of the communication between child and parent when calling functions of each other.

like image 252
Jeroen Avatar asked Jul 09 '13 19:07

Jeroen


People also ask

Can you have multiple C files?

A large C or C++ program should be divided into multiple files. This makes each file short enough to conveniently edit, print, etc. It also allows some of the code, e.g. utility functions such as linked list handlers or array allocation code, to be shared with other programs.

How do you invoke a program?

In Windows, to run a program, double-click the executable file or double-click the shortcut icon pointing to the executable file. If you have a hard time double-clicking an icon, you can click the icon once to highlight it and then press the Enter key on the keyboard.

What does system do in C?

In the C Programming Language, the system function allows a C program to run another program by passing a command line (pointed to by string) to the operating system's command processor that will then be executed.


2 Answers

You will have to write your own compiler.

Consider: No normal OS supports what you want. You want both executables to run inside a single process, yet that process may or may not make OS calls depending on some weirdness inside the process which the OS doesn't understand at all.

This is no longer a problem with your custom compiler, as it simply will not create the offending instructions. It's similar to Java and .Net, which also prevent such OS calls outside their control.

like image 191
MSalters Avatar answered Nov 03 '22 16:11

MSalters


A portable solution: Google Native Client

One possible Linux solution:

  1. Make AppArmor profile with "hats" (a "hat" is a sandboxing configuration to which the application can switch programmatically with libapparmor),
  2. have the main application create a "pipe",
  3. have the main application "fork",
  4. change into a "hat" corresponding to the child application,
  5. "exec" the child application,
  6. the main application and the child application communicate via the "pipe" created earlier.
like image 36
ArtemGr Avatar answered Nov 03 '22 14:11

ArtemGr