0

I want to call the ulimit shell command through a C program. right now, I have:

execlp("ulimit", "ulimit", "-u", "%d", ulimit_a);

It compiles fine, but does not actually edit the user-id max process value when I later check it with ulimit -a (both the dash and bash version). The same was happening with system() as well, yet this seems to be a known issue for system()

1
  • And execlp() is not a system call, and you're lucky if that call works at all instead of crashing (as expected) -- the argument list of execlp should be terminated by a null pointer. Commented May 5, 2020 at 0:16

2 Answers 2

3

The ulimit shell command only applies to the current shell and its descendants. It's a built-in not a separate command.

You cannot start a separate shell process, run ulimit in it, and expect it to have any effect on the parent process.

From C you should use the getrlimit(2) and setrlimit(2) functions to get and set the resource limits for the current process and its descendants. Though, judging by your approach, I guess they won't do what you expect them to do, either.

2
  • Is there a way to limit the maximum amount of processes a user can make permanently? IE not getrlimit/setrlimit because, like you said, those effect descendants of the process. I basically just want to make an interface for the root user to manage this. Commented May 5, 2020 at 2:01
  • @Connor Setting resource limits persistently (not "permanently", there is no such thing) is a completely different question that you may want to ask separately. Commented May 5, 2020 at 6:55
1

Take a look at pam_limits, a PAM (Pluggable Authentication Modules) module for setting resource limits when a user session is created. You will have to configure PAM to use this module, and then you will have to configure the limits in /etc/security/limits.d/. See https://linux.die.net/man/8/pam_limits for more information.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.