2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
|
Tcl_SetObjResult(interp, Tcl_NewStringObj("fork failed", -1));
return(TCL_ERROR);
}
if (child != 0) {
/* 4.parent. Get PGID from child */
/* 4.parent.a. Close write end of pipe -- we are read-only */
close(fds[1]);
fd = fds[0];
/* 4.parent.b. Read process group ID of child from pipe */
select_timeout.tv_sec = timeout_val;
select_timeout.tv_usec = 0;
FD_ZERO(&read_fdset);
FD_SET(fd, &read_fdset);
select_ret = select(fd + 1, &read_fdset, NULL, NULL, &select_timeout);
if (select_ret == 0) {
/* On timeout, terminate starting process */
child_pgid = getpgid(child);
if (child_pgid != -1) {
kill(-child_pgid, SIGKILL);
}
Tcl_SetObjResult(interp, Tcl_NewStringObj("timeout", -1));
return(TCL_ERROR);
}
if (select_ret > 0) {
read_ret = read(fd, &child_pgid, sizeof(child_pgid));
}
/* 4.parent.c. Close read end of pipe */
close(fd);
/* 4.parent.d. Verify read was meaningful */
if (read_ret != sizeof(child_pgid)) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("failed to communicate with started service", -1));
return(TCL_ERROR);
}
/* 4.parent.e. If the PGID given is actually an error, return error */
if (child_pgid == -1) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("service failed to start", -1));
return(TCL_ERROR);
}
/* 4.parent.f. Return PGID to Tcl */
Tcl_SetObjResult(interp, Tcl_NewWideIntObj((Tcl_WideInt) child_pgid));
return(TCL_OK);
}
/* 4.child.a. Close read end of pipe -- we only write to it */
close(fds[0]);
fd = fds[1];
|
>
>
>
|
|
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
|
Tcl_SetObjResult(interp, Tcl_NewStringObj("fork failed", -1));
return(TCL_ERROR);
}
if (child != 0) {
/* 4.parent. Get PGID from child */
/* 4.parent.a. Open log file */
log_fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
/* 4.parent.b. Close write end of pipe -- we are read-only */
close(fds[1]);
fd = fds[0];
/* 4.parent.c. Read process group ID of child from pipe */
select_timeout.tv_sec = timeout_val;
select_timeout.tv_usec = 0;
FD_ZERO(&read_fdset);
FD_SET(fd, &read_fdset);
select_ret = select(fd + 1, &read_fdset, NULL, NULL, &select_timeout);
if (select_ret == 0) {
/* On timeout, terminate starting process */
child_pgid = getpgid(child);
if (child_pgid != -1) {
kill(-child_pgid, SIGKILL);
}
Tcl_SetObjResult(interp, Tcl_NewStringObj("timeout", -1));
currtime = time(NULL);
strftime(logmsg, sizeof(logmsg), "[ %b %e %H:%M:%S ", localtime(&currtime));
write(log_fd, logmsg, strlen(logmsg));
snprintf(logmsg, sizeof(logmsg), "Method \"start\" timed out after %i seconds ]\n", (int) timeout_val);
write(log_fd, logmsg, strlen(logmsg));
close(log_fd);
return(TCL_ERROR);
}
if (select_ret > 0) {
read_ret = read(fd, &child_pgid, sizeof(child_pgid));
}
/* 4.parent.d. Close read end of pipe */
close(fd);
/* 4.parent.e. Verify read was meaningful */
if (read_ret != sizeof(child_pgid)) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("failed to communicate with started service", -1));
currtime = time(NULL);
strftime(logmsg, sizeof(logmsg), "[ %b %e %H:%M:%S ", localtime(&currtime));
write(log_fd, logmsg, strlen(logmsg));
snprintf(logmsg, sizeof(logmsg), "Method \"start\" failed: communication with started service broken ]\n");
write(log_fd, logmsg, strlen(logmsg));
close(log_fd);
return(TCL_ERROR);
}
/* 4.parent.f. If the PGID given is actually an error, return error */
if (child_pgid == -1) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("service failed to start", -1));
currtime = time(NULL);
strftime(logmsg, sizeof(logmsg), "[ %b %e %H:%M:%S ", localtime(&currtime));
write(log_fd, logmsg, strlen(logmsg));
snprintf(logmsg, sizeof(logmsg), "Method \"start\" failed ]\n");
write(log_fd, logmsg, strlen(logmsg));
close(log_fd);
return(TCL_ERROR);
}
/* 4.parent.g. Return PGID to Tcl */
Tcl_SetObjResult(interp, Tcl_NewWideIntObj((Tcl_WideInt) child_pgid));
currtime = time(NULL);
strftime(logmsg, sizeof(logmsg), "[ %b %e %H:%M:%S ", localtime(&currtime));
write(log_fd, logmsg, strlen(logmsg));
snprintf(logmsg, sizeof(logmsg), "Method \"start\" completed, process group = %lu ]\n", (unsigned long) child_pgid);
write(log_fd, logmsg, strlen(logmsg));
close(log_fd);
return(TCL_OK);
}
/* 4.child.a. Close read end of pipe -- we only write to it */
close(fds[0]);
fd = fds[1];
|