2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* the wrapper nanny process around weblog that would restart it in case it
11 * failed and hopefully log the error somewhere! The need for this nanny
12 * process arises because the pipe descriptors need to be maintained. The
13 * process catches SIGTERM and kills the weblog process and exits.
14 * The following line needs to precede the kill -TERM line for httpd.pid
15 * in the stopd script to stop the server
16 kill -TERM `<cat /local/stronghold/apache/logs/httpd.pid>.afs`
17 * or whatever the pid file
23 #include <sys/types.h>
28 #include "weblog_errors.h"
29 #include "AFS_component_version_number.c"
31 #define WEBLOG_BIN "weblog"
33 /* TODO - set restart_attempts to 0 if not within certain time period */
34 #define MAX_RESTARTS 3 /* after this many restarts just die */
35 static int restart_attempts
= 0;
37 int error_fd
; /* error file descriptor */
43 pid_t pgrp
= getpgrp();
45 pid_t pgrp
= getpgrp(0);
50 write(2, "weblog_starter: caught SIGTERM, shutting down", 43);
53 log_error("weblog_starter: caught SIGTERM, shutting down");
55 if (pgrp
== (pid_t
) - 1) {
56 log_error("getpgrp failed - kill weblog manually");
57 write(2, "getpgrp failed - kill weblog manually", 38);
64 /* strip out this binary and replace it with the weblog binary's name */
65 getpath(char *this, char *path
)
67 char *temp
= &this[0];
68 int len
= strlen(this);
69 int pos
= 0, i
= 0, j
= 0;
70 char bin_name
[] = WEBLOG_BIN
;
71 int len1
= strlen(bin_name
);
76 while (*temp
!= '/') {
80 for (i
= (pos
+ 1); i
< len
; i
++, j
++) {
81 path
[i
] = bin_name
[j
];
94 time_string
= (char *)ctime(&t
);
95 time_string
[strlen(time_string
) - 1] = '\0';
103 sprintf(err_msg
, "[%s] weblog:%s\n", get_time(), msg
);
104 write(error_fd
, (void *)err_msg
, strlen(err_msg
));
109 main(int argc
, char **argv
)
114 char rn
[] = "weblog_starter";
116 char error_fname
[1024];
120 memset(&sa
, 0, sizeof sa
);
121 sa
.sa_handler
= (void (*)())sig_term
;
122 if (sigaction(SIGTERM
, &sa
, NULL
) < 0) {
123 perror("sigaction(SIGTERM)");
126 getpath(argv
[1], path
);
127 strcpy(error_fname
, argv
[2]);
129 error_fd
= open(error_fname
, O_WRONLY
| O_APPEND
| O_CREAT
);
131 fprintf(stderr
, "%s:Error opening log file:%s\nExiting\n", rn
,
137 log_error("weblog_starter resuming normal operation");
139 while (restart_attempts
< MAX_RESTARTS
) {
140 /* fork the weblog process and wait till it exits */
141 if ((weblog_pid
= fork()) < 0) {
142 log_error("Could not fork process");
144 perror("apache_afs_weblog:Could not fork process");
148 switch (weblog_pid
) {
150 /* the child process - in this case weblog */
151 execlp(path
, "weblog", argv
[3], argv
[4], argv
[5], argv
[6], NULL
);
153 perror("apache_afs_weblog:Could not execute weblog");
158 /* parent: just wait for the child */
159 weblog_pid
= waitpid((pid_t
) - 1, &stat
, 0);
161 if (weblog_pid
== -1) {
163 perror("apache_Afs_weblog: wait error");
165 log_error("wait error");
166 kill(getpid(), SIGTERM
);
169 if (WIFEXITED(stat
)) {
170 exitstatus
= WEXITSTATUS(stat
);
171 switch (exitstatus
) {
174 fprintf(stderr
, "%s:No error ... restarting\n", rn
);
180 fprintf(stderr
, "%s:%s...Exiting\n", rn
, RESTARTMSG
);
182 log_error(RESTARTMSG
);
187 fprintf(stderr
, "%s:%s...Exiting\n", rn
, NULLARGSMSG
);
188 log_error(NULLARGSMSG
);
194 fprintf(stderr
, "%s:%s...Restarting\n", rn
, PIPESENDMSG
);
196 log_error(PIPESENDMSG
);
201 fprintf(stderr
, "%s:%s...Exiting\n", rn
, PIPEREADMSG
);
203 log_error(PIPEREADMSG
);
208 fprintf(stderr
, "%s:%s...Exiting\n", rn
, PARSEMSG
);
215 fprintf(stderr
, "%s:%s...Exiting\n", rn
, KAMSG
);
222 fprintf(stderr
, "%s:Unknown error...Exiting\n", rn
);
224 log_error("Unknown error");
226 } /* switch (exitstatus) */
228 /* if weblog exited */
229 else { /* weblog terminated abnormally */
230 if (WIFSIGNALED(stat
)) {
233 "%s:The signal that terminated weblog:%d\n"
234 "Restarting weblog ...\n", rn
, WTERMSIG(stat
));
238 else if (WCOREDUMP(stat
)) {
240 fprintf(stderr
, "%s: weblog dumped core" "Exiting ...\n",
243 log_error("Core dump");
250 "%s: weblog died under STRANGE circumstances..."
251 "restarting weblog\n", rn
);
256 } /* switch(weblog_pid) */