pgpool2.git / summary / log / commit / refs
commit 4f53860caee6dc5e930a7b9737b5207da6a256b1
Author: Tatsuo Ishii <ishii@postgresql.org>
Date: Mon Jun 11 01:19:47 2012 +0000
Modify read_pid_file() and write_pid_file(). write_pid_file() used to
use fopen() and friends, but it was pointed out that pid file might
not be written to persistent storage in erroneous system. Solution to
this is using fsync() althoug it's a little bit tricky to use with
fopen() and friends, so I decided to rewrite them to use open(2) and
friends. Changes made to read_pid_file() as well, but this is rather
for consistency of coding.
main.c | 38 ++++++++++++++++++++++++++------------
1 file changed, 26 insertions(+), 12 deletions(-)
diff --git a/main.c b/main.c
index 008869f9b..4fbd0ee87 100644
--- a/main.c
+++ b/main.c
@@ -960,22 +960,22 @@ static void stop_me(void)
*/
static int read_pid_file(void)
{
- FILE *fd;
+ int fd;
char pidbuf[128];
- fd = fopen(pool_config->pid_file_name, "r");
- if (!fd)
+ fd = open(pool_config->pid_file_name, O_RDONLY);
+ if (fd == -1)
{
return -1;
}
- if (fread(pidbuf, 1, sizeof(pidbuf), fd) <= 0)
+ if (read(fd, pidbuf, sizeof(pidbuf)) == -1)
{
pool_error("could not read pid file as %s. reason: %s",
pool_config->pid_file_name, strerror(errno));
- fclose(fd);
+ close(fd);
return -1;
}
- fclose(fd);
+ close(fd);
return(atoi(pidbuf));
}
@@ -984,11 +984,11 @@ static int read_pid_file(void)
*/
static void write_pid_file(void)
{
- FILE *fd;
+ int fd;
char pidbuf[128];
- fd = fopen(pool_config->pid_file_name, "w");
- if (!fd)
+ fd = open(pool_config->pid_file_name, O_CREAT|O_WRONLY);
+ if (fd == -1)
{
pool_error("could not open pid file as %s. reason: %s",
pool_config->pid_file_name, strerror(errno));
@@ -996,12 +996,26 @@ static void write_pid_file(void)
exit(1);
}
snprintf(pidbuf, sizeof(pidbuf), "%d", (int)getpid());
- fwrite(pidbuf, strlen(pidbuf)+1, 1, fd);
- fsync(fd);
- if (fclose(fd))
+ if (write(fd, pidbuf, strlen(pidbuf)+1) == -1)
{
pool_error("could not write pid file as %s. reason: %s",
pool_config->pid_file_name, strerror(errno));
+ close(fd);
+ pool_shmem_exit(1);
+ exit(1);
+ }
+ if (fsync(fd) == -1)
+ {
+ pool_error("could not fsync pid file as %s. reason: %s",
+ pool_config->pid_file_name, strerror(errno));
+ close(fd);
+ pool_shmem_exit(1);
+ exit(1);
+ }
+ if (close(fd) == -1)
+ {
+ pool_error("could not close pid file as %s. reason: %s",
+ pool_config->pid_file_name, strerror(errno));
pool_shmem_exit(1);
exit(1);
}
[parent: 107f2e4e43a8]