Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added --daemon option

  • Loading branch information...
commit 7f54026efeeb614326fdd8ec7c38c057a6177dfa 1 parent fdbbfe1
Leonid A. Movsesjan lmovsesjan authored
6 debian/changelog
... ... @@ -1,3 +1,9 @@
  1 +fastcgi-daemon2 (2.10-12) unstable; urgency=low
  2 +
  3 + * added --daemon option
  4 +
  5 + -- Leonid A. Movsesjan <lmovsesjan@yandex-team.ru> Fri, 15 Jun 2012 10:54:07 +0400
  6 +
1 7 fastcgi-daemon2 (2.10-11) unstable; urgency=low
2 8
3 9 * Fixed build on Ubuntu 12.04 Presice
2  fastcgi-daemon2-bf.spec
... ... @@ -1,7 +1,7 @@
1 1 Summary: fastcgi-daemon2 is an application server for FastCGI
2 2 Name: fastcgi-daemon2
3 3 Version: 2.10
4   -Release: 10%{?dist}
  4 +Release: 12%{?dist}
5 5
6 6 License: Yandex License
7 7 Group: System Environment/Libraries
71 main/main.cpp
@@ -5,6 +5,9 @@
5 5 #include <iostream>
6 6 #include <stdexcept>
7 7
  8 +#include <sys/types.h>
  9 +#include <sys/stat.h>
  10 +
8 11 #include "fcgi_server.h"
9 12 #include "fastcgi2/config.h"
10 13 #include "details/globals.h"
@@ -15,6 +18,58 @@
15 18
16 19 fastcgi::FCGIServer *server;
17 20
  21 +bool
  22 +daemonize() {
  23 + pid_t pid = fork();
  24 + if (pid == -1) {
  25 + std::stringstream ss;
  26 + ss << "Could not become a daemon: fork #1 failed: " << errno;
  27 + throw std::logic_error(ss.str());
  28 + }
  29 + if (pid != 0) {
  30 + _exit(0); // exit parent
  31 + }
  32 +
  33 + pid_t sid = setsid();
  34 + if (sid == -1) {
  35 + std::stringstream ss;
  36 + ss << "Could not become a daemon: setsid failed: " << errno;;
  37 + throw std::logic_error(ss.str());
  38 + }
  39 +
  40 + // check fork for child
  41 + pid = fork();
  42 + if (pid == -1) {
  43 + std::stringstream ss;
  44 + ss << "Could not become a daemon: fork #2 failed: " << errno;
  45 + throw std::logic_error(ss.str());
  46 + }
  47 + if (pid != 0) {
  48 + _exit(0); // exit session leader
  49 + }
  50 +
  51 + for (int i = getdtablesize(); i--; ) {
  52 + close(i);
  53 + }
  54 + umask(0002); // disable: S_IWOTH
  55 + chdir("/");
  56 +
  57 + const char *devnull = "/dev/null";
  58 + stdin = fopen(devnull, "a+");
  59 + if (stdin == NULL) {
  60 + return false;
  61 + }
  62 + stdout = fopen(devnull, "w");
  63 + if (stdout == NULL) {
  64 + return false;
  65 + }
  66 + stderr = fopen(devnull, "w");
  67 + if (stderr == NULL) {
  68 + return false;
  69 + }
  70 + return true;
  71 +}
  72 +
18 73 void
19 74 signalHandler(int signo) {
20 75 if ((SIGINT == signo || SIGTERM == signo) && ::server != NULL) {
@@ -33,12 +88,20 @@ setUpSignalHandlers() {
33 88 }
34 89
35 90 int
36   -main(int argc, char *argv[]) {
37   -
  91 +main(int argc, char *argv[]) {
38 92 using namespace fastcgi;
39 93 try {
40   - std::auto_ptr<Config> config = Config::create(argc, argv);
41   - boost::shared_ptr<Globals> globals(new Globals(config.get()));
  94 + for (int i = 1; i < argc; ++i) {
  95 + if (!strcmp(argv[i], "--daemon")) {
  96 + if (!daemonize()) {
  97 + return EXIT_FAILURE;
  98 + }
  99 + break;
  100 + }
  101 + }
  102 +
  103 + std::auto_ptr<Config> config = Config::create(argc, argv);
  104 + boost::shared_ptr<Globals> globals(new Globals(config.get()));
42 105 FCGIServer server(globals);
43 106 ::server = &server;
44 107 setUpSignalHandlers();

0 comments on commit 7f54026

Please sign in to comment.
Something went wrong with that request. Please try again.