3

I am trying to provide support for coredump file generation from my rootfs ,I have modified /etc/limits file with "ulimit -c unlimited" command and "* hard core -1" ,Now when I give kill -6 $$ ,expecting core file generation but to get this core file have to run ulimit -c unlimited explicitly .

But I want it to happen automatically , no need to run ulimit -c unlimited it again in shell.

Can anybody tell me what changes I have to make for the same to happen

Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • 1
    Put it in your `.profile` file? – Nobilis Aug 21 '13 at 12:16
  • 1
    Add the line to your shell startup file, maybe to `~/.profile`. – devnull Aug 21 '13 at 12:16
  • Might wanna read up on [this](http://www.linuxfromscratch.org/blfs/view/6.3/postlfs/profile.html). Looks like you could do with some info on how to set up your own profile file. – Nobilis Aug 21 '13 at 12:21
  • Well for given reasons can't put it into profile file,If I change into profile file ,This will only take affect for program launched from a login shell, not for processes/services started by systemd. /etc/limits would the the proper location for settings these defaults.This reason have been told – Amit Singh Tomar Aug 21 '13 at 12:23
  • @Nobilis would you like to comment on the above – Amit Singh Tomar Aug 21 '13 at 12:28
  • @AmitSinghTomar My understanding is that since you still have to run `ulimit -c unlimited` explicitly (presumably from the command line) you could still benefit from putting it into your profile. – Nobilis Aug 21 '13 at 12:29
  • @Nobils but the would it be true "This will only take affect for program launched from a login shell, not for processes/services started by system" – Amit Singh Tomar Aug 21 '13 at 12:40
  • @AmitSinghTomar Looking at alk's answer this looks like a way to have the application take care of the core dump. However, I still think putting the call in `.profile` will be of help, since you have to run it yourself separately anyway (by the sound of things). You can certainly try and see if it works :) – Nobilis Aug 21 '13 at 13:07

1 Answers1

7

From a program you can use setrlimit(RLIMIT_CORE, ...) to set the core file's maximum size. To specify an infinite size pass RLIM_INFINITY.

For details on this please read here: http://manpages.debian.net/cgi-bin/man.cgi?query=getrlimit&sektion=2


Using the sysctl command you can do

sysctl kernel.core_pattern=/var/core/core.%p

to have the kernel create cores named core.<pid> in /var/core.

Adding kernel.core_pattern=/var/core/core.%p to /etc/sysctl.conf makes it permanent. (run sysctl -p to process your changes to /etc/sysctl.conf)

Besides %p (for the process id) there are other placeholders as follows (taken from here):

%%  a single % character
%p  PID of dumped process
%u  (numeric) real UID of dumped process
%g  (numeric) real GID of dumped process
%s  number of signal causing dump
%t  time  of dump, expressed as seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)
%h  hostname (same as nodename returned by uname(2))
%e  executable filename (without path prefix)
%E  pathname of executable, with slashes ('/') replaced by exclamation marks ('!').
%c  core  file  size soft resource limit of crashing process (since Linux 2.6.24)
alk
  • 69,737
  • 10
  • 105
  • 255
  • Thanks @alk ,something like this I am looking for ,So I need to specify sysctl kernel.core_pattern=/var/cor/core.%p in my /etc/sysctl.conf ?? and Do I really need to run ulimit -c unlimited explicitly then?? – Amit Singh Tomar Aug 21 '13 at 12:57
  • @AmitSinghTomar: `kernel.core_pattern` specifies **where** to place the core file and **how** to name it. – alk Aug 21 '13 at 14:02
  • Fine @alk but the it won't solve my problem,Cav we set core file's maximum size in /etc/limits file? – Amit Singh Tomar Aug 21 '13 at 14:08
  • @AmitSinghTomar: You might like to use `setrlimit()` from aout of your program to she the maximum size of the core file, as proposed by my answer. – alk Aug 21 '13 at 14:19
  • yes @alk that is what I am looking into .Do you know where in Linux code its been set. – Amit Singh Tomar Aug 21 '13 at 14:20
  • @AmitSinghTomar: `setrlimit()` is a C function which could be called from your code directly. For details on this please see here: http://manpages.debian.net/cgi-bin/man.cgi?query=getrlimit&sektion=2 – alk Aug 21 '13 at 14:40