00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00026 #include "config.h"
00027
00028 #include <errno.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <sys/types.h>
00033 #include <sys/wait.h>
00034 #include <unistd.h>
00035
00036 #include <glib.h>
00037
00038 #include "connectionMgrLog.h"
00039
00040 static pid_t g_forkexec_child_pid = 0;
00041
00042 pid_t get_forkexec_child_pid()
00043 {
00044 return g_forkexec_child_pid;
00045 }
00046
00047
00048
00049 pid_t fork_exec_no_wait(const int argc, char *const argv[])
00050 {
00051 pid_t pid = 0;
00052 int rc;
00053
00054 CN_LOGPRINTF("entry: [%s]", argv[0]);
00055 g_assert(argv[argc] == NULL);
00056
00057
00058 switch (pid = fork())
00059 {
00060 case 0:
00061
00062 rc = execvp(argv[0], argv);
00063 CN_ERRORPRINTF("execvp [%s] returns [%d] errno [%d] - %s",
00064 argv[0], rc, errno, strerror(errno));
00065 g_assert_not_reached();
00066 exit(1);
00067
00068 case -1:
00069
00070 CN_ERRORPRINTF("fork returns [%d] errno [%d] - %s",
00071 pid, errno, strerror(errno));
00072 g_assert_not_reached();
00073 exit(1);
00074
00075 default:
00076
00077 CN_WARNPRINTF("my pid [%d] child pid [%d]", getpid(), pid);
00078 return pid;
00079 }
00080 }
00081
00082
00083
00084
00085 int fork_exec(const int argc, char *const argv[])
00086 {
00087 int ret = -1;
00088 pid_t pid;
00089 int status;
00090
00091 CN_LOGPRINTF("entry: [%s]", argv[0]);
00092 g_assert(g_forkexec_child_pid == 0);
00093
00094
00095 pid = fork_exec_no_wait(argc, argv);
00096 if (pid > 0)
00097 {
00098
00099 g_forkexec_child_pid = pid;
00100 waitpid(pid, &status, 0);
00101 CN_WARNPRINTF("WIFEXITED [%d]", WIFEXITED(status));
00102 if (WIFEXITED(status))
00103 {
00104 CN_WARNPRINTF("WEXITSTATUS [%d]", WEXITSTATUS(status));
00105 ret = WEXITSTATUS(status);
00106 }
00107 g_forkexec_child_pid = 0;
00108 }
00109
00110 return ret;
00111 }
00112