在主函数中调用能让自己成为守护进程的函数

摘自pptp-1.7.2/pptp_compat.c

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <strings.h>
  8. #include <stdio.h>

  9. /*
  10.  * daemon implementation from uClibc
  11.  * nochdir:0设置为根目录
  12.  * noclose:0关闭标准输入输出,一般调用daemon(0,0);
  13.  */
  14. int daemon(int nochdir, int noclose)
  15. {
  16.     int fd;

  17.     switch (fork()) {
  18.     case -1:
  19.         return (-1);
  20.     case 0:
  21.         break;
  22.     default:
  23.         _exit(0);
  24.     }

  25.     if (setsid() == -1)
  26.         return (-1);

  27.     if (!nochdir)
  28.         chdir("/");

  29.     if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
  30.         dup2(fd, STDIN_FILENO);
  31.         dup2(fd, STDOUT_FILENO);
  32.         dup2(fd, STDERR_FILENO);
  33.         if (fd > 2)
  34.             close (fd);
  35.     }
  36.     return (0);
  37. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值