-
Notifications
You must be signed in to change notification settings - Fork 29
/
SelfDetect.cpp
144 lines (125 loc) · 4.33 KB
/
SelfDetect.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
/**@file Module for preventing two instances of a program from running. */
/*
* Copyright 2013, 2014 Range Networks, Inc.
*
* This software is distributed under the terms of the GNU Affero Public License.
* See the COPYING file in the main directory for details.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include "UnixSignal.h"
#include "SelfDetect.h"
#include "Logger.h"
#include "Exit.h"
#include "Configuration.h"
//SelfDetect gSelf;
extern ConfigurationTable gConfig;
static void e(void)
{
gSelf.Exit(-999);
}
static void sigfcn(int sig)
{
gSelf.Exit(sig);
}
void SelfDetect::RegisterProgram(const char *argv0)
{
const char *p = strrchr((char*)argv0,'/');
if (p == NULL) { p = argv0; }
char buf[100];
snprintf(buf, sizeof(buf)-1, "/var/run/%s.pid", p);
LOG(NOTICE) << "*** Registering program " << argv0 << " to " << buf;
// first, verify we aren't already running.
struct stat stbuf;
if (stat(buf, &stbuf) >= 0)
{
LOG(CRIT) << "*** An instance of " << p << " is already running. ";
LOG(CRIT) << "*** If this is not the case, deleting this file will allow " << p << " to start: " << buf << " exiting...";
Exit::exit(Exit::DETECTFILE);
}
FILE *fp = fopen(buf, "w");
if (fp == NULL)
{
LOG(CRIT) << "*** Unable to create " << buf << ": " << strerror(errno) << " exiting...";
Exit::exit(Exit::CREATEFILE);
}
fprintf(fp, "%d\n", getpid());
fclose(fp);
atexit(e);
gSigVec.CoreName(gConfig.getStr("Core.File"), gConfig.getBool("Core.Pid"));
gSigVec.TarName(gConfig.getStr("Core.TarFile"), gConfig.getBool("Core.SaveFiles"));
// Now, register for all signals to do the cleanup
for (int i = 1; i < UnixSignal::C_NSIG; i++)
{
switch(i)
{
// Add any signals that need to bypass the signal handling behavior
// here. Currently, SIGCHLD is needed because a signal is generated
// when stuff related to the transciever (which is a child process)
// occurs. In that case, the openbts log output was:
// openbts: ALERT 3073816320 05:03:50.4 OpenBTS.cpp:491:main: starting the transceiver
// openbts: NOTICE 3073816320 05:03:50.4 SelfDetect.cpp:91:Exit: *** Terminating because of signal 17
// openbts: NOTICE 3031243584 05:03:50.4 OpenBTS.cpp:165:startTransceiver: starting transceiver ./transceiver w/ 1 ARFCNs and Args:
// openbts: NOTICE 3073816320 05:03:50.4 SelfDetect.cpp:98:Exit: *** Terminating ./OpenBTS
// openbts: NOTICE 3073816320 05:03:50.4 SelfDetect.cpp:105:Exit: *** Removing pid file /var/run/OpenBTS.pid
case SIGCONT:
case SIGCHLD:
break;
default:
gSigVec.Register(sigfcn, i);
break;
}
}
mProg = strdup(argv0);
mFile = strdup(buf);
}
void SelfDetect::Exit(int sig)
{
LOG(NOTICE) << "*** Terminating because of signal " << sig;
if (mProg == NULL)
{
LOG(NOTICE) << "*** Terminating without registration of program";
} else
{
LOG(NOTICE) << "*** Terminating " << mProg;
}
if (mFile == NULL)
{
LOG(NOTICE) << "*** Terminating without pid file";
} else
{
LOG(NOTICE) << "*** Removing pid file " << mFile;
unlink(mFile);
}
for (std::list<std::string>::iterator i = mListFiles.begin();
i != mListFiles.end(); ++i)
{
LOG(NOTICE) << "*** Removing " << i->c_str();
unlink(i->c_str());
}
}
void SelfDetect::RegisterFile(const char *file)
{
LOG(NOTICE) << "*** Registering " << file << " for removal at program exit";
std::string s(file);
mListFiles.push_back(s);
}