system()函数从守护进程返回值始终为-1

时间:2014-06-06 09:49:13

标签: c linux bash system return-value

我试图通过获取从 c守护程序程序启动的 bash脚本返回值 system()功能。

如果注释了skeleton_daemon,则输出正确5120(20 <&lt; 8),否则它始终为-1

int main (void)
{
    skeleton_daemon();
    printf("%d", system("/tmp/prova.sh") ); // prova.sh return 20
}

void skeleton_daemon(void)
{
pid_t pid;
pid = fork();
if (pid < 0)
    exit(EXIT_FAILURE);
if (pid > 0)
    exit(EXIT_SUCCESS);
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
umask(0);
//    chdir("/");   // commented as per nos suggestion, nothing changed
}

任何人都知道如何从deamon获取脚本值?

编辑:按照nos

的要求使用strace -f 运行

说实话,我不明白一件事

execve("./pr", ["./pr"], [/* 17 vars */]) = 0
brk(0)                                  = 0xd79000
uname({sys="Linux", node="alarm", ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f80000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=30110, ...}) = 0
mmap2(NULL, 30110, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb6f53000
close(3)                                = 0
open("/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\370\203\1\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1541182, ...}) = 0
mmap2(NULL, 1262984, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6e1e000
mprotect(0xb6f45000, 32768, PROT_NONE)  = 0
mmap2(0xb6f4d000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x127000) = 0xb6f4d000
mmap2(0xb6f50000, 9608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6f50000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f7f000
set_tls(0xb6f7f4c0, 0xb6f7fba8, 0xb6f82000, 0xb6f7f4c0, 0xb6f82000) = 0
mprotect(0xb6f4d000, 8192, PROT_READ)   = 0
mprotect(0xb6f81000, 4096, PROT_READ)   = 0
munmap(0xb6f53000, 30110)               = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb6f7f068) = 7344
exit_group(0)                           = ?
+++ exited with 0 +++

提前致谢

1 个答案:

答案 0 :(得分:3)

这是因为你的skeleton_daemon()函数忽略了SIGCHLD,但是system()调用假定它不被忽略,所以它可以正常等到执行的程序完成。