Chinaunix首页 | 论坛 | 博客
  • 博客访问: 558991
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: LINUX

2015-05-29 17:16:42

become_daemon.h

点击(此处)折叠或打开

  1. /***become_daemon.h***/
  2. #ifndef __BECOME_DAEMON_H__
  3. #define __BECOME_DAEMON_H__

  4. #define BD_NO_CHDIR 01 /*Don't chdir("/")*/
  5. #define BD_NO_CLOSE_FILES 02 /*Don't close all open files*/
  6. #define BD_NO_REOPEN_STD_FDS 04 /*Don't reopen stdin, stdout, and stderr to /dev/null*/
  7. #define BD_NO_UNMASK 010 /*Don't do a unmask(0)*/
  8. #define BD_MAX_CLOSE 8192 /*Maximum file descriptors to close if sysconf(_SC_OPEN_MAX) is
  9.                         indeterminate*/
  10. int becomeDaemon(int flags);

  11. #endif
become_daemon.c

点击(此处)折叠或打开

  1. /*become_daemon.c*/
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include "become_daemon.h"

  7. int becomeDamon(int flags)
  8. {
  9.         int maxfd, fd;
  10.         switch(fork()) {
  11.         case -1:
  12.             return -1;
  13.         case 0:
  14.             break;
  15.         default:
  16.             _exit(0);
  17.         }
  18.     
  19.     if(setsid() < 0) {
  20.         printf("setsid error\n");
  21.         return -1;
  22.     }

  23.     switch(fork()) {
  24.         case -1:
  25.             return -1;
  26.         case 0:
  27.             break;
  28.         default:
  29.             _exit(0);
  30.     }

  31.     if(!(flags && BD_NO_UNMASK)) {
  32.         umask(0);
  33.     }
  34.     
  35.     if(!(flags && BD_NO_CHDIR)) {
  36.         chdir("/");
  37.     }

  38.     if(!(flags && BD_NO_CLOSE_FILES)) {
  39.         maxfd = sysconf(_SC_OPEN_MAX);
  40.         printf("maxfd = %d,BD_MAX_CLOSE=%d\n",maxfd,BD_MAX_CLOSE);
  41.         if(maxfd == -1)
  42.             maxfd = BD_MAX_CLOSE;
  43.         for(fd = 0; fd < maxfd; fd++)
  44.             close(fd);
  45.     }

  46.     if(!(flags && BD_NO_REOPEN_STD_FDS)) {
  47.         close(STDIN_FILENO);
  48.         
  49.         fd = open("/dev/null", O_RDWR);
  50.         if(fd != STDIN_FILENO)
  51.             return -1;
  52.         if(dup2(fd,STDOUT_FILENO) != STDOUT_FILENO)
  53.             return -1;
  54.         if(dup2(fd,STDERR_FILENO) != STDERR_FILENO)
  55.             return -1;
  56.     }
  57.     return 0;
  58. }
deamon_case.c

点击(此处)折叠或打开

  1. /**deamon_case.c**/
  2. #include "become_daemon.h"
  3. #include <stdio.h>
  4. #include <unistd.h>

  5. int main(int argc, char *argv[])
  6. {
  7.     becomeDamon(0);
  8.     sleep(100);
  9.     return 0;
  10. }
编译运行:

点击(此处)折叠或打开

  1. [root@localhost 37_daemon]# gcc -g become_daemon.c deamon_case.c -o daemon
  2. [root@localhost 37_daemon]# ./daemon
  3. maxfd = 1024,BD_MAX_CLOSE=8192
  4. [root@localhost 37_daemon]# ps -ef | grep daemon
  5. dbus 3054 1 0 10:13 ? 00:00:02 dbus-daemon --system
  6. xfs 3384 1 0 10:13 ? 00:00:00 xfs -droppriv -daemon
  7. avahi 3428 1 3 10:13 ? 00:14:34 avahi-daemon: running [localhost.local]
  8. avahi 3429 3428 0 10:13 ? 00:00:00 avahi-daemon: chroot helper
  9. root 3507 1 0 10:13 ? 00:00:00 /usr/sbin/gdm-binary -nodaemon
  10. root 3544 3507 0 10:13 ? 00:00:00 /usr/sbin/gdm-binary -nodaemon
  11. root 4025 1 0 10:38 ? 00:00:00 /bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session
  12. root 4035 1 0 10:38 ? 00:00:00 /usr/bin/gnome-keyring-daemon
  13. root 4037 1 0 10:38 ? 00:00:00 /usr/libexec/gnome-settings-daemon
  14. root 4070 1 0 10:38 ? 00:00:00 /usr/libexec/gnome-vfs-daemon
  15. root 4142 1 0 10:38 ? 00:00:00 /usr/libexec/mapping-daemon
  16. root 19840 1 0 17:15 ? 00:00:00 ./daemon
  17. root 19842 19657 0 17:15 pts/2 00:00:00 grep daemon
  18. [root@localhost 37_daemon]# ps -C daemon -o "pid ppid pgid sid tty command"
  19.   PID PPID PGID SID TT COMMAND
  20. 19840 1 19839 19839 ? ./daemon
  21. [root@localhost 37_daemon]#



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