Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add pidfile(3), which write a pid file in /var/run, and arra...



details:   https://anonhg.NetBSD.org/src/rev/db7c58328035
branches:  trunk
changeset: 473501:db7c58328035
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sun Jun 06 01:50:00 1999 +0000

description:
Add pidfile(3), which write a pid file in /var/run, and arranges for it
to be removed on normal exit.

diffstat:

 include/util.h            |   3 +-
 lib/libutil/Makefile      |   8 ++--
 lib/libutil/pidfile.3     |  75 ++++++++++++++++++++++++++++++++++++++
 lib/libutil/pidfile.c     |  91 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/libutil/shlib_version |   4 +-
 5 files changed, 174 insertions(+), 7 deletions(-)

diffs (224 lines):

diff -r 6649c7df89e0 -r db7c58328035 include/util.h
--- a/include/util.h    Sat Jun 05 21:58:16 1999 +0000
+++ b/include/util.h    Sun Jun 06 01:50:00 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.h,v 1.12 1998/07/26 22:01:48 mycroft Exp $        */
+/*     $NetBSD: util.h,v 1.13 1999/06/06 01:50:00 thorpej Exp $        */
 
 /*-
  * Copyright (c) 1995
@@ -74,6 +74,7 @@
 int    getmaxpartitions __P((void));
 int    getrawpartition __P((void));
 int    opendisk __P((const char *, int, char *, size_t, int));
+void   pidfile __P((const char *));
 int    pidlock __P((const char *, int, pid_t *, const char *));
 int    ttylock __P((const char *, int, pid_t *));
 int    ttyunlock __P((const char *));
diff -r 6649c7df89e0 -r db7c58328035 lib/libutil/Makefile
--- a/lib/libutil/Makefile      Sat Jun 05 21:58:16 1999 +0000
+++ b/lib/libutil/Makefile      Sun Jun 06 01:50:00 1999 +0000
@@ -1,14 +1,14 @@
-#      $NetBSD: Makefile,v 1.21 1999/01/15 13:31:22 bouyer Exp $
+#      $NetBSD: Makefile,v 1.22 1999/06/06 01:50:00 thorpej Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/4/93
 
 LIB=   util
 CPPFLAGS+=-DLIBC_SCCS
 SRCS=  fparseln.c getmaxpartitions.c getrawpartition.c login.c login_tty.c \
-       logout.c logwtmp.c opendisk.c passwd.c pw_scan.c pidlock.c pty.c \
-       ttyaction.c ttymsg.c 
+       logout.c logwtmp.c opendisk.c passwd.c pw_scan.c pidfile.c pidlock.c \
+       pty.c ttyaction.c ttymsg.c 
 
 MAN=   fparseln.3 getmaxpartitions.3 getrawpartition.3 login.3 opendisk.3 \
-       openpty.3 pidlock.3 pw_init.3 pw_lock.3 ttyaction.3 ttymsg.3 
+       openpty.3 pidfile.3 pidlock.3 pw_init.3 pw_lock.3 ttyaction.3 ttymsg.3 
 
 .PATH: ${.CURDIR}/../libc/gen
 
diff -r 6649c7df89e0 -r db7c58328035 lib/libutil/pidfile.3
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libutil/pidfile.3     Sun Jun 06 01:50:00 1999 +0000
@@ -0,0 +1,75 @@
+.\"    $NetBSD: pidfile.3,v 1.1 1999/06/06 01:50:00 thorpej 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.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\" 3. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"        This product includes software developed by the NetBSD
+.\"        Foundation, Inc. and its contributors.
+.\" 4. Neither the name of The NetBSD Foundation nor the names of its
+.\"    contributors may be used to endorse or promote products derived
+.\"    from this software without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd June 5, 1999
+.Dt PIDFILE 3
+.Os
+.Sh NAME
+.Nm pidfile
+.Nd write a daemon pid file
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.Fd #include <util.h>
+.Ft void
+.Fn pidfile "const char *basename"
+.Sh DESCRIPTION
+.Fn pidfile
+writes a file containing the process ID of the program to the
+.Pa /var/run
+directory.  The file name has the form
+.Pa /var/run/basename.pid .
+If the
+.Ar basename
+argument is NULL,
+.Nm
+will determine the program name and use that instead.
+.Pp
+The pid file can be used as a quick reference if
+the process needs to be sent a signal.  When the program exists,
+the pid file will be removed automatically, unless the program
+receives a fatal signal.
+.Pp
+Note that only the first invocation of
+.Nm
+causes a pid file to be written; subsequent invocations have no effect.
+.Sh SEE ALSO
+.Xr atexit 3
+.Sh HISTORY
+The
+.Nm
+function call appeared in
+.Nx 1.5 .
diff -r 6649c7df89e0 -r db7c58328035 lib/libutil/pidfile.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libutil/pidfile.c     Sun Jun 06 01:50:00 1999 +0000
@@ -0,0 +1,91 @@
+/*     $NetBSD: pidfile.c,v 1.1 1999/06/06 01:50:00 thorpej 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.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: pidfile.c,v 1.1 1999/06/06 01:50:00 thorpej Exp $");
+#endif
+
+#include <sys/param.h>
+#include <paths.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <util.h>
+
+static char pidfile_path_store[MAXPATHLEN];
+static char *pidfile_path;
+
+static void pidfile_cleanup __P((void));
+
+extern const char *__progname;         /* from crt0.o */
+
+void
+pidfile(basename)
+       const char *basename;
+{
+       FILE *f;
+
+       if (pidfile_path != NULL)
+               return;
+
+       if (basename == NULL)
+               basename = __progname;
+
+       /* _PATH_VARRUN includes trailing / */
+       (void) snprintf(pidfile_path_store, sizeof(pidfile_path_store),
+           "%s%s.pid", _PATH_VARRUN, basename);
+
+       if ((f = fopen(pidfile_path_store, "w")) == NULL)
+               return;
+
+       (void) fprintf(f, "%d\n", getpid());
+       (void) fclose(f);
+
+       pidfile_path = pidfile_path_store;
+
+       (void) atexit(pidfile_cleanup);
+}
+
+static void
+pidfile_cleanup()
+{
+
+       if (pidfile_path != NULL)
+               (void) unlink(pidfile_path);
+}
diff -r 6649c7df89e0 -r db7c58328035 lib/libutil/shlib_version
--- a/lib/libutil/shlib_version Sat Jun 05 21:58:16 1999 +0000
+++ b/lib/libutil/shlib_version Sun Jun 06 01:50:00 1999 +0000
@@ -1,5 +1,5 @@
-#      $NetBSD: shlib_version,v 1.18 1999/02/25 08:02:20 abs Exp $
+#      $NetBSD: shlib_version,v 1.19 1999/06/06 01:50:01 thorpej Exp $
 #      Remember to update distrib/sets/lists/base/shl.* when changing
 #
 major=5
-minor=0
+minor=1



Home | Main Index | Thread Index | Old Index