-
Notifications
You must be signed in to change notification settings - Fork 0
/
sircond.cpp
220 lines (187 loc) · 4.13 KB
/
sircond.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
/*
* $Id: //depot/sircond/sircond.cpp#30 $
* $Change: 2154 $
* $DateTime: 2015/08/23 13:00:12 $
*
* Copyright (C) 2015 Swarga Research (http://www.swarga-research.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//!
//! \file sircond.cpp
//! \brief Main Module.
//!
#ifdef WIN32
#pragma warning(disable:4996)
#endif
#include "pch.h"
#include <iostream>
#include "sirserver.h"
//!
//! \brief A simple application wrapper.
//!
class CDaemon
{
public:
CDaemon ();
int Launch (int argc, char* argv[]);
protected:
bool Init (int argc, char* argv[]);
void Run ();
void Shutdown ();
private:
string m_pidfile;
string m_logroot;
string m_logfile;
bool m_shutdown;
CSirServer* m_server;
};
//========================================================================
CDaemon::CDaemon () : m_shutdown(false), m_server(0)
{
#ifndef WIN32
m_pidfile = "/var/run/sircond.pid";
m_logroot = "/var/log/";
#endif
m_logfile = m_logroot + "sircond.log";
}
//========================================================================
bool CDaemon::Init (int argc, char* argv[])
{
#ifdef WIN32
WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) != 0)
{
printf("Network initialization failed.\n");
return 1;
}
#endif
// PROCESS COMMAND LINE ARGS
if (argc < 2)
{
return false;
}
// OPEN A LOG FILE
LogSetLevel(EL_DEBUG);
if (!LogOpen(m_logfile))
{
std::cout << "Error opening log!" << std::endl;
return false;
}
LogWrite(LEVEL_INFO, "Startup.");
#ifndef _WIN32
// CREATE A PID FILE
CreatePidFile(m_pidfile);
#endif
// INSTANTIATE THE SERVER OBJECT
m_server = new CSirServer(argv[1]);
if (m_server == 0)
{
LogWrite(LEVEL_CRITICAL, "Failed to instantiate server object.");
return false;
}
// LAUNCH THE SERVER
if (!m_server->Start())
{
return false;
}
while (m_server->GetState() == CSirServer::STARTING)
{
sleep(1);
}
// RETURN LAUNCH STATUS
return (m_server->GetState() == CSirServer::RUNNING);
}
//========================================================================
void CDaemon::Run ()
{
#ifndef WIN32
sigset_t mask;
// RUN UNTIL SIGNALLED
sigfillset(&mask);
if (pthread_sigmask(SIG_BLOCK, &mask, 0) == 0)
{
while (!m_shutdown)
{
siginfo_t info;
if (sigwaitinfo(&mask, &info) > 0)
{
switch (info.si_signo)
{
case SIGHUP:
LogReopen(m_logfile);
LogWrite(LEVEL_INFO, "Log pinched.");
break;
case SIGINT:
case SIGTERM:
default:
LogWrite(LEVEL_DEBUG,
"Got signal %d from process %d - exiting.",
info.si_signo,
info.si_pid);
m_shutdown = true;
break;
}
}
else
{
LogWrite(LEVEL_ERROR, "sigwait fail");
}
}
}
else
{
LogWrite(LEVEL_ERROR, "pthread_sigmask fail.");
}
#else
// UNDER WINDOWS JUST RUN UNTIL THE USER CLOSES THE APP
while (!m_shutdown && (m_server->GetState() == CSirServer::RUNNING))
{
sleep(1);
}
#endif
}
//========================================================================
void CDaemon::Shutdown ()
{
if (m_server != 0)
{
m_server->Stop();
delete m_server;
m_server = 0;
}
LogClose();
#ifndef WIN32
unlink(m_pidfile.c_str());
#else
WSACleanup();
#endif
}
//========================================================================
int CDaemon::Launch (int argc, char* argv[])
{
if (Init(argc, argv))
{
Run();
}
Shutdown();
return 0;
}
//========================================================================
int main (int argc, char* argv[])
{
CDaemon d;
return (d.Launch(argc, argv));
}