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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
// --------------------------------------------------------------------------
//
// File
// Name: BackupStoreDaemon.cpp
// Purpose: Backup store daemon
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
#include "Box.h"
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#include "BackupStoreContext.h"
#include "BackupStoreDaemon.h"
#include "BackupStoreConfigVerify.h"
#include "autogen_BackupProtocol.h"
#include "RaidFileController.h"
#include "BackupStoreAccountDatabase.h"
#include "BackupStoreAccounts.h"
#include "BannerText.h"
#include "MemLeakFindOn.h"
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::BackupStoreDaemon()
// Purpose: Constructor
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
BackupStoreDaemon::BackupStoreDaemon()
: mpAccountDatabase(0),
mpAccounts(0),
mExtendedLogging(false),
mHaveForkedHousekeeping(false),
mIsHousekeepingProcess(false),
mHousekeepingInited(false),
mInterProcessComms(mInterProcessCommsSocket),
mpTestHook(NULL)
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::~BackupStoreDaemon()
// Purpose: Destructor
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
BackupStoreDaemon::~BackupStoreDaemon()
{
// Must delete this one before the database ...
if(mpAccounts != 0)
{
delete mpAccounts;
mpAccounts = 0;
}
// ... as the mpAccounts object has a reference to it
if(mpAccountDatabase != 0)
{
delete mpAccountDatabase;
mpAccountDatabase = 0;
}
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::DaemonName()
// Purpose: Name of daemon
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
const char *BackupStoreDaemon::DaemonName() const
{
return "bbstored";
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::DaemonBanner()
// Purpose: Daemon banner
// Created: 1/1/04
//
// --------------------------------------------------------------------------
std::string BackupStoreDaemon::DaemonBanner() const
{
return BANNER_TEXT("Backup Store Server");
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::GetConfigVerify()
// Purpose: Configuration definition
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
const ConfigurationVerify *BackupStoreDaemon::GetConfigVerify() const
{
return &BackupConfigFileVerify;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::SetupInInitialProcess()
// Purpose: Setup before we fork -- get raid file controller going
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
void BackupStoreDaemon::SetupInInitialProcess()
{
const Configuration &config(GetConfiguration());
// Initialise the raid files controller
RaidFileController &rcontroller = RaidFileController::GetController();
std::string raidFileConfig;
#ifdef WIN32
if (!config.KeyExists("RaidFileConf"))
{
raidFileConfig = BOX_GET_DEFAULT_RAIDFILE_CONFIG_FILE;
}
else
{
raidFileConfig = config.GetKeyValue("RaidFileConf");
}
#else
raidFileConfig = config.GetKeyValue("RaidFileConf");
#endif
rcontroller.Initialise(raidFileConfig);
// Load the account database
std::auto_ptr<BackupStoreAccountDatabase> pdb(BackupStoreAccountDatabase::Read(config.GetKeyValue("AccountDatabase").c_str()));
mpAccountDatabase = pdb.release();
// Create a accounts object
mpAccounts = new BackupStoreAccounts(*mpAccountDatabase);
// Ready to go!
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::Run()
// Purpose: Run shim for the store daemon -- read some config details
// Created: 2003/10/24
//
// --------------------------------------------------------------------------
void BackupStoreDaemon::Run()
{
// Get extended logging flag
mExtendedLogging = false;
const Configuration &config(GetConfiguration());
mExtendedLogging = config.GetKeyValueBool("ExtendedLogging");
// Fork off housekeeping daemon -- must only do this the first
// time Run() is called. Housekeeping runs synchronously on Win32
// because IsSingleProcess() is always true
#ifndef WIN32
if(!IsSingleProcess() && !mHaveForkedHousekeeping)
{
// Open a socket pair for communication
int sv[2] = {-1,-1};
if(::socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sv) != 0)
{
THROW_EXCEPTION(ServerException, SocketPairFailed)
}
int whichSocket = 0;
// Fork
switch(::fork())
{
case -1:
{
// Error
THROW_EXCEPTION(ServerException, ServerForkError)
}
break;
case 0:
{
// In child process
mIsHousekeepingProcess = true;
SetProcessTitle("housekeeping, idle");
whichSocket = 1;
// Change the log name
::openlog("bbstored/hk", LOG_PID, LOG_LOCAL6);
// Log that housekeeping started
BOX_INFO("Housekeeping process started");
// Ignore term and hup
// Parent will handle these and alert the
// child via the socket, don't want to
// randomly die!
::signal(SIGHUP, SIG_IGN);
::signal(SIGTERM, SIG_IGN);
}
break;
default:
{
// Parent process
whichSocket = 0;
}
break;
}
// Mark that this has been, so -HUP doesn't try and do this again
mHaveForkedHousekeeping = true;
// Attach the comms thing to the right socket, and close the other one
mInterProcessCommsSocket.Attach(sv[whichSocket]);
if(::close(sv[(whichSocket == 0)?1:0]) != 0)
{
THROW_EXCEPTION(ServerException, SocketCloseError)
}
}
#endif // WIN32
if(mIsHousekeepingProcess)
{
// Housekeeping process -- do other stuff
HousekeepingProcess();
}
else
{
// In server process -- use the base class to do the magic
ServerTLS<BOX_PORT_BBSTORED>::Run();
if (!mInterProcessCommsSocket.IsOpened())
{
return;
}
// Why did it stop? Tell the housekeeping process to do the same
if(IsReloadConfigWanted())
{
mInterProcessCommsSocket.Write("h\n", 2);
}
if(IsTerminateWanted())
{
mInterProcessCommsSocket.Write("t\n", 2);
}
}
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::Connection(SocketStreamTLS &)
// Purpose: Handles a connection, by catching exceptions and
// delegating to Connection2
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
void BackupStoreDaemon::Connection(std::auto_ptr<SocketStreamTLS> apStream)
{
try
{
Connection2(apStream);
}
catch(BoxException &e)
{
BOX_ERROR("Error in child process, terminating connection: " <<
e.what() << " (" << e.GetType() << "/" <<
e.GetSubType() << ")");
}
catch(std::exception &e)
{
BOX_ERROR("Error in child process, terminating connection: " <<
e.what());
}
catch(...)
{
BOX_ERROR("Error in child process, terminating connection: " <<
"unknown exception");
}
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDaemon::Connection2(SocketStreamTLS &)
// Purpose: Handles a connection from bbackupd
// Created: 2006/11/12
//
// --------------------------------------------------------------------------
void BackupStoreDaemon::Connection2(std::auto_ptr<SocketStreamTLS> apStream)
{
// Get the common name from the certificate
std::string clientCommonName(apStream->GetPeerCommonName());
// Log the name
BOX_INFO("Client certificate CN: " << clientCommonName);
// Check it
int32_t id;
if(::sscanf(clientCommonName.c_str(), "BACKUP-%x", &id) != 1)
{
// Bad! Disconnect immediately
BOX_WARNING("Failed login: invalid client common name: " <<
clientCommonName);
return;
}
// Make ps listings clearer
std::ostringstream tag;
tag << "client=" << BOX_FORMAT_ACCOUNT(id);
SetProcessTitle(tag.str().c_str());
Logging::Tagger tagWithClientID(tag.str());
// Create a context, using this ID
BackupStoreContext context(id, this, GetConnectionDetails());
if (mpTestHook)
{
context.SetTestHook(*mpTestHook);
}
// See if the client has an account?
if(mpAccounts && mpAccounts->AccountExists(id))
{
std::string root;
int discSet;
mpAccounts->GetAccountRoot(id, root, discSet);
context.SetClientHasAccount(root, discSet);
}
// Handle a connection with the backup protocol
std::auto_ptr<SocketStream> apPlainStream(apStream);
BackupProtocolServer server(apPlainStream);
server.SetLogToSysLog(mExtendedLogging);
server.SetTimeout(BACKUP_STORE_TIMEOUT);
try
{
server.DoServer(context);
}
catch(...)
{
LogConnectionStats(id, context.GetAccountName(), server);
throw;
}
LogConnectionStats(id, context.GetAccountName(), server);
context.CleanUp();
}
void BackupStoreDaemon::LogConnectionStats(uint32_t accountId,
const std::string& accountName, const BackupProtocolServer &server)
{
// Log the amount of data transferred
BOX_NOTICE("Connection statistics for " <<
BOX_FORMAT_ACCOUNT(accountId) << " "
"(name=" << accountName << "):"
" IN=" << server.GetBytesRead() <<
" OUT=" << server.GetBytesWritten() <<
" NET_IN=" << (server.GetBytesRead() - server.GetBytesWritten()) <<
" TOTAL=" << (server.GetBytesRead() + server.GetBytesWritten()));
}
|