/
mssqlvdi-fd.c
1873 lines (1623 loc) · 46.8 KB
/
mssqlvdi-fd.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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2010 Zilvinas Krapavickas <zkrapavickas@gmail.com>
Copyright (C) 2013-2014 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation, which is
listed in the file LICENSE.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* MSSQL backup restore plugin using VDI.
*/
#include "bareos.h"
#include "fd_plugins.h"
#include "fd_common.h"
/*
* Microsoft® Component Object Model (COM)
*/
#include <comutil.h>
/*
* Microsoft® MSSQL Virtual Device Interface (VDI)
*/
#include "vdi.h"
#include "vdierror.h"
#include "vdiguid.h"
/*
* Microsoft® ActiveX® Data Objects
*/
#include <initguid.h>
#include <adoid.h>
#include <adoint.h>
static const int dbglvl = 150;
#define PLUGIN_LICENSE "Bareos AGPLv3"
#define PLUGIN_AUTHOR "Zilvinas Krapavickas"
#define PLUGIN_DATE "October 2014"
#define PLUGIN_VERSION "2"
#define PLUGIN_DESCRIPTION "Bareos MSSQL VDI Windows File Daemon Plugin"
#define PLUGIN_USAGE "\n mssqlvdi:\n"\
" serveraddress=<hostname>:\n"\
" instance=<instance name>:\n"\
" database=<database name>:\n"\
" username=<database username>:\n"\
" password=<database password>:\n"\
" norecovery=<yes|no>:\n"\
" replace=<yes|no>:\n"\
" recoverafterrestore=<yes|no>:\n"\
" stopbeforemark=<log sequence number specification>:\n"\
" stopatmark=<log sequence number specification>:\n"\
" stopat=<timestamp>\n"\
" \n"\
" examples:\n"\
" timestamp: 'Apr 15, 2020 12:00 AM'\n"\
" log sequence number: 'lsn:15000000040000037'"
#define DEFAULT_SERVER_ADDRESS "localhost"
#define DEFAULT_INSTANCE "default"
#define DEFAULT_BLOCKSIZE 65536
#define DEFAULT_BUFFERS 10
#define VDS_NAME_LENGTH 50
#define VDI_DEFAULT_WAIT 60000 /* 60 seconds */
#define VDI_WAIT_TIMEOUT 0xFFFFFFFF /* INFINITE */
/*
* Forward referenced functions
*/
static bRC newPlugin(bpContext *ctx);
static bRC freePlugin(bpContext *ctx);
static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
static bRC endBackupFile(bpContext *ctx);
static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
static bRC startRestoreFile(bpContext *ctx, const char *cmd);
static bRC endRestoreFile(bpContext *ctx);
static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
static bRC checkFile(bpContext *ctx, char *fname);
static bRC parse_plugin_definition(bpContext *ctx, void *value);
static bRC end_restore_job(bpContext *ctx, void *value);
static void close_vdi_deviceset(struct plugin_ctx *p_ctx);
static bool adoReportError(bpContext *ctx);
/*
* Pointers to Bareos functions
*/
static bFuncs *bfuncs = NULL;
static bInfo *binfo = NULL;
/*
* Plugin Information block
*/
static genpInfo pluginInfo = {
sizeof(pluginInfo),
FD_PLUGIN_INTERFACE_VERSION,
FD_PLUGIN_MAGIC,
PLUGIN_LICENSE,
PLUGIN_AUTHOR,
PLUGIN_DATE,
PLUGIN_VERSION,
PLUGIN_DESCRIPTION,
PLUGIN_USAGE
};
/*
* Plugin entry points for Bareos
*/
static pFuncs pluginFuncs = {
sizeof(pluginFuncs),
FD_PLUGIN_INTERFACE_VERSION,
/* Entry points into plugin */
newPlugin, /* new plugin instance */
freePlugin, /* free plugin instance */
getPluginValue,
setPluginValue,
handlePluginEvent,
startBackupFile,
endBackupFile,
startRestoreFile,
endRestoreFile,
pluginIO,
createFile,
setFileAttributes,
checkFile
};
/*
* Plugin private context
*/
struct plugin_ctx {
int RestoreFD;
bool RestoreToFile;
bool DoNoRecovery;
bool ForceReplace;
bool RecoverAfterRestore;
char *plugin_options;
char *filename;
char *server_address;
char *instance;
char *database;
char *username;
char *password;
char *stopbeforemark;
char *stopatmark;
char *stopat;
char *ado_connect_string;
char *ado_query;
char *ado_errorstr;
wchar_t *vdsname;
int32_t backup_level;
IClientVirtualDeviceSet2 *VDIDeviceSet;
IClientVirtualDevice *VDIDevice;
VDConfig VDIConfig;
bool AdoThreadStarted;
pthread_t ADOThread;
};
struct adoThreadContext {
_ADOConnection *adoConnection;
BSTR ado_connect_string;
BSTR ado_query;
};
/*
* This defines the arguments that the plugin parser understands.
*/
enum plugin_argument_type {
argument_none,
argument_server_address,
argument_instance,
argument_database,
argument_username,
argument_password,
argument_norecovery,
argument_replace,
argument_recover_after_restore,
argument_stopbeforemark,
argument_stopatmark,
argument_stopat
};
struct plugin_argument {
const char *name;
enum plugin_argument_type type;
};
static plugin_argument plugin_arguments[] = {
{ "serveraddress", argument_server_address },
{ "instance", argument_instance },
{ "database", argument_database },
{ "username", argument_username },
{ "password", argument_password },
{ "norecovery", argument_norecovery },
{ "replace", argument_replace },
{ "recoverafterrestore", argument_recover_after_restore },
{ "stopbeforemark", argument_stopbeforemark },
{ "stopatmark", argument_stopatmark },
{ "stopat", argument_stopat },
{ NULL, argument_none }
};
#ifdef __cplusplus
extern "C" {
#endif
/*
* loadPlugin() and unloadPlugin() are entry points that are exported, so Bareos can
* directly call these two entry points they are common to all Bareos plugins.
*
* External entry point called by Bareos to "load" the plugin
*/
bRC DLL_IMP_EXP loadPlugin(bInfo *lbinfo,
bFuncs *lbfuncs,
genpInfo **pinfo,
pFuncs **pfuncs)
{
bfuncs = lbfuncs; /* set Bareos funct pointers */
binfo = lbinfo;
*pinfo = &pluginInfo; /* return pointer to our info */
*pfuncs = &pluginFuncs; /* return pointer to our functions */
return bRC_OK;
}
/*
* External entry point to unload the plugin
*/
bRC DLL_IMP_EXP unloadPlugin()
{
return bRC_OK;
}
#ifdef __cplusplus
}
#endif
/*
* The following entry points are accessed through the function pointers we supplied to Bareos.
* Each plugin type (dir, fd, sd) has its own set of entry points that the plugin must define.
*
* Create a new instance of the plugin i.e. allocate our private storage
*/
static bRC newPlugin(bpContext *ctx)
{
HRESULT hr;
plugin_ctx *p_ctx;
/*
* Initialize COM for this thread.
*/
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (!SUCCEEDED (hr)) {
return bRC_Error;
}
p_ctx = (plugin_ctx *)malloc(sizeof(plugin_ctx));
if (!p_ctx) {
return bRC_Error;
}
memset(p_ctx, 0, sizeof(plugin_ctx));
ctx->pContext = (void *)p_ctx; /* set our context pointer */
/*
* Only register the events we are really interested in.
*/
bfuncs->registerBareosEvents(ctx,
6,
bEventLevel,
bEventRestoreCommand,
bEventBackupCommand,
bEventPluginCommand,
bEventEndRestoreJob,
bEventNewPluginOptions);
return bRC_OK;
}
/*
* Free a plugin instance, i.e. release our private storage
*/
static bRC freePlugin(bpContext *ctx)
{
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx) {
return bRC_Error;
}
Dmsg(ctx, dbglvl, "mssqlvdi-fd: entering freePlugin\n");
/*
* Close any open VDI deviceset.
*/
close_vdi_deviceset(p_ctx);
/*
* See if there is any error to report from the ADO layer.
*/
adoReportError(ctx);
/*
* Cleanup the context.
*/
if (p_ctx->plugin_options) {
free(p_ctx->plugin_options);
}
if (p_ctx->filename) {
free(p_ctx->filename);
}
if (p_ctx->server_address) {
free(p_ctx->server_address);
}
if (p_ctx->instance) {
free(p_ctx->instance);
}
if (p_ctx->database) {
free(p_ctx->database);
}
if (p_ctx->username) {
free(p_ctx->username);
}
if (p_ctx->password) {
free(p_ctx->password);
}
if (p_ctx->stopbeforemark) {
free(p_ctx->stopbeforemark);
}
if (p_ctx->stopatmark) {
free(p_ctx->stopatmark);
}
if (p_ctx->stopat) {
free(p_ctx->stopat);
}
if (p_ctx->vdsname) {
free(p_ctx->vdsname);
}
if (p_ctx->ado_connect_string) {
free(p_ctx->ado_connect_string);
}
if (p_ctx->ado_query) {
free(p_ctx->ado_query);
}
free(p_ctx);
p_ctx = NULL;
/*
* Tear down COM for this thread.
*/
CoUninitialize();
Dmsg(ctx, dbglvl, "mssqlvdi-fd: leaving freePlugin\n");
return bRC_OK;
}
/*
* Return some plugin value (none defined)
*/
static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
{
return bRC_OK;
}
/*
* Set a plugin value (none defined)
*/
static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
{
return bRC_OK;
}
/*
* Handle an event that was generated in Bareos
*/
static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
{
bRC retval;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx) {
return bRC_Error;
}
switch (event->eventType) {
case bEventLevel:
p_ctx->backup_level = (int64_t)value;
retval = bRC_OK;
break;
case bEventRestoreCommand:
/*
* Fall-through wanted
*/
case bEventBackupCommand:
/*
* Fall-through wanted
*/
case bEventPluginCommand:
retval = parse_plugin_definition(ctx, value);
break;
case bEventNewPluginOptions:
/*
* Free any previous value.
*/
if (p_ctx->plugin_options) {
free(p_ctx->plugin_options);
p_ctx->plugin_options = NULL;
}
retval = parse_plugin_definition(ctx, value);
/*
* Save that we got a plugin override.
*/
p_ctx->plugin_options = bstrdup((char *)value);
break;
case bEventEndRestoreJob:
retval = end_restore_job(ctx, value);
break;
default:
Jmsg(ctx, M_FATAL, "mssqlvdi-fd: unknown event=%d\n", event->eventType);
Dmsg(ctx, dbglvl, "mssqlvdi-fd: unknown event=%d\n", event->eventType);
retval = bRC_Error;
break;
}
return retval;
}
/*
* Start the backup of a specific file
*/
static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
{
time_t now;
POOL_MEM fname(PM_NAME);
char dt[MAX_TIME_LENGTH];
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx) {
return bRC_Error;
}
/*
* If no explicit instance name given use the DEFAULT_INSTANCE.
*/
if (!p_ctx->instance) {
p_ctx->instance = bstrdup(DEFAULT_INSTANCE);
}
/*
* If no explicit server address given use the DEFAULT_SERVER_ADDRESS.
*/
if (!p_ctx->server_address) {
p_ctx->server_address = bstrdup(DEFAULT_SERVER_ADDRESS);
}
now = time(NULL);
switch (p_ctx->backup_level) {
case L_FULL:
Mmsg(fname, "/@MSSQL/%s/%s/db-full", p_ctx->instance, p_ctx->database);
break;
case L_DIFFERENTIAL:
Mmsg(fname, "/@MSSQL/%s/%s/db-diff", p_ctx->instance, p_ctx->database);
break;
case L_INCREMENTAL:
bstrutime(dt, sizeof(dt), now);
Mmsg(fname, "/@MSSQL/%s/%s/log-%s", p_ctx->instance, p_ctx->database, dt);
break;
default:
Jmsg(ctx, M_FATAL, "Unsuported backup level (%c).\n", p_ctx->backup_level);
Dmsg(ctx, dbglvl, "Unsuported backup level (%c).\n", p_ctx->backup_level);
return bRC_Error;
}
p_ctx->filename = bstrdup(fname.c_str());
Dmsg(ctx, dbglvl, "startBackupFile: Generated filename %s\n", p_ctx->filename);
sp->fname = p_ctx->filename;
sp->type = FT_REG;
sp->statp.st_mode = S_IFREG | S_IREAD | S_IWRITE | S_IEXEC;
sp->statp.st_ctime = now;
sp->statp.st_mtime = now;
sp->statp.st_atime = now;
sp->statp.st_size = 0;
sp->statp.st_blksize = DEFAULT_BLOCKSIZE;
sp->statp.st_blocks = 1;
return bRC_OK;
}
/*
* Done with backup of this file
*/
static bRC endBackupFile(bpContext *ctx)
{
/*
* We would return bRC_More if we wanted startBackupFile to be called again to backup another file
*/
return bRC_OK;
}
/*
* Parse a boolean value e.g. check if its yes or true anything else translates to false.
*/
static inline bool parse_boolean(const char *argument_value)
{
if (bstrcasecmp(argument_value, "yes") ||
bstrcasecmp(argument_value, "true")) {
return true;
} else {
return false;
}
}
/*
* Only set destination to value when it has no previous setting.
*/
static inline void set_string_if_null(char **destination, char *value)
{
if (!*destination) {
*destination = bstrdup(value);
}
}
/*
* Always set destination to value and clean any previous one.
*/
static inline void set_string(char **destination, char *value)
{
if (*destination) {
free(*destination);
}
*destination = bstrdup(value);
}
/*
* Parse the plugin definition passed in.
*
* The definition is in this form:
*
* mssqlvdi:instance=<instance>:database=<databasename>:
*/
static bRC parse_plugin_definition(bpContext *ctx, void *value)
{
int i;
bool keep_existing;
char *plugin_definition, *bp, *argument, *argument_value;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx || !value) {
return bRC_Error;
}
keep_existing = (p_ctx->plugin_options) ? true : false;
/*
* Parse the plugin definition.
* Make a private copy of the whole string.
*/
plugin_definition = bstrdup((char *)value);
bp = strchr(plugin_definition, ':');
if (!bp) {
Jmsg(ctx, M_FATAL, "Illegal plugin definition %s\n", plugin_definition);
Dmsg(ctx, dbglvl, "Illegal plugin definition %s\n", plugin_definition);
goto bail_out;
}
/*
* Skip the first ':'
*/
bp++;
while (bp) {
if (strlen(bp) == 0) {
break;
}
/*
* Each argument is in the form:
* <argument> = <argument_value>
*
* So we setup the right pointers here, argument to the beginning
* of the argument, argument_value to the beginning of the argument_value.
*/
argument = bp;
argument_value = strchr(bp, '=');
if (!argument_value) {
Jmsg(ctx, M_FATAL, "Illegal argument %s without value\n", argument);
Dmsg(ctx, dbglvl, "Illegal argument %s without value\n", argument);
goto bail_out;
}
*argument_value++ = '\0';
/*
* See if there are more arguments and setup for the next run.
*/
bp = strchr(argument_value, ':');
if (bp) {
*bp++ = '\0';
}
for (i = 0; plugin_arguments[i].name; i++) {
if (bstrcasecmp(argument, plugin_arguments[i].name)) {
char **str_destination = NULL;
bool *bool_destination = NULL;
switch (plugin_arguments[i].type) {
case argument_server_address:
str_destination = &p_ctx->server_address;
break;
case argument_instance:
str_destination = &p_ctx->instance;
break;
case argument_database:
str_destination = &p_ctx->database;
break;
case argument_username:
str_destination = &p_ctx->username;
break;
case argument_password:
str_destination = &p_ctx->password;
break;
case argument_norecovery:
bool_destination = &p_ctx->DoNoRecovery;
break;
case argument_replace:
bool_destination = &p_ctx->ForceReplace;
break;
case argument_recover_after_restore:
bool_destination = &p_ctx->RecoverAfterRestore;
break;
case argument_stopbeforemark:
str_destination = &p_ctx->stopbeforemark;
break;
case argument_stopatmark:
str_destination = &p_ctx->stopatmark;
break;
case argument_stopat:
str_destination = &p_ctx->stopat;
break;
default:
break;
}
/*
* Keep the first value, ignore any next setting.
*/
if (str_destination) {
if (keep_existing) {
set_string_if_null(str_destination, argument_value);
} else {
set_string(str_destination, argument_value);
}
}
/*
* Set any boolean variable.
*/
if (bool_destination) {
*bool_destination = parse_boolean(argument_value);
}
/*
* When we have a match break the loop.
*/
break;
}
}
/*
* Got an invalid keyword ?
*/
if (!plugin_arguments[i].name) {
Jmsg(ctx, M_FATAL, "Illegal argument %s with value %s in plugin definition\n", argument, argument_value);
Dmsg(ctx, dbglvl, "Illegal argument %s with value %s in plugin definition\n", argument, argument_value);
goto bail_out;
}
}
free(plugin_definition);
return bRC_OK;
bail_out:
free(plugin_definition);
return bRC_Error;
}
/*
* Close the VDI deviceset if is is opened.
*/
static void close_vdi_deviceset(plugin_ctx *p_ctx)
{
/*
* Close VDI Device.
*/
if (p_ctx->VDIDevice) {
p_ctx->VDIDevice->Release();
p_ctx->VDIDevice = NULL;
}
/*
* Close VDI DeviceSet.
*/
if (p_ctx->VDIDeviceSet) {
p_ctx->VDIDeviceSet->Close();
p_ctx->VDIDeviceSet->Release();
p_ctx->VDIDeviceSet = NULL;
}
/*
* Cancel the started database thread.
*/
if (p_ctx->AdoThreadStarted) {
if (!pthread_equal(p_ctx->ADOThread, pthread_self())) {
pthread_cancel(p_ctx->ADOThread);
}
}
}
/*
* Generic COM error reporting function.
*/
static void comReportError(bpContext *ctx, HRESULT hrErr)
{
IErrorInfo *pErrorInfo;
BSTR pSource = NULL;
BSTR pDescription = NULL;
HRESULT hr;
char *source, *description;
/*
* See if there is anything to report.
*/
hr = GetErrorInfo(0, &pErrorInfo);
if (hr == S_FALSE) {
return;
}
/*
* Get the description of the COM error.
*/
hr = pErrorInfo->GetDescription(&pDescription);
if (!SUCCEEDED (hr)) {
pErrorInfo->Release();
return;
}
/*
* Get the source of the COM error.
*/
hr = pErrorInfo->GetSource(&pSource);
if (!SUCCEEDED (hr)) {
SysFreeString(pDescription);
pErrorInfo->Release();
return;
}
/*
* Convert windows BSTR to normal strings.
*/
source = BSTR_2_str(pSource);
description = BSTR_2_str(pDescription);
if (source && description) {
Jmsg(ctx, M_FATAL, "%s(x%X): %s\n", source, hrErr, description);
Dmsg(ctx, dbglvl, "%s(x%X): %s\n", source, hrErr, description);
}
if (source) {
free(source);
}
if (description) {
free(description);
}
/*
* Generic cleanup (free the description and source as those are returned in
* dynamically allocated memory by the COM routines.)
*/
SysFreeString(pSource);
SysFreeString(pDescription);
pErrorInfo->Release();
}
/*
* Retrieve errors from ADO Connection.
*/
static bool adoGetErrors(bpContext *ctx, _ADOConnection *adoConnection, POOL_MEM &ado_errorstr)
{
HRESULT hr;
ADOErrors *adoErrors;
long errCount;
/*
* Get any errors that are reported.
*/
hr = adoConnection->get_Errors(&adoErrors);
if (!SUCCEEDED (hr)) {
comReportError(ctx, hr);
goto bail_out;
}
/*
* See how many errors there are.
*/
hr = adoErrors->get_Count(&errCount);
if (!SUCCEEDED (hr)) {
comReportError(ctx, hr);
adoErrors->Release();
goto bail_out;
}
/*
* Loop over all error and append them into one big error string.
*/
pm_strcpy(ado_errorstr, "");
for (long i = 0; i < errCount; i++ ) {
ADOError *adoError;
BSTR pDescription = NULL;
char *description;
hr = adoErrors->get_Item(_variant_t(i), &adoError);
if (!SUCCEEDED (hr)) {
comReportError(ctx, hr);
adoErrors->Release();
goto bail_out;
}
hr = adoError->get_Description(&pDescription);
if (!SUCCEEDED (hr)) {
comReportError(ctx, hr);
adoError->Release();
adoErrors->Release();
goto bail_out;
}
description = BSTR_2_str(pDescription);
if (description) {
Dmsg(ctx, dbglvl, "adoGetErrors: ADO error %s\n", description);
pm_strcat(ado_errorstr, description);
pm_strcat(ado_errorstr, "\n");
free(description);
}
SysFreeString(pDescription);
adoError->Release();
}
/*
* Generic cleanup.
*/
adoErrors->Clear();
adoErrors->Release();
return true;
bail_out:
return false;
}
/*
* Print errors (when available) collected by adoThreadSetError function.
*/
static bool adoReportError(bpContext *ctx)
{
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (p_ctx->ado_errorstr) {
Jmsg(ctx, M_FATAL, "%s\n", p_ctx->ado_errorstr);
Dmsg(ctx, dbglvl, "%s\n", p_ctx->ado_errorstr);
free(p_ctx->ado_errorstr);
p_ctx->ado_errorstr = NULL;
return true;
}
return false;
}
/*
* Retrieve errors from ADO Connection when running the query in a seperate thread.
*/
static void adoThreadSetError(bpContext *ctx, _ADOConnection *adoConnection)
{
int cancel_type;
POOL_MEM ado_errorstr(PM_NAME);
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (p_ctx->ado_errorstr) {
return;
}
/*
* Set the threads cancellation type to defered.
*/
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &cancel_type);
if (!adoGetErrors(ctx, adoConnection, ado_errorstr)) {
goto bail_out;
}
/*
* Keep the errors in a buffer these will be printed by the adoReportError function.
*/
p_ctx->ado_errorstr = bstrdup(ado_errorstr.c_str());
bail_out:
/*
* Restore the threads cancellation type.
*/
pthread_setcanceltype(cancel_type, NULL);
return;
}
/*
* Cleanup function called on thread destroy.
*/
static void adoCleanupThread(void *data)
{
adoThreadContext *ado_ctx;
ado_ctx = (adoThreadContext *)data;
if (!data) {
return;
}
/*
* Generic cleanup.
*/
if (ado_ctx->ado_connect_string) {
SysFreeString(ado_ctx->ado_connect_string);
}
if (ado_ctx->ado_query) {
SysFreeString(ado_ctx->ado_query);
}
if (ado_ctx->adoConnection) {
LONG adoState;
ado_ctx->adoConnection->get_State(&adoState);
if (adoState & adStateExecuting) {
ado_ctx->adoConnection->Cancel();
}
if (adoState & adStateOpen) {
ado_ctx->adoConnection->Close();
}
ado_ctx->adoConnection->Release();
}
/*
* Tear down COM for this thread.
*/
CoUninitialize();
}
/*
* Run a seperate thread that connects to the database server and
* controls the backup or restore. When we close the VDI device we
* also tear down this database control thread.
*/
static void *adoThread(void *data)
{