summaryrefslogtreecommitdiff
path: root/lib/libutil/pidfile.c
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2001-12-08 02:25:07 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2001-12-08 02:25:07 +0000
commit3af01c3a97d0973d5eafc28299d86384ca229fa2 (patch)
treefa7d3c0fac6ef78b540872ef310f9b7ba352acbe /lib/libutil/pidfile.c
parent9d60d5abd9786f2184ff5714f221264c291225aa (diff)
save the pid as well, and only do the atexit in the same pid
Diffstat (limited to 'lib/libutil/pidfile.c')
-rw-r--r--lib/libutil/pidfile.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/lib/libutil/pidfile.c b/lib/libutil/pidfile.c
index 89dc002b51b..945889fc929 100644
--- a/lib/libutil/pidfile.c
+++ b/lib/libutil/pidfile.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pidfile.c,v 1.1 2001/09/28 20:16:42 jakob Exp $ */
+/* $OpenBSD: pidfile.c,v 1.2 2001/12/08 02:25:06 deraadt Exp $ */
/* $NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $ */
/*-
@@ -38,7 +38,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static const char rcsid[] = "$OpenBSD: pidfile.c,v 1.1 2001/09/28 20:16:42 jakob Exp $";
+static const char rcsid[] = "$OpenBSD: pidfile.c,v 1.2 2001/12/08 02:25:06 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@@ -50,6 +50,7 @@ static const char rcsid[] = "$OpenBSD: pidfile.c,v 1.1 2001/09/28 20:16:42 jakob
#include <util.h>
static char *pidfile_path;
+static int pidfile_pid;
static void pidfile_cleanup(void);
@@ -60,19 +61,20 @@ pidfile(const char *basename)
{
FILE *f;
int save_errno;
+ pid_t pid;
if (basename == NULL)
basename = __progname;
- if (pidfile_path != NULL) {
- free(pidfile_path);
- pidfile_path = NULL;
- }
+ if (pidfile_path != NULL) {
+ free(pidfile_path);
+ pidfile_path = NULL;
+ }
/* _PATH_VARRUN includes trailing / */
(void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
if (pidfile_path == NULL)
- return (-1);
+ return (-1);
if ((f = fopen(pidfile_path, "w")) == NULL) {
save_errno = errno;
@@ -82,7 +84,8 @@ pidfile(const char *basename)
return (-1);
}
- if (fprintf(f, "%d\n", getpid()) <= 0 || fclose(f) != 0) {
+ pid = getpid();
+ if (fprintf(f, "%d\n", pid) <= 0 || fclose(f) != 0) {
save_errno = errno;
(void) unlink(pidfile_path);
free(pidfile_path);
@@ -91,6 +94,7 @@ pidfile(const char *basename)
return (-1);
}
+ pidfile_pid = pid;
(void) atexit(pidfile_cleanup);
return (0);
@@ -100,6 +104,6 @@ static void
pidfile_cleanup(void)
{
- if (pidfile_path != NULL)
+ if (pidfile_path != NULL && pidfile_pid == getpid())
(void) unlink(pidfile_path);
}