Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87865
  • 博文数量: 18
  • 博客积分: 510
  • 博客等级: 中士
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-27 20:37
文章分类

全部博文(18)

文章存档

2011年(18)

分类: LINUX

2011-04-26 11:14:09

 

  1. /*------------- daemon.c -----------*/
  2. /*
  3.     说明:让程序自动在后台运行
  4.     
  5.     cc -o daemon daemon.c    
  6.  */

  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <stdbool.h>

  12. int daemon(int nochdir, int noclose);

  13. int main(int argc, char** argv)
  14. {
  15.     int c;
  16.     bool daemonize = false;
  17.     int maxcore = 0;
  18.     int verbose = 0;
  19.     
  20.     /* set stderr non-buffering (for running under, say, daemontools) */
  21.     setbuf(stderr, NULL);
  22.     
  23.     /* process arguments */
  24.     while ((c = getopt(argc, argv, "dc:v")) != -1) {
  25.         switch (c) {
  26.         case 'd':
  27.      daemonize = true;
  28.      break;    
  29.      case 'c':
  30.             maxcore = atoi(optarg);
  31.             break;
  32.         case 'v':
  33.             verbose = 1;
  34.             break;
  35.      default:
  36.             fprintf(stderr, "Illegal argument \"%c\"\n", c);
  37.             return 1;
  38.         }
  39.     }
  40.     
  41.     /* daemonize if requested */
  42.     /* if we want to ensure our ability to dump core, don't chdir to / */
  43.     if (daemonize) {
  44.         int res;
  45.         res = daemon(maxcore, verbose);
  46.         if (res == -1) {
  47.             fprintf(stderr, "failed to daemon() in order to daemonize\n");
  48.             return 1;
  49.         }
  50.     }    
  51.    
  52.        int i = 100;
  53.        while(i--)
  54.        {
  55.            sleep(2);
  56.            fprintf(stdout,"%d ......\n", i);    
  57.        }
  58.         
  59.     return 0;
  60. }

  61. int daemon(int nochdir, int noclose)
  62. {
  63.     int fd;

  64.     switch (fork()) {
  65.     case -1:
  66.         return (-1);
  67.     case 0:
  68.         break;
  69.     default:
  70.         _exit(EXIT_SUCCESS);
  71.     }

  72.     if (setsid() == -1)
  73.         return (-1);

  74.     if (nochdir == 0) {
  75.         if(chdir("/") != 0) {
  76.             perror("chdir");
  77.             return (-1);
  78.         }
  79.     }

  80.     if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
  81.         if(dup2(fd, STDIN_FILENO) < 0) {
  82.             perror("dup2 stdin");
  83.             return (-1);
  84.         }
  85.         if(dup2(fd, STDOUT_FILENO) < 0) {
  86.             perror("dup2 stdout");
  87.             return (-1);
  88.         }
  89.         if(dup2(fd, STDERR_FILENO) < 0) {
  90.             perror("dup2 stderr");
  91.             return (-1);
  92.         }

  93.         if (fd > STDERR_FILENO) {
  94.             if(close(fd) < 0) {
  95.                 perror("close");
  96.                 return (-1);
  97.             }
  98.         }
  99.     }
  100.     return (0);
  101. }
阅读(686) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~