linux
Whether a program dumps core on segfault is not really supposed to be left up to the program itself, but rather up to the user and/or sysadmin. So in my opinion, this isn't really a sensible option for a program to provide.
Having said that, the most common way to control whether a core dump is produced is via the rlimit for core file size. If it is zero, no core dumps are produced. Normally the user would set this with ulimit -c XXX
or equivalent in their shell. However your program can also attempt to set it with the setrlimit(2)
system call. For instance you might do
struct rlimit r;
getrlimit(RLIMIT_CORE, &r);
r.rlim_cur = r.rlim_max;
setrlimit(RLIMIT_CORE, &r);
to set the limit equal to its maximum allowed value. However, that maximum allowed value might be 0 (set by some ancestor of your process) in which case you cannot increase it.
There are other system settings that might affect whether core dumps are possible, which you may or may not have enough privileges to modify. The Linux core(5)
man page explains the necessary conditions.