Hi
PANDA and I have a question about calling fork() action.
Target function is RUN_COMMAND() in "src/libs/zbxsysinfo/common/common.c".
That's as follows code.
This code means 3 processes exist normally.
I named them PARENT, CHILD and GRAND-CHILD.
At line 417-419, we suspect strange behavior is returning "SYSINFO_RET_FAIL" if "fork2" failed.
Because, we suspect the PARENT keep to wait at line 443.
However, it may run normally instead of PARENT after CHILD was returned.
I think the best way is exiting waited original PARENT process.
What do you think?
I made sample code(named t2y_fork.c) verifying this problem. That's the result.
PANDA and I have a question about calling fork() action.
Target function is RUN_COMMAND() in "src/libs/zbxsysinfo/common/common.c".
That's as follows code.
This code means 3 processes exist normally.
I named them PARENT, CHILD and GRAND-CHILD.
At line 417-419, we suspect strange behavior is returning "SYSINFO_RET_FAIL" if "fork2" failed.
Because, we suspect the PARENT keep to wait at line 443.
However, it may run normally instead of PARENT after CHILD was returned.
I think the best way is exiting waited original PARENT process.
What do you think?
Code:
407 pid = zbx_fork(); /* run new thread 1 */
408 switch(pid)
409 {
410 case -1:
411 zabbix_log(LOG_LEVEL_WARNING, "fork failed for command '%s'",command);
412 return SYSINFO_RET_FAIL;
413 case 0:
414 pid = zbx_fork(); /* run new tread 2 to replace by command */
415 switch(pid)
416 {
417 case -1:
418 zabbix_log(LOG_LEVEL_WARNING, "fork2 failed for '%s'",command);
419 return SYSINFO_RET_FAIL;
420 case 0:
421 /*
422 * DON'T REMOVE SLEEP
423 * sleep needed to return server result as "1"
424 * then we can run "execl"
425 * otherwise command print result into socket with STDOUT id
426 */
427 sleep(3);
428 /**/
429
430 /* replace thread 2 by the execution of command */
431 if(execl("/bin/sh", "sh", "-c", command, (char *)0))
432 {
433 zabbix_log(LOG_LEVEL_WARNING, "execl failed for command '%s'",command);
434 }
435 /* In normal case the program will never reach this point */
436 exit(0);
437 default:
438 waitpid(pid, NULL, WNOHANG); /* NO WAIT can be used for thread 2 closing */
439 exit(0); /* close thread 1 and transmit thread 2 to system (solve zombie state) */
440 break;
441 }
442 default:
443 waitpid(pid, NULL, 0); /* wait thread 1 closing */
444 break;
445 }
Code:
$ gcc -g -Wall t2y_fork.c $ ./a.out -- start fork @ I am child, pid is 1199, ppid is 1198 # I am parent, pid is 1198, ppid is 2836 fork: Success ^C You have to Ctrl-C to stop.

Comment