Zivjo!

On Mon, 29 Jan 2001, [EMAIL PROTECTED] wrote:

> Samo fork() tako kot zgoraj ni dovolj. Najprej enkrat forkas stran,

Tole sem uporabljal za nek daemon. 

void daemon() {
  int rc,fd,i;
 
  rc=fork();
  if (-1==rc) {
    syslog(LOG_ERR,"Daemon() - Unable to fork()\n");
    exit(-1);
  }
  if (rc>0) exit(0); //parent should exit and return control, it's OK.
  rc=setsid();
  if (-1==rc) {
    syslog(LOG_ERR,"Daemon() - Unable to setsid()\n");
    exit(-1);
  }
  rc=fork();
  if (-1==rc) {
    syslog(LOG_ERR,"Daemon() - Unable to fork()\n");
    exit(-1);
  }
  if (rc>0) exit(0); //parent should exit and return control, it's OK.

  rc=chdir(global.pchDataDir);
  if (-1==rc) {
    syslog(LOG_ERR,"Daemon() - Unable to chdir()\n");
    exit(-1);
  }
  umask(0);
  for (i=0;i<3;i++) close(i); // playing safe :).

  // reopen stdin, out and err with /dev/null.
  fd=open("/dev/null",O_RDWR);
  if(-1==fd) {
    syslog(LOG_ERR,"Daemon() - Unable to open \"/dev/null\"\n");
    exit(-1);
  }
  for (i=0;i<3;i++)
    if(-1==dup2(fd,i)){
      syslog(LOG_ERR,"Daemon() - Unable to dup2(%d)\n",i);
      exit(-1);
    }

  close(fd);
  return; // a grandchild returns... free as a bird.. ;*)
}


-- 
best regards,
Rok Papez.

Reply via email to