Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src Implement pidfile_lock, pidfile_read and pidfile_clean.
details: https://anonhg.NetBSD.org/src/rev/388e589d40eb
branches: trunk
changeset: 344662:388e589d40eb
user: roy <roy%NetBSD.org@localhost>
date: Sun Apr 10 19:05:50 2016 +0000
description:
Implement pidfile_lock, pidfile_read and pidfile_clean.
Discussed on tech-net@, ok core@.
diffstat:
distrib/sets/lists/comp/mi | 11 +-
include/util.h | 5 +-
lib/libutil/Makefile | 5 +-
lib/libutil/pidfile.3 | 103 ++++++++++++++---
lib/libutil/pidfile.c | 269 ++++++++++++++++++++++++++++----------------
5 files changed, 275 insertions(+), 118 deletions(-)
diffs (truncated from 564 to 300 lines):
diff -r 9a63deae5698 -r 388e589d40eb distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi Sun Apr 10 18:27:56 2016 +0000
+++ b/distrib/sets/lists/comp/mi Sun Apr 10 19:05:50 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.2028 2016/04/09 06:21:16 riastradh Exp $
+# $NetBSD: mi,v 1.2029 2016/04/10 19:05:50 roy Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
./etc/mtree/set.comp comp-sys-root
@@ -8026,6 +8026,9 @@
./usr/share/man/cat3/pechochar.0 comp-c-catman .cat
./usr/share/man/cat3/perror.0 comp-c-catman .cat
./usr/share/man/cat3/pidfile.0 comp-c-catman .cat
+./usr/share/man/cat3/pidfile_clean.0 comp-c-catman .cat
+./usr/share/man/cat3/pidfile_lock.0 comp-c-catman .cat
+./usr/share/man/cat3/pidfile_read.0 comp-c-catman .cat
./usr/share/man/cat3/pidlock.0 comp-c-catman .cat
./usr/share/man/cat3/pmap_getmaps.0 comp-c-catman .cat
./usr/share/man/cat3/pmap_getport.0 comp-c-catman .cat
@@ -15174,6 +15177,9 @@
./usr/share/man/html3/pechochar.html comp-c-htmlman html
./usr/share/man/html3/perror.html comp-c-htmlman html
./usr/share/man/html3/pidfile.html comp-c-htmlman html
+./usr/share/man/html3/pidfile_clean.html comp-c-htmlman html
+./usr/share/man/html3/pidfile_lock.html comp-c-htmlman html
+./usr/share/man/html3/pidfile_read.html comp-c-htmlman html
./usr/share/man/html3/pidlock.html comp-c-htmlman html
./usr/share/man/html3/pmap_getmaps.html comp-c-htmlman html
./usr/share/man/html3/pmap_getport.html comp-c-htmlman html
@@ -22311,6 +22317,9 @@
./usr/share/man/man3/pechochar.3 comp-c-man .man
./usr/share/man/man3/perror.3 comp-c-man .man
./usr/share/man/man3/pidfile.3 comp-c-man .man
+./usr/share/man/man3/pidfile_clean.3 comp-c-man .man
+./usr/share/man/man3/pidfile_lock.3 comp-c-man .man
+./usr/share/man/man3/pidfile_read.3 comp-c-man .man
./usr/share/man/man3/pidlock.3 comp-c-man .man
./usr/share/man/man3/pmap_getmaps.3 comp-c-man .man
./usr/share/man/man3/pmap_getport.3 comp-c-man .man
diff -r 9a63deae5698 -r 388e589d40eb include/util.h
--- a/include/util.h Sun Apr 10 18:27:56 2016 +0000
+++ b/include/util.h Sun Apr 10 19:05:50 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: util.h,v 1.68 2015/09/24 14:39:37 christos Exp $ */
+/* $NetBSD: util.h,v 1.69 2016/04/10 19:05:50 roy Exp $ */
/*-
* Copyright (c) 1995
@@ -103,6 +103,9 @@
__RENAME(__parsedate50);
#endif
int pidfile(const char *);
+pid_t pidfile_lock(const char *);
+pid_t pidfile_read(const char *);
+int pidfile_clean(void);
int pidlock(const char *, int, pid_t *, const char *);
int pw_abort(void);
#ifndef __LIBC12_SOURCE__
diff -r 9a63deae5698 -r 388e589d40eb lib/libutil/Makefile
--- a/lib/libutil/Makefile Sun Apr 10 18:27:56 2016 +0000
+++ b/lib/libutil/Makefile Sun Apr 10 19:05:50 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.78 2015/09/24 14:39:20 christos Exp $
+# $NetBSD: Makefile,v 1.79 2016/04/10 19:05:50 roy Exp $
# @(#)Makefile 8.1 (Berkeley) 6/4/93
USE_SHLIBDIR= yes
@@ -58,6 +58,9 @@
MLINKS+=loginx.3 logoutx.3 loginx.3 logwtmpx.3
MLINKS+=openpty.3 login_tty.3
MLINKS+=openpty.3 forkpty.3
+MLINKS+=pidfile.3 pidfile_clean.3
+MLINKS+=pidfile.3 pidfile_lock.3
+MLINKS+=pidfile.3 pidfile_read.3
MLINKS+=pw_getconf.3 pw_getpwconf.3
MLINKS+=pw_init.3 pw_edit.3
MLINKS+=pw_init.3 pw_prompt.3
diff -r 9a63deae5698 -r 388e589d40eb lib/libutil/pidfile.3
--- a/lib/libutil/pidfile.3 Sun Apr 10 18:27:56 2016 +0000
+++ b/lib/libutil/pidfile.3 Sun Apr 10 19:05:50 2016 +0000
@@ -1,10 +1,10 @@
-.\" $NetBSD: pidfile.3,v 1.13 2011/03/29 13:55:37 jmmv Exp $
+.\" $NetBSD: pidfile.3,v 1.14 2016/04/10 19:05:50 roy Exp $
.\"
-.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
+.\" Copyright (c) 1999, 2016 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
-.\" by Jason R. Thorpe.
+.\" by Jason R. Thorpe and Roy Marples.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
@@ -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 March 23, 2011
+.Dd April 10, 2016
.Dt PIDFILE 3
.Os
.Sh NAME
@@ -39,13 +39,21 @@
.In util.h
.Ft int
.Fn pidfile "const char *path"
+.Ft pid_t
+.Fn pidfile_lock "const char *path"
+.Ft pid_t
+.Fn pidfile_read "const char *path"
+.Ft int
+.Fn pidfile_clean "void"
.Sh DESCRIPTION
.Fn pidfile
-creates a file containing the process ID of the caller program.
+and
+.Fn pidfile_lock
+create and lock a file containing the process ID of the calling 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.
+The pid file is truncated and removed automatically when the program exits,
+unless the program receives a fatal signal.
.Pp
If
.Ar path
@@ -72,21 +80,60 @@
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 path
-is supplied.
If called with a new
.Ar path ,
.Fn pidfile
-will remove the old pid file and write the new one.
+and
+.Fn pidfile_lock
+will remove the old pid file.
+.Pp
+The pid file is truncated, so these functions can be called multiple times and
+allow a child process to take over the lock.
+.Pp
+.Fn pidfile_read
+will read the last pid file created, or specified by
+.Ar path ,
+and return the process ID it contains.
+.Pp
+.Fn pidfile_clean
+will
+.Xr ftruncate 2 ,
+.Xr close 2
+and
+.Xr unlink 2
+the last opening pid file if, and only if, the current process wrote it.
+This function should be called if the program needs to call
+.Xr _exit 2
+(such as from a signal handler) and needs to clean up the pid file.
.Sh RETURN VALUES
.Fn pidfile
+and
+.Fn pidfile_clean
returns 0 on success and -1 on failure.
+.Pp
+.Fn pidfile_lock
+returns 0 on success.
+Otherwise, the process ID who owns the lock is returned and if that
+cannot be derived then -1 is returned.
+.Pp
+.Fn pidfile_read
+returns the process ID if known, otherwise -1.
+.Sh ERRORS
+The
+.Fn pidfile
+and
+.Fn pidfile_lock
+functions will fail if:
+.Bl -tag -width Er
+.It Bq Er EEXIST
+Some process already holds the lock on the given pid file, meaning that a
+daemon is already running.
+.It Bq Er ENAMETOOLONG
+Specified pidfile's name is too long.
+.El
.Sh SEE ALSO
-.Xr atexit 3
+.Xr atexit 3 ,
+.Xr flock 2
.Sh HISTORY
The
.Fn pidfile
@@ -94,12 +141,30 @@
.Nx 1.5 .
Support for creating pid files in any arbitrary path was added in
.Nx 6.0 .
-.Sh BUGS
+.Pp
+The
+.Fn pidfile_lock ,
+.Fn pidfile_read
+and
+.Fn pidfile_clean
+function calls appeared in
+.Nx 8 .
+.Sh CAVEATS
.Fn pidfile
-uses
+and
+.Fn pidfile_lock
+use
.Xr atexit 3
-to ensure the pid file is unlinked at program exit.
+to ensure the pid file is cleaned at program exit.
However, programs that use the
.Xr _exit 2
function (for example, in signal handlers)
-will not trigger this behaviour.
+will not trigger this behaviour and should call
+.Xr pidfile_clean.
+Like-wise, if the program creates a pid file before
+.Xr fork 2 Ns ing
+a child to take over, it should use the
+.Xr _exit 2
+function instead of returning or using the
+.Xr exit 2
+function to ensure the pid file is not cleaned.
diff -r 9a63deae5698 -r 388e589d40eb lib/libutil/pidfile.c
--- a/lib/libutil/pidfile.c Sun Apr 10 18:27:56 2016 +0000
+++ b/lib/libutil/pidfile.c Sun Apr 10 19:05:50 2016 +0000
@@ -1,11 +1,11 @@
-/* $NetBSD: pidfile.c,v 1.11 2015/01/22 19:04:28 christos Exp $ */
+/* $NetBSD: pidfile.c,v 1.12 2016/04/10 19:05:50 roy Exp $ */
/*-
- * Copyright (c) 1999 The NetBSD Foundation, Inc.
+ * Copyright (c) 1999, 2016 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
- * by Jason R. Thorpe, Matthias Scheler and Julio Merino.
+ * by Jason R. Thorpe, Matthias Scheler, Julio Merino and Roy Marples.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -31,11 +31,14 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pidfile.c,v 1.11 2015/01/22 19:04:28 christos Exp $");
+__RCSID("$NetBSD: pidfile.c,v 1.12 2016/04/10 19:05:50 roy Exp $");
#endif
#include <sys/param.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
#include <paths.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -45,131 +48,205 @@
#include <util.h>
static pid_t pidfile_pid;
-static char *pidfile_path;
+static char pidfile_path[PATH_MAX];
+static int pidfile_fd = -1;
+
+/* Closes pidfile resources.
+ *
+ * Returns 0 on success, otherwise -1. */
+static int
+pidfile_close(void)
+{
+ int error;
+
+ pidfile_pid = 0;
+ error = close(pidfile_fd);
+ pidfile_fd = -1;
+ pidfile_path[0] = '\0';
+ return error;
+}
-/* Deletes an existent pidfile iff it was created by this process. */
+/* Truncate, close and unlink an existent pidfile,
+ * if and only if it was created by this process.
+ * The pidfile is truncated because we may have dropped permissions
+ * or entered a chroot and thus unable to unlink it.
+ *
+ * Returns 0 on truncation success, otherwise -1. */
+int
+pidfile_clean(void)
+{
+ int error;
Home |
Main Index |
Thread Index |
Old Index