No coredumps for daemons started at boot by init.d on Ubuntu

How can I get daemons started by init.d at boot to coredump on Ubuntu? This is what I have done so far...

echo "ulimit -c unlimited" >> /etc/profile
mkdir /corefiles/
chmod 777 /corefiles/
echo "kernel.core_pattern=/corefiles/core.%e.%u.%t" >> /etc/sysctl.conf
echo "fs.suid_dumpable=1" >> /etc/sysctl.conf
echo "kernel.core_uses_pid = 1" >> /etc/sysctl.conf
sysctl -p

This works great for everything except a daemon that is started by init.d at boot. I am running Ubuntu 10.04. I am looking for a solution that does not involve editing each daemons init.d file.

EDIT: Also, daemons started with sudo do not coredump.


Solution 1:

Why not use Apport? It's disabled by default on non-development versions of Ubuntu, but it's still installed by default AFAIK.

Solution 2:

/etc/profile is executed when a user logs in for an interactive session (and even then that depends on the login method and login shell). It has no effect on daemons started at boot.

Apparently (I haven't tested to confirm) core dumps start out disabled on Ubuntu 10.04. They can be enabled by setting a nonzero size limit in /etc/security/limits.conf. See the comments in that file and the limits.conf man page for more information. I think you'll want to add a line like

*  soft  core  2000000

Programs that have elevated privileges generally don't dump core (I don't know the exact rule off the top of my head). This may affect processes launched directly through privilege elevation such as sudo foo; try sudo sh -c foo instead (so that the child process starts at its final privilege level).

Solution 3:

/etc/profile is only sourced by your login shell, not by initscripts.

/etc/security/limits.conf will also only affect login sessions as well, as those limits are put in place by pam_limits.so; from pam_limits manpage:

The pam_limits PAM module sets limits on the system resources that can be obtained in a user-session.

To get the behaviour you want, you should modify the initscript and insert your ulimit -c unlimited command. As Dom mentioned, you could also do this by editing the lsb init-functions library.