GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gammalibd.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * gammalibd.cpp - GammaLib daemon launcher *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2022 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file gammalibd.cpp
23  * @brief GammaLib daemon launcher
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <fcntl.h> // for file locking
32 #include <unistd.h> // access() function
33 #include <cstdlib> // exit() function
34 #include <cstdio> // std::fopen(), etc. functions
35 #include <signal.h> // signal() function
36 #include "GammaLib.hpp" // make GammaLib classes available
37 
38 
39 /***********************************************************************//**
40  * @brief GammaLib daemon launcher
41  *
42  * This executable launched the GammaLib daemon. Calling this executable
43  * during the initialisation of GammaLib will launch a daemon that has the
44  * correct process name on all platforms.
45  ***************************************************************************/
46 int main(void) {
47 
48  // Initialise daemon
49  GDaemon daemon;
50 
51  // If daemon is not alive then create instance
52  if (!daemon.alive()) {
53 
54  // Create child process to start the daemon. Do nothing if child
55  // process creation fails.
56  int pid = fork();
57  if (pid >= 0) {
58 
59  // If we have a PID of 0 we are in the child process. In this
60  // case we create and start the daemon ...
61  if (pid == 0) {
62 
63  // The child process becomes session leader
64  setsid();
65 
66  // Ignore signals
67  signal(SIGCHLD, SIG_IGN); // Ignore if child has stopped
68  signal(SIGHUP, SIG_IGN); // Ignore death of controlling process
69  signal(SIGTERM, SIG_IGN); // Ignore termination signal
70 
71  // Fork for the second time and let the first fork
72  // process terminate
73  pid = fork();
74  if (pid >= 0) {
75  if (pid == 0) {
76  GDaemon daemon;
77  daemon.start();
78  exit(EXIT_SUCCESS);
79  }
80  } // endif: child of child process created
81 
82  }
83 
84  } // endif: child proces created
85 
86  } // endif: daemon
87 
88  // Always exit with success
89  return EXIT_SUCCESS;
90 }
bool alive(void) const
Check if daemon is alive.
Definition: GDaemon.cpp:282
int main(void)
GammaLib daemon launcher.
Definition: gammalibd.cpp:46
Daemon class.
Definition: GDaemon.hpp:51
GammaLib definitions.
void start(void)
Starts the daemon.
Definition: GDaemon.cpp:177