Skip to content

Commit

Permalink
Replaced fixed buffer in child_pids_print with a dynamic
Browse files Browse the repository at this point in the history
Signed-off-by: andreas@florath.net <andreas@florath.net>
  • Loading branch information
florath committed Oct 16, 2024
1 parent acdb2d5 commit c42b3a3
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/pipexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,51 @@ void child_pids_unset(pid_t cpid) {
}

void child_pids_print() {
int const pilen = 4096;
char pbuf[pilen];
int pilen = 4096;
char *pbuf = (char *)malloc(pilen * sizeof(char));
if (pbuf == NULL) {
logging(lid_internal, "status", "error", "Memory allocation failed", 0);
return;
}
pbuf[0] = '[';
int poffset = 1;
int plen = pilen - poffset;
bool first = true;

for (unsigned int child_idx = 0; child_idx < g_child_cnt; ++child_idx) {
if (g_child_pids[child_idx] == 0) {
continue;
}
if(! first && plen > 0) {
pbuf[poffset] = ',';
++poffset;

if (!first) {
pbuf[poffset++] = ',';
}

while (poffset + 32 >= pilen) { // Ensure enough space for new data
pilen *= 2;
char *new_pbuf = (char *)realloc(pbuf, pilen * sizeof(char));
if (new_pbuf == NULL) {
free(pbuf);
logging(lid_internal, "status", "error", "Memory reallocation failed", 0);
return;
}
pbuf = new_pbuf;
}

int const written = snprintf(pbuf + poffset, pilen - poffset, "%d", g_child_pids[child_idx]);
if (written < 0) {
free(pbuf);
logging(lid_internal, "status", "error", "snprintf failed", 0);
return;
}
plen = pilen - poffset;
poffset += snprintf(pbuf + poffset, plen, "%d", g_child_pids[child_idx]);
poffset += written;
first = false;
}
if(plen > 2) {
pbuf[poffset] = ']';
pbuf[poffset+1] = '\0';
}

pbuf[poffset++] = ']';
pbuf[poffset] = '\0';

logging(lid_internal, "status", "info", "Child pids", 1, "pids", pbuf);
free(pbuf);
}

void child_pids_kill_all() {
Expand Down

0 comments on commit c42b3a3

Please sign in to comment.