Re: Tcl crashes and/or hangs in RHEL5?
- From: Darren New <dnew@xxxxxxxxxx>
- Date: Mon, 01 Oct 2007 21:05:42 -0700
John Kelly wrote:
The example in the wiki, where they fork twice, is a technique I've
read elsewhere, independently.
I can't imagine what possible reason there is to fork more than once.
In any case, this is what I use under Linux. Compile it into "Invoke", then copy that to (say) "/home/Fred/mydeamon" and name your Tcl code as "/home/Fred/mydeamon.tcl". Then change initd.skeleton to refer to this executable in the right directory (as $BIN) and away you go. Tcl 8.4.
env(FROMINETD) gets set to 1 if invoked from inet.d, otherwise send debugging to stdout as well as your normal logging mechanism.
Anyone who cares to is free to post this to the wiki under whatever category is appropriate. :-)
--
Darren New / San Diego, CA, USA (PST)
Remember the good old days, when we
used to complain about cryptography
being export-restricted?
/*
* This is a version of TclMain that sources
* a file based on the name under which it is invoked.
* In other words, if you invoke this as ./xyz it will
* source and run ./xyz.tcl without changing argv[0].
* It is used for init.d scripts.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "tcl.h"
static char buf[200]; // Because TclMain doesn't pass us argv/argc directly.
void close_std() {
int i = open("/dev/null", O_RDWR);
if (i < 0) return;
if (isatty(0)) dup2(i, 0);
if (isatty(1)) dup2(i, 1);
if (isatty(2)) dup2(i, 2);
if (2 < i) close(i);
}
int Tcl_AppInit3(Tcl_Interp * interp) {
int i;
i = Tcl_Init(interp);
if (i != TCL_OK) {
fprintf(stderr, "ERROR - Tcl_AppInit %s failed: %s\n", buf, Tcl_GetStringResult(interp));
exit(1);
}
i = Tcl_PutEnv("FROMINETD=1");
close_std();
return TCL_OK;
}
int main(int argc, char * argv[]) {
int new_argc;
char * new_argv[10];
int i;
char * final;
if (7 < argc) {
fprintf(stderr, "ERROR - Argc %d is too big\n", argc);
exit(1);
}
if (sizeof(buf) - 50 < strlen(argv[0])) {
fprintf(stderr, "ERROR - Argv[0] %s too long\n", argv[0]);
exit(1);
}
strcpy(buf, argv[0]);
i = strlen(buf)-1;
while (1 < i && buf[i] != '/') i -= 1;
if (buf[i] == '/') {
buf[i] = '\0';
final = buf + i + 1;
i = chdir(buf);
// printf("cd to %s returns %d\n", buf, i);
} else {
final = buf;
}
strcat(final, ".tcl");
new_argv[0] = argv[0];
new_argv[1] = final;
for (i = 1; argv[i]; i++) new_argv[i+1] = argv[i];
new_argv[i+1] = NULL;
new_argc = argc + 1;
if (0) {
for (i = 0; new_argv[i]; i++) {printf("argv[%d]=%s\n", i, new_argv[i]);}
printf("argc=%d\n", new_argc);
printf("About to call Tcl_Main with buf=%s\n", buf);
}
if (fork() != 0) {
sleep(4);
exit(0);
}
Tcl_Main(new_argc, new_argv, Tcl_AppInit3);
exit(0);
}
- Follow-Ups:
- Re: Tcl crashes and/or hangs in RHEL5?
- From: John Kelly
- Re: Tcl crashes and/or hangs in RHEL5?
- References:
- Re: Tcl crashes and/or hangs in RHEL5?
- From: Darren New
- Re: Tcl crashes and/or hangs in RHEL5?
- From: John Kelly
- Re: Tcl crashes and/or hangs in RHEL5?
- Prev by Date: Re: How can I catch unexpected error in background?
- Next by Date: Re: Tcl crashes and/or hangs in RHEL5?
- Previous by thread: Re: Tcl crashes and/or hangs in RHEL5?
- Next by thread: Re: Tcl crashes and/or hangs in RHEL5?
- Index(es):
Relevant Pages
|