eeq
eeq

Reputation: 1004

Prevent forking in a child process

I have a Linux process (C program) which spawns a couple of child processes. I'd like to forbid another forking in those child processes on the system level, so that those processes would get killed if they tried to fork anyway. How to achieve that? I prefer to embed this policy in the host C code. Can setrlimit(2) do that?

Upvotes: 3

Views: 1906

Answers (1)

askb
askb

Reputation: 6768

Yes, setrlimit() can do this. Refer to man page and read up on

RLIMIT_NPROC
The maximum number of processes (or, more precisely on Linux, threads) that can be created for the real user ID of the calling process. Upon encountering this limit, fork(2) fails with the error EAGAIN.

Alternatively, you can set a hard limit on a process using /etc/security/limits.conf. Look up some of the examples in the file.

#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0

Upvotes: 1

Related Questions