-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
239 lines (193 loc) · 5.24 KB
/
main.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
########## Copyright (C) 2016 Vincenzo Pacella
## ## Distributed under MIT license, see file LICENSE
## ## or <http://opensource.org/licenses/MIT>
## ##
########## #############################################################
shaduzlabs.com #####*/
#include <memory>
#include <tuple>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include <chrono>
#include <thread>
#include <iostream>
#include "Ports.h"
#define DAEMON_NAME "portsd"
int pidFilehandle;
void daemonShutdown()
{
close(pidFilehandle);
}
void signalHandler(int sig)
{
switch (sig)
{
case SIGHUP:
syslog(LOG_WARNING, "Received SIGHUP signal.");
break;
case SIGINT:
case SIGTERM:
syslog(LOG_INFO, "Daemon exiting");
daemonShutdown();
exit(EXIT_SUCCESS);
break;
default:
syslog(LOG_WARNING, "Unhandled signal %s", strsignal(sig));
break;
}
}
//--------------------------------------------------------------------------------------------------
static void daemonize(const char* runDir_, const char* pidFile_)
{
/* Our process ID and Session ID */
pid_t pid, sid;
int i;
char str[10];
struct sigaction newSigAction;
sigset_t newSigSet;
/* Check if parent process id is set */
if (getppid() == 1)
{
/* PPID exists, therefore we are already a daemon */
return;
}
sigemptyset(&newSigSet);
sigaddset(&newSigSet, SIGCHLD); /* ignore child - i.e. we don't need to wait for it */
sigaddset(&newSigSet, SIGTSTP); /* ignore Tty stop signals */
sigaddset(&newSigSet, SIGTTOU); /* ignore Tty background writes */
sigaddset(&newSigSet, SIGTTIN); /* ignore Tty background reads */
sigprocmask(SIG_BLOCK, &newSigSet, NULL); /* Block the above specified signals */
/* Set up a signal handler */
newSigAction.sa_handler = signalHandler;
sigemptyset(&newSigAction.sa_mask);
newSigAction.sa_flags = 0;
/* Signals to handle */
sigaction(SIGHUP, &newSigAction, NULL); /* catch hangup signal */
sigaction(SIGTERM, &newSigAction, NULL); /* catch term signal */
sigaction(SIGINT, &newSigAction, NULL); /* catch interrupt signal */
/* Fork off the parent process */
pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0)
{
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(027);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
/* Log the failure */
syslog(LOG_ERR, "ERROR: Failed to create a new session");
exit(EXIT_FAILURE);
}
/* Close out the open file descriptors */
for (int fd = sysconf(_SC_OPEN_MAX); fd > 0; fd--)
{
close(fd);
}
/* Route I/O connections */
/* Open STDIN */
i = open("/dev/null", O_RDWR);
/* STDOUT */
dup(i);
/* STDERR */
dup(i);
if ((chdir(runDir_)) < 0)
{
syslog(LOG_ERR, "ERROR: Failed to change the working directory to %s", runDir_);
exit(EXIT_FAILURE);
}
/* Ensure only one copy */
pidFilehandle = open(pidFile_, O_RDWR | O_CREAT, 0600);
if (pidFilehandle == -1)
{
/* Couldn't open lock file */
syslog(LOG_INFO, "Could not open PID lock file %s, exiting", pidFile_);
exit(EXIT_FAILURE);
}
/* Try to lock file */
if (lockf(pidFilehandle, F_TLOCK, 0) == -1)
{
/* Couldn't get lock on lock file */
syslog(LOG_INFO, "Could not lock PID lock file %s, exiting", pidFile_);
exit(EXIT_FAILURE);
}
/* Get and format PID */
sprintf(str, "%d\n", getpid());
/* write pid to lockfile */
write(pidFilehandle, str, strlen(str));
}
//--------------------------------------------------------------------------------------------------
class ScopedLog
{
public:
ScopedLog()
{
setlogmask(LOG_UPTO(LOG_INFO));
openlog(DAEMON_NAME, LOG_CONS | LOG_PERROR, LOG_USER);
}
~ScopedLog()
{
closelog();
}
void info(const char* message_)
{
syslog(LOG_INFO, message_);
}
void exception(const std::exception& e_)
{
syslog(LOG_ERR, "ERROR: An exception occurred (%s)", e_.what());
}
};
//--------------------------------------------------------------------------------------------------
int main(int argc_, char* argv_[])
{
ScopedLog log;
log.info("daemon starting");
//if (argc_ != 2 || std::string(argv_[1]) != "no-daemon")
//{
// daemonize("/tmp/", "/var/run/portsd.pid");
//}
portsInstance.start();
try
{
while (!portsInstance.stop)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
catch (const std::exception& e)
{
log.exception(e);
}
std::cout << "aaa\n";
if (portsInstance.restart){
std::cout << "xx\n";
//char buf[20];
//sprintf(buf, "%d", count);
char* newargv[3];
newargv[0] = "/usr/sbin/portsd";
newargv[1] = "no-daemon";
newargv[2] = NULL;
//execve("/usr/sbin/portsd", newargv, NULL);
//system("sleep 3; sudo nohup /usr/sbin/portsd no-daemon &");
}
log.info("daemon terminated");
return 0;
}
//--------------------------------------------------------------------------------------------------