-6

I want to write a C/C++ Program having the following functionalities:

  1. Run a binary executable C program as a child process.
  2. Watch the memory usage of the child process, and be able to terminate the child process when it overuse memory, like over 100MB.
  3. When the child process have been running for a given time, like 1 sec, I can terminate it.
  4. Keep it away from any linux kernel functions, which means when the child process ask for functions not belongs to standard C Library, I can prevent it and terminate the process.

Any idea to how to implements these? Or you can just give me a hint and I can find out by myself! Thanks!

Di Wu
  • 19
  • 1
  • 6

1 Answers1

4

First, you should have some basic Linux programming knowledge, so read carefully Advanced Linux Programming. Study the source code of some simple shell like sash, and play with strace(1) (e.g. on existing commands) to guess what syscalls are used.

Then you probably want to use the setrlimit(2) and perhaps ptrace(2) syscalls.

You write:

Keep it away from any linux kernel functions, which means when the child process ask for functions not belongs to standard C Library, I can prevent it and terminate the process.

I'm not sure this has any sense. Any Linux program (except the non-sense never-terminating while(true); loop) is doing some syscalls, notably to write(2) some output (see syscalls(2) for their list). And syscalls are the mean for an application program to ask the kernel to do something (so syscalls are using linux kernel functions in a controlled way).

Notice that a program can do a syscall without using the GlibC. And there are alternate libc implementations like MUSL-Libc for example.

You might be also interested by LD_PRELOAD tricks.

The /proc/ filesystem is surely useful. Read proc(5).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547