Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src Extend pidfile(3) to support creating pid files in arbitrary...
details: https://anonhg.NetBSD.org/src/rev/f0a0121c663a
branches: trunk
changeset: 763640:f0a0121c663a
user: jmmv <jmmv%NetBSD.org@localhost>
date: Tue Mar 29 13:55:37 2011 +0000
description:
Extend pidfile(3) to support creating pid files in arbitrary locations.
If the argument provided to pidfile(3) contains a '/', then the value is
considered to be an absolute/relative path and the pid file is created
in the given location.
Otherwise, pidfile(3) behaves as before and treats the provided value as
a basename to construct a pid file in /var/run/<basename>.pid. This means
that to create a pid file named "foo.pid" in the current directory, one
must specify "./foo.pid".
diffstat:
lib/libutil/pidfile.3 | 48 ++++++++----
lib/libutil/pidfile.c | 160 ++++++++++++++++++++++++++++-------------
tests/lib/libutil/t_pidfile.c | 119 +++++++++++++++++++++++++++++--
3 files changed, 250 insertions(+), 77 deletions(-)
diffs (truncated from 482 to 300 lines):
diff -r b7578d3f3fe5 -r f0a0121c663a lib/libutil/pidfile.3
--- a/lib/libutil/pidfile.3 Tue Mar 29 07:48:13 2011 +0000
+++ b/lib/libutil/pidfile.3 Tue Mar 29 13:55:37 2011 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: pidfile.3,v 1.12 2010/05/05 22:05:31 wiz Exp $
+.\" $NetBSD: pidfile.3,v 1.13 2011/03/29 13:55:37 jmmv Exp $
.\"
.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
.\" All rights reserved.
@@ -27,7 +27,7 @@
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd May 4, 2010
+.Dd March 23, 2011
.Dt PIDFILE 3
.Os
.Sh NAME
@@ -38,34 +38,48 @@
.Sh SYNOPSIS
.In util.h
.Ft int
-.Fn pidfile "const char *basename"
+.Fn pidfile "const char *path"
.Sh DESCRIPTION
.Fn pidfile
-writes a file containing the process ID of the program to the
+creates a file containing the process ID of the caller program.
+The pid file can be used as a quick reference if
+the process needs to be sent a signal.
+When the program exits, the pid file is removed automatically, unless
+the program receives a fatal signal.
+.Pp
+If
+.Ar path
+is
+.Dv NULL
+or a plain basename (a name containing no directory components), the pid file
+is created in the
.Pa /var/run
directory.
The file name has the form
.Pa /var/run/basename.pid .
-If the
-.Ar basename
-argument is
+The basename part is either the value of
+.Ar path
+if it was not
.Dv NULL ,
-.Fn pidfile
-will determine the program name and use that instead.
+or the program name as returned by
+.Xr getprogname 3
+otherwise.
.Pp
-The pid file can be used as a quick reference if
-the process needs to be sent a signal.
-When the program exits, the pid file will be removed automatically, unless
-the program receives a fatal signal.
+If
+.Ar path
+is an absolute or relative path (i.e. it contains the
+.Sq /
+character),
+the pid file is created in the provided location.
.Pp
Note that only the first invocation of
.Fn pidfile
causes a pid file to be written; subsequent invocations have no effect
unless a new
-.Ar basename
+.Ar path
is supplied.
If called with a new
-.Ar basename ,
+.Ar path ,
.Fn pidfile
will remove the old pid file and write the new one.
.Sh RETURN VALUES
@@ -78,11 +92,13 @@
.Fn pidfile
function call appeared in
.Nx 1.5 .
+Support for creating pid files in any arbitrary path was added in
+.Nx 6.0 .
.Sh BUGS
.Fn pidfile
uses
.Xr atexit 3
-to ensure the pidfile is unlinked at program exit.
+to ensure the pid file is unlinked at program exit.
However, programs that use the
.Xr _exit 2
function (for example, in signal handlers)
diff -r b7578d3f3fe5 -r f0a0121c663a lib/libutil/pidfile.c
--- a/lib/libutil/pidfile.c Tue Mar 29 07:48:13 2011 +0000
+++ b/lib/libutil/pidfile.c Tue Mar 29 13:55:37 2011 +0000
@@ -1,11 +1,11 @@
-/* $NetBSD: pidfile.c,v 1.8 2008/04/28 20:23:03 martin Exp $ */
+/* $NetBSD: pidfile.c,v 1.9 2011/03/29 13:55:37 jmmv Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
- * by Jason R. Thorpe and Matthias Scheler.
+ * by Jason R. Thorpe, Matthias Scheler and Julio Merino.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -31,90 +31,144 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pidfile.c,v 1.8 2008/04/28 20:23:03 martin Exp $");
+__RCSID("$NetBSD: pidfile.c,v 1.9 2011/03/29 13:55:37 jmmv Exp $");
#endif
#include <sys/param.h>
+
#include <paths.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
-static int pidfile_atexit_done;
static pid_t pidfile_pid;
-static char *pidfile_basename;
static char *pidfile_path;
-static void pidfile_cleanup(void);
-
-int
-pidfile(const char *basename)
+/* Deletes an existent pidfile iff it was created by this process. */
+static void
+pidfile_cleanup(void)
{
- FILE *f;
+
+ if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
+ (void) unlink(pidfile_path);
+}
- /*
- * Register handler which will remove the pidfile later.
- */
- if (!pidfile_atexit_done) {
+/* Registers an atexit(3) handler to delete the pidfile we have generated.
+ * We only register the handler when we create a pidfile, so we can assume
+ * that the pidfile exists.
+ *
+ * Returns 0 on success or -1 if the handler could not be registered. */
+static int
+register_atexit_handler(void)
+{
+ static bool done = false;
+
+ if (!done) {
if (atexit(pidfile_cleanup) < 0)
return -1;
- pidfile_atexit_done = 1;
+ done = true;
}
+ return 0;
+}
+
+/* Given a new pidfile name in 'path', deletes any previously-created pidfile
+ * if the previous file differs to the new one.
+ *
+ * If a previous file is deleted, returns 1, which means that a new pidfile
+ * must be created. Otherwise, this returns 0, which means that the existing
+ * file does not need to be touched. */
+static int
+cleanup_old_pidfile(const char* path)
+{
+ if (pidfile_path != NULL) {
+ if (strcmp(pidfile_path, path) != 0) {
+ pidfile_cleanup();
+
+ free(pidfile_path);
+ pidfile_path = NULL;
+
+ return 1;
+ } else
+ return 0;
+ } else
+ return 1;
+}
+
+/* Constructs a name for a pidfile in the default location (/var/run). If
+ * 'basename' is NULL, uses the name of the current program for the name of
+ * the pidfile.
+ *
+ * Returns a pointer to a dynamically-allocatd string containing the absolute
+ * path to the pidfile; NULL on failure. */
+static char *
+generate_varrun_path(const char *basename)
+{
+ char *path;
+
if (basename == NULL)
basename = getprogname();
- /*
- * If pidfile has already been created for the supplied basename
- * we don't need to create a pidfile again.
- */
- if (pidfile_path != NULL) {
- if (strcmp(pidfile_basename, basename) == 0)
- return 0;
- /*
- * Remove existing pidfile if it was created by this process.
- */
- pidfile_cleanup();
+ /* _PATH_VARRUN includes trailing / */
+ (void) asprintf(&path, "%s%s.pid", _PATH_VARRUN, basename);
+ return path;
+}
+/* Creates a pidfile with the provided name. The new pidfile is "registered"
+ * in the global variables pidfile_path and pidfile_pid so that any further
+ * call to pidfile(3) can check if we are recreating the same file or a new
+ * one.
+ *
+ * Returns 0 on success or -1 if there is any error. */
+static int
+create_pidfile(const char* path)
+{
+ FILE *f;
+
+ if (register_atexit_handler() == -1)
+ return -1;
+
+ if (cleanup_old_pidfile(path) == 0)
+ return 0;
+
+ pidfile_path = strdup(path);
+ if (pidfile_path == NULL)
+ return -1;
+
+ if ((f = fopen(path, "w")) == NULL) {
free(pidfile_path);
pidfile_path = NULL;
- free(pidfile_basename);
- pidfile_basename = NULL;
+ return -1;
}
pidfile_pid = getpid();
- pidfile_basename = strdup(basename);
- if (pidfile_basename == NULL)
- return -1;
-
- /* _PATH_VARRUN includes trailing / */
- (void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
- if (pidfile_path == NULL) {
- free(pidfile_basename);
- pidfile_basename = NULL;
- return -1;
- }
-
- if ((f = fopen(pidfile_path, "w")) == NULL) {
- free(pidfile_path);
- pidfile_path = NULL;
- free(pidfile_basename);
- pidfile_basename = NULL;
- return -1;
- }
-
(void) fprintf(f, "%d\n", pidfile_pid);
(void) fclose(f);
+
return 0;
}
-static void
-pidfile_cleanup(void)
+int
+pidfile(const char *path)
{
- /* Only remove the pidfile if it was created by this process. */
- if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
- (void) unlink(pidfile_path);
+
+ if (path == NULL || strchr(path, '/') == NULL) {
+ char *default_path;
+
+ if ((default_path = generate_varrun_path(path)) == NULL)
+ return -1;
+
+ if (create_pidfile(default_path) == -1) {
+ free(default_path);
Home |
Main Index |
Thread Index |
Old Index