void setdaemon(void)
{
    pid_t pid;

    
if((pid = fork()) < 0){   
        mylog(
"fork1 failed");
        exit(
-1);
    }

    
if (pid){   
        exit(
0);
    }   

    setsid();

    
if ((pid = fork()) < 0){   
        fprintf(stderr, 
"fork2 failed");
        exit(
-1);
    }   

    
if (pid){   
        exit(
0);
    }   
    write_pidfile();
    
/*
    chdir("/");
    umask(0);
    
*/
}

void write_pidfile(void)
{
    
int  fd;
    
char buff[20];
    
if ((fd = open("send.pid", O_CREAT | O_WRONLY, 0600)) >= 0)
    {
        bzero(buff, 
sizeof(buff));
        sprintf(buff, 
"%5d\n", (int)getpid());
        
if (write(fd, buff, strlen(buff)) == -1)
            mylog(
"Error writing to pid file send.pid");
        (
void)close(fd);
        
return;
    }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2022-12-23
  • 2022-02-21
  • 2022-02-01
  • 2021-05-24
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2022-12-23
  • 2022-03-06
  • 2021-12-14
  • 2021-06-26
  • 2022-01-15
  • 2022-12-23
相关资源
相似解决方案