forked from FirebirdSQL/php-firebird
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbird_inspection.c
More file actions
executable file
·502 lines (426 loc) · 12.5 KB
/
fbird_inspection.c
File metadata and controls
executable file
·502 lines (426 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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
/* SPDX-License-Identifier: PHP-3.01
* SPDX-FileCopyrightText: The PHP Group and contributors (see CREDITS) */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_firebird.h"
#include "php_fbird_includes.h"
#include "firebird_utils.h"
/* =============================================================================
* OO API Only Helper Functions for Inspection
*
* These helpers use fbs_* functions for SQL execution via the modern OO API.
* All connections MUST have fbc_connection (OO API is the only supported path).
* ============================================================================= */
/**
* Execute a DELETE statement with one BIGINT parameter using OO API.
* Used for killing attachments.
*
* @param link Database connection
* @param trans Transaction
* @param attachment_id The attachment ID to delete
* @return SUCCESS or FAILURE
*/
static int _fbird_exec_kill(fbird_db_link *link, fbird_transaction *trans, ISC_INT64 attachment_id)
{
static const char *sql = "DELETE FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = ?";
void *stmt = NULL;
void *attachment = NULL;
void *transaction = NULL;
int result = FAILURE;
/* OO API Only: Require fbc_connection */
if (!link->fbc_connection) {
_php_fbird_module_error("fbird_kill_attachment requires OO API connection (fbc_connection required)");
return FAILURE;
}
if (!trans->fbt_transaction) {
_php_fbird_module_error("fbird_kill_attachment requires OO API transaction (fbt_transaction required)");
return FAILURE;
}
/* Get OO API handles */
attachment = fbc_get_attachment(link->fbc_connection);
if (!attachment) {
return FAILURE;
}
transaction = fbt_get_handle(trans->fbt_transaction);
if (!transaction) {
return FAILURE;
}
/* Prepare statement */
stmt = fbs_prepare(
IBG(master_instance),
attachment,
transaction,
sql,
0, /* null-terminated */
SQL_DIALECT_V6,
IB_STATUS
);
if (!stmt) {
_php_fbird_error();
return FAILURE;
}
/* Get input metadata for parameter binding */
void *in_metadata = fbs_get_input_metadata(IBG(master_instance), stmt, IB_STATUS);
if (!in_metadata) {
_php_fbird_error();
fbs_free(stmt, IB_STATUS);
return FAILURE;
}
/* Build input message buffer
* For a single BIGINT parameter:
* - 2 bytes null indicator (short)
* - 8 bytes BIGINT value (ISC_INT64)
* Aligned to 8 bytes
*/
unsigned char in_msg[16];
memset(in_msg, 0, sizeof(in_msg));
/* Set null indicator (0 = not null) at offset 0 */
*(short *)&in_msg[0] = 0;
/* Set value at offset 8 (aligned) */
*(ISC_INT64 *)&in_msg[8] = attachment_id;
/* Execute the statement */
if (!fbs_execute(
IBG(master_instance),
stmt,
transaction,
in_msg,
in_metadata,
NULL, /* no output */
NULL,
IB_STATUS
)) {
_php_fbird_error();
fbs_free(stmt, IB_STATUS);
return FAILURE;
}
result = SUCCESS;
fbs_free(stmt, IB_STATUS);
return result;
}
/**
* Execute DROP TABLE statement using OO API.
*
* @param link Database connection
* @param trans Transaction
* @param table_name Name of table to drop
* @return SUCCESS or FAILURE
*/
static int _fbird_drop_table(fbird_db_link *link, fbird_transaction *trans, const char *table_name)
{
char *drop_sql = NULL;
void *stmt = NULL;
void *attachment = NULL;
void *transaction = NULL;
int result = FAILURE;
/* OO API Only: Require fbc_connection */
if (!link->fbc_connection) {
_php_fbird_module_error("fbird_drop_table_force requires OO API connection (fbc_connection required)");
return FAILURE;
}
if (!trans->fbt_transaction) {
_php_fbird_module_error("fbird_drop_table_force requires OO API transaction (fbt_transaction required)");
return FAILURE;
}
/* Get OO API handles */
attachment = fbc_get_attachment(link->fbc_connection);
if (!attachment) {
return FAILURE;
}
transaction = fbt_get_handle(trans->fbt_transaction);
if (!transaction) {
return FAILURE;
}
/* Build DROP statement */
spprintf(&drop_sql, 0, "DROP TABLE %s", table_name);
/* Prepare statement */
stmt = fbs_prepare(
IBG(master_instance),
attachment,
transaction,
drop_sql,
0, /* null-terminated */
SQL_DIALECT_V6,
IB_STATUS
);
if (!stmt) {
_php_fbird_error();
efree(drop_sql);
return FAILURE;
}
/* Execute the statement (no parameters) */
if (!fbs_execute(
IBG(master_instance),
stmt,
transaction,
NULL, /* no input */
NULL,
NULL, /* no output */
NULL,
IB_STATUS
)) {
_php_fbird_error();
fbs_free(stmt, IB_STATUS);
efree(drop_sql);
return FAILURE;
}
/* Free statement before commit */
fbs_free(stmt, IB_STATUS);
efree(drop_sql);
/* Commit for DDL visibility
* fbt_commit returns 0 on success, -1 on error. */
if (fbt_commit(trans->fbt_transaction, IB_STATUS) != 0) {
_php_fbird_error();
return FAILURE;
}
/* CRITICAL: Clean up the wrapper after commit.
* fbt_commit() does NOT free the wrapper (verified in firebird_utils.cpp).
* The PHP resource destructor (_php_fbird_free_trans) only calls fbt_rollback()
* for uncommitted transactions (when fbt_transaction != NULL).
* For committed transactions, the destructor skips rollback, leaving the wrapper
* orphaned. We MUST call fbt_free() here to prevent memory leak.
*
* Setting trans->fbt_transaction = NULL tells the destructor the transaction
* is already handled, preventing double-free attempts. */
fbt_free(trans->fbt_transaction);
trans->fbt_transaction = NULL;
/* Remove this transaction from all connection tr_lists to prevent
* use-after-free during PHP shutdown. The destructor tries to traverse
* db_link[i]->tr_list, but if cleanup order is unexpected, db_link[i]
* could be stale. Setting db_link[i] = NULL tells the destructor to skip.
* This mirrors what _php_fbird_commit_link() does when closing connections.
*
* IMPORTANT: The first node in tr_list is reserved for the default transaction
* (see _php_fbird_def_trans). We must NOT efree() that first node - just clear
* its trans pointer. Only non-first nodes should be unlinked and freed. */
for (unsigned short i = 0; i < trans->link_cnt; ++i) {
if (trans->db_link[i] != NULL) {
fbird_tr_list **l;
for (l = &trans->db_link[i]->tr_list; *l != NULL; l = &(*l)->next) {
if ((*l)->trans == trans) {
/* Is this the first node (default transaction slot)? */
if (*l == trans->db_link[i]->tr_list) {
/* Don't free the first node - it's the default trans slot.
* Just clear the trans pointer so it can be reused. */
(*l)->trans = NULL;
} else {
/* Non-first node: unlink and free */
fbird_tr_list *p = *l;
*l = p->next;
efree(p);
}
break;
}
}
trans->db_link[i] = NULL;
}
}
return SUCCESS;
}
PHP_FUNCTION(fbird_kill_attachment)
{
zval *link_arg;
zend_long attachment_id;
fbird_db_link *link;
fbird_transaction *trans;
RESET_ERRMSG;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &link_arg, &attachment_id) == FAILURE) {
return;
}
PHP_FBIRD_LINK_TRANS(link_arg, link, trans);
if (_fbird_exec_kill(link, trans, (ISC_INT64) attachment_id) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(fbird_list_table_blockers)
{
zval *link_arg;
char *table_name;
size_t table_name_len;
fbird_db_link *link;
fbird_transaction *trans;
void *stmt = NULL;
void *attachment = NULL;
void *transaction = NULL;
void *in_metadata = NULL;
void *out_metadata = NULL;
RESET_ERRMSG;
/* SQL to find attachments using the table in statements.
* CONTAINING is Firebird's BLOB-aware, case-insensitive substring search. */
static const char *sql =
"SELECT DISTINCT A.MON$ATTACHMENT_ID, A.MON$USER "
"FROM MON$ATTACHMENTS A "
"JOIN MON$STATEMENTS S ON S.MON$ATTACHMENT_ID = A.MON$ATTACHMENT_ID "
"WHERE A.MON$ATTACHMENT_ID <> CURRENT_CONNECTION "
"AND S.MON$SQL_TEXT CONTAINING ?";
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &link_arg, &table_name, &table_name_len) == FAILURE) {
return;
}
PHP_FBIRD_LINK_TRANS(link_arg, link, trans);
/* OO API Only: Require fbc_connection */
if (!link->fbc_connection) {
_php_fbird_module_error("fbird_list_table_blockers requires OO API connection (fbc_connection required)");
RETURN_FALSE;
}
if (!trans->fbt_transaction) {
_php_fbird_module_error("fbird_list_table_blockers requires OO API transaction (fbt_transaction required)");
RETURN_FALSE;
}
/* Get OO API handles */
attachment = fbc_get_attachment(link->fbc_connection);
if (!attachment) {
RETURN_FALSE;
}
transaction = fbt_get_handle(trans->fbt_transaction);
if (!transaction) {
RETURN_FALSE;
}
/* Prepare statement */
stmt = fbs_prepare(
IBG(master_instance),
attachment,
transaction,
sql,
0, /* null-terminated */
SQL_DIALECT_V6,
IB_STATUS
);
if (!stmt) {
_php_fbird_error();
RETURN_FALSE;
}
/* Get input metadata for parameter binding */
in_metadata = fbs_get_input_metadata(IBG(master_instance), stmt, IB_STATUS);
if (!in_metadata) {
_php_fbird_error();
fbs_free(stmt, IB_STATUS);
RETURN_FALSE;
}
/* Get output metadata for result fetching */
out_metadata = fbs_get_output_metadata(IBG(master_instance), stmt, IB_STATUS);
if (!out_metadata) {
_php_fbird_error();
fbs_free(stmt, IB_STATUS);
RETURN_FALSE;
}
/* Build input message buffer for VARCHAR parameter
* VARCHAR format: 2-byte length + data
* With null indicator at offset determined by metadata
*
* Simple layout for single VARCHAR parameter:
* - bytes 0-1: null indicator (short)
* - bytes 2-3: varchar length (short)
* - bytes 4+: varchar data
*/
size_t in_msg_size = 4 + table_name_len + 4; /* null ind + len + data + padding */
unsigned char *in_msg = emalloc(in_msg_size);
memset(in_msg, 0, in_msg_size);
/* Set null indicator (0 = not null) at offset 0 */
*(short *)&in_msg[0] = 0;
/* Set VARCHAR length at offset 2 */
*(short *)&in_msg[2] = (short)table_name_len;
/* Copy string data at offset 4 */
memcpy(&in_msg[4], table_name, table_name_len);
/* Open cursor for fetching results */
if (!fbs_open_cursor(
IBG(master_instance),
stmt,
transaction,
in_msg,
in_metadata,
0, /* cursor_flags */
IB_STATUS
)) {
_php_fbird_error();
efree(in_msg);
fbs_free(stmt, IB_STATUS);
RETURN_FALSE;
}
efree(in_msg);
/* Prepare output buffer
* Output columns: MON$ATTACHMENT_ID (BIGINT), MON$USER (VARCHAR/CHAR)
*
* Layout:
* - bytes 0-1: null indicator for attachment_id
* - bytes 8-15: attachment_id (BIGINT, aligned to 8)
* - bytes 16-17: null indicator for user
* - bytes 18-19: varchar length for user
* - bytes 20+: user data (up to 255 chars)
*/
unsigned char out_msg[512];
memset(out_msg, 0, sizeof(out_msg));
array_init(return_value);
/* Fetch loop */
while (1) {
memset(out_msg, 0, sizeof(out_msg));
int fetch_result = fbs_fetch(
IBG(master_instance),
stmt,
out_msg,
IB_STATUS
);
if (fetch_result == 0) {
/* EOF - no more rows */
break;
} else if (fetch_result < 0) {
/* Error */
_php_fbird_error();
fbs_close_cursor(stmt, IB_STATUS);
fbs_free(stmt, IB_STATUS);
/* Return partial result */
return;
}
/* Extract attachment_id (BIGINT at offset 8, null indicator at offset 0) */
short null_ind_id = *(short *)&out_msg[0];
ISC_INT64 attachment_id = *(ISC_INT64 *)&out_msg[8];
/* Extract user (VARCHAR at offset 16+, null indicator at offset 16) */
short null_ind_user = *(short *)&out_msg[16];
short user_len = *(short *)&out_msg[18];
char *user_data = (char *)&out_msg[20];
/* Build result row */
zval row;
array_init(&row);
if (null_ind_id == 0) {
add_assoc_long(&row, "attachment_id", (zend_long)attachment_id);
} else {
add_assoc_null(&row, "attachment_id");
}
if (null_ind_user == 0 && user_len > 0) {
/* Trim trailing spaces */
while (user_len > 0 && user_data[user_len - 1] == ' ') {
user_len--;
}
add_assoc_stringl(&row, "user", user_data, user_len);
} else {
add_assoc_null(&row, "user");
}
add_next_index_zval(return_value, &row);
}
fbs_close_cursor(stmt, IB_STATUS);
fbs_free(stmt, IB_STATUS);
}
PHP_FUNCTION(fbird_drop_table_force)
{
zval *link_arg;
char *table_name;
size_t table_name_len;
fbird_db_link *link;
fbird_transaction *trans;
int result;
RESET_ERRMSG;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &link_arg, &table_name, &table_name_len) == FAILURE) {
return;
}
PHP_FBIRD_LINK_TRANS(link_arg, link, trans);
/* Note: Blocker detection via MON$SQL_TEXT requires complex BLOB handling.
* For now, we skip the blocker-killing step and just do the DROP.
* In most cases, DDL will fail cleanly if there are active locks. */
result = _fbird_drop_table(link, trans, table_name);
if (result == SUCCESS) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}