返回列表 回复 发帖

Forked PHP Daemon Example

  1. <?php

  2. /*

  3.     PHP forked daemon
  4.     Standalone PHP binary must be compiled with --enable-sockets and --enable-pcntl
  5.     Dave M. -2002
  6.         Online Services USA
  7. */

  8. function sig_handler($signo) {

  9.      switch($signo) {
  10.          case SIGTERM:
  11.              // handle shutdown tasks
  12.              exit;
  13.              break;
  14.          case SIGHUP:
  15.              // handle restart tasks
  16.              break;
  17.          case SIGUSR1:
  18.              print "Caught SIGUSR1...\n";
  19.              break;
  20.          case SIGCHLD:
  21.              while( pcntl_waitpid(-1,$status,WNOHANG)>0 ) {
  22.              }
  23.              break;
  24.          case SIGINT:
  25.         exit;
  26.          default:
  27.              // not implemented yet...
  28.              break;
  29.      }

  30. }

  31. function interact($sock) {

  32.     // Custom code goes here... e.g: socket_read() socket_write()...

  33. }

  34. function become_daemon() {

  35.     $child = pcntl_fork();
  36.     if($child) {
  37.         exit; // kill parent
  38.     }
  39.     posix_setsid(); // become session leader
  40.     chdir("/");
  41.     umask(0); // clear umask
  42.     return posix_getpid();

  43. }

  44. function open_pid_file($file) {

  45.     if(file_exists($file)) {
  46.         $fp = fopen($file,"r");
  47.         $pid = fgets($fp,1024);
  48.         fclose($fp);
  49.         if(posix_kill($pid,0)) {
  50.             print "Server already running with PID: $pid\n";
  51.             exit;
  52.         }
  53.         print "Removing PID file for defunct server process $pid\n";
  54.         if(!unlink($file)) {
  55.             print "Cannot unlink PID file $file\n";
  56.             exit;
  57.         }
  58.     }
  59.     if($fp = fopen($file,"w")) {
  60.         return $fp;
  61.     } else {
  62.         print "Unable to open PID file $file for writing...\n";
  63.         exit;
  64.     }
  65. }

  66. function change_identity($uid,$gid) {
  67.     global $pid_file;
  68.     if(!posix_setgid($gid)) {
  69.         print "Unable to setgid to $gid!\n";
  70.         unlink($pid_file);
  71.         exit;
  72.     }   
  73.     if(!posix_setuid($uid)) {
  74.         print "Unable to setuid to $uid!\n";
  75.         unlink($pid_file);
  76.         exit;
  77.     }
  78.    
  79.    
  80. }

  81. error_reporting (4);

  82. set_time_limit (0);

  83. ob_implicit_flush ();

  84. $pid_file = '/tmp/php_daemon.pid';

  85. $underpriv_uid = '99'; // uid 99 == user nobody, at least on my system.
  86. $underpriv_gid = '99';

  87. $port = 10000;
  88. $address = 0; // 0 binds to all addresses, may not work on fbsd

  89. $quit = 0;

  90. pcntl_signal(SIGCHLD, "sig_handler");
  91. pcntl_signal(SIGTERM, "sig_handler");
  92. pcntl_signal(SIGINT, "sig_handler");

  93. $fh = open_pid_file($pid_file);

  94. if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
  95.     print "socket_create() failed: reason: " . socket_strerror ($sock) . "\n";
  96. }

  97. if (($ret = socket_bind ($sock, $address, $port)) < 0) {
  98.     print "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
  99. }

  100. if (($ret = socket_listen ($sock, 0)) < 0) {
  101.     print "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
  102. }

  103. change_identity($underpriv_uid,$underpriv_gid);

  104. print "Server ready. Waiting for connections.....\n";

  105. $pid = become_daemon();
  106. fputs($fh,$pid);
  107. fclose($fh);

  108. while(!$quit) {

  109.     if (($connection = socket_accept($sock)) < 0) {
  110.         next;
  111.     }
  112.    
  113.     if( ($child = pcntl_fork()) == -1 ) {
  114.         print  "Could not fork!!\n";
  115.         print "Dying...\n";
  116.         $quit++;
  117.     }
  118.     elseif($child == 0) {
  119.         socket_close($sock);
  120.         interact($connection);
  121.         exit;
  122.     }
  123.    
  124.     socket_close($connection);
  125.    
  126. }

  127. if(posix_getpid() == $pid) {
  128.     unlink($pid_file);
  129. }
  130. ?>
复制代码
Postfix技术专业支持论坛
http://www.thismail.org/bbs
-----------------------------------
提供专业postfix技术支持,邮件系统开发定制
QQ:187159779 注明(Postfix技术支持)
返回列表
开源邮件服务器 开源邮件服务器 web 开源邮件 开源 mail 开源 邮件服务器 邮件技术 mail技术 反垃圾邮件 反垃圾mail mail投递
邮件服务器 mail服务器 开源软件 mail软件 mail服务新品牌 开源邮件服务新品牌
开源mail服务新网站 邮件服务新品牌 mail tmail mailserver 163邮件 sendmail |Linux维护|Linux代维|成都Linux维护|成都Linux代维