-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
163 lines (126 loc) · 3.8 KB
/
main.cpp
File metadata and controls
163 lines (126 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: abratchik
*
* Created on January 8, 2022, 2:12 PM
*/
#include <dirent.h>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <sys/stat.h>
#include <syslog.h>
#include <vector>
#include <signal.h>
#include "AuxDisplay.h"
using namespace std;
const string AUXDISPLAY = "auxdisplay-daemon";
bool keepalive = true;
void do_heartbeat(AuxDisplay* auxdisplay)
{
auxdisplay->show();
}
void signal_handler(int sig) {
switch(sig) {
case SIGTERM:
keepalive = false;
break;
default:
break;
}
}
/*
*
*/
int main(void) {
// Define variables
pid_t pid, sid;
#ifndef NOFORK
// Fork the current process
pid = fork();
// The parent process continues with a process ID greater than 0
if(pid > 0)
{
exit(EXIT_SUCCESS);
}
// A process ID lower than 0 indicates a failure in either process
else if(pid < 0)
{
exit(EXIT_FAILURE);
}
// The parent process has now terminated, and the forked child process will continue
// (the pid of the child process was 0)
// Since the child process is a daemon, the umask needs to be set so files and logs can be written
umask(0);
#endif
// Open system logs for the child process
openlog(AUXDISPLAY.c_str(), LOG_NOWAIT | LOG_PID, LOG_USER);
syslog(LOG_NOTICE, "Successfully started %s", AUXDISPLAY.c_str());
#ifndef NOSESSION
// Generate a session ID for the child process
sid = setsid();
// Ensure a valid SID for the child process
if(sid < 0)
{
// Log failure and exit
syslog(LOG_ERR, "Could not generate session ID for child process of %s", AUXDISPLAY.c_str());
// If a new session ID could not be generated, we must terminate the child process
// or it will be orphaned
exit(EXIT_FAILURE);
}
#endif
#ifndef NOWORKDIR
// Change the current working directory to a directory guaranteed to exist
if((chdir("/tmp")) < 0)
{
// Log failure and exit
syslog(LOG_ERR, "Could not change working directory to / for %s", AUXDISPLAY.c_str());
// If our guaranteed directory does not exist, terminate the child process to ensure
// the daemon has not been hijacked
exit(EXIT_FAILURE);
}
#endif
// A daemon cannot use the terminal, so close standard file descriptors for security reasons
close(STDIN_FILENO);
#ifndef NOCLOSEIO
close(STDOUT_FILENO);
close(STDERR_FILENO);
#endif
// Enter daemon loop
AuxDisplay auxdisplay;
syslog(LOG_NOTICE, "Initializing driver for %s", AUXDISPLAY.c_str());
if (auxdisplay.load_config() != CONFIG_LOAD_SUCCESS) {
syslog(LOG_ERR, "Could not initialize %s", AUXDISPLAY.c_str());
}
// Register signal handlers
signal(SIGTERM, signal_handler);
syslog(LOG_NOTICE, "Starting daemon loop for %s", AUXDISPLAY.c_str());
while(keepalive)
{
if (auxdisplay.initialize()) {
// Execute daemon heartbeat, where your recurring activity occurs
do_heartbeat(&auxdisplay);
// Sleep for a period of time
// syslog(LOG_NOTICE, "Executed show for %s", AUXDISPLAY.c_str());
}
else {
syslog(LOG_ERR, "Could not initialize %s", AUXDISPLAY.c_str());
}
usleep(auxdisplay.get_refresh() * 1000);
}
// Close system logs for the child process
syslog(LOG_NOTICE, "Stopping %s", AUXDISPLAY.c_str());
closelog();
// Terminate the child process when the daemon completes
exit(EXIT_SUCCESS);
return 0;
}