-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.cpp
More file actions
358 lines (310 loc) · 10.2 KB
/
main.cpp
File metadata and controls
358 lines (310 loc) · 10.2 KB
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
/*
* Copyright (C) 2014-2018 Softus Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 2.
*
* 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 Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "product.h"
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QUtf8Settings>
#ifdef UNICODE
#define DCMTK_UNICODE_BUG_WORKAROUND
#undef UNICODE
#endif
#include "printscp.h"
#include "storescp.h"
#include <dcmtk/oflog/logger.h>
#include <dcmtk/dcmdata/dcdeftag.h>
#include <dcmtk/dcmdata/dcfilefo.h>
#include <dcmtk/dcmpstat/dvpsdef.h>
// DCMTK prior to 3.6.1 has no its own namespace.
namespace dcmtk{}
using namespace dcmtk;
#ifdef DCMTK_UNICODE_BUG_WORKAROUND
#define UNICODE
#undef DCMTK_UNICODE_BUG_WORKAROUND
#endif
#define DEFAULT_SPOOL_INTERVAL 600
static int resendWorkerPid = 0;
static void cleanChildren()
{
qDebug() << __func__;
#ifdef HAVE_WAITPID
int status;
#elif HAVE_WAIT3
struct rusage rusage;
#if defined(__NeXT__)
// some systems need a union wait as argument to wait3
union wait status;
#else
int status;
#endif
#endif
#if defined(HAVE_WAITPID) || defined(HAVE_WAIT3)
int child = 1;
int options = WNOHANG;
while (child > 0)
{
#ifdef HAVE_WAITPID
child = (int)(waitpid(-1, &status, options));
#elif defined(HAVE_WAIT3)
child = wait3(&status, options, &rusage);
#endif
if (child < 0)
{
if (errno != ECHILD)
{
qDebug() << "wait for child failed: " << QString::fromLocal8Bit(strerror(errno));
}
}
else if (child > 0)
{
qDebug() << "Child process" << child << "terminated with status" << status;
if (resendWorkerPid == child)
{
resendWorkerPid = 0;
}
}
}
#endif
}
static bool resendFailedPrints(QSettings& settings)
{
// Retry failed prints
//
auto spoolPath = settings.value("spool-path").toString();
if (spoolPath.isEmpty())
{
// Retry spool is disabled
return false;
}
if (QDateTime::currentDateTime() < settings.value("next-spool-ts").toDateTime())
{
// Not yet. May be next time
qDebug() << __func__ << "delayed";
return false;
}
auto spoolInterval = settings.value("spool-interval-in-seconds", DEFAULT_SPOOL_INTERVAL).toInt();
settings.setValue("next-spool-ts", QDateTime::currentDateTime().addSecs(spoolInterval));
// Save it immediatelly to avoid corruption.
//
settings.sync();
#ifdef HAVE_FORK
if (resendWorkerPid > 0)
{
qDebug() << "Worker process" << resendWorkerPid << "is still alive, resend delayed";
return false;
}
resendWorkerPid = fork();
if (resendWorkerPid > 0)
{
qDebug() << "Worker process to resend failed prints spawned. Pid" << resendWorkerPid;
return false;
}
#endif
// Really start to process failed prints
//
OFCondition cond;
// Retry failed web queries
//
qDebug() << __func__ << "retrying prints";
Q_FOREACH (auto file, QDir(spoolPath).entryInfoList(QDir::Files))
{
auto filePath = file.absoluteFilePath();
qDebug() << "Retrying " << filePath;
DcmFileFormat dcmFF;
cond = dcmFF.loadFile((const char*)filePath.toLocal8Bit());
if (cond.bad())
{
qDebug() << "Failed to load " << filePath << ": " << QString::fromLocal8Bit(cond.text());
continue;
}
const char* printer = nullptr;
dcmFF.getDataset()->findAndGetString(DCM_RETIRED_PrintQueueID, printer);
if (printer == nullptr)
{
qDebug() << "Failed to retry " << filePath << ": no printer instance specified";
continue;
}
PrintSCP retryPrintSCP(nullptr, nullptr, QString::fromUtf8(printer));
if (retryPrintSCP.webQuery(dcmFF.getDataset()))
{
foreach (auto server, settings.value("storage-servers").toStringList())
{
StoreSCP sscp(server);
const char* SOPInstanceUID = nullptr;
dcmFF.getDataset()->findAndGetString(DCM_SOPInstanceUID, SOPInstanceUID);
cond = sscp.sendToServer(dcmFF.getDataset(), SOPInstanceUID);
if (cond.bad())
{
// The Web query secceded, but store failed.
// Move the file down to the queue.
// At this point, we will copy the dataset as many times,
// as need for each failed store server.
//
saveToDisk(QString(spoolPath).append(QDir::separator()).append(server), dcmFF.getDataset());
}
}
if (!QFile::remove(filePath))
{
qDebug() << "Failed to remove file " << filePath
<< ": " << QString::fromLocal8Bit(strerror(errno));
}
}
}
qDebug() << __func__ << "retrying dcmstore";
foreach (auto server, settings.value("storage-servers").toStringList())
{
StoreSCP sscp(server);
auto path = QDir(QString(spoolPath).append(QDir::separator()).append(server));
Q_FOREACH (auto file, path.entryInfoList(QDir::Files))
{
auto filePath = file.absoluteFilePath();
qDebug() << "Resending " << filePath;
DcmFileFormat dcmFF;
cond = dcmFF.loadFile((const char*)filePath.toLocal8Bit());
if (cond.bad())
{
qDebug() << "Failed to load " << filePath
<< ": " << QString::fromLocal8Bit(cond.text());
continue;
}
const char* SOPInstanceUID = nullptr;
dcmFF.getDataset()->findAndGetString(DCM_SOPInstanceUID, SOPInstanceUID);
cond = sscp.sendToServer(dcmFF.getDataset(), SOPInstanceUID);
if (cond.good())
{
if (!QFile::remove(filePath))
{
qDebug() << "Failed to remove file " << filePath
<< ": " << QString::fromLocal8Bit(strerror(errno));
}
}
}
}
qDebug() << __func__ << "done";
return true;
}
void handleClient(T_ASC_Association *assoc)
{
PrintSCP printSCP(assoc);
if (printSCP.negotiateAssociation())
{
printSCP.handleClient();
}
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
app.setApplicationName(PRODUCT_SHORT_NAME);
app.setOrganizationName(ORGANIZATION_DOMAIN);
QUtf8Settings settings;
auto logLevel = settings.value("log-level").toString();
if (!logLevel.isEmpty())
{
auto level = log4cplus::getLogLevelManager().fromString(logLevel.toUtf8().constData());
log4cplus::Logger::getRoot().setLogLevel(level);
}
auto debugUpstream = settings.value("debug-upstream").toBool();
if (debugUpstream)
{
log4cplus::Logger log = log4cplus::Logger::getInstance("dcmtk.dcmpstat.dump");
log.setLogLevel(OFLogger::DEBUG_LOG_LEVEL);
}
auto port = settings.value("port", DEFAULT_LISTEN_PORT).toInt();
auto tout = settings.value("timeout", DEFAULT_TIMEOUT).toInt();
T_ASC_Network *net;
OFCondition cond = ASC_initializeNetwork(NET_ACCEPTOR, port, tout, &net);
// When the listener process has been recently crashed,
// the port will be busy for some time (see CLOSE_WAIT).
//
for (int i = 0; cond.bad() && i < 20; ++i)
{
usleep(200000);
cond = ASC_initializeNetwork(NET_ACCEPTOR, port, tout, &net);
}
if (cond.bad())
{
qWarning() << "cannot initialize network" << QString::fromLocal8Bit(cond.text());
return 1;
}
#if defined(HAVE_SETUID) && defined(HAVE_GETUID)
// Return to normal uid so that we can't do too much damage in case
// things go very wrong. Only relevant if the program is setuid root,
// and run by another user. Running as root user may be
// potentially disasterous if this program screws up badly.
//
auto err = setuid(getuid());
if (err)
{
qWarning() << "setuid failed" << errno << "this may be dangerous";
}
#endif
#ifdef HAVE_FORK
int listen_timeout=10;
#else
int listen_timeout=10000;
#endif
qCritical() << "Virtual DICOM printer version" << PRODUCT_VERSION_STR
<< "started. Master process pid" << getpid();
Q_FOREVER
{
do
{
cleanChildren();
if (resendFailedPrints(settings))
{
// Resend worker routine has been completed
return 0;
}
qDebug() << "waiting for connection";
}
while (!ASC_associationWaiting(net, listen_timeout));
qDebug() << "Client connected";
T_ASC_Association *assoc = nullptr;
cond = ASC_receiveAssociation(net, &assoc, DEFAULT_MAXPDU);
if (cond.bad())
{
qWarning() << "Failed to receive association";
ASC_dropSCPAssociation(assoc);
ASC_destroyAssociation(&assoc);
continue;
}
#if defined(HAVE_FORK) && !defined(QT_DEBUG)
auto pid = fork();
if (pid < 0)
{
qDebug() << "fork() failed, err" << errno
<< "\nWill handle client connection in the main process";
handleClient(assoc);
}
else if (pid == 0)
{
// Do the real work.
//
handleClient(assoc);
qDebug() << "Child process completed. pid" << getpid();
return 0;
}
else
{
qDebug() << "Child process" << pid << "spawned";
}
#else
qDebug() << "Will handle client connection in the main process";
handleClient(assoc);
#endif
}
return 0;
}