Skip to content

Commit

Permalink
[agroal#198] Guard PID file creation
Browse files Browse the repository at this point in the history
Log FATAL when the PID file already exists.

If create_pid() fails because there is already a PID file, a log with
level FATAL is inserted before the program exits.
  • Loading branch information
fluca1978 authored and jesperpedersen committed Feb 15, 2022
1 parent ce557da commit eb59eba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pgagroal was created by the following authors:
Jesper Pedersen <jesper.pedersen@redhat.com>
David Fetter <david@fetter.org>
Will Leinweber <will@bitfission.com>
Junduo Dong <andj4cn@gmail.com>
Junduo Dong <andj4cn@gmail.com>
Luca Ferrari <fluca1978@gmail.com>
2 changes: 1 addition & 1 deletion doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ See a [sample](./etc/pgagroal/pgagroal.conf) configuration for running `pgagroal
| hugepage | `try` | String | No | Huge page support (`off`, `try`, `on`) |
| tracker | off | Bool | No | Track connection lifecycle |
| track_prepared_statements | off | Bool | No | Track prepared statements (transaction pooling) |
| pidfile | | String | No | Path to the PID file |
| pidfile | | String | No | Path to the PID file. If omitted, automatically set to `unix_socket_dir`/pgagroal.`port`.pid |

__Danger zone__

Expand Down
2 changes: 1 addition & 1 deletion src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ struct configuration
int authentication_timeout; /**< The authentication timeout in seconds */
int disconnect_client; /**< Disconnect client if idle for more than the specified seconds */
bool disconnect_client_force; /**< Force a disconnect client if active for more than the specified seconds */
char pidfile[MISC_LENGTH]; /**< File containing the PID */
char pidfile[MAX_PATH]; /**< File containing the PID */

char libev[MISC_LENGTH]; /**< Name of libev mode */
int buffer_size; /**< Socket buffer size */
Expand Down
18 changes: 17 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1913,8 +1913,24 @@ create_pidfile(void)

config = (struct configuration*)shmem;

if (strlen(config->pidfile) == 0)
{
// no pidfile set, use a default one
snprintf(config->pidfile, sizeof( config->pidfile ), "%s/pgagraol.%d.pid",
config->unix_socket_dir,
config->port);
pgagroal_log_debug("PID file automatically set to: [%s]", config->pidfile);
}

if (strlen(config->pidfile) > 0)
{
// check pidfile is not there
if (access(config->pidfile, F_OK) == 0)
{
pgagroal_log_fatal("PID file [%s] exists, is there another instance running ?", config->pidfile);
goto error;
}

pid = getpid();

fd = open(config->pidfile, O_WRONLY | O_CREAT | O_EXCL, 0644);
Expand Down Expand Up @@ -1949,7 +1965,7 @@ static void remove_pidfile(void)

config = (struct configuration*)shmem;

if (strlen(config->pidfile) > 0)
if (strlen(config->pidfile) > 0 && access(config->pidfile, F_OK) == 0)
{
unlink(config->pidfile);
}
Expand Down

0 comments on commit eb59eba

Please sign in to comment.