Skip to content

Commit

Permalink
Refactoring (#46)
Browse files Browse the repository at this point in the history
* Use strlcpy instead of strncpy

* Make query variable constant

* Use palloc_object instead of palloc
  • Loading branch information
artemgavrilov authored Mar 6, 2025
1 parent ad69dd0 commit 0da4ec5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
41 changes: 18 additions & 23 deletions percona_pg_telemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pt_shmem_init(void)
uint64 system_id = GetSystemIdentifier();

/* Set paths */
strncpy(ptss->telemetry_path, t_folder, MAXPGPATH);
strlcpy(ptss->telemetry_path, t_folder, MAXPGPATH);
pg_snprintf(ptss->dbtemp_filepath, MAXPGPATH, "%s/%lu.temp", ptss->telemetry_path, system_id);

/*
Expand Down Expand Up @@ -582,14 +582,11 @@ server_uptime(void)
static void
write_pg_settings(void)
{
SPITupleTable *tuptable;
int spi_result;
char *query = "SELECT name, unit, setting FROM pg_settings where vartype != 'string'";
const char *const query = "SELECT name, unit, setting FROM pg_settings where vartype != 'string'";
char buf[4096] = {0};
size_t buf_size = sizeof(buf);
FILE *fp;
int flags;


/* Open file in append mode. */
fp = open_telemetry_file(ptss->dbtemp_filepath, "a+");
Expand All @@ -602,10 +599,7 @@ write_pg_settings(void)
StartTransactionCommand();

/* Initialize SPI */
if (SPI_connect() != SPI_OK_CONNECT)
{
ereport(ERROR, (errmsg("Failed to connect to SPI")));
}
SPI_connect();

PushActiveSnapshot(GetTransactionSnapshot());

Expand All @@ -620,7 +614,8 @@ write_pg_settings(void)
/* Process the result */
if (SPI_processed > 0)
{
tuptable = SPI_tuptable;
int flags;
SPITupleTable *tuptable = SPI_tuptable;

for (int row_count = 0; row_count < SPI_processed; row_count++)
{
Expand Down Expand Up @@ -678,7 +673,6 @@ get_database_list(void)
Relation rel;
TableScanDesc scan;
HeapTuple tup;
MemoryContext oldcxt;
ScanKeyData key;

/* Start a transaction to access pg_database */
Expand All @@ -697,6 +691,7 @@ get_database_list(void)

while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{
MemoryContext oldcxt;
PTDatabaseInfo *dbinfo;
int64 datsize;
Form_pg_database pgdatabase = (Form_pg_database) GETSTRUCT(tup);
Expand All @@ -705,11 +700,11 @@ get_database_list(void)

/* Switch to our memory context instead of the transaction one */
oldcxt = MemoryContextSwitchTo(pt_cxt);
dbinfo = (PTDatabaseInfo *) palloc(sizeof(PTDatabaseInfo));
dbinfo = palloc_object(PTDatabaseInfo);

/* Fill in the structure */
dbinfo->datid = pgdatabase->oid;
strncpy(dbinfo->datname, NameStr(pgdatabase->datname), sizeof(dbinfo->datname));
strlcpy(dbinfo->datname, NameStr(pgdatabase->datname), sizeof(dbinfo->datname));
dbinfo->datsize = datsize;

/* Add to the list */
Expand Down Expand Up @@ -739,7 +734,6 @@ get_extensions_list(PTDatabaseInfo *dbinfo, MemoryContext cxt)
Relation rel;
TableScanDesc scan;
HeapTuple tup;
MemoryContext oldcxt;

Assert(dbinfo);

Expand All @@ -752,16 +746,17 @@ get_extensions_list(PTDatabaseInfo *dbinfo, MemoryContext cxt)

while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{
MemoryContext oldcxt;
PTExtensionInfo *extinfo;
Form_pg_extension extform = (Form_pg_extension) GETSTRUCT(tup);

/* Switch to the given memory context */
oldcxt = MemoryContextSwitchTo(cxt);
extinfo = (PTExtensionInfo *) palloc(sizeof(PTExtensionInfo));
extinfo = palloc_object(PTExtensionInfo);

/* Fill in the structure */
extinfo->db_data = dbinfo;
strncpy(extinfo->extname, NameStr(extform->extname), sizeof(extinfo->extname));
strlcpy(extinfo->extname, NameStr(extform->extname), sizeof(extinfo->extname));

/* Add to the list */
extlist = lappend(extlist, extinfo);
Expand Down Expand Up @@ -859,10 +854,6 @@ percona_pg_telemetry_main(Datum main_arg)
int rc = 0;
List *dblist = NIL;
ListCell *lc = NULL;
FILE *fp;
char str[2048] = {0};
char buf[4096] = {0};
size_t buf_size = sizeof(buf);
bool first_time = true;

/* Setup signal callbacks */
Expand Down Expand Up @@ -894,6 +885,10 @@ percona_pg_telemetry_main(Datum main_arg)
/* Should never really terminate unless... */
while (!sigterm_recvd && ptss->error_code == PT_SUCCESS)
{
FILE *fp;
char buf[4096] = {0};
size_t buf_size = sizeof(buf);

/* Don't sleep the first time */
if (first_time == false)
{
Expand Down Expand Up @@ -927,7 +922,7 @@ percona_pg_telemetry_main(Datum main_arg)
*/
if (dblist == NIL && (rc & WL_TIMEOUT || first_time))
{
char temp_buff[100];
char str[2048] = {0};

/* Data collection will start now */
first_time = false;
Expand Down Expand Up @@ -959,8 +954,8 @@ percona_pg_telemetry_main(Datum main_arg)
write_telemetry_file(fp, buf);

/* Construct and initiate the active extensions array block. */
pg_snprintf(temp_buff, sizeof(temp_buff), "%d", list_length(dblist));
construct_json_block(buf, buf_size, "databases_count", temp_buff, PT_JSON_KEY_VALUE, &ptss->json_file_indent);
pg_snprintf(str, sizeof(str), "%d", list_length(dblist));
construct_json_block(buf, buf_size, "databases_count", str, PT_JSON_KEY_VALUE, &ptss->json_file_indent);
write_telemetry_file(fp, buf);

/* Let's close the file now so that processes may add their stuff. */
Expand Down
3 changes: 1 addition & 2 deletions pt_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static char *json_escape_str(char *str);
char *
json_escape_str(char *str)
{
int i;
int len;
int maxlen;
char *str_escaped;
Expand All @@ -40,7 +39,7 @@ json_escape_str(char *str)
str_escaped = (char *) palloc(maxlen);
s = str_escaped;

for (i = 0; i < len; i++)
for (int i = 0; i < len; i++)
{
/* Escape the quote and backslash characters. */
if (str[i] == '"' || str[i] == '\\')
Expand Down

0 comments on commit 0da4ec5

Please sign in to comment.