Skip to content

Commit

Permalink
[#41] Set process title using a hack
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed Oct 15, 2020
1 parent 8c91679 commit 0cd3e9e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ pgagroal_base64_encode(char* raw, int raw_length, char** encoded);
int
pgagroal_base64_decode(char* encoded, size_t encoded_length, char** raw, int* raw_length);

/**
* Set process title
* @param argv The argv pointer
* @param s1 The first string
* @param s2 The second string
*/
void
pgagroal_set_proc_title(char** argv, char* s1, char *s2);

#ifdef DEBUG

/**
Expand Down
3 changes: 2 additions & 1 deletion src/include/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ extern volatile int exit_code;
* @param address The client address
* @param shmem The shared memory segment
* @param pipeline_shmem The shared memory segment for the pipeline
* @param argv The argv
*/
void
pgagroal_worker(int fd, char* address, void* shmem, void* pipeline_shmem);
pgagroal_worker(int fd, char* address, void* shmem, void* pipeline_shmem, char** argv);

#ifdef __cplusplus
}
Expand Down
19 changes: 19 additions & 0 deletions src/libpgagroal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,25 @@ pgagroal_base64_decode(char* encoded, size_t encoded_length, char** raw, int* ra
return 1;
}

void
pgagroal_set_proc_title(char** argv, char* s1, char *s2)
{
char title[256];

memset(&title, 0, sizeof(title));

if (s1 != NULL && s2 != NULL)
{
snprintf(title, sizeof(title) - 1, "pgagroal: %s/%s", s1, s2);
}
else
{
snprintf(title, sizeof(title) - 1, "pgagroal: %s", s1);
}

memcpy(*argv, title, 256);
}

#ifdef DEBUG

int
Expand Down
3 changes: 2 additions & 1 deletion src/libpgagroal/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ volatile int exit_code = WORKER_FAILURE;
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents);

void
pgagroal_worker(int client_fd, char* address, void* shmem, void* pipeline_shmem)
pgagroal_worker(int client_fd, char* address, void* shmem, void* pipeline_shmem, char** argv)
{
struct ev_loop *loop = NULL;
struct signal_info signal_watcher;
Expand Down Expand Up @@ -100,6 +100,7 @@ pgagroal_worker(int client_fd, char* address, void* shmem, void* pipeline_shmem)
}

pgagroal_pool_status(shmem);
pgagroal_set_proc_title(argv, config->connections[slot].username, config->connections[slot].database);

if (config->pipeline == PIPELINE_PERFORMANCE)
{
Expand Down
13 changes: 12 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct accept_io
int socket;
void* shmem;
void* pipeline_shmem;
char** argv;
};

struct periodic_info
Expand All @@ -102,6 +103,7 @@ struct client
};

static volatile int keep_running = 1;
static char** argv_ptr;
static struct ev_loop* main_loop = NULL;
static struct accept_io io_main[MAX_FDS];
static struct accept_io io_mgt;
Expand Down Expand Up @@ -130,6 +132,7 @@ start_mgt()
io_mgt.socket = unix_management_socket;
io_mgt.shmem = shmem;
io_mgt.pipeline_shmem = pipeline_shmem;
io_mgt.argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_mgt);
}

Expand All @@ -155,6 +158,7 @@ start_uds()
io_uds.socket = unix_pgsql_socket;
io_uds.shmem = shmem;
io_uds.pipeline_shmem = pipeline_shmem;
io_uds.argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_uds);
}

Expand Down Expand Up @@ -188,6 +192,7 @@ start_io()
io_main[i].socket = sockfd;
io_main[i].shmem = shmem;
io_main[i].pipeline_shmem = pipeline_shmem;
io_main[i].argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_main[i]);
}
}
Expand Down Expand Up @@ -215,6 +220,7 @@ start_metrics()
io_metrics[i].socket = sockfd;
io_metrics[i].shmem = shmem;
io_metrics[i].pipeline_shmem = pipeline_shmem;
io_metrics[i].argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_metrics[i]);
}
}
Expand Down Expand Up @@ -242,6 +248,7 @@ start_management()
io_management[i].socket = sockfd;
io_management[i].shmem = shmem;
io_management[i].pipeline_shmem = pipeline_shmem;
io_management[i].argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_management[i]);
}
}
Expand Down Expand Up @@ -316,6 +323,8 @@ main(int argc, char **argv)
int ret;
int c;

argv_ptr = argv;

while (1)
{
static struct option long_options[] =
Expand Down Expand Up @@ -661,6 +670,8 @@ main(int argc, char **argv)
pgagroal_start_logging(shmem);
pgagroal_pool_init(shmem);

pgagroal_set_proc_title(argv, "main", NULL);

/* Bind Unix Domain Socket for file descriptor transfers */
if (pgagroal_bind_unix_socket(config->unix_socket_dir, MAIN_UDS, shmem, &unix_management_socket))
{
Expand Down Expand Up @@ -1026,7 +1037,7 @@ accept_main_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)

ev_loop_fork(loop);
/* We are leaving the socket descriptor valid such that the client won't reuse it */
pgagroal_worker(client_fd, addr, ai->shmem, ai->pipeline_shmem);
pgagroal_worker(client_fd, addr, ai->shmem, ai->pipeline_shmem, ai->argv);
}

pgagroal_disconnect(client_fd);
Expand Down

0 comments on commit 0cd3e9e

Please sign in to comment.