Skip to content

Commit 7c2abd2

Browse files
author
marttcw
committed
0.0.1 release - a proper daemon
1 parent 73e7afa commit 7c2abd2

File tree

6 files changed

+100
-3
lines changed

6 files changed

+100
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ newappid 244630 "NEOTOKYO" 0
4949
newappid 282440 "Quake Live" 100
5050
```
5151

52+
## Releases
53+
### 0.0.1 Alpha release
54+
* First release
55+
* Basic daemon implementation implemented
56+

meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project('steamcountsnotifyd'
22
, 'cpp'
33
, default_options : ['warning_level=3', 'cpp_std=c++17', 'optimization=3', 'werror=false']
4-
, version : 'PRE-ALPHA 20190911'
4+
, version : '0.0.1'
55
)
66

77
cc = meson.get_compiler('cpp')
@@ -32,6 +32,7 @@ src = [
3232
, './src/tool/getPlayerCount.cpp'
3333
, './src/tool/homeDirectory.cpp'
3434
, './src/wrapper/curl.cpp'
35+
, './src/wrapper/daemon.cpp'
3536
, './src/wrapper/notify.cpp'
3637
]
3738

src/core/help.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"steamcountsnotifyd\n"\
1717
"The Steam player count notification daemon\n"\
1818
"\n"\
19-
"Version Pre-Alpha 2019-09-08\n"
19+
"0.0.1 - Alpha Release\n"
2020

2121
#endif

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ int main(int argc, char **argv)
3535
// If help or version message not used (normal execution)
3636
if (running)
3737
{
38+
wrapper::daemon::init();
39+
3840
//std::function<void(unsigned int, param::appidName_s, const param::config &)> thread::appidRunning;
3941
wrapper::notify::init("steamcountsnotifyd");
4042

@@ -56,6 +58,7 @@ int main(int argc, char **argv)
5658
}
5759

5860
wrapper::notify::uninit();
61+
wrapper::daemon::uninit();
5962
}
6063

6164
return 0;

src/wrapper/daemon.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "wrapper/daemon.h"
2+
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
#include <signal.h>
6+
#include <sys/types.h>
7+
#include <sys/stat.h>
8+
#include <syslog.h>
9+
10+
void signal_handler(int sig)
11+
{
12+
switch (sig)
13+
{
14+
case SIGHUP:
15+
syslog(LOG_NOTICE, "Daemon hangup signal catched");
16+
break;
17+
case SIGTERM:
18+
syslog(LOG_NOTICE, "Daemon terminate signal catched");
19+
exit(0);
20+
break;
21+
}
22+
}
23+
24+
void wrapper::daemon::init()
25+
{
26+
pid_t pid;
27+
pid = fork(); // Fork off parent process
28+
29+
// Error occurred
30+
if (pid < 0)
31+
{
32+
exit(EXIT_FAILURE);
33+
}
34+
35+
// Success: Let parent terminate
36+
if (pid > 0)
37+
{
38+
exit(EXIT_SUCCESS);
39+
}
40+
41+
// On success: Child process becomes session leader
42+
if (setsid() < 0)
43+
{
44+
exit(EXIT_FAILURE);
45+
}
46+
47+
// Catch, ignore and handle signals
48+
signal(SIGCHLD, SIG_IGN); // Ignore child
49+
signal(SIGTSTP, SIG_IGN); // Ignore tty signals
50+
signal(SIGTTOU, SIG_IGN); // Ignore terminal output
51+
signal(SIGTTIN, SIG_IGN); // Ignore terminal input
52+
signal(SIGHUP, signal_handler);
53+
signal(SIGTERM, signal_handler);
54+
55+
pid = fork();
56+
57+
// Error occurred
58+
if (pid < 0)
59+
{
60+
exit(EXIT_FAILURE);
61+
}
62+
63+
// Success: Let parent terminate
64+
if (pid > 0)
65+
{
66+
exit(EXIT_SUCCESS);
67+
}
68+
69+
umask(0);
70+
chdir("/");
71+
72+
// Close all open file descriptors
73+
int x;
74+
for (x = sysconf(_SC_OPEN_MAX); x >= 0; x--)
75+
{
76+
close(x);
77+
}
78+
79+
// Open logging
80+
openlog("steamcountsnotifyd", LOG_PID, LOG_DAEMON);
81+
}
82+
83+
void wrapper::daemon::uninit()
84+
{
85+
closelog();
86+
}
87+

src/wrapper/daemon.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace wrapper
55
{
66
namespace daemon
77
{
8-
8+
void init();
9+
void uninit();
910
}
1011
}
1112

0 commit comments

Comments
 (0)