以下代码生成coredump文件:
#include <iostream>
#include <string>
#include <pwd.h>
#include <grp.h>
#include <sys/resource.h>
int main() {
int b = 0;
int a = 140/b;
return 0;
} 输出:Floating point exception (core dumped)
Coredump在/opt/cores中生成
$ ls -al /opt/cores
total 188
drwxrwxrwx 2 root root 4096 Jan 13 16:46 .
drwxr-xr-x 28 root root 4096 Jan 12 11:57 ..
-rw------- 1 root root 344064 Jan 13 16:46 core.prueba.6776.8但是,这不会生成coredump文件:
#include <iostream>
#include <string>
#include <pwd.h>
#include <grp.h>
#include <sys/resource.h>
int main() {
std::string usr = "nobody";
std::string grp = "oinstall";
group* gp = getgrnam(grp.data());
passwd* user = getpwnam(usr.data());
if (gp && user && setgid(gp->gr_gid) == 0 && setuid(user->pw_uid) == 0) {
std::cout << "changed!" << std::endl;
} else {
std::cout << "not changed =(" << std::endl;
}
struct rlimit rlim;
rlim.rlim_cur = RLIM_INFINITY;
rlim.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &rlim) != 0) {
std::cout << "setrlimit error" << std::endl;
}
getrlimit(RLIMIT_CORE, &rlim);
std::cout << "rlim_cur: " << (int)rlim.rlim_cur <<", rlim_max: " << (int)rlim.rlim_max << std::endl;
int b = 0;
int a = 140/b;
return 0;
} 输出:
changed!
rlim_cur: -1, rlim_max: -1
Floating point exception我已经使用修改后的用户运行了第一段代码,它生成了coredump文件,因此目录具有正确的权限。问题是当我在代码中更改用户时。有什么线索吗?
这发生在Linux上(CentOS 6,CentOS 7,RHEL 6)。
在Solaris工作得很好。
https://stackoverflow.com/questions/34775840
复制相似问题