diff --git a/lib/libutil/pidfile.3 b/lib/libutil/pidfile.3 index fe5ea18ff9cb..d10ab81e4f8b 100644 --- a/lib/libutil/pidfile.3 +++ b/lib/libutil/pidfile.3 @@ -1,290 +1,289 @@ .\" Copyright (c) 2005 Pawel Jakub Dawidek .\" All rights reserved. .\" .\" 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. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS 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 AUTHORS 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. .\" .\" $FreeBSD$ .\" .Dd October 16, 2011 .Dt PIDFILE 3 .Os .Sh NAME .Nm pidfile_open , .Nm pidfile_write , .Nm pidfile_close , .Nm pidfile_remove .Nd "library for PID files handling" .Sh LIBRARY .Lb libutil .Sh SYNOPSIS -.In sys/param.h .In libutil.h .Ft "struct pidfh *" .Fn pidfile_open "const char *path" "mode_t mode" "pid_t *pidptr" .Ft int .Fn pidfile_write "struct pidfh *pfh" .Ft int .Fn pidfile_close "struct pidfh *pfh" .Ft int .Fn pidfile_remove "struct pidfh *pfh" .Ft int .Fn pidfile_fileno "struct pidfh *pfh" .Sh DESCRIPTION The .Nm pidfile family of functions allows daemons to handle PID files. It uses .Xr flopen 3 to lock a pidfile and detect already running daemons. .Pp The .Fn pidfile_open function opens (or creates) a file specified by the .Fa path argument and locks it. If .Fa pidptr argument is not .Dv NULL and file can not be locked, the function will use it to store a PID of an already running daemon or .Li -1 in case daemon did not write its PID yet. The function does not write process' PID into the file here, so it can be used before .Fn fork Ns ing and exit with a proper error message when needed. If the .Fa path argument is .Dv NULL , .Pa /var/run/ Ns Ao Va progname Ac Ns Pa .pid file will be used. The .Fn pidfile_open function sets the O_CLOEXEC close-on-exec flag when opening the pidfile. .Pp The .Fn pidfile_write function writes process' PID into a previously opened file. .Pp The .Fn pidfile_close function closes a pidfile. It should be used after daemon .Fn fork Ns s to start a child process. .Pp The .Fn pidfile_remove function closes and removes a pidfile. .Pp The .Fn pidfile_fileno function returns the file descriptor for the open pidfile. .Sh RETURN VALUES The .Fn pidfile_open function returns a valid pointer to a .Vt pidfh structure on success, or .Dv NULL if an error occurs. If an error occurs, .Va errno will be set. .Pp .Rv -std pidfile_write pidfile_close pidfile_remove .Pp The .Fn pidfile_fileno function returns the low-level file descriptor. It returns .Li -1 and sets .Va errno if a NULL .Vt pidfh is specified, or if the pidfile is no longer open. .Sh EXAMPLES The following example shows in which order these functions should be used. Note that it is safe to pass .Dv NULL to .Fn pidfile_write , .Fn pidfile_remove , .Fn pidfile_close and .Fn pidfile_fileno functions. .Bd -literal struct pidfh *pfh; pid_t otherpid, childpid; pfh = pidfile_open("/var/run/daemon.pid", 0600, &otherpid); if (pfh == NULL) { if (errno == EEXIST) { errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", (intmax_t)otherpid); } /* If we cannot create pidfile from other reasons, only warn. */ warn("Cannot open or create pidfile"); } if (daemon(0, 0) == -1) { warn("Cannot daemonize"); pidfile_remove(pfh); exit(EXIT_FAILURE); } pidfile_write(pfh); for (;;) { /* Do work. */ childpid = fork(); switch (childpid) { case -1: syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno)); break; case 0: pidfile_close(pfh); /* Do child work. */ break; default: syslog(LOG_INFO, "Child %jd started.", (intmax_t)childpid); break; } } pidfile_remove(pfh); exit(EXIT_SUCCESS); .Ed .Sh ERRORS The .Fn pidfile_open function will fail if: .Bl -tag -width Er .It Bq Er EEXIST Some process already holds the lock on the given pidfile, meaning that a daemon is already running. If .Fa pidptr argument is not .Dv NULL the function will use it to store a PID of an already running daemon or .Li -1 in case daemon did not write its PID yet. .It Bq Er ENAMETOOLONG Specified pidfile's name is too long. .It Bq Er EINVAL Some process already holds the lock on the given pidfile, but PID read from there is invalid. .El .Pp The .Fn pidfile_open function may also fail and set .Va errno for any errors specified for the .Xr fstat 2 , .Xr open 2 , and .Xr read 2 calls. .Pp The .Fn pidfile_write function will fail if: .Bl -tag -width Er .It Bq Er EDOOFUS Improper function use. Probably called before .Fn pidfile_open . .El .Pp The .Fn pidfile_write function may also fail and set .Va errno for any errors specified for the .Xr fstat 2 , .Xr ftruncate 2 , and .Xr write 2 calls. .Pp The .Fn pidfile_close function may fail and set .Va errno for any errors specified for the .Xr close 2 and .Xr fstat 2 calls. .Pp The .Fn pidfile_remove function will fail if: .Bl -tag -width Er .It Bq Er EDOOFUS Improper function use. Probably called not from the process which made .Fn pidfile_write . .El .Pp The .Fn pidfile_remove function may also fail and set .Va errno for any errors specified for the .Xr close 2 , .Xr fstat 2 , .Xr write 2 , and .Xr unlink 2 system calls and the .Xr flopen 3 library function. .Pp The .Fn pidfile_fileno function will fail if: .Bl -tag -width Er .It Bq Er EDOOFUS Improper function use. Probably called not from the process which used .Fn pidfile_open . .El .Sh SEE ALSO .Xr open 2 , .Xr daemon 3 , .Xr flopen 3 .Sh AUTHORS .An -nosplit The .Nm pidfile functionality is based on ideas from .An John-Mark Gurney Aq jmg@FreeBSD.org . .Pp The code and manual page was written by .An Pawel Jakub Dawidek Aq pjd@FreeBSD.org . diff --git a/lib/libutil/property.3 b/lib/libutil/property.3 index b3630890c8ca..efe0079f07f9 100644 --- a/lib/libutil/property.3 +++ b/lib/libutil/property.3 @@ -1,99 +1,98 @@ .\" .\" Copyright (c) 1998 Jordan Hubbard .\" .\" All rights reserved. .\" .\" 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. .\" .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``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 DEVELOPERS 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. .\" .\" $FreeBSD$ .\" " .Dd October 7, 1998 .Dt PROPERTIES 3 .Os .Sh NAME .Nm properties_read , .Nm property_find , .Nm properties_free .Nd "functions to allow creating simple property lists from ASCII file data" .Sh LIBRARY .Lb libutil .Sh SYNOPSIS -.In sys/types.h .In libutil.h .Ft properties .Fn properties_read "int fd" .Ft char * .Fn property_find "properties list" "const char *name" .Ft void .Fn properties_free "properties list" .Sh DESCRIPTION .Bd -literal typedef struct _properties { struct _properties *next; char *name; char *value; } *properties; .Ed .Pp The function .Fn properties_read reads .Fa name = value pairs from the file descriptor passed in .Fa fd and returns the head of a new property list, assuming that the file's contents have been parsed properly, or NULL in case of error. .Pp The .Fn property_find function returns the associated value string for the property named .Fa name if found, otherwise NULL. The value returned may be up to .Dv PROPERTY_MAX_VALUE bytes in length. .Pp The .Fn properties_free function is used to free the structure returned by .Fn properties_read when it is no longer needed. .Sh FILE FORMAT Each property in the file is assumed to have the format of .Fa name = value where .Fa name is an alphanumeric string (and any punctuation not including the `=' character) and .Fa value is an arbitrary string of text terminated by a newline character. If newlines are desired, the entire value should be enclosed in { } (curly-bracket) characters. Any line beginning with a # or ; character is assumed to be a comment and will be ignored. .Sh SEE ALSO .Xr auth_getval 3 .Sh AUTHORS .An Jordan Hubbard .Sh BUGS Simplistic. diff --git a/lib/libutil/realhostname.3 b/lib/libutil/realhostname.3 index 0300ed3525b5..28e232567bf9 100644 --- a/lib/libutil/realhostname.3 +++ b/lib/libutil/realhostname.3 @@ -1,104 +1,102 @@ .\" Copyright (c) 1999 Brian Somers .\" All rights reserved. .\" .\" 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. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. .\" .\" $FreeBSD$ .\" .Dd April 6, 1999 .Dt REALHOSTNAME 3 .Os .Sh NAME .Nm realhostname .Nd "convert an IP number to the real host name" .Sh LIBRARY .Lb libutil .Sh SYNOPSIS -.In sys/types.h -.In netinet/in.h .In libutil.h .Ft int .Fn realhostname "char *host" "size_t hsize" "const struct in_addr *ip" .Sh DESCRIPTION The function .Fn realhostname converts .Ar ip to the corresponding host name. This is done by resolving .Ar ip to a host name and then ensuring that the host name resolves back to .Ar ip . .Pp .Ar host must point to a buffer of at least .Ar hsize bytes, and will always be written to by this function. .Pp If the name resolution does not work both ways or if the host name is longer than .Ar hsize bytes, .Xr inet_ntoa 3 is used to convert .Ar ip to an ASCII form. .Pp If the string written to .Ar host is .Ar hsize bytes long, .Ar host will not be NUL terminated. .Sh RETURN VALUES The .Fn realhostname function will return one of the following constants which are defined in .In libutil.h : .Bl -tag -width XXX -offset XXX .It Li HOSTNAME_FOUND A valid host name was found. .It Li HOSTNAME_INCORRECTNAME A host name was found, but it did not resolve back to the passed .Ar ip . .Ar host now contains the numeric value of .Ar ip . .It Li HOSTNAME_INVALIDADDR .Ar ip could not be resolved. .Ar host now contains the numeric value of .Ar ip . .It Li HOSTNAME_INVALIDNAME A host name was found, but it could not be resolved back to any ip number. .Ar host now contains the numeric value of .Ar ip . .El .Sh SEE ALSO .Xr gethostbyaddr 3 , .Xr gethostbyname 3 , .Xr inet_ntoa 3 , .Xr realhostname_sa 3