[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: exim help needed



On Tue, 31 Oct 2000, Carel Fellinger wrote:

>down /etc/init.d/eam  tcp nowait carel  /usr/bin/ssh /usr/bin/ssh iae
bin/nc mail.iae.nl eam tcp  nowait carel /usr/bin/ssh /usr/bin/ssh ire
bin/nc uucp.iae.nl uucp-pipes stop

Come again? 

> >     SSHARGS="${SSHFLAGS} -l ${USER} -L ${LOCALPORT}:${DEST}:${REMOTEPORT} ${HOST} ${PIPE}l"
> 
> what is this ${PIPE}l? some program you run on the other side?
> I would have expected something like "sleep forever". 

Oh yeah.  It's basically "sleep  forever" except replace  "sleep" with
"wait" and  add this  feature: if an  instance of the  program already
exists, replace it with this one. Code follows below.

> So I did it differently using tcp-wrappers/inetd, like this:

Cool, let me try it..

-chris

/* program that hangs forever */
/* only one instance of it (by a given name) may run at a time */
/* ln(1) the executable to whatever names you need */
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <malloc.h>
#include <libgen.h>

int
main(int arc, const char** argv)
{
  const char* home = getenv("HOME");
  char* fullName = (char *)malloc(1000*sizeof(char));
  char* name = NULL;
  pid_t pid = getpid();
  pid_t previousPid = 0;
  char* fileName = (char*)malloc(1000*sizeof(char));
  FILE* file = NULL;
  int _signal = 9;

  strcpy(fullName, argv[0]);
  name = basename(fullName);
  fileName[0] = (char)0;
  strcat(fileName, home);
  strcat(fileName, "/var/run/");
  strcat(fileName, name);
  strcat(fileName, ".pid");
  file = fopen(fileName, "r");
  if (file == NULL) {
    fprintf(stderr, "Failed to open file '%s' for reading\n", fileName);
    exit(1);
  }
  fscanf(file, "%d", &previousPid);
  if (previousPid != pid) {
    if (previousPid > 0) {
      /* There is a race condition here. If we kill the old instance
	 before overwriting its PID, a third instance could try to kill 
	 a nonexistent process. If on the other hand we overwrite
	 the PID first, a third instance could kill us before we have 
	 a chance to kill the old instance. 
      */
      int result = kill(previousPid, _signal);
      if (result != 0) {
	fprintf(stderr, 
		"Failed to kill PID '%d' with signal '%d'\n"
		"Continuing anyway...\n",
		previousPid, 
		_signal);
      }
    }
  } else {
    fprintf(stderr,
	    "Suicide attempt thwarted\n");
    exit(1);
  }
  {
    int result;
    fclose(file);
    file = fopen(fileName, "w");
    if (file == NULL) {
      fprintf(stderr, "Failed to open file '%s' for writing\n", fileName);
      exit(1);
    }
    fprintf(file, "%d", pid);
    fflush(file);
    fclose(file);
    result = pause();
    fprintf(stderr, "pause() returned value '%d'\n", result);
    return 0;
  }
}



Reply to: