-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_gridfactory.c
1772 lines (1565 loc) · 61.7 KB
/
mod_gridfactory.c
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mod_gridfactory
*
* Apache module providing a web interface to the gridfactory job database.
*
* Copyright (c) 2008 Frederik Orellana, Niels Bohr Institute,
* University of Copenhagen. All rights reserved.
*
* This program 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; 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* This program is based loosely on mod_authn_dbd of the Apache foundation,
* http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/aaa/mod_authn_dbd.c?revision=658046&view=markup.
* mod_authn_dbd is covered by the pache License, Version 2.0,
* http://www.apache.org/licenses/LICENSE-2.0
*
*******************************************************************************
* This module allows you to create a GridFactory web service for job pulling.
* It requires mod_dbm to be loaded and configured.
*
* Load the module with:
*
* LoadModule gridfactory_module /usr/lib/apache2/modules/mod_gridfactory.so
* <Location /db>
* SetHandler gridfactory
* </Location>
*
* The directory "db" should be a symlink to /var/spool/gridfactory.
*
* three configuration directives are available:
*
* PrepareStatements "On|Off"
* Whether or not MySQL prepared statements should be used. I don't
* really see any reason to set this to Off.
*
* DBBaseURL "URL"
* The URL served with this module. If this is not specified,
* "https://my.host.hame/db/jobs/" is used.
*
* XSLDirURL "URL"
* Where to find job.xsl, jobs.xsl, history.xsl, nodes.xsl and node.xsl.
* These are used for formatting the output when ?mode=xsl is used.
*
*
*/
#include "ap_provider.h"
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_log.h"
#include "http_request.h"
#include "apr_lib.h"
#include "apr_dbd.h"
#include "mod_dbd.h"
#include "apr_strings.h"
#include "mod_auth.h"
#include "apr_md5.h"
#include "apu_version.h"
#include <mysql/mysql.h>
#define JOB_TABLE_NUM 1
#define HIST_TABLE_NUM 2
#define NODE_TABLE_NUM 3
#define MY_POOL_MAX_FREE_SIZE 128
/* Apache environment variable. This is used to get the DN used for authorizing
* node updates. */
static const char* CLIENT_S_DN_STRING = "SSL_CLIENT_S_DN";
/* The sub-directory containing the job information. */
static const char* JOB_DIR = "/jobs/";
/* The sub-directory containing the job history. */
static const char* HIST_DIR = "/history/";
/* The sub-directory containing the node information. */
static const char* NODE_DIR = "/nodes/";
/* Keys in the has table of prepared statements. */
static const char* LABEL = "gridfactory_dbd_0";
static const char* LABEL1 = "gridfactory_dbd_1";
static const char* LABEL2 = "gridfactory_dbd_2";
static const char* LABEL3 = "gridfactory_dbd_3";
/* Name of the identifier column. */
static const char* ID_COL = "identifier";
/* Column holding the identifier. */
static int id_col_nr;
/* Name of the status column. */
static const char* STATUS_COL = "csStatus";
/* Name of the name column. */
static const char* NAME_COL = "name";
/* Name of the host column. */
static const char* HOST_COL = "host";
/* Name of the subnodes DB URL column. */
static const char* SUBNODES_DB_URL_COL = "subnodesDbUrl";
/* ready value of the status column. */
static const char* READY = "ready";
/* Name of the lastModified column. */
static const char* LASTMODIFIED_COL = "lastModified";
/* Name of the providerInfo column. */
static const char* PROVIDERINFO_COL = "providerInfo";
/* Name of the providerInfo column. */
static const char* NODEID_COL = "nodeId";
/* Name of the allowedVOs column. */
static const char* ALLOWED_VOS_COL = "allowedVOs";
/* Name of the hypervisors column. */
static const char* HYPERVISORS_COL = "hypervisors";
/* Name of the inputFileURLs column. */
static const char* INPUT_FILE_URLS_COL = "inputFileURLs";
/* Name of the runtimeEnvironments column. */
static const char* RUNTIME_ENVIRONMENTS_COL = "runtimeEnvironments";
/* Name of the outFileMapping column. */
static const char* OUT_FILE_MAPPING_COL = "outFileMapping";
/* Column holding the name. */
static int name_col_nr;
/* Column holding the status. */
static int status_col_nr;
/* Column holding the host. */
static int host_col_nr;
/* Column holding the subnodes DB URL. */
static int subnodes_db_url_col_nr;
/* Name of the DB URL pseudo-column. */
static const char* DBURL_COL = "dbUrl";
/* SQL query to get list of fields. */
static const char* JOB_REC_SHOW_F_Q = "SHOW fields FROM `jobDefinition`";
/* SQL query to get list of fields. */
static const char* HIST_REC_SHOW_F_Q = "SHOW fields FROM `jobHistory`";
/* SQL query to get list of fields. */
static const char* NODE_REC_SHOW_F_Q = "SHOW fields FROM `nodeInformation`";
/* SQL query to get all job definition records. */
static const char* JOB_RECS_SELECT_Q = "SELECT * FROM `jobDefinition`";
/* SQL query to get all job history records. */
static const char* HIST_RECS_SELECT_Q = "SELECT * FROM `jobHistory`";
/* SQL query to get all node information records. */
static const char* NODE_RECS_SELECT_Q = "SELECT * FROM `nodeInformation`";
/* Prepared statement string to get job record. */
static const char* JOB_REC_SELECT_PS = "SELECT * FROM jobDefinition WHERE identifier LIKE ?";
/* Prepared statement string to get history record. */
static const char* HIST_REC_SELECT_PS = "SELECT * FROM jobHistory WHERE identifier LIKE ?";
/* Prepared statement string to get node record. */
static const char* NODE_REC_SELECT_PS = "SELECT * FROM nodeInformation WHERE identifier LIKE ?";
/* Query to get job record. */
static const char* JOB_REC_SELECT_Q = "SELECT * FROM `jobDefinition` WHERE identifier LIKE '%/%s'";
/* Query to get history record. */
static const char* HIST_REC_SELECT_Q = "SELECT * FROM `jobHistory` WHERE identifier LIKE '%/%s'";
/* Query to get node record. */
static const char* NODE_REC_SELECT_Q = "SELECT * FROM `nodeInformation` WHERE identifier = '%s'";
/* Prepared statement string to update job record. */
static const char* JOB_REC_UPDATE_PS_1 = "UPDATE `jobDefinition` SET lastModified = NOW() WHERE identifier LIKE ?";
/* Query to update job record. */
static const char* JOB_REC_UPDATE_Q = "UPDATE `jobDefinition` SET lastModified = NOW()";
/* Query to update node record. */
static const char* NODE_REC_UPDATE_Q = "UPDATE `nodeInformation` SET lastModified = NOW()";
/* Query to create node record. */
static const char* NODE_REC_INSERT_Q = "INSERT INTO nodeInformation SET created = NOW(), lastModified = NOW()";
/* Optional function - look it up once in post_config. */
static ap_dbd_t* (*dbd_acquire_fn)(request_rec*) = NULL;
static void (*dbd_prepare_fn)(server_rec*, const char*, const char*) = NULL;
/* Max size of each field name. */
static int MAX_F_SIZE = 256;
/* Max size of all field names. */
//static int MAX_T_F_SIZE = 5120;
/* Max size (bytes) of response and PUT bodies.
* This is to protect against memory leaking.
* Notice that it should be MAX_SELECT_ROWS * [longest expected row]*/
static int MAX_SIZE = 10000000;
/* Max number of rows that we will return from a DB query.
* This is to protect against memory leaking of the parsing functions. */
static int MAX_SELECT_ROWS = 10000;
/* Whether or not to operate in private mode (1 = private). */
static int PRIVATE = 1;
/* String to use in GET request to require format. */
static char* FORMAT_STR = "format";
/* String to use in GET request to require starting at a given record. */
static char* START_STR = "start";
/* String to use in GET request to require ending at a givenrecord. */
static char* END_STR = "end";
/* Text format directive. */
static char* TEXT_FORMAT_STR = "text";
/* XML format directive. */
static char* XML_FORMAT_STR = "xml";
/* Public fields of the jobDefinition table. */
static char* JOB_PUB_FIELDS_STR = "identifier\tname\tcsStatus\tuserInfo\tcreated\tlastModified\trunningSeconds\tramMb\topSys\truntimeEnvironments\tallowedVOs\tvirtualize\tdbUrl";
/* Public fields of the nodeInformation table. */
static char* NODE_PUB_FIELDS_STR = "identifier\thost\tsubNodesDbUrl\tmaxJobs\tallowedVOs\tvirtualize\thypervisors\tmaxMBPerJob\tproviderInfo\tcreated\tlastModified\tdbUrl";
/* Value indicating output should be text formatted. */
static int TEXT_FORMAT = 0;
/* Value indicating output should be XML formatted. */
static int XML_FORMAT = 1;
/* Base URL for the DB web service. */
char* base_url;
/* URL to directory containing job.xsl, jobs.xsl, history.xsl, node.xsl and nodes.xsl. */
char* xsl_dir;
/* Forward declaration */
module AP_MODULE_DECLARE_DATA gridfactory_module;
/**
* Configuration
*/
typedef struct {
char* ps_;
char* url_;
char* xsl_;
} config_rec;
static void*
do_config(apr_pool_t* p, char* d)
{
/* Apparently ap_log_perror only works for log levels higher than APLOG_INFO,
i.e. not with APLOG_INFO and APLOG_DEBUG. */
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "Doing DB config with %s", p);
dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
if(dbd_acquire_fn == NULL){
dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
if(dbd_acquire_fn == NULL){
return "You must load mod_dbd to use mod_gridfactory";
}
}
dbd_prepare_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
config_rec* conf = (config_rec*)apr_pcalloc(p, sizeof(config_rec));
conf->ps_ = 0; /* null pointer */
conf->url_ = 0; /* null pointer */
conf->xsl_ = 0; /* null pointer */
return conf;
}
/**
* DB stuff
*/
static void
dbd_prepare(cmd_parms* cmd, void* cfg)
{
dbd_prepare_fn(cmd->server, JOB_REC_SELECT_PS, LABEL);
dbd_prepare_fn(cmd->server, JOB_REC_UPDATE_PS_1, LABEL1);
dbd_prepare_fn(cmd->server, HIST_REC_SELECT_PS, LABEL2);
dbd_prepare_fn(cmd->server, NODE_REC_SELECT_PS, LABEL3);
}
static const char*
config_ps(cmd_parms* cmd, void* mconfig, const char* arg)
{
if (((config_rec*)mconfig)->ps_)
return "PrepareStatements already set.";
((config_rec*)mconfig)->ps_ = (char*) arg;
if(apr_strnatcasecmp(((config_rec*)mconfig)->ps_, "On") == 0){
dbd_prepare(cmd, mconfig);
}
return 0;
}
static const char*
config_url(cmd_parms* cmd, void* mconfig, const char* arg)
{
if(((config_rec*)mconfig)->url_){
return "DBBaseURL already set.";
}
((config_rec*)mconfig)->url_ = (char*) arg;
return 0;
}
static const char*
config_xsl(cmd_parms* cmd, void* mconfig, const char* arg)
{
if(((config_rec*)mconfig)->xsl_){
return "XSLDirURL already set.";
}
((config_rec*)mconfig)->xsl_ = (char*) arg;
return 0;
}
static const command_rec command_table[] =
{
AP_INIT_TAKE1("PrepareStatements", config_ps,
NULL, OR_FILEINFO,
"Whether or not to use prepared statements."),
AP_INIT_TAKE1("DBBaseURL", config_url,
NULL, OR_FILEINFO,
"Base URL of the DB web service."),
AP_INIT_TAKE1("XSLDirURL", config_xsl,
NULL, OR_FILEINFO,
"Where to get XSL files for formatting XML output."),
{NULL}
};
unsigned long countchr(const char *str, const char *ch)
{
unsigned long count = 0;
for ( ; (*str); ++str ){
if(*str == *ch){
++count;
}
}
return count;
}
/* From apr_dbd_mysql.c */
/*struct apr_dbd_results_t {
int random;
MYSQL_RES *res;
MYSQL_STMT *statement;
MYSQL_BIND *bind;
#if APU_MAJOR_VERSION >= 2 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
apr_pool_t *pool;
#endif
};*/
typedef struct {
int format;
char* res;
/* Used only by update_rec, to check if a job is
* "ready" before allowing writing. If a job is "ready"
* only writing the csStatus, nodeId and providerInfo is allowed. */
char* status;
/* Used only by update_rec to check if a node record
* was created by the user trying to modify it. */
char* providerInfo;
} db_result;
int tokenize_fields_str(apr_pool_t* p, char* fields_str, char** fields, const char* delim){
char* field;
char* last;
int i = 0;
// Use a copy of pub_fields_str (it's modified by the tokenizing)
char* tmp_fields_str = (char*)apr_pcalloc(p, 512 * sizeof(char*));
apr_cpystrn(tmp_fields_str, fields_str, strlen(fields_str)+1);
/* Split the fields on "\t" */
for(field = apr_strtok(tmp_fields_str, delim, &last); field != NULL;
field = apr_strtok(NULL, delim, &last)){
//fields[i] = malloc(MAX_F_SIZE * sizeof(char));
fields[i] = (char*)apr_pcalloc(p, MAX_F_SIZE * sizeof(char*));
if(fields[i] == NULL){
ap_log_perror(APLOG_MARK, APLOG_CRIT, 0, p, "Out of memory.");
return -1;
}
apr_cpystrn(fields[i], field, strlen(field)+1);
++i;
}
return i;
}
char** set_fields(apr_pool_t* p, ap_dbd_t* dbd, char* fields_str, char* query){
apr_status_t rv;
const char* ret = "";
// Hmm, does not work. Memory gets overwritten...
//fields_str = apr_pcalloc(p, MAX_T_F_SIZE * sizeof(char));
/*fields_str = (char*) malloc(MAX_T_F_SIZE * sizeof(char*));
if(fields_str == NULL){
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Out of memory.");
return NULL;
}*/
apr_dbd_results_t *res = NULL;
apr_dbd_row_t* row;
char* val;
int firstrow = 0;
int i = 0;
int first_row_num = 1;
if(APR_VERSION<130){ // @suppress("Symbol is not resolved")
first_row_num=0;
}
/*ap_dbd_t* dbd = (ap_dbd_t*)apr_pcalloc(p, 256 * sizeof(ap_dbd_t*));
dbd = dbd_acquire_fn(r);
if(dbd == NULL){
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Failed to acquire database connection.");
return NULL;
}*/
// This crashes with last argument 0 - i.e. only random access works
int ret_val = apr_dbd_select(dbd->driver, p, dbd->handle, &res, query, 1);
if(ret_val != 0){
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, "Query execution error in set_fields, %i.", ret_val);
return NULL;
}
int cols = apr_dbd_num_tuples(dbd->driver, res);
//char** fields = malloc(cols * sizeof(char*));
char** fields = (char**)apr_pcalloc(p, cols * sizeof(char*));
if(fields == NULL){
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p,
"Out of memory while allocating %i columns.", cols);
return NULL;
}
for(i = 0; i < cols; i++){
//fields[i] = malloc(MAX_F_SIZE * sizeof(char));
fields[i] = (char*)apr_pcalloc(p, MAX_F_SIZE * sizeof(char*));
if(fields[i] == NULL){
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, "Out of memory.");
return NULL;
}
}
/* Get the list of fields. */
i = 0;
while(i<cols){
row = NULL;
// We have to use last argument 1, ... NOT -1. Only random access works.
// Older libaprutil1 may start with 0 instead of 1...
rv = apr_dbd_get_row(dbd->driver, p, res, &row, i+first_row_num);
if(rv != 0){
ap_log_perror(APLOG_MARK, APLOG_ERR, rv, p, "Error retrieving results");
return NULL;
}
val = (char*) apr_dbd_get_entry(dbd->driver, row, 0);
if(firstrow != 0){
ret = apr_pstrcat(p, ret, "\t", NULL);
}
ret = apr_pstrcat(p, ret, val, NULL);
apr_cpystrn(fields[i], val, strlen(val)+1);
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "field --> %s", fields[i]);
if(apr_strnatcmp(val, ID_COL) == 0){
id_col_nr = i;
}
if(apr_strnatcmp(val, NAME_COL) == 0){
name_col_nr = i;
}
if(apr_strnatcmp(val, STATUS_COL) == 0){
status_col_nr = i;
}
if(apr_strnatcmp(val, HOST_COL) == 0){
host_col_nr = i;
}
if(apr_strnatcmp(val, SUBNODES_DB_URL_COL) == 0){
subnodes_db_url_col_nr = i;
}
firstrow = -1;
i++;
/* we can't break out here or row won't get cleaned up */
}
/* append the pseudo-column 'dbUrl' */
ret = apr_pstrcat(p, ret, "\t", NULL);
ret = apr_pstrcat(p, ret, DBURL_COL, NULL);
apr_cpystrn(fields_str, ret, strlen(ret)+1);
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "Found fields: %s; first field: %s", fields_str, fields[0]);
return fields;
}
char* constructUUID(apr_pool_t* p, char* job_id){
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "Constructing URL from %s", job_id);
char* uuid = memrchr(job_id, '/', strlen(job_id) - 1);
if(uuid != NULL){
uuid = uuid + 1;
}
else{
uuid = job_id;
}
return uuid;
}
// From http://stackoverflow.com/questions/2674312/how-to-append-strings-using-sprintf
int bytes_added( int result_of_sprintf ){
return (result_of_sprintf > 0) ? result_of_sprintf : 0;
}
char* recs_text_format(apr_pool_t* p, ap_dbd_t* dbd, apr_dbd_results_t *res,
int priv, char* pub_fields_str, char* fields_str, char** fields){
apr_status_t rv;
char* val;
apr_dbd_row_t* row;
int i = 0;
char* uuid = "";
char* checkStr;
char* checkStart;
char* checkEnd;
int fieldLen;
char* recs = malloc(MAX_SIZE);
int cols = apr_dbd_num_cols(dbd->driver,res);
// Works only for synchronous selects (1 instead of 0)
//int numrows = apr_dbd_num_tuples(dbd->driver,res);
int pub_check[cols];
if(priv){
strcpy(recs, pub_fields_str);
}
else{
strcpy(recs, fields_str);
}
int rownum = 0;
int length = strlen(recs);
//while(rownum <= numrows){
while(rownum<MAX_SELECT_ROWS){
row = NULL;
rv = apr_dbd_get_row(dbd->driver, p, res, &row, -1);
if(rv != 0){
break;
}
length += bytes_added(sprintf(recs+length, "%s", "\n"));
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "retrieved row %i, cols %i", rownum, cols);
for(i = 0 ; i < cols ; i++){
// To check if a field is a member of pub_fields, just see if pub_fields_str contains
// "\tfield\t".
if(rownum>0 && !pub_check[i]){
continue;
}
if(rownum==0){
checkStr = strstr(pub_fields_str, fields[i]);
fieldLen = strlen(fields[i]);
checkStart = checkStr-1;
checkEnd = checkStr+fieldLen;
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "Checking field %s", fields[i]);
if(priv && checkStr != pub_fields_str && (
checkStr == NULL ||
(checkStart != NULL && *checkStart != '\t') ||
(checkEnd != NULL && *checkEnd != '\t')
)){
pub_check[i] = 0;
continue;
}
pub_check[i] = 1;
}
val = (char*) apr_dbd_get_entry(dbd->driver, row, i);
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "--> %s", val);
length += bytes_added(sprintf(recs+length, "%s", val));
length += bytes_added(sprintf(recs+length, "%s", "\t"));
if(i == id_col_nr){
uuid = constructUUID(p, val);
}
}
length += bytes_added(sprintf(recs+length, "%s", base_url));
length += bytes_added(sprintf(recs+length, "%s", uuid));
/* we can't break out here or row won't get cleaned up */
rownum++;
}
if(rownum>=MAX_SELECT_ROWS-1){
ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, "WARNING: max number of rows reached by recs_text_format.");
}
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "Returning %i rows", rownum);
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "%s", recs);
char* ret = (char*)apr_pcalloc(p, strlen(recs) * sizeof(char*));
apr_cpystrn(ret, recs, strlen(recs)+1);
free(recs);
return ret;
}
char* recs_xml_format(apr_pool_t* p, ap_dbd_t* dbd, apr_dbd_results_t *res,
int priv, int table_num){
apr_status_t rv;
char* val;
apr_dbd_row_t* row;
int i = 0;
char* id = "";
char* recs = malloc(MAX_SIZE);
char* rec_name = (char*)apr_pcalloc(p, 8 * sizeof(char*));
char* list_name = (char*)apr_pcalloc(p, 8 * sizeof(char*));
switch(table_num){
case JOB_TABLE_NUM:
sprintf(rec_name, "job");
sprintf(list_name, "jobs");
break;
case HIST_TABLE_NUM:
sprintf(rec_name, "job");
sprintf(list_name, "history");
break;
case NODE_TABLE_NUM:
sprintf(rec_name, "node");
sprintf(list_name, "nodes");
break;
default:
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, "Invalid path: %i", table_num);
}
strcpy(recs, "<?xml version=\"1.0\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"");
int length = strlen(recs);
length += bytes_added(sprintf(recs+length, "%s", xsl_dir));
length += bytes_added(sprintf(recs+length, "%s", list_name));
length += bytes_added(sprintf(recs+length, "%s", ".xsl\"?>\n<"));
length += bytes_added(sprintf(recs+length, "%s", list_name));
length += bytes_added(sprintf(recs+length, "%s", ">"));
//int numrows = apr_dbd_num_tuples(dbd->driver,res);
int cols = apr_dbd_num_cols(dbd->driver,res);
int rownum = 0;
while(rownum<MAX_SELECT_ROWS){
row = NULL;
rv = apr_dbd_get_row(dbd->driver, p, res, &row, -1);
if (rv != 0) {
break;
}
length += bytes_added(sprintf(recs+length, "%s", "\n <"));
length += bytes_added(sprintf(recs+length, "%s", rec_name));
length += bytes_added(sprintf(recs+length, "%s", ">"));
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "cols: %i, %i, %i", status_col_nr, host_col_nr, subnodes_db_url_col_nr);
for (i = 0 ; i < cols ; i++) {
val = (char*) apr_dbd_get_entry(dbd->driver, row, i);
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "%i/%i --> %s", table_num, i, val);
if(val == NULL){
continue;
}
if(i == id_col_nr){
id = val;
length += bytes_added(sprintf(recs+length, "%s", "\n <"));
length += bytes_added(sprintf(recs+length, "%s", ID_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
length += bytes_added(sprintf(recs+length, "%s", val));
length += bytes_added(sprintf(recs+length, "%s", "</"));
length += bytes_added(sprintf(recs+length, "%s", ID_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
}
else if((table_num == JOB_TABLE_NUM || table_num == HIST_TABLE_NUM) && i == name_col_nr){
length += bytes_added(sprintf(recs+length, "%s", "\n <"));
length += bytes_added(sprintf(recs+length, "%s", NAME_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
length += bytes_added(sprintf(recs+length, "%s", val));
length += bytes_added(sprintf(recs+length, "%s", "</"));
length += bytes_added(sprintf(recs+length, "%s", NAME_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
}
else if((table_num == JOB_TABLE_NUM || table_num == HIST_TABLE_NUM) && i == status_col_nr){
length += bytes_added(sprintf(recs+length, "%s", "\n <"));
length += bytes_added(sprintf(recs+length, "%s", STATUS_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
length += bytes_added(sprintf(recs+length, "%s", val));
length += bytes_added(sprintf(recs+length, "%s", "</"));
length += bytes_added(sprintf(recs+length, "%s", STATUS_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
}
else if(table_num == NODE_TABLE_NUM && i == host_col_nr){
length += bytes_added(sprintf(recs+length, "%s", "\n <"));
length += bytes_added(sprintf(recs+length, "%s", HOST_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
length += bytes_added(sprintf(recs+length, "%s", val));
length += bytes_added(sprintf(recs+length, "%s", "</"));
length += bytes_added(sprintf(recs+length, "%s", HOST_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
}
else if(table_num == NODE_TABLE_NUM && i == subnodes_db_url_col_nr){
length += bytes_added(sprintf(recs+length, "%s", "\n <"));
length += bytes_added(sprintf(recs+length, "%s", SUBNODES_DB_URL_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
length += bytes_added(sprintf(recs+length, "%s", val));
length += bytes_added(sprintf(recs+length, "%s", "</"));
length += bytes_added(sprintf(recs+length, "%s", SUBNODES_DB_URL_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
}
}
length += bytes_added(sprintf(recs+length, "%s", "\n <"));
length += bytes_added(sprintf(recs+length, "%s", DBURL_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
length += bytes_added(sprintf(recs+length, "%s", base_url));
length += bytes_added(sprintf(recs+length, "%s", constructUUID(p, id)));
length += bytes_added(sprintf(recs+length, "%s", "</"));
length += bytes_added(sprintf(recs+length, "%s", DBURL_COL));
length += bytes_added(sprintf(recs+length, "%s", ">"));
length += bytes_added(sprintf(recs+length, "%s", "\n </"));
length += bytes_added(sprintf(recs+length, "%s", rec_name));
length += bytes_added(sprintf(recs+length, "%s", "> "));
rownum++;
}
if(rownum>=MAX_SELECT_ROWS-1){
ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, "WARNING: max number of rows reached by recs_xml_format.");
}
length += bytes_added(sprintf(recs+length, "%s", "\n</"));
length += bytes_added(sprintf(recs+length, "%s", list_name));
length += bytes_added(sprintf(recs+length, "%s", "> "));
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "Returning rows");
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "%s", recs);
char* ret = (char*)apr_pcalloc(p, strlen(recs) * sizeof(char*));
apr_cpystrn(ret, recs, strlen(recs)+1);
free(recs);
return ret;
}
/**
* Appends tab separated lines representing DB records to db_result->res, the first line of which
* is the tab separated list of fields.
* Setting the switch 'priv' to 1 turns on privacy. Privacy means that only the fields of
* 'pub_fields' are shown.
*/
db_result* get_recs(request_rec* r, apr_pool_t* p, db_result* ret, int priv, int table_num){
apr_dbd_results_t *res = NULL;
char* last;
char* last1;
char* token;
char* subtoken1;
char* subtoken2;
int start = -1;
int end = -1;
ret->format = 0;
char* query = (char*)apr_pcalloc(p, 256 * sizeof(char*));
char* fields_str = (char*)apr_pcalloc(p, 512 * sizeof(char*));
char** fields;
char* pub_fields_str = (char*)apr_pcalloc(p, 512 * sizeof(char*));
char* fields_query = (char*)apr_pcalloc(p, 256 * sizeof(char*));
char** pub_fields = (char**)apr_pcalloc(p, 256 * sizeof(char**));
//apr_cpystrn(query, JOB_RECS_SELECT_Q, strlen(JOB_RECS_SELECT_Q)+1);
switch(table_num){
case JOB_TABLE_NUM:
snprintf(query, strlen(JOB_RECS_SELECT_Q)+1, "%s", JOB_RECS_SELECT_Q);
apr_cpystrn(fields_query, JOB_REC_SHOW_F_Q, strlen(JOB_REC_SHOW_F_Q)+1);
apr_cpystrn(pub_fields_str, JOB_PUB_FIELDS_STR, strlen(JOB_PUB_FIELDS_STR)+1);
break;
case HIST_TABLE_NUM:
snprintf(query, strlen(HIST_RECS_SELECT_Q)+1, "%s", HIST_RECS_SELECT_Q);
apr_cpystrn(fields_query, HIST_REC_SHOW_F_Q, strlen(HIST_REC_SHOW_F_Q)+1);
apr_cpystrn(pub_fields_str, JOB_PUB_FIELDS_STR, strlen(JOB_PUB_FIELDS_STR)+1);
break;
case NODE_TABLE_NUM:
snprintf(query, strlen(NODE_RECS_SELECT_Q)+1, "%s", NODE_RECS_SELECT_Q);
apr_cpystrn(fields_query, NODE_REC_SHOW_F_Q, strlen(NODE_REC_SHOW_F_Q)+1);
apr_cpystrn(pub_fields_str, NODE_PUB_FIELDS_STR, strlen(NODE_PUB_FIELDS_STR)+1);
break;
default:
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, "Invalid path: %i --> %s", table_num, r->uri);
}
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Query0: %s", query);
/* For URLs like
* GET /db/jobs/?format=text|xml&csStatus=ready|requested|running&...
* append WHERE statements to query.*/
if(r->args && countchr(r->args, "=") > 0){
char buffer[strlen(r->args)+1];
snprintf(buffer, strlen(r->args)+1, "%s", r->args);
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "args: %s", buffer);
for ((token = strtok_r(buffer, "&", &last)); token;
token = strtok_r(NULL, "&", &last)) {
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "token: %s", token);
subtoken1 = strtok_r(token, "=", &last1);
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "subtoken1: %s", subtoken1);
if(apr_strnatcmp(subtoken1, FORMAT_STR) == 0){
subtoken2 = strtok_r(NULL, "=", &last1);
if(apr_strnatcmp(subtoken2, TEXT_FORMAT_STR) == 0){
ret->format = TEXT_FORMAT;
}
else if(apr_strnatcmp(subtoken2, XML_FORMAT_STR) == 0){
ret->format = XML_FORMAT;
}
else{
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Format %s unknown.", subtoken2);
//return NULL;
}
}
else if(apr_strnatcmp(subtoken1, START_STR) != 0 && apr_strnatcmp(subtoken1, END_STR) != 0){
subtoken2 = strtok_r(NULL, "=", &last1);
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "subtoken2: %s", subtoken2);
query = apr_pstrcat(p, query, " WHERE ", subtoken1, " = '", subtoken2, "'", NULL);
}
else if(apr_strnatcmp(subtoken1, START_STR) == 0){
subtoken2 = strtok_r(NULL, "=", &last1);
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "subtoken2: %s", subtoken2);
start = atoi(subtoken2);
}
else if(apr_strnatcmp(subtoken1, END_STR) == 0){
subtoken2 = strtok_r(NULL, "=", &last1);
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "subtoken2: %s", subtoken2);
end = atoi(subtoken2);
}
}
if(start > 0 && end < 0){
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "When specifying 'start' you MUST specify 'end' as well.");
return NULL;
}
else if(start >= 0 && end >= 0){
query = apr_pstrcat(p, query, " LIMIT ", apr_itoa(p, start), ",", apr_itoa(p, end - start +1), NULL);
}
else if(start < 0 && end >= 0){
query = apr_pstrcat(p, query, " LIMIT ", apr_itoa(p, end +1), NULL);
}
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Query: %s", query);
}
/* For a plain URL like GET /db/jobs/, just use query unmodified. */
else{
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "GET with no args");
}
/* If outputting text, set fields. Be ware, after select,
results MUST be traversed before another select can be done. */
ap_dbd_t* dbd;// = (ap_dbd_t*)apr_pcalloc(p, sizeof(ap_dbd_t*));
dbd = dbd_acquire_fn(r);
if(dbd == NULL){
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Failed to acquire database connection.");
return NULL;
}
if((fields=set_fields(p, dbd, fields_str, fields_query))==NULL){
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Failed to set fields.");
return NULL;
}
if(ret->format == TEXT_FORMAT){
if(tokenize_fields_str(p, pub_fields_str, pub_fields, "\t") < 0 && priv){
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Failed to set public fields.");
return NULL;
}
}
/* Now do the query */
if(apr_dbd_select(dbd->driver, p, dbd->handle, &res, query, 0) != 0){
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Query execution error in get_recs.");
return NULL;
}
// format result
if(ret->format == TEXT_FORMAT){
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Returning text");
ret->res = recs_text_format(p, dbd, res, priv, pub_fields_str, fields_str, fields);
}
else if(ret->format == XML_FORMAT){
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Returning XML");
ret->res = recs_xml_format(p, dbd, res, priv, table_num);
}
// Dont't do this. It causes segfaults...
//dbd->pool = NULL;
//apr_dbd_close(dbd->driver, dbd->handle);
return ret;
}
void get_rec_s(apr_pool_t* p, ap_dbd_t* dbd, apr_dbd_results_t* res,
char* uuid, char* query){
char* my_query = (char*)apr_pcalloc(p, 256 * sizeof(char*));
snprintf(my_query, strlen(query)+strlen(uuid)-1, query, uuid);
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "Query: %s", my_query);
if(apr_dbd_select(dbd->driver, p, dbd->handle, &res, my_query, 0) != 0){
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, "Query execution error in get_rec_s.");
}
}
void get_rec_ps(apr_pool_t* p, ap_dbd_t* dbd, apr_dbd_results_t* res,
char* uuid, int table_num){
apr_dbd_prepared_t* statement = NULL;
char* str = NULL;
switch(table_num){
case JOB_TABLE_NUM:
statement = apr_hash_get(dbd->prepared, LABEL, APR_HASH_KEY_STRING);
str = apr_pstrcat(p, "%", uuid, NULL);
break;
case HIST_TABLE_NUM:
statement = apr_hash_get(dbd->prepared, LABEL2, APR_HASH_KEY_STRING);
str = apr_pstrcat(p, "/%", uuid, NULL);
break;
case NODE_TABLE_NUM:
statement = apr_hash_get(dbd->prepared, LABEL3, APR_HASH_KEY_STRING);
str = apr_pstrcat(p, "/%", uuid, NULL);
break;
default:
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, "Invalid path: %i", table_num);
}
if(statement == NULL){
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p,
"A prepared statement could not be found for getting job records.");
}
if(apr_dbd_pvselect(dbd->driver, p, dbd->handle, &res, statement,
0, str, NULL) != 0) {
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p,
"Query execution error looking up '%s' in database", uuid);
}
}
void rec_text_format(apr_pool_t* p, ap_dbd_t* dbd, apr_dbd_results_t* res,
db_result* ret, char** fields){
apr_dbd_row_t* row;
apr_status_t rv;
char* val;
int i;
int firstrow = 0;
char* rec = "";
//int numrows = apr_dbd_num_tuples(dbd->driver,res);
int cols = apr_dbd_num_cols(dbd->driver,res);
int rownum = 0;
while(rownum<MAX_SELECT_ROWS){
row = NULL;
rv = apr_dbd_get_row(dbd->driver, p, res, &row, -1);
if(rv != 0){
break;
}
if(firstrow != 0){
rec = apr_pstrcat(p, rec, "\n\n", NULL);
}
for(i = 0 ; i < cols ; i++){
val = (char*) apr_dbd_get_entry(dbd->driver, row, i);
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "--> %s", val);
if(i > 0){
rec = apr_pstrcat(p, rec, "\n", NULL);
}
rec = apr_pstrcat(p, rec, fields[i], ": ", val, NULL);
// Set res.status
if(strcmp(fields[i], STATUS_COL) == 0 && val != NULL){
ret->status = val;
}
// Set res.providerInfo
else if(strcmp(fields[i], PROVIDERINFO_COL) == 0 && val != NULL){
ret->providerInfo = val;
}
}
firstrow = -1;
rownum++;
/* we can't break out here or row won't get cleaned up */
}
if(rownum>=MAX_SELECT_ROWS-1){
ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, "WARNING: max number of rows reached by rec_text_format.");
}
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "Returning record:");
//ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p, "%s", rec);
ret->res = rec;
}
static int is_list_field(char* field){
// allowedVOs, hypervisors, inputFileURLs, runtimeEnvironments
if(strcmp(field, ALLOWED_VOS_COL) == 0){
return 1;
}
else if(strcmp(field, HYPERVISORS_COL) == 0){