--- source-trees/gt2-cvs/gatekeeper/source/globus_gatekeeper.c 18 Apr 2005 21:33:06 -0000 1.93 +++ source-trees/gt2-cvs/gatekeeper/source/globus_gatekeeper.c 28 Jun 2007 16:28:50 -0000 @@ -203,6 +203,11 @@ #define failure3(t,a,b,c) {sprintf(tmpbuf, a,b,c); failure(t,tmpbuf);} #define failure4(t,a,b,c,d) {sprintf(tmpbuf, a,b,c,d); failure(t,tmpbuf);} +#define FORK_AND_EXIT 1 +#define FORK_AND_WAIT 2 +#define DONT_FORK 3 +static int launch_method = FORK_AND_EXIT; + extern int errno; static int connection_fd; @@ -349,6 +354,8 @@ int status; # endif + if (launch_method == DONT_FORK) return; + # ifdef HAS_WAIT3 while ((pid = wait3(&status, WNOHANG, NULL)) > 0) ; # else @@ -753,6 +760,33 @@ run_from_inetd = 0; } } + else if ((strcmp(argv[i], "-launch_method") == 0) + && (i + 1 < argc)) + { + if(!run_from_inetd) + { + fprintf(stderr, "Gatekeeper running as daemon, " + "ignoring -launch_method!\n"); + } + else if (strcmp(argv[i + 1], "fork_and_exit") == 0) + { + launch_method = FORK_AND_EXIT; + } + else if (strcmp(argv[i + 1], "fork_and_wait") == 0) + { + launch_method = FORK_AND_WAIT; + } + else if (strcmp(argv[i + 1], "dont_fork") == 0) + { + launch_method = DONT_FORK; + } + else + { + fprintf(stderr, "Bad -launch_method argument %s\n", + argv[i + 1]); + } + i++; + } else { @@ -1138,7 +1172,7 @@ static void doit() { int p1[2]; - int pid; + int pid = 0; int n; int i; int service_uid; @@ -1867,7 +1901,8 @@ close_on_exec_read_fd = p1[0]; close_on_exec_write_fd = p1[1]; - if (fcntl(close_on_exec_write_fd, F_SETFD, 1) != 0) + if (fcntl(close_on_exec_write_fd, F_SETFD, 1) != 0 || + fcntl(close_on_exec_read_fd, F_SETFD, 1) != 0) { failure2(FAILED_SERVER, "fcntl F_SETFD failed: %s", strerror(errno)); } @@ -2012,13 +2047,21 @@ chdir(pw->pw_dir); - pid = fork(); - if (pid < 0) + if ( launch_method != DONT_FORK ) { - failure2(FAILED_SERVER, "fork failed: %s", strerror(errno)); + pid = fork(); + if (pid < 0) + { + failure2(FAILED_SERVER, "fork failed: %s", strerror(errno)); + } } - if (pid == 0) + if (launch_method == DONT_FORK) + { + notice2(0, "Starting child %d", pid); + } + + if (pid == 0 || launch_method == DONT_FORK) { close(close_on_exec_read_fd); @@ -2056,7 +2099,10 @@ { sprintf(tmpbuf, "Exec failed: %s\n", strerror(errno)); write(close_on_exec_write_fd, tmpbuf, strlen(tmpbuf)); - exit(0); + if (launch_method != DONT_FORK) + { + exit(0); + } } } @@ -2084,7 +2130,34 @@ } close(close_on_exec_read_fd); - notice2(0, "Child %d started", pid); + if (launch_method != DONT_FORK) + { + notice2(0, "Child %d started", pid); + } + + if (launch_method == FORK_AND_WAIT) + { + /* wait until child is reaped */ + int dead_pid; +# ifdef HAS_WAIT_UNION_WAIT + union wait status; +# else + int status; +# endif + + do + { +# ifdef HAS_WAIT3 + dead_pid = wait3(&status, 0, NULL); +# else + dead_pid = waitpid(-1, &status, 0); +# endif + if (dead_pid < 0 && errno != EINTR) + { + break; + } + } while (dead_pid != pid); + } ok_to_send_errmsg = 0; } /* doit() */