------------------------------------------------------------------------- psi_include Read file to include. Copy its content into an iobuffer. This is the function doing blocking calls and called by the plugin's threads Input: data continuation for the current transaction Output : data->psi_buffer contains the file content data->psi_success 0 if include failed, 1 if
| 455 | 1 if success |
| 456 | -------------------------------------------------------------------------*/ |
| 457 | static int |
| 458 | psi_include(TSCont contp, void *edata ATS_UNUSED) |
| 459 | { |
| 460 | #define BUFFER_SIZE 1024 |
| 461 | ContData *data; |
| 462 | TSFile filep; |
| 463 | char inc_file[PSI_PATH_MAX_SIZE + PSI_FILENAME_MAX_SIZE]; |
| 464 | |
| 465 | /* We manipulate plugin continuation data from a separate thread. |
| 466 | Grab mutex to avoid concurrent access */ |
| 467 | TSMutexLock(TSContMutexGet(contp)); |
| 468 | data = contDataGet(contp); |
| 469 | TSAssert(data->magic == MAGIC_ALIVE); |
| 470 | |
| 471 | if (!data->psi_buffer) { |
| 472 | data->psi_buffer = TSIOBufferCreate(); |
| 473 | data->psi_reader = TSIOBufferReaderAlloc(data->psi_buffer); |
| 474 | } |
| 475 | |
| 476 | /* For security reason, we do not allow to include files that are |
| 477 | not in the directory <plugin_path>/include. |
| 478 | Also include file cannot contain any path. */ |
| 479 | snprintf(inc_file, sizeof(inc_file), "%s/%s", psi_directory, _basename(data->psi_filename)); |
| 480 | |
| 481 | /* Read the include file and copy content into iobuffer */ |
| 482 | if ((filep = TSfopen(inc_file, "r")) != nullptr) { |
| 483 | Dbg(dbg_ctl, "Reading include file %s", inc_file); |
| 484 | |
| 485 | char buf[BUFFER_SIZE]; |
| 486 | while (TSfgets(filep, buf, BUFFER_SIZE) != nullptr) { |
| 487 | int64_t len, ndone, ntodo; |
| 488 | |
| 489 | len = strlen(buf); |
| 490 | ndone = 0; |
| 491 | ntodo = len; |
| 492 | while (ntodo > 0) { |
| 493 | /* TSIOBufferStart allocates more blocks if required */ |
| 494 | TSIOBufferBlock block = TSIOBufferStart(data->psi_buffer); |
| 495 | int64_t avail; |
| 496 | char *ptr_block = TSIOBufferBlockWriteStart(block, &avail); |
| 497 | int64_t towrite = MIN(ntodo, avail); |
| 498 | |
| 499 | memcpy(ptr_block, buf + ndone, towrite); |
| 500 | TSIOBufferProduce(data->psi_buffer, towrite); |
| 501 | ntodo -= towrite; |
| 502 | ndone += towrite; |
| 503 | } |
| 504 | } |
| 505 | TSfclose(filep); |
| 506 | data->psi_success = 1; |
| 507 | if (ts_log) { |
| 508 | TSTextLogObjectWrite(ts_log, "Successfully included file: %s", inc_file); |
| 509 | } |
| 510 | } else { |
| 511 | data->psi_success = 0; |
| 512 | if (ts_log) { |
| 513 | TSTextLogObjectWrite(ts_log, "Failed to include file: %s", inc_file); |
| 514 | } |
nothing calls this directly
no test coverage detected