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
|
/*
* Copyright (C) 2009 Red Hat Inc.
*
* This application 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; version 2.
*
* This application 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.
*/
/**
* @file rteval_parserd.c
* @author David Sommerseth <davids@redhat.com>
* @date Thu Oct 15 11:59:27 2009
*
* @brief Polls the rteval.submissionqueue table for notifications
* from new inserts and sends the file to a processing thread
*
*
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <assert.h>
#include <eurephia_nullsafe.h>
#include <eurephia_values.h>
#include <configparser.h>
#include <pgsql.h>
#include <threadinfo.h>
#include <parsethread.h>
#define DEFAULT_MSG_MAX 5 /**< Default size of the message queue */
#define XMLPARSER_XSL "xmlparser.xsl" /**< rteval report parser XSLT, parses XML into database friendly data*/
static int shutdown = 0; /**< Variable indicating if the program should shutdown */
static LogContext *logctx = NULL; /**< Initialsed log context, to be used by sigcatch() */
/**
* Simple signal catcher. Used for SIGINT and SIGTERM signals, and will set the global shutdown
* shutdown flag. It's expected that all threads behaves properly and exits as soon as their current
* work is completed
*
* @param sig Recieved signal (not used)
*/
void sigcatch(int sig) {
if( shutdown == 0 ) {
shutdown = 1;
writelog(logctx, LOG_INFO, "[SIGNAL] Starting shutting down");
} else {
writelog(logctx, LOG_INFO, "[SIGNAL] Shutdown in progress ... please be patient ...");
}
// re-enable signals, to avoid brute force exits.
// If brute force is needed, SIGKILL is available.
signal(SIGINT, sigcatch);
signal(SIGTERM, sigcatch);
}
/**
* Opens and reads /proc/sys/fs/mqueue/msg_max, to get the maximum number of allowed messages
* on POSIX MQ queues. rteval_parserd will use as much of this as possible when needed.
*
* @return Returns the system msg_max value, or DEFAULT_MSG_MAX on failure to read the setting.
*/
unsigned int get_mqueue_msg_max(LogContext *log) {
FILE *fp = NULL;
char buf[130];
unsigned int msg_max = DEFAULT_MSG_MAX;
fp = fopen("/proc/sys/fs/mqueue/msg_max", "r");
if( !fp ) {
writelog(log, LOG_WARNING,
"Could not open /proc/sys/fs/mqueue/msg_max, defaulting to %i",
msg_max);
writelog(log, LOG_INFO, "%s", strerror(errno));
return msg_max;
}
memset(&buf, 0, 130);
if( fread(&buf, 1, 128, fp) < 1 ) {
writelog(log, LOG_WARNING,
"Could not read /proc/sys/fs/mqueue/msg_max, defaulting to %i",
msg_max);
writelog(log, LOG_INFO, "%s", strerror(errno));
} else {
msg_max = atoi_nullsafe(buf);
if( msg_max < 1 ) {
msg_max = DEFAULT_MSG_MAX;
writelog(log, LOG_WARNING,
"Failed to parse /proc/sys/fs/mqueue/msg_max,"
"defaulting to %i", msg_max);
}
}
fclose(fp);
return msg_max;
}
/**
* Main loop, which polls the submissionqueue table and puts jobs found here into a POSIX MQ queue
* which the worker threads will pick up.
*
* @param dbc Database connection, where to query the submission queue
* @param msgq file descriptor for the message queue
*
* @return Returns 0 on successful run, otherwise > 0 on errors.
*/
int process_submission_queue(dbconn *dbc, mqd_t msgq) {
pthread_mutex_t mtx_submq = PTHREAD_MUTEX_INITIALIZER;
parseJob_t *job = NULL;
int rc = 0;
while( shutdown == 0 ) {
// Fetch an available job
job = db_get_submissionqueue_job(dbc, &mtx_submq);
if( !job ) {
writelog(dbc->log, LOG_EMERG,
"Failed to get submission queue job - shutting down");
shutdown = 1;
rc = 1;
goto exit;
}
if( job->status == jbNONE ) {
free_nullsafe(job);
if( db_wait_notification(dbc, &shutdown, "rteval_submq") < 1 ) {
writelog(dbc->log, LOG_EMERG,
"Failed to wait for DB notification - shutting down");
shutdown = 1;
rc = 1;
goto exit;
}
continue;
}
// Send the job to the queue
writelog(dbc->log, LOG_INFO, "** New job: submid %i, %s", job->submid, job->filename);
do {
int res;
errno = 0;
res = mq_send(msgq, (char *) job, sizeof(parseJob_t), 1);
if( (res < 0) && (errno != EAGAIN) ) {
writelog(dbc->log, LOG_EMERG,
"Could not send parse job to the queue "
"- shutting down");
shutdown = 1;
rc = 2;
goto exit;
} else if( errno == EAGAIN ) {
writelog(dbc->log, LOG_WARNING,
"Message queue filled up. "
"Will not add new messages to queue for the next 60 seconds");
sleep(60);
}
} while( (errno == EAGAIN) );
free_nullsafe(job);
}
exit:
return rc;
}
/**
* rtevald_parser main function.
*
* @param argc
* @param argv
*
* @return Returns the result of the process_submission_queue() function.
*/
int main(int argc, char **argv) {
eurephiaVALUES *config = NULL;
char xsltfile[2050], *reportdir = NULL;
xsltStylesheet *xslt = NULL;
dbconn *dbc = NULL;
pthread_t **threads = NULL;
pthread_attr_t **thread_attrs = NULL;
pthread_mutex_t mtx_sysreg = PTHREAD_MUTEX_INITIALIZER;
threadData_t **thrdata = NULL;
struct mq_attr msgq_attr;
mqd_t msgq;
int i,rc, max_threads = 5;
// Initialise XML and XSLT libraries
xsltInit();
xmlInitParser();
// Setup a log context
logctx = init_log("stderr:", LOG_DEBUG);
if( !logctx ) {
fprintf(stderr, "** ERROR ** Could not setup a log context\n");
rc = 2;
goto exit;
}
// Fetch configuration
config = read_config(logctx, "/etc/rteval.conf", "xmlrpc_parser");
// Parse XSLT template
snprintf(xsltfile, 512, "%s/%s", eGet_value(config, "xsltpath"), XMLPARSER_XSL);
writelog(logctx, LOG_DEBUG, "Parsing XSLT file: %s", xsltfile);
xslt = xsltParseStylesheetFile((xmlChar *) xsltfile);
if( !xslt ) {
writelog(logctx, LOG_EMERG, "Could not parse XSLT template: %s", xsltfile);
rc = 2;
goto exit;
}
// Open a POSIX MQ
writelog(logctx, LOG_DEBUG, "Preparing POSIX MQ queue: /rteval_parsequeue");
memset(&msgq, 0, sizeof(mqd_t));
msgq_attr.mq_maxmsg = get_mqueue_msg_max(logctx);
msgq_attr.mq_msgsize = sizeof(parseJob_t);
msgq_attr.mq_flags = O_NONBLOCK;
msgq = mq_open("/rteval_parsequeue", O_RDWR | O_CREAT | O_NONBLOCK, 0600, &msgq_attr);
if( msgq < 0 ) {
writelog(logctx, LOG_EMERG,
"Could not open message queue: %s", strerror(errno));
rc = 2;
goto exit;
}
// Get a database connection for the main thread
dbc = db_connect(config, max_threads, logctx);
if( !dbc ) {
rc = 2;
goto exit;
}
// Prepare all threads
threads = calloc(max_threads + 1, sizeof(pthread_t *));
thread_attrs = calloc(max_threads + 1, sizeof(pthread_attr_t *));
thrdata = calloc(max_threads + 1, sizeof(threadData_t *));
assert( (threads != NULL) && (thread_attrs != NULL) && (thrdata != NULL) );
reportdir = eGet_value(config, "reportdir");
writelog(logctx, LOG_INFO, "Starting %i worker threads", max_threads);
for( i = 0; i < max_threads; i++ ) {
// Prepare thread specific data
thrdata[i] = malloc_nullsafe(logctx, sizeof(threadData_t));
if( !thrdata[i] ) {
writelog(logctx, LOG_EMERG,
"Could not allocate memory for thread data");
rc = 2;
goto exit;
}
// Get a database connection for the thread
thrdata[i]->dbc = db_connect(config, i, logctx);
if( !thrdata[i]->dbc ) {
writelog(logctx, LOG_EMERG,
"Could not connect to the database for thread %i", i);
rc = 2;
shutdown = 1;
goto exit;
}
thrdata[i]->shutdown = &shutdown;
thrdata[i]->id = i;
thrdata[i]->msgq = msgq;
thrdata[i]->mtx_sysreg = &mtx_sysreg;
thrdata[i]->xslt = xslt;
thrdata[i]->destdir = reportdir;
thread_attrs[i] = malloc_nullsafe(logctx, sizeof(pthread_attr_t));
if( !thread_attrs[i] ) {
writelog(logctx, LOG_EMERG,
"Could not allocate memory for thread attributes");
rc = 2;
goto exit;
}
pthread_attr_init(thread_attrs[i]);
pthread_attr_setdetachstate(thread_attrs[i], PTHREAD_CREATE_JOINABLE);
threads[i] = malloc_nullsafe(logctx, sizeof(pthread_t));
if( !threads[i] ) {
writelog(logctx, LOG_EMERG,
"Could not allocate memory for pthread_t");
rc = 2;
goto exit;
}
}
// Setup signal catchers
signal(SIGINT, sigcatch);
signal(SIGTERM, sigcatch);
// Start the threads
for( i = 0; i < max_threads; i++ ) {
int thr_rc = pthread_create(threads[i], thread_attrs[i], parsethread, thrdata[i]);
if( thr_rc < 0 ) {
writelog(logctx, LOG_EMERG,
"** ERROR ** Failed to start thread %i: %s",
i, strerror(thr_rc));
rc = 3;
goto exit;
}
}
// Main routine
//
// checks the submission queue and puts unprocessed records on the POSIX MQ
// to be parsed by one of the threads
//
writelog(logctx, LOG_DEBUG, "Starting submission queue checker");
rc = process_submission_queue(dbc, msgq);
writelog(logctx, LOG_DEBUG, "Submission queue checker shut down");
exit:
// Clean up all threads
for( i = 0; i < max_threads; i++ ) {
// Wait for all threads to exit
if( threads && threads[i] ) {
void *thread_rc;
int j_rc;
if( (j_rc = pthread_join(*threads[i], &thread_rc)) != 0 ) {
writelog(logctx, LOG_CRIT,
"Failed to join thread %i: %s",
i, strerror(j_rc));
}
pthread_attr_destroy(thread_attrs[i]);
free_nullsafe(threads[i]);
free_nullsafe(thread_attrs[i]);
}
// Disconnect threads database connection
if( thrdata && thrdata[i] ) {
db_disconnect(thrdata[i]->dbc);
free_nullsafe(thrdata[i]);
}
}
free_nullsafe(thrdata);
free_nullsafe(threads);
free_nullsafe(thread_attrs);
// Close message queue
errno = 0;
if( mq_close(msgq) < 0 ) {
writelog(logctx, LOG_CRIT, "Failed to close message queue: %s",
strerror(errno));
}
errno = 0;
if( mq_unlink("/rteval_parsequeue") < 0 ) {
writelog(logctx, LOG_ALERT, "Failed to remove the message queue: %s",
strerror(errno));
}
// Disconnect from database, main thread connection
db_disconnect(dbc);
// Free up the rest
eFree_values(config);
xsltFreeStylesheet(xslt);
xmlCleanupParser();
xsltCleanupGlobals();
return rc;
}
|