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
|
/*
*
* Copyright (c) 2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "Daemon.h"
#include "qpid/log/Statement.h"
#include "qpid/Exception.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
namespace qpid {
namespace broker {
using namespace std;
namespace {
/** Throw an exception containing msg and strerror if throwIf is true.
* Name is supposed to be reminiscent of perror().
*/
void throwIf(bool condition, const string& msg, int errNo=errno) {
if (condition)
throw Exception(msg + (errNo? ": "+strError(errNo) : string(".")));
}
/*
* Rewritten using low-level IO, for compatibility
* with earlier Boost versions, i.e. 103200.
*/
struct LockFile {
LockFile(const std::string& path_, bool create)
: path(path_), fd(-1), created(create)
{
errno = 0;
int flags=create ? O_WRONLY|O_CREAT|O_NOFOLLOW : O_RDWR;
fd = ::open(path.c_str(), flags, 0644);
throwIf(fd < 0,"Cannot open "+path);
throwIf(::lockf(fd, F_TLOCK, 0) < 0, "Cannot lock "+path);
}
~LockFile() {
if (fd >= 0) {
::lockf(fd, F_ULOCK, 0);
::close(fd);
}
}
std::string path;
int fd;
bool created;
};
} // namespace
Daemon::Daemon(std::string _pidDir) : pidDir(_pidDir) {
pid = -1;
pipeFds[0] = pipeFds[1] = -1;
}
string Daemon::pidFile(string pidDir, uint16_t port) {
ostringstream path;
path << pidDir << "/qpidd." << port << ".pid";
return path.str();
}
/*
* Rewritten using low-level IO, for compatibility
* with earlier Boost versions, i.e. 103200.
*/
void Daemon::fork()
{
throwIf(::pipe(pipeFds) < 0, "Can't create pipe");
throwIf((pid = ::fork()) < 0, "Daemon fork failed");
if (pid == 0) { // Child
try {
QPID_LOG(debug, "Forked daemon child process");
// File descriptors
throwIf(::close(pipeFds[0])<0, "Cannot close read pipe");
throwIf(::close(0)<0, "Cannot close stdin");
throwIf(::close(1)<0, "Cannot close stdout");
throwIf(::close(2)<0, "Cannot close stderr");
int fd=::open("/dev/null",O_RDWR); // stdin
throwIf(fd != 0, "Cannot re-open stdin");
throwIf(::dup(fd)<0, "Cannot re-open stdout");
throwIf(::dup(fd)<0, "Cannot re-open stderror");
// Misc
throwIf(setsid()<0, "Cannot set session ID");
throwIf(chdir(pidDir.c_str()) < 0, "Cannot change directory to "+pidDir);
umask(027);
// Child behavior
child();
}
catch (const exception& e) {
QPID_LOG(critical, "Daemon startup failed: " << e.what());
uint16_t port = 0;
write(pipeFds[1], &port, sizeof(uint16_t));
std::string pipeFailureMessage = e.what();
write ( pipeFds[1],
pipeFailureMessage.c_str(),
strlen(pipeFailureMessage.c_str())
);
}
}
else { // Parent
close(pipeFds[1]); // Write side.
parent();
}
}
Daemon::~Daemon() {
if (!lockFile.empty())
unlink(lockFile.c_str());
}
uint16_t Daemon::wait(int timeout) { // parent waits for child.
errno = 0;
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
/*
* Rewritten using low-level IO, for compatibility
* with earlier Boost versions, i.e. 103200.
*/
fd_set fds;
FD_ZERO(&fds);
FD_SET(pipeFds[0], &fds);
int n=select(FD_SETSIZE, &fds, 0, 0, &tv);
throwIf(n==0, "Timed out waiting for daemon");
throwIf(n<0, "Error waiting for daemon");
uint16_t port = 0;
/*
* Read the child's port number from the pipe.
*/
int desired_read = sizeof(uint16_t);
if ( desired_read > ::read(pipeFds[0], & port, desired_read) ) {
throw Exception("Cannot write lock file "+lockFile);
}
/*
* If the port number is 0, the child has put an error message
* on the pipe. Get it and throw it.
*/
if ( 0 == port ) {
// Skip whitespace
char c = ' ';
while ( isspace(c) ) {
if ( 1 > ::read(pipeFds[0], &c, 1) )
throw Exception("Child port == 0, and no error message on pipe.");
}
// Get Message
string errmsg;
do {
errmsg += c;
} while (::read(pipeFds[0], &c, 1));
throw Exception("Daemon startup failed"+
(errmsg.empty() ? string(".") : ": " + errmsg));
}
return port;
}
/*
* When the child is ready, it writes its pid to the
* lockfile and its port number on the pipe back to
* its parent process. This indicates that the
* child has successfully daemonized. When the parent
* hears the good news, it ill exit.
*/
void Daemon::ready(uint16_t port) { // child
lockFile = pidFile(pidDir, port);
LockFile lf(lockFile, true);
/*
* Rewritten using low-level IO, for compatibility
* with earlier Boost versions, i.e. 103200.
*/
/*
* Write the PID to the lockfile.
*/
pid_t pid = getpid();
int desired_write = sizeof(pid_t);
if ( desired_write > ::write(lf.fd, & pid, desired_write) ) {
throw Exception("Cannot write lock file "+lockFile);
}
/*
* Write the port number to the parent.
*/
desired_write = sizeof(uint16_t);
if ( desired_write > ::write(pipeFds[1], & port, desired_write) ) {
throw Exception("Error writing to parent." );
}
QPID_LOG(debug, "Daemon ready on port: " << port);
}
/*
* The parent process reads the child's pid
* from the lockfile.
*/
pid_t Daemon::getPid(string _pidDir, uint16_t port) {
string name = pidFile(_pidDir, port);
LockFile lf(name, false);
pid_t pid;
/*
* Rewritten using low-level IO, for compatibility
* with earlier Boost versions, i.e. 103200.
*/
int desired_read = sizeof(pid_t);
if ( desired_read > ::read(lf.fd, & pid, desired_read) ) {
throw Exception("Cannot read lock file " + name);
}
if (kill(pid, 0) < 0 && errno != EPERM) {
unlink(name.c_str());
throw Exception("Removing stale lock file "+name);
}
return pid;
}
}} // namespace qpid::broker
|