3434#include < unistd.h>
3535
3636/* stl header */
37+ #if __has_include(<filesystem>)
38+ #include < filesystem>
39+ #endif
3740#include < fstream>
3841#include < sstream>
3942#include < string>
@@ -44,11 +47,8 @@ constexpr auto DAEMON_NAME = "Demo";
4447int main () {
4548
4649 // DAEMONIZE START
47- /* Define variables */
48- pid_t pid, sid;
49-
5050 /* Fork the current process */
51- pid = fork ();
51+ pid_t pid = fork ();
5252 /* The parent process continues with a process ID greater than 0 */
5353 if ( pid > 0 ) {
5454
@@ -70,7 +70,7 @@ int main() {
7070 syslog ( LOG_NOTICE, " Successfully started %s" , DAEMON_NAME );
7171
7272 // Generate a session ID for the child process
73- sid = setsid ();
73+ pid_t sid = setsid ();
7474 /* Ensure a valid SID for the child process */
7575 if ( sid < 0 ) {
7676
@@ -103,31 +103,19 @@ int main() {
103103 ss << " /var/run/" << DAEMON_NAME << " .pid" ;
104104 std::string pidfile = ss.str ();
105105
106- std::ifstream fileCheck ( pidfile );
107- /* Try to lock file - or even check if its available on c++ */
108- if ( !fileCheck.good () ) {
109-
110- /* Couldn't get lock on lock file */
111- /* NOTE: In c++ it is not (yet) possible to lock a file */
112- std::ofstream fileCreate;
113- fileCreate.open ( pidfile );
114- if ( !fileCreate.is_open () ) {
115-
116- /* Couldn't create lock file */
117- syslog ( LOG_INFO, " Could not create PID lock file %s, exiting" , pidfile.c_str () );
118- exit ( EXIT_FAILURE );
119- }
120- fileCreate.close ();
121- }
122-
123106 std::ofstream file;
124- file.open ( pidfile, std::ofstream::out | std::ofstream::in );
125- if ( !file. is_open () ) {
107+ file.exceptions ( std::ofstream::failbit | std::ofstream::badbit );
108+ try {
126109
127- /* Couldn't open lock file */
128- syslog ( LOG_INFO, " Could not open PID lock file %s, exiting" , pidfile.c_str () );
110+ file.open ( pidfile, std::ofstream::out | std::ofstream::in );
111+ }
112+ catch ( const std::ofstream::failure &_exception ) {
113+
114+ /* Couldn't open pid lock file */
115+ syslog ( LOG_INFO, " Could not open or create PID lock file %s: %s, exiting" , pidfile.c_str (), _exception.what () );
129116 exit ( EXIT_FAILURE );
130117 }
118+ file.exceptions ( 0 );
131119
132120 /* Read pid from file, if there is any, find if the process is running - EXIT */
133121 std::stringstream buffer;
@@ -145,7 +133,16 @@ int main() {
145133
146134 /* write pid to lockfile */
147135 file << str;
148- file.close ();
136+ try {
137+
138+ file.close ();
139+ }
140+ catch ( ... ) {
141+
142+ /* Couldn't close pid lock file */
143+ syslog ( LOG_INFO, " Could not close PID lock file %s, exiting" , pidfile.c_str () );
144+ exit ( EXIT_FAILURE );
145+ }
149146 // DAEMONIZE END
150147
151148 // SERVICE START
@@ -159,9 +156,12 @@ int main() {
159156 closelog ();
160157
161158 /* Remove content of file */
159+ #if __has_include(<filesystem>)
160+ std::filesystem::remove ( pidfile );
161+ #else
162162 std::ofstream ofs;
163163 ofs.open ( pidfile, std::ofstream::out | std::ofstream::trunc );
164- ofs. close ();
164+ # endif
165165 // CLEANUP END
166166
167167 /* Terminate the child process when the daemon completes */
0 commit comments