Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2259568
  • 博文数量: 218
  • 博客积分: 5767
  • 博客等级: 大校
  • 技术积分: 5883
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-01 14:44
文章存档

2012年(53)

2011年(131)

2009年(1)

2008年(33)

分类: LINUX

2012-08-23 09:05:22

摘自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. }

阅读(1702) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~