Give daemon(8) the ability to create a pid-file. Since the target program
does not know anything about the pid-file and we don't keep a babysitting process for the task, the pid-file will linger. Submitted by: mi PR: 56398
This commit is contained in:
parent
dc03363dd8
commit
846be7bd0e
@ -35,6 +35,7 @@
|
|||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl cf
|
.Op Fl cf
|
||||||
|
.Op Fl p Ar pidfile
|
||||||
.Ar command arguments ...
|
.Ar command arguments ...
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
The
|
The
|
||||||
@ -50,13 +51,20 @@ Change the current working directory to the root
|
|||||||
.It Fl f
|
.It Fl f
|
||||||
Redirect standard input, standard output and standard error to
|
Redirect standard input, standard output and standard error to
|
||||||
.Pa /dev/null .
|
.Pa /dev/null .
|
||||||
|
.It Fl p Ar file
|
||||||
|
Write the id of the created process into the
|
||||||
|
.Ar file .
|
||||||
|
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).
|
||||||
.El
|
.El
|
||||||
.Sh DIAGNOSTICS
|
.Sh DIAGNOSTICS
|
||||||
The
|
The
|
||||||
.Nm
|
.Nm
|
||||||
utility exits 1 if an error is returned by the
|
utility exits 1 if an error is returned by the
|
||||||
.Xr daemon 3
|
.Xr daemon 3
|
||||||
library routine, otherwise 0.
|
library routine, 2 if the pid-file is requested, but can not be opened,
|
||||||
|
otherwise 0.
|
||||||
If the command cannot be executed, an error message is displayed on
|
If the command cannot be executed, an error message is displayed on
|
||||||
standard error unless the
|
standard error unless the
|
||||||
.Fl f
|
.Fl f
|
||||||
|
@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -43,10 +44,13 @@ static void usage(void);
|
|||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ch, nochdir, noclose;
|
int ch, nochdir, noclose, errcode;
|
||||||
|
FILE *pidf;
|
||||||
|
const char *pidfile;
|
||||||
|
|
||||||
nochdir = noclose = 1;
|
nochdir = noclose = 1;
|
||||||
while ((ch = getopt(argc, argv, "-cf")) != -1) {
|
pidfile = NULL;
|
||||||
|
while ((ch = getopt(argc, argv, "-cfp:")) != -1) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'c':
|
case 'c':
|
||||||
nochdir = 0;
|
nochdir = 0;
|
||||||
@ -54,7 +58,9 @@ main(int argc, char *argv[])
|
|||||||
case 'f':
|
case 'f':
|
||||||
noclose = 0;
|
noclose = 0;
|
||||||
break;
|
break;
|
||||||
case '?':
|
case 'p':
|
||||||
|
pidfile = optarg;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
@ -64,17 +70,43 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (argc == 0)
|
if (argc == 0)
|
||||||
usage();
|
usage();
|
||||||
|
/*
|
||||||
|
* Try to open the pidfile before calling daemon(3),
|
||||||
|
* to be able to report the error intelligently
|
||||||
|
*/
|
||||||
|
if (pidfile) {
|
||||||
|
pidf = fopen(pidfile, "w");
|
||||||
|
if (pidf == NULL)
|
||||||
|
err(2, "pidfile ``%s''", pidfile);
|
||||||
|
}
|
||||||
|
|
||||||
if (daemon(nochdir, noclose) == -1)
|
if (daemon(nochdir, noclose) == -1)
|
||||||
err(1, NULL);
|
err(1, NULL);
|
||||||
|
|
||||||
|
/* Now that we are the child, write out the pid */
|
||||||
|
if (pidfile) {
|
||||||
|
fprintf(pidf, "%lu\n", (unsigned long)getpid());
|
||||||
|
fclose(pidf);
|
||||||
|
}
|
||||||
|
|
||||||
execvp(argv[0], argv);
|
execvp(argv[0], argv);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* execvp() failed -- unlink pidfile if any, and
|
||||||
|
* report the error
|
||||||
|
*/
|
||||||
|
errcode = errno; /* Preserve errcode -- unlink may reset it */
|
||||||
|
if (pidfile)
|
||||||
|
unlink(pidfile);
|
||||||
|
|
||||||
/* The child is now running, so the exit status doesn't matter. */
|
/* The child is now running, so the exit status doesn't matter. */
|
||||||
err(1, "%s", argv[0]);
|
errc(1, errcode, "%s", argv[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
(void)fprintf(stderr, "usage: daemon [-cf] command arguments ...\n");
|
(void)fprintf(stderr,
|
||||||
|
"usage: daemon [-cf] [-p pidfile] command arguments ...\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user