/
jrd.cpp
7489 lines (6279 loc) · 180 KB
/
jrd.cpp
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
/*
* PROGRAM: JRD Access Method
* MODULE: jrd.cpp
* DESCRIPTION: User visible entrypoints
*
* The contents of this file are subject to the Interbase Public
* License Version 1.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.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, as the engine now fully supports
* readonly databases.
* 2001.07.09 Sean Leyne - Restore default setting to Force Write = "On", for
* Windows NT platform, for new database files. This was changed
* with IB 6.0 to OFF and has introduced many reported database
* corruptions.
*
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
*/
#include "firebird.h"
#include "../jrd/common.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../jrd/common.h"
#include "../jrd/thd.h"
#include "../jrd/os/thd_priority.h"
#include <stdarg.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <errno.h>
#include "../jrd/ibase.h"
#include "../jrd/jrd.h"
#include "../jrd/irq.h"
#include "../jrd/isc.h"
#include "../jrd/drq.h"
#include "../jrd/req.h"
#include "../jrd/tra.h"
#include "../jrd/blb.h"
#include "../jrd/lck.h"
#include "../jrd/nbak.h"
#include "../jrd/scl.h"
//#include "../jrd/license.h"
#include "../jrd/os/pio.h"
#include "../jrd/ods.h"
#include "../jrd/exe.h"
#include "../jrd/val.h"
#include "../jrd/rse.h"
#include "../jrd/all.h"
#ifdef REPLAY_OSRI_API_CALLS_SUBSYSTEM
#include "../jrd/log.h"
#endif
#include "../jrd/fil.h"
#include "../jrd/intl.h"
#include "../jrd/sbm.h"
#include "../jrd/svc.h"
#include "../jrd/sdw.h"
#include "../jrd/lls.h"
#include "../jrd/cch.h"
#include "../jrd/iberr.h"
//#include "../common/classes/timestamp.h"
#include "../intl/charsets.h"
#include "../jrd/sort.h"
#include "../jrd/blb_proto.h"
#include "../jrd/cch_proto.h"
#include "../jrd/cmp_proto.h"
#include "../jrd/dbg_proto.h"
#include "../jrd/dyn_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/exe_proto.h"
#include "../jrd/ext_proto.h"
#include "../jrd/fun_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/iberr_proto.h"
#include "../jrd/inf_proto.h"
#include "../jrd/ini_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/inuse_proto.h"
#include "../jrd/isc_f_proto.h"
#include "../jrd/isc_proto.h"
#include "../jrd/jrd_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/log_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/pag_proto.h"
#include "../jrd/par_proto.h"
#include "../jrd/os/pio_proto.h"
#include "../jrd/sch_proto.h"
#include "../jrd/scl_proto.h"
#include "../jrd/sdw_proto.h"
#include "../jrd/shut_proto.h"
#include "../jrd/sort_proto.h"
#include "../jrd/svc_proto.h"
#include "../jrd/thread_proto.h"
#include "../jrd/tra_proto.h"
#include "../jrd/val_proto.h"
#include "../jrd/vio_proto.h"
#include "../jrd/file_params.h"
#include "../jrd/event_proto.h"
#include "../jrd/why_proto.h"
#include "../jrd/flags.h"
#include "../common/utils_proto.h"
#include "../common/config/config.h"
#include "../common/config/dir_list.h"
#include "../jrd/plugin_manager.h"
#include "../jrd/db_alias.h"
#include "../jrd/IntlManager.h"
#include "../common/classes/fb_tls.h"
#include "../common/classes/ClumpletReader.h"
#include "../jrd/DebugInterface.h"
using namespace Jrd;
const SSHORT WAIT_PERIOD = -1;
#ifdef SUPPORT_RAW_DEVICES
#define unlink PIO_unlink
#endif
#ifdef SUPERSERVER
#define V4_THREADING
#endif // SUPERSERVER
#define V4_JRD_MUTEX_LOCK(mutx)
#define V4_JRD_MUTEX_UNLOCK(mutx)
/* dimitr: note the condition below - it's never true, so V4 locking
is not used. But I keep the code to outline the possible
MULTI_THREAD logic to be applied here in order to protect
global data. It's the only V4_THREADING part remaining in
the codebase before being wiped out.
*/
#ifdef V4_THREADING
#ifndef SUPERSERVER
static Firebird::Mutex databases_mutex;
#define V4_JRD_MUTEX_LOCK(mutx) {THREAD_EXIT(); mutx.enter(); THREAD_ENTER();}
#define V4_JRD_MUTEX_UNLOCK(mutx) mutx.leave()
#endif // SUPERSERVER
#endif // V4_THREADING
#ifdef SUPERSERVER
extern SLONG trace_pools;
namespace
{
REC_MUTX_T databases_rec_mutex;
class DatabasesInit
{
public:
static void init()
{
THD_rec_mutex_init(&databases_rec_mutex);
}
static void cleanup()
{
THD_rec_mutex_destroy(&databases_rec_mutex);
}
};
class DatabasesInitInstance : public Firebird::InitMutex<DatabasesInit>
{
public:
DatabasesInitInstance()
{
init();
}
~DatabasesInitInstance()
{
cleanup();
}
};
DatabasesInitInstance holder;
}
#define JRD_SS_MUTEX_LOCK {THREAD_EXIT();\
THD_rec_mutex_lock(&databases_rec_mutex);\
THREAD_ENTER();}
#define JRD_SS_MUTEX_UNLOCK THD_rec_mutex_unlock(&databases_rec_mutex)
#else
#define JRD_SS_MUTEX_LOCK
#define JRD_SS_MUTEX_UNLOCK
#endif
namespace {
void validateAccess(const Attachment* attachment)
{
if (!attachment->locksmith())
{
ERR_post(isc_adm_task_denied, isc_arg_end);
}
}
}
#ifdef WIN_NT
#include <windows.h>
// these should stop a most annoying warning
#undef TEXT
#define TEXT SCHAR
#endif // WIN_NT
Jrd::Database::~Database()
{
delete dbb_sys_trans;
destroyIntlObjects();
pool_ptr* itr = dbb_pools.begin();
while (itr != dbb_pools.end())
{
if (*itr && *itr == dbb_bufferpool)
dbb_bufferpool = 0;
if (*itr && *itr != dbb_permanent)
itr = JrdMemoryPool::deletePool(*itr);
else
++itr;
}
if (dbb_bufferpool)
JrdMemoryPool::deletePool(dbb_bufferpool);
}
void Jrd::Trigger::compile(thread_db* tdbb)
{
if (!request /*&& !compile_in_progress*/)
{
SET_TDBB(tdbb);
#ifdef SUPERSERVER
Database* dbb = tdbb->getDatabase();
if (!(dbb->dbb_flags & DBB_sp_rec_mutex_init))
{
THD_rec_mutex_init(&dbb->dbb_sp_rec_mutex);
dbb->dbb_flags |= DBB_sp_rec_mutex_init;
}
THREAD_EXIT();
const int error = THD_rec_mutex_lock(&dbb->dbb_sp_rec_mutex);
THREAD_ENTER();
if (error) {
Firebird::system_call_failed::raise("mutex_lock", error);
}
if (request)
{
THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex);
return;
}
#endif /* SUPERSERVER */
compile_in_progress = true;
// Allocate statement memory pool
JrdMemoryPool* new_pool = JrdMemoryPool::createPool();
// Trigger request is not compiled yet. Lets do it now
USHORT par_flags = (USHORT)
(flags & TRG_ignore_perm) ? csb_ignore_perm : 0;
if (type & 1)
par_flags |= csb_pre_trigger;
else
par_flags |= csb_post_trigger;
CompilerScratch* csb = NULL;
try {
Jrd::ContextPoolHolder context(tdbb, new_pool);
csb = CompilerScratch::newCsb(*tdbb->getDefaultPool(), 5);
csb->csb_g_flags |= par_flags;
if (!dbg_blob_id.isEmpty())
DBG_parse_debug_info(tdbb, &dbg_blob_id, csb->csb_dbg_info);
PAR_blr(tdbb, relation, blr.begin(), NULL, &csb, &request, (relation ? true : false),
par_flags);
delete csb;
}
catch (const Firebird::Exception&) {
compile_in_progress = false;
if (csb) {
delete csb;
csb = NULL;
}
if (request) {
CMP_release(tdbb, request);
request = NULL;
}
else {
JrdMemoryPool::deletePool(new_pool);
}
#ifdef SUPERSERVER
THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex);
#endif
throw;
}
request->req_trg_name = name;
if (sys_trigger)
{
request->req_flags |= req_sys_trigger;
}
if (flags & TRG_ignore_perm)
{
request->req_flags |= req_ignore_perm;
}
compile_in_progress = false;
#ifdef SUPERSERVER
THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex);
#endif
}
}
void Jrd::Trigger::release(thread_db* tdbb)
{
if (blr.getCount() == 0 //sys_trigger
|| !request || CMP_clone_is_active(request))
{
return; // FALSE;
}
CMP_release(tdbb, request);
request = NULL;
return; // TRUE;
}
// Option block for database parameter block
class DatabaseOptions
{
public:
USHORT dpb_wal_action;
SLONG dpb_sweep_interval;
ULONG dpb_page_buffers;
bool dpb_set_page_buffers;
ULONG dpb_buffers;
USHORT dpb_debug;
USHORT dpb_verify;
USHORT dpb_sweep;
USHORT dpb_trace;
USHORT dpb_disable;
USHORT dpb_dbkey_scope;
USHORT dpb_page_size;
bool dpb_activate_shadow;
bool dpb_delete_shadow;
USHORT dpb_no_garbage;
#ifdef REPLAY_OSRI_API_CALLS_SUBSYSTEM
bool dpb_quit_log;
#endif
USHORT dpb_shutdown;
SSHORT dpb_shutdown_delay;
USHORT dpb_online;
SSHORT dpb_force_write;
UCHAR dpb_set_force_write;
UCHAR dpb_no_reserve;
UCHAR dpb_set_no_reserve;
SSHORT dpb_interp;
USHORT dpb_single_user;
bool dpb_overwrite;
bool dpb_sec_attach;
bool dpb_disable_wal;
bool dpb_gsec_attach;
SLONG dpb_connect_timeout;
SLONG dpb_dummy_packet_interval;
bool dpb_db_readonly;
bool dpb_set_db_readonly;
bool dpb_gfix_attach;
bool dpb_gstat_attach;
USHORT dpb_sql_dialect;
USHORT dpb_set_db_sql_dialect;
SLONG dpb_remote_pid;
bool dpb_no_db_triggers;
bool dpb_gbak_attach;
// here begin compound objects
// for constructor to work properly dpb_sys_user_name
// MUST be FIRST
Firebird::string dpb_sys_user_name;
Firebird::string dpb_user_name;
Firebird::string dpb_password;
Firebird::string dpb_password_enc;
Firebird::string dpb_role_name;
Firebird::string dpb_journal;
Firebird::string dpb_key;
#ifdef REPLAY_OSRI_API_CALLS_SUBSYSTEM
Firebird::PathName dpb_log;
#endif
Firebird::PathName dpb_lc_messages;
Firebird::string dpb_lc_ctype;
Firebird::PathName dpb_working_directory;
Firebird::string dpb_set_db_charset;
Firebird::string dpb_network_protocol;
Firebird::string dpb_remote_address;
#ifdef TRUSTED_AUTH
Firebird::string dpb_trusted_login;
#endif
Firebird::PathName dpb_remote_process;
public:
DatabaseOptions()
{
memset(this, 0,
reinterpret_cast<char*>(&this->dpb_sys_user_name) -
reinterpret_cast<char*>(this));
}
void get(const UCHAR*, USHORT);
};
#ifndef SUPERSERVER
static int blocking_ast_dsql_cache(void*);
#endif
static blb* check_blob(thread_db*, ISC_STATUS*, blb**);
static ISC_STATUS check_database(thread_db*, Attachment*, ISC_STATUS*);
static ISC_STATUS check_transaction(thread_db*, jrd_tra*, ISC_STATUS*);
static void cleanup(void*);
static ISC_STATUS commit(ISC_STATUS*, jrd_tra**, const bool);
static ISC_STATUS compile_request(ISC_STATUS*, Attachment**, jrd_req**, SSHORT, const SCHAR*, USHORT, const char*, USHORT, const UCHAR*);
static bool drop_files(const jrd_file*);
static ISC_STATUS error(ISC_STATUS*, const Firebird::Exception& ex);
static ISC_STATUS error(ISC_STATUS*);
static void find_intl_charset(thread_db*, Attachment*, const DatabaseOptions*);
static jrd_tra* find_transaction(thread_db*, jrd_tra*, ISC_STATUS);
static void init_database_locks(thread_db*, Database*);
static ISC_STATUS handle_error(ISC_STATUS*, ISC_STATUS, thread_db*);
static void run_commit_triggers(thread_db* tdbb, jrd_tra* transaction);
static void verify_request_synchronization(jrd_req*& request, SSHORT level);
static unsigned int purge_transactions(thread_db*, Attachment*, const bool, const ULONG);
namespace {
enum vdnResult {vdnFail, vdnOk, vdnSecurity};
}
static vdnResult verify_database_name(const Firebird::PathName&, ISC_STATUS*);
static ISC_STATUS unwindAttach(const Firebird::Exception& ex,
ISC_STATUS* userStatus,
thread_db* tdbb,
Attachment* attachment,
Database* dbb);
#if defined (WIN_NT)
#ifdef SERVER_SHUTDOWN
static void ExtractDriveLetter(const TEXT*, ULONG*);
#else // SERVER_SHUTDOWN
static void setup_NT_handlers(void);
static BOOLEAN handler_NT(SSHORT);
#endif // SERVER_SHUTDOWN
#endif // WIN_NT
static Database* init(thread_db*, ISC_STATUS*, const Firebird::PathName&, bool);
static ISC_STATUS prepare(thread_db*, jrd_tra*, ISC_STATUS*, USHORT, const UCHAR*);
static void release_attachment(Attachment*);
static void detachLocksFromAttachment(Attachment*);
static ISC_STATUS return_success(thread_db*);
static bool rollback(thread_db*, jrd_tra*, ISC_STATUS*, const bool);
static void shutdown_database(Database*, const bool);
static void strip_quotes(Firebird::string&);
static void purge_attachment(thread_db*, ISC_STATUS*, Attachment*, const bool);
static void getUserInfo(UserId&, const DatabaseOptions&);
static bool initialized = false;
static Database* databases = NULL;
static ULONG JRD_cache_default;
#ifdef GOVERNOR
const int ATTACHMENTS_PER_USER = 1;
static ULONG JRD_max_users = 0;
static ULONG num_attached = 0;
#endif // GOVERNOR
#if !defined(REQUESTER)
int debug;
#endif // !REQUESTER
//____________________________________________________________
//
// check whether we need to perform an autocommit;
// do it here to prevent committing every record update
// in a statement
//
static void check_autocommit(jrd_req* request, thread_db* tdbb)
{
// dimitr: we should ignore autocommit for requests
// created by EXECUTE STATEMENT
if (request->req_transaction->tra_callback_count > 0)
return;
if (request->req_transaction->tra_flags & TRA_perform_autocommit)
{
if (!(tdbb->getAttachment()->att_flags & ATT_no_db_triggers) &&
!(request->req_transaction->tra_flags & TRA_prepared))
{
// run ON TRANSACTION COMMIT triggers
run_commit_triggers(tdbb, request->req_transaction);
}
request->req_transaction->tra_flags &= ~TRA_perform_autocommit;
TRA_commit(tdbb, request->req_transaction, true);
}
}
inline static void api_entry_point_init(ISC_STATUS* user_status)
{
user_status[0] = isc_arg_gds;
user_status[1] = FB_SUCCESS;
user_status[2] = isc_arg_end;
}
inline static thread_db* JRD_MAIN_set_thread_data(thread_db& thd_context)
{
thread_db* tdbb = &thd_context;
JRD_set_context(tdbb);
return tdbb;
}
#define CHECK_HANDLE(blk, type, error) \
if (!blk || MemoryPool::blk_type(blk) != type) \
return handle_error (user_status, error, tdbb)
#define NULL_CHECK(ptr, code) \
if (*ptr) return handle_error (user_status, code, tdbb)
const int SWEEP_INTERVAL = 20000;
const char DBL_QUOTE = '\042';
const char SINGLE_QUOTE = '\'';
const int BUFFER_LENGTH128 = 128;
bool invalid_client_SQL_dialect = false;
#define GDS_ATTACH_DATABASE jrd8_attach_database
#define GDS_BLOB_INFO jrd8_blob_info
#define GDS_CANCEL_BLOB jrd8_cancel_blob
#define GDS_CANCEL_EVENTS jrd8_cancel_events
#define GDS_CANCEL_OPERATION jrd8_cancel_operation
#define GDS_CLOSE_BLOB jrd8_close_blob
#define GDS_COMMIT jrd8_commit_transaction
#define GDS_COMMIT_RETAINING jrd8_commit_retaining
#define GDS_COMPILE jrd8_compile_request
#define GDS_CREATE_BLOB2 jrd8_create_blob2
#define GDS_CREATE_DATABASE jrd8_create_database
#define GDS_DATABASE_INFO jrd8_database_info
#define GDS_DDL jrd8_ddl
#define GDS_DETACH jrd8_detach_database
#define GDS_DROP_DATABASE jrd8_drop_database
#define GDS_INTL_FUNCTION jrd8_intl_function
#define GDS_DSQL_CACHE jrd8_dsql_cache
#define GDS_INTERNAL_COMPILE jrd8_internal_compile_request
#define GDS_GET_SEGMENT jrd8_get_segment
#define GDS_GET_SLICE jrd8_get_slice
#define GDS_OPEN_BLOB2 jrd8_open_blob2
#define GDS_PREPARE jrd8_prepare_transaction
#define GDS_PUT_SEGMENT jrd8_put_segment
#define GDS_PUT_SLICE jrd8_put_slice
#define GDS_QUE_EVENTS jrd8_que_events
#define GDS_RECONNECT jrd8_reconnect_transaction
#define GDS_RECEIVE jrd8_receive
#define GDS_RELEASE_REQUEST jrd8_release_request
#define GDS_REQUEST_INFO jrd8_request_info
#define GDS_ROLLBACK jrd8_rollback_transaction
#define GDS_ROLLBACK_RETAINING jrd8_rollback_retaining
#define GDS_SEEK_BLOB jrd8_seek_blob
#define GDS_SEND jrd8_send
#define GDS_SERVICE_ATTACH jrd8_service_attach
#define GDS_SERVICE_DETACH jrd8_service_detach
#define GDS_SERVICE_QUERY jrd8_service_query
#define GDS_SERVICE_START jrd8_service_start
#define GDS_START_AND_SEND jrd8_start_and_send
#define GDS_START jrd8_start_request
#define GDS_START_MULTIPLE jrd8_start_multiple
#define GDS_START_TRANSACTION jrd8_start_transaction
#define GDS_TRANSACT_REQUEST jrd8_transact_request
#define GDS_TRANSACTION_INFO jrd8_transaction_info
#define GDS_UNWIND jrd8_unwind_request
// External hook definitions
/* dimitr: just uncomment the following line to use this feature.
Requires support from the PIO modules. Only Win32 is 100% ready
for this so far. Note that the database encryption code in the
PIO layer seems to be incompatible with the SUPERSERVER_V2 code.
2003.02.09 */
//#define ISC_DATABASE_ENCRYPTION
static const char* CRYPT_IMAGE = "fbcrypt";
static const char* ENCRYPT = "encrypt";
static const char* DECRYPT = "decrypt";
namespace {
TLS_DECLARE(bool, thread_security_disabled);
}
void JRD_thread_security_disable(bool disable)
{
/**************************************
*
* J R D _ t h r e a d _ s e c u r i t y _ d i s a b l e
*
**************************************
*
* Functional description
* Disable database attache security for this thread for the purposes of database attach
*
**************************************/
TLS_SET(thread_security_disabled, disable);
}
bool JRD_get_thread_security_disabled()
{
/**************************************
*
* J R D _ g e t _ t h r e a d _ s e c u r i t y _ d i s a b l e d
*
**************************************
*
* Functional description
* Don't run internal handles thru the security gauntlet.
*
**************************************/
return TLS_GET(thread_security_disabled);
}
void JRD_print_pools(const char* filename)
{
FILE* out = fopen(filename, "w");
if (out)
{
ALL_print_memory_pool_info(out, databases);
fclose(out);
}
}
ISC_STATUS GDS_ATTACH_DATABASE(ISC_STATUS* user_status,
SSHORT _file_length,
const TEXT* _file_name,
Attachment** handle,
SSHORT dpb_length,
const UCHAR* dpb,
const TEXT* _expanded_filename)
{
/**************************************
*
* g d s _ $ a t t a c h _ d a t a b a s e
*
**************************************
*
* Functional description
* Attach a moldy, grungy, old database
* sullied by user data.
*
**************************************/
api_entry_point_init(user_status);
if (*handle) {
return handle_error(user_status, isc_bad_db_handle, 0);
}
Firebird::PathName file_name(_file_name,
_file_length ? _file_length : strlen(_file_name));
Firebird::PathName expanded_name(file_name);
// Resolve given alias name
const bool is_alias = ResolveDatabaseAlias(expanded_name, expanded_name);
if (is_alias)
{
ISC_expand_filename(expanded_name, false);
}
else
{
expanded_name = _expanded_filename;
}
thread_db thd_context;
thread_db* tdbb = JRD_MAIN_set_thread_data(thd_context);
// Check database against conf file.
const vdnResult vdn = verify_database_name(expanded_name, user_status);
if (!is_alias && vdn == vdnFail)
{
JRD_restore_context();
return user_status[1];
}
else
user_status[0] = 0; // Clear status vector
// Unless we're already attached, do some initialization
Database* dbb = init(tdbb, user_status, expanded_name, true);
if (!dbb) {
V4_JRD_MUTEX_UNLOCK(databases_mutex);
JRD_SS_MUTEX_UNLOCK;
JRD_restore_context();
return user_status[1];
}
// use database context pool
Jrd::ContextPoolHolder context(tdbb, dbb->dbb_permanent);
dbb->dbb_flags |= DBB_being_opened;
V4_JRD_MUTEX_LOCK(dbb->dbb_mutexes + DBB_MUTX_init_fini);
V4_JRD_MUTEX_UNLOCK(databases_mutex);
tdbb->setDatabase(dbb);
// Initialize special error handling
ISC_STATUS* status;
tdbb->tdbb_status_vector = status = user_status;
Attachment* attachment = NULL;
tdbb->setAttachment(NULL);
tdbb->setRequest(NULL);
tdbb->setTransaction(NULL);
// Count active thread in database
++dbb->dbb_use_count;
bool initing_security = false;
/* The following line seems to fix a bug that appears on
SCO EVEREST. It probably has to do with the fact that
the error handler below used to contain the first reference to
variable transaction, which is actually initialized a few lines
below that. */
attachment = *(&attachment);
try {
// Process database parameter block
DatabaseOptions options;
options.get(dpb, dpb_length);
// First check for correct credentials supplied
UserId userId;
getUserInfo(userId, options);
#ifndef NO_NFS
// Don't check nfs if single user
if (!options.dpb_single_user)
#endif
{
// Check to see if the database is truly local or if it just looks
// that way
if (ISC_check_if_remote(expanded_name, true)) {
ERR_post(isc_unavailable, isc_arg_end);
}
}
/* If database to be opened is SecurityDatabase, then only
gsec or SecurityDatabase may open it. This protects from use
of old gsec to write wrong password hashes into it. */
if (vdn == vdnSecurity && !options.dpb_gsec_attach && !options.dpb_sec_attach)
{
ERR_post(isc_no_priv,
isc_arg_string, "direct",
isc_arg_string, "security database",
isc_arg_string,
ERR_string(file_name),
isc_arg_end);
}
// Worry about encryption key
if (dbb->dbb_decrypt) {
if (dbb->dbb_filename.hasData() &&
(dbb->dbb_encrypt_key.hasData() || options.dpb_key.hasData()))
{
if ((dbb->dbb_encrypt_key.hasData() && options.dpb_key.isEmpty()) ||
(dbb->dbb_encrypt_key.empty() && options.dpb_key.hasData()) ||
(dbb->dbb_encrypt_key != options.dpb_key))
{
ERR_post(isc_no_priv,
isc_arg_string, "encryption",
isc_arg_string, "database",
isc_arg_string,
ERR_string(file_name),
isc_arg_end);
}
}
else if (options.dpb_key.hasData())
{
dbb->dbb_encrypt_key = options.dpb_key;
}
}
tdbb->setAttachment((attachment = FB_NEW(*dbb->dbb_permanent) Attachment(dbb)));
attachment->att_filename = is_alias ? file_name : expanded_name;
attachment->att_network_protocol = options.dpb_network_protocol;
attachment->att_remote_address = options.dpb_remote_address;
attachment->att_remote_pid = options.dpb_remote_pid;
attachment->att_remote_process = options.dpb_remote_process;
attachment->att_next = dbb->dbb_attachments;
dbb->dbb_attachments = attachment;
dbb->dbb_flags &= ~DBB_being_opened;
dbb->dbb_sys_trans->tra_attachment = attachment;
tdbb->tdbb_quantum = (ThreadPriorityScheduler::boosted() ?
Config::getPriorityBoost() : 1) * QUANTUM;
tdbb->setRequest(NULL);
tdbb->setTransaction(NULL);
tdbb->tdbb_flags = 0;
attachment->att_charset = options.dpb_interp;
if (options.dpb_lc_messages.hasData()) {
attachment->att_lc_messages = options.dpb_lc_messages;
}
if (options.dpb_no_garbage)
attachment->att_flags |= ATT_no_cleanup;
if (options.dpb_gbak_attach)
attachment->att_flags |= ATT_gbak_attachment;
if (options.dpb_gstat_attach)
attachment->att_flags |= ATT_gstat_attachment;
if (options.dpb_gfix_attach)
attachment->att_flags |= ATT_gfix_attachment;
if (options.dpb_working_directory.hasData()) {
attachment->att_working_directory = options.dpb_working_directory;
}
// If we're a not a secondary attachment, initialize some stuff
bool first = false;
LCK_init(tdbb, LCK_OWNER_attachment); // For the attachment
attachment->att_flags |= ATT_lck_init_done;
if (dbb->dbb_filename.empty())
{
first = true;
dbb->dbb_filename = expanded_name;
// NS: Use alias as database ID only if accessing database using file name is not possible.
//
// This way we:
// 1. Ensure uniqueness of ID even in presence of multiple processes
// 2. Make sure that ID value can be used to connect back to database
//
if (is_alias && vdn == vdnFail)
dbb->dbb_database_name = file_name;
else
dbb->dbb_database_name = expanded_name;
// Extra LCK_init() done to keep the lock table until the
// database is shutdown() after the last detach.
LCK_init(tdbb, LCK_OWNER_database);
dbb->dbb_flags |= DBB_lck_init_done;
INI_init();
PageSpace* pageSpace = dbb->dbb_page_manager.findPageSpace(DB_PAGE_SPACE);
pageSpace->file =
PIO_open(dbb, expanded_name, options.dpb_trace != 0, file_name, false);
SHUT_init(dbb);
PAG_header_init();
INI_init2();
PAG_init();
if (options.dpb_set_page_buffers) {
#ifdef SUPERSERVER
// Here we do not let anyone except SYSDBA (like DBO) to change dbb_page_buffers,
// cause other flags is UserId can be set only when DB is opened.
// No idea how to test for other cases before init is complete.
if (userId.locksmith())
#endif
dbb->dbb_page_buffers = options.dpb_page_buffers;
}
CCH_init(tdbb, options.dpb_buffers);
// Initialize locks
init_database_locks(tdbb, dbb);
// Initialize backup difference subsystem. This must be done before WAL and shadowing
// is enabled because nbackup it is a lower level subsystem
dbb->dbb_backup_manager = FB_NEW(*dbb->dbb_permanent) BackupManager(tdbb, dbb, nbak_state_unknown);
PAG_init2(0);
PAG_header(false);
#ifdef REPLAY_OSRI_API_CALLS_SUBSYSTEM
LOG_init(expanded_name, length_expanded);
#endif
// initialize shadowing as soon as the database is ready for it
// but before any real work is done
SDW_init(options.dpb_activate_shadow,
options.dpb_delete_shadow);
/* dimitr: disabled due to unreliable behaviour of minor ODS upgrades
a) in the case of any failure it's impossible to attach the database
b) there's no way to handle failures properly, because upgrade is
being made in the context of system transaction which doesn't use
the backout logic
INI_update_database();
*/
}
// Attachments to a ReadOnly database need NOT do garbage collection
if (dbb->dbb_flags & DBB_read_only) {
attachment->att_flags |= ATT_no_cleanup;
}
if (options.dpb_disable_wal) {
ERR_post(isc_lock_timeout, isc_arg_gds, isc_obj_in_use,
isc_arg_string,
ERR_string(file_name),
isc_arg_end);
}
if (options.dpb_buffers && !dbb->dbb_page_buffers) {
CCH_expand(tdbb, options.dpb_buffers);
}
if (!options.dpb_verify && CCH_exclusive(tdbb, LCK_PW, LCK_NO_WAIT))
{
TRA_cleanup(tdbb);
}
V4_JRD_MUTEX_UNLOCK(dbb->dbb_mutexes + DBB_MUTX_init_fini);
initing_security = true;
if (invalid_client_SQL_dialect)
{
ERR_post(isc_inv_client_dialect_specified, isc_arg_number,
options.dpb_sql_dialect,
isc_arg_gds, isc_valid_client_dialects,
isc_arg_string, "1, 2 or 3", isc_arg_end);
}
invalid_client_SQL_dialect = false;
if (userId.usr_sql_role_name.hasData())
{
switch (options.dpb_sql_dialect)
{
case 0:
{
// V6 Client --> V6 Server, dummy client SQL dialect 0 was passed
// It means that client SQL dialect was not set by user
// and takes DB SQL dialect as client SQL dialect
if (ENCODE_ODS(dbb->dbb_ods_version, dbb->dbb_minor_original)
>= ODS_10_0)
{
if (dbb->dbb_flags & DBB_DB_SQL_dialect_3) {
// DB created in IB V6.0 by client SQL dialect 3
options.dpb_sql_dialect = SQL_DIALECT_V6;
}
else {
// old DB was gbaked in IB V6.0
options.dpb_sql_dialect = SQL_DIALECT_V5;
}
}
else {
options.dpb_sql_dialect = SQL_DIALECT_V5;
}
}