mirror of
https://github.com/freebsd/freebsd-src.git
synced 2024-12-01 08:33:24 +00:00
Teach daemon(8) how to use pidfile(3).
This commit is contained in:
parent
412fa8f114
commit
c6262cb601
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=149424
@ -3,6 +3,9 @@
|
||||
PROG= daemon
|
||||
MAN= daemon.8
|
||||
|
||||
DPADD= ${LIBUTIL}
|
||||
LDADD= -lutil
|
||||
|
||||
WARNS?= 2
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
@ -53,7 +53,10 @@ Redirect standard input, standard output and standard error to
|
||||
.Pa /dev/null .
|
||||
.It Fl p Ar file
|
||||
Write the ID of the created process into the
|
||||
.Ar file .
|
||||
.Ar file
|
||||
using
|
||||
.Xr pidfile 3
|
||||
functionality.
|
||||
Note, that the file will be created shortly before the process is
|
||||
actually executed, and will remain after the process exits (although
|
||||
it will be removed if the execution fails).
|
||||
@ -65,7 +68,8 @@ utility exits 1 if an error is returned by the
|
||||
.Xr daemon 3
|
||||
library routine, 2 if the
|
||||
.Ar pidfile
|
||||
is requested, but cannot be opened,
|
||||
is requested, but cannot be opened, 3 if process is already running (pidfile
|
||||
exists and is locked),
|
||||
otherwise 0.
|
||||
.Sh DIAGNOSTICS
|
||||
If the command cannot be executed, an error message is displayed on
|
||||
@ -75,6 +79,7 @@ flag is specified.
|
||||
.Sh SEE ALSO
|
||||
.Xr daemon 3 ,
|
||||
.Xr exec 3 ,
|
||||
.Xr pidfile 3 ,
|
||||
.Xr termios 4 ,
|
||||
.Xr tty 4
|
||||
.Sh HISTORY
|
||||
|
@ -31,10 +31,11 @@
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <libutil.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
@ -44,9 +45,10 @@ static void usage(void);
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct pidfh *pfh;
|
||||
int ch, nochdir, noclose, errcode;
|
||||
FILE *pidf;
|
||||
const char *pidfile;
|
||||
pid_t otherpid;
|
||||
|
||||
nochdir = noclose = 1;
|
||||
pidfile = NULL;
|
||||
@ -75,19 +77,22 @@ main(int argc, char *argv[])
|
||||
* to be able to report the error intelligently
|
||||
*/
|
||||
if (pidfile) {
|
||||
pidf = fopen(pidfile, "w");
|
||||
if (pidf == NULL)
|
||||
pfh = pidfile_open(pidfile, 0600, &otherpid);
|
||||
if (pfh == NULL) {
|
||||
if (errno == EEXIST) {
|
||||
errx(3, "process already running, pid: %d",
|
||||
otherpid);
|
||||
}
|
||||
err(2, "pidfile ``%s''", pidfile);
|
||||
}
|
||||
}
|
||||
|
||||
if (daemon(nochdir, noclose) == -1)
|
||||
err(1, NULL);
|
||||
|
||||
/* Now that we are the child, write out the pid */
|
||||
if (pidfile) {
|
||||
fprintf(pidf, "%lu\n", (unsigned long)getpid());
|
||||
fclose(pidf);
|
||||
}
|
||||
if (pidfile)
|
||||
pidfile_write(pfh);
|
||||
|
||||
execvp(argv[0], argv);
|
||||
|
||||
@ -97,7 +102,7 @@ main(int argc, char *argv[])
|
||||
*/
|
||||
errcode = errno; /* Preserve errcode -- unlink may reset it */
|
||||
if (pidfile)
|
||||
unlink(pidfile);
|
||||
pidfile_remove(pfh);
|
||||
|
||||
/* The child is now running, so the exit status doesn't matter. */
|
||||
errc(1, errcode, "%s", argv[0]);
|
||||
|
Loading…
Reference in New Issue
Block a user