1*dba3562dSLionel Sambuc.\" $NetBSD: pidfile.3,v 1.13 2011/03/29 13:55:37 jmmv Exp $ 20c3983b2SBen Gras.\" 30c3983b2SBen Gras.\" Copyright (c) 1999 The NetBSD Foundation, Inc. 40c3983b2SBen Gras.\" All rights reserved. 50c3983b2SBen Gras.\" 60c3983b2SBen Gras.\" This code is derived from software contributed to The NetBSD Foundation 70c3983b2SBen Gras.\" by Jason R. Thorpe. 80c3983b2SBen Gras.\" 90c3983b2SBen Gras.\" Redistribution and use in source and binary forms, with or without 100c3983b2SBen Gras.\" modification, are permitted provided that the following conditions 110c3983b2SBen Gras.\" are met: 120c3983b2SBen Gras.\" 1. Redistributions of source code must retain the above copyright 130c3983b2SBen Gras.\" notice, this list of conditions and the following disclaimer. 140c3983b2SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 150c3983b2SBen Gras.\" notice, this list of conditions and the following disclaimer in the 160c3983b2SBen Gras.\" documentation and/or other materials provided with the distribution. 170c3983b2SBen Gras.\" 180c3983b2SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 190c3983b2SBen Gras.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 200c3983b2SBen Gras.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 210c3983b2SBen Gras.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 220c3983b2SBen Gras.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 230c3983b2SBen Gras.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 240c3983b2SBen Gras.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 250c3983b2SBen Gras.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 260c3983b2SBen Gras.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 270c3983b2SBen Gras.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 280c3983b2SBen Gras.\" POSSIBILITY OF SUCH DAMAGE. 290c3983b2SBen Gras.\" 30*dba3562dSLionel Sambuc.Dd March 23, 2011 310c3983b2SBen Gras.Dt PIDFILE 3 320c3983b2SBen Gras.Os 330c3983b2SBen Gras.Sh NAME 340c3983b2SBen Gras.Nm pidfile 350c3983b2SBen Gras.Nd write a daemon pid file 360c3983b2SBen Gras.Sh LIBRARY 370c3983b2SBen Gras.Lb libutil 380c3983b2SBen Gras.Sh SYNOPSIS 390c3983b2SBen Gras.In util.h 400c3983b2SBen Gras.Ft int 41*dba3562dSLionel Sambuc.Fn pidfile "const char *path" 420c3983b2SBen Gras.Sh DESCRIPTION 430c3983b2SBen Gras.Fn pidfile 44*dba3562dSLionel Sambuccreates a file containing the process ID of the caller program. 45*dba3562dSLionel SambucThe pid file can be used as a quick reference if 46*dba3562dSLionel Sambucthe process needs to be sent a signal. 47*dba3562dSLionel SambucWhen the program exits, the pid file is removed automatically, unless 48*dba3562dSLionel Sambucthe program receives a fatal signal. 49*dba3562dSLionel Sambuc.Pp 50*dba3562dSLionel SambucIf 51*dba3562dSLionel Sambuc.Ar path 52*dba3562dSLionel Sambucis 53*dba3562dSLionel Sambuc.Dv NULL 54*dba3562dSLionel Sambucor a plain basename (a name containing no directory components), the pid file 55*dba3562dSLionel Sambucis created in the 560c3983b2SBen Gras.Pa /var/run 570c3983b2SBen Grasdirectory. 580c3983b2SBen GrasThe file name has the form 590c3983b2SBen Gras.Pa /var/run/basename.pid . 60*dba3562dSLionel SambucThe basename part is either the value of 61*dba3562dSLionel Sambuc.Ar path 62*dba3562dSLionel Sambucif it was not 630c3983b2SBen Gras.Dv NULL , 64*dba3562dSLionel Sambucor the program name as returned by 65*dba3562dSLionel Sambuc.Xr getprogname 3 66*dba3562dSLionel Sambucotherwise. 670c3983b2SBen Gras.Pp 68*dba3562dSLionel SambucIf 69*dba3562dSLionel Sambuc.Ar path 70*dba3562dSLionel Sambucis an absolute or relative path (i.e. it contains the 71*dba3562dSLionel Sambuc.Sq / 72*dba3562dSLionel Sambuccharacter), 73*dba3562dSLionel Sambucthe pid file is created in the provided location. 740c3983b2SBen Gras.Pp 750c3983b2SBen GrasNote that only the first invocation of 760c3983b2SBen Gras.Fn pidfile 770c3983b2SBen Grascauses a pid file to be written; subsequent invocations have no effect 780c3983b2SBen Grasunless a new 79*dba3562dSLionel Sambuc.Ar path 800c3983b2SBen Grasis supplied. 810c3983b2SBen GrasIf called with a new 82*dba3562dSLionel Sambuc.Ar path , 830c3983b2SBen Gras.Fn pidfile 840c3983b2SBen Graswill remove the old pid file and write the new one. 850c3983b2SBen Gras.Sh RETURN VALUES 860c3983b2SBen Gras.Fn pidfile 870c3983b2SBen Grasreturns 0 on success and -1 on failure. 880c3983b2SBen Gras.Sh SEE ALSO 890c3983b2SBen Gras.Xr atexit 3 900c3983b2SBen Gras.Sh HISTORY 910c3983b2SBen GrasThe 920c3983b2SBen Gras.Fn pidfile 930c3983b2SBen Grasfunction call appeared in 940c3983b2SBen Gras.Nx 1.5 . 95*dba3562dSLionel SambucSupport for creating pid files in any arbitrary path was added in 96*dba3562dSLionel Sambuc.Nx 6.0 . 970c3983b2SBen Gras.Sh BUGS 980c3983b2SBen Gras.Fn pidfile 990c3983b2SBen Grasuses 1000c3983b2SBen Gras.Xr atexit 3 1010c3983b2SBen Grasto ensure the pid file is unlinked at program exit. 1020c3983b2SBen GrasHowever, programs that use the 1030c3983b2SBen Gras.Xr _exit 2 1040c3983b2SBen Grasfunction (for example, in signal handlers) 1050c3983b2SBen Graswill not trigger this behaviour. 106