Re: Tcl crashes and/or hangs in RHEL5?



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);
}



Relevant Pages

  • Re: Critique
    ... I invoke the program with an int ... > representing a site code and a long representing a date and my ... > with the int and the long I pass in. ... Since you already have a template file, ...
    (comp.lang.c)
  • Re: [MSDOS] Trouble with mouse programming
    ... >> which not only consume a lot of stack but probably also invoke ... All that int 29h does is to invoke the int 10h ... and forgetting a declaration when calling it. ...
    (comp.programming)
  • Re: what is difference between sizeof and strlen
    ... >>int main; ... >>others are not standard and invoke an undefined behaviour. ... The behavior of void main, for example, is ...
    (comp.lang.c)
  • Re: How to Invoke Methods by Name using JAVA reflection
    ... > invoke int a and int b. ... How can i modify to invoke another data, ... > String and Double. ...
    (comp.lang.java.programmer)
  • InvokeMember and byte[] parameter
    ... I am trying to invoke a method from a COM dll using InvokeMember which ... int GetStatus(ref bytescannedBytes, System.Int32 length); ... This is the line I am getting "type mismatch" exception. ...
    (microsoft.public.dotnet.languages.csharp)