Christian
Christian

Reputation: 161

separate (binary) implementation of ulimit command?

Is there a source code for /usr/bin/ulimit? I need to see a user's complete list of rlimit's without him being in a shell.

In this case, we have problems running memory-intensive programs from the web server (user wwwrun, usually having /bin/false as login shell in /etc/passwd), and we suspect some system-imposed limits are blocking it from completing these programs.

In former times, there has been a /usr/bin/ulimit binary, but these seem to be gone.

So what I'm looking for is a C implementation of the ulimit command (nowadays usually being only available as (ba)sh built-in). It doesn't need to have any "setrlimit" capabilities - just printing all limits would be sufficient. We'd then set this command as wwwrun's login shell and a "su - wwwrun" would show all rlimits this user has.

Thanks for any hints!

Upvotes: 1

Views: 820

Answers (2)

Christian
Christian

Reputation: 161

Thanks for the hints - I've set a simple text file cat-limits as login shell for wwwrun, simply containing one line with

#!/bin/cat /proc/self/limits

and that gave me what I want.

Cheers

Upvotes: 2

There cannot be any /usr/bin/ulimit (or /usr/bin/cd) executable, because the setrlimit(2) syscall operate on the current shell process (changing a property of the current process which will by default be inherited by further child processes, see fork(2) & execve(2)). So ulimit, like cd (which invokes the chdir(2) syscall), has to be a shell builtin

From inside your C program just call the setrlimit syscall. Don't forget to test for failure (and on failure display errno perhaps using perror or strerror). To query the current limits, use getrlimit(2) or read -within your program- sequentially the /proc/self/limits pseudo-file (see proc(5) for more, or /proc/1234/limits for process of pid 1234).

FILE* f = fopen("/proc/self/limits", "r");
if (f) {
   char linbuf[128];
   do { 
     memset (linbuf, 0, sizeof(linbuf));
     fgets (linbuf, sizeof(linbuf), f);
     fputs(linbuf, stderr);
   } while (!feof(f));
   fclose (f);
   fflush(NULL);
} else perror("/proc/self/limits");

I also recommend reading Advanced Linux Programming and intro(2). See also Linux PAM and configuration files like /etc/security/limits.conf and under /etc/pam.d/

Upvotes: 3

Related Questions