MFC pidfile(3) function, and convert several applications to make use
of it. Bump __FreeBSDversion for the MFC. Discussed with: pjd
This commit is contained in:
parent
a06eca80b3
commit
68b438857e
@ -8,15 +8,16 @@ CFLAGS+=-DLIBC_SCCS -I${.CURDIR} -I${.CURDIR}/../libc/gen/
|
||||
CFLAGS+=-DINET6
|
||||
SRCS= _secure_path.c auth.c fparseln.c humanize_number.c login.c \
|
||||
login_auth.c login_cap.c login_class.c login_crypt.c login_ok.c \
|
||||
login_times.c login_tty.c logout.c logwtmp.c property.c pty.c \
|
||||
pw_util.c realhostname.c stub.c \
|
||||
login_times.c login_tty.c logout.c logwtmp.c \
|
||||
pidfile.c property.c pty.c pw_util.c realhostname.c stub.c \
|
||||
trimdomain.c uucplock.c
|
||||
INCS= libutil.h login_cap.h
|
||||
|
||||
MAN+= login.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 pty.3 \
|
||||
login_cap.3 login_class.3 login_times.3 login_ok.3 \
|
||||
_secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \
|
||||
realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3
|
||||
realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 \
|
||||
pidfile.3
|
||||
MAN+= login.conf.5 auth.conf.5
|
||||
MLINKS+= property.3 properties_read.3 property.3 properties_free.3
|
||||
MLINKS+= property.3 property_find.3
|
||||
@ -39,5 +40,9 @@ MLINKS+=login_ok.3 auth_ttyok.3 login_ok.3 auth_hostok.3 \
|
||||
MLINKS+=login_auth.3 auth_checknologin.3 login_auth.3 auth_cat.3
|
||||
MLINKS+=uucplock.3 uu_lock.3 uucplock.3 uu_lock_txfr.3 \
|
||||
uucplock.3 uu_unlock.3 uucplock.3 uu_lockerr.3
|
||||
MLINKS+=pidfile.3 pidfile_open.3 \
|
||||
pidfile.3 pidfile_write.3 \
|
||||
pidfile.3 pidfile_close.3 \
|
||||
pidfile.3 pidfile_remove.3
|
||||
|
||||
.include <bsd.lib.mk>
|
||||
|
@ -49,6 +49,16 @@ typedef struct _property {
|
||||
char *value;
|
||||
} *properties;
|
||||
|
||||
#ifdef _SYS_PARAM_H_
|
||||
/* for pidfile.c */
|
||||
struct pidfh {
|
||||
int pf_fd;
|
||||
char pf_path[MAXPATHLEN + 1];
|
||||
__dev_t pf_dev;
|
||||
ino_t pf_ino;
|
||||
};
|
||||
#endif
|
||||
|
||||
/* Avoid pulling in all the include files for no need */
|
||||
struct termios;
|
||||
struct winsize;
|
||||
@ -102,6 +112,13 @@ struct passwd *pw_scan(const char *_line, int _flags);
|
||||
const char *pw_tempname(void);
|
||||
int pw_tmp(int _mfd);
|
||||
#endif
|
||||
|
||||
#ifdef _SYS_PARAM_H_
|
||||
struct pidfh *pidfile_open(const char *path, mode_t mode, pid_t *pidptr);
|
||||
int pidfile_write(struct pidfh *pfh);
|
||||
int pidfile_close(struct pidfh *pfh);
|
||||
int pidfile_remove(struct pidfh *pfh);
|
||||
#endif
|
||||
__END_DECLS
|
||||
|
||||
#define UU_LOCK_INUSE (1)
|
||||
|
@ -57,7 +57,7 @@
|
||||
* is created, otherwise 1.
|
||||
*/
|
||||
#undef __FreeBSD_version
|
||||
#define __FreeBSD_version 600102 /* Master, propagated to newvers */
|
||||
#define __FreeBSD_version 600103 /* Master, propagated to newvers */
|
||||
|
||||
#ifndef LOCORE
|
||||
#include <sys/types.h>
|
||||
|
@ -44,7 +44,7 @@
|
||||
.Nd find or signal processes by name
|
||||
.Sh SYNOPSIS
|
||||
.Nm pgrep
|
||||
.Op Fl Sfilnovx
|
||||
.Op Fl LSfilnovx
|
||||
.Op Fl F Ar pidfile
|
||||
.Op Fl G Ar gid
|
||||
.Op Fl M Ar core
|
||||
@ -60,7 +60,7 @@
|
||||
.Ar pattern ...
|
||||
.Nm pkill
|
||||
.Op Fl Ar signal
|
||||
.Op Fl finovx
|
||||
.Op Fl Lfinovx
|
||||
.Op Fl F Ar pidfile
|
||||
.Op Fl G Ar gid
|
||||
.Op Fl M Ar core
|
||||
@ -95,6 +95,15 @@ file.
|
||||
Restrict matches to processes with a real group ID in the comma-separated
|
||||
list
|
||||
.Ar gid .
|
||||
.It Fl L
|
||||
The
|
||||
.Ar pidfile
|
||||
file given for the
|
||||
.Fl F
|
||||
option must be locked with the
|
||||
.Xr flock 2
|
||||
syscall or created with
|
||||
.Xr pidfile 3 .
|
||||
.It Fl M Ar core
|
||||
Extract values associated with the name list from the specified core
|
||||
instead of the currently running system.
|
||||
@ -233,8 +242,10 @@ An internal error occurred.
|
||||
.Xr kill 1 ,
|
||||
.Xr killall 1 ,
|
||||
.Xr ps 1 ,
|
||||
.Xr flock 2 ,
|
||||
.Xr kill 2 ,
|
||||
.Xr sigaction 2 ,
|
||||
.Xr pidfile 3 ,
|
||||
.Xr re_format 7
|
||||
.\" Xr signal 7
|
||||
.Sh HISTORY
|
||||
|
@ -124,17 +124,17 @@ void usage(void);
|
||||
void killact(struct kinfo_proc *);
|
||||
void grepact(struct kinfo_proc *);
|
||||
void makelist(struct listhead *, enum listtype, char *);
|
||||
int takepid(const char *);
|
||||
int takepid(const char *, int);
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
|
||||
char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q, *pidfile;
|
||||
const char *execf, *coref;
|
||||
int debug_opt;
|
||||
int i, ch, bestidx, rv, criteria, pidfromfile;
|
||||
int i, ch, bestidx, rv, criteria, pidfromfile, pidfilelock;
|
||||
size_t jsz;
|
||||
void (*action)(struct kinfo_proc *);
|
||||
struct kinfo_proc *kp;
|
||||
@ -176,22 +176,26 @@ main(int argc, char **argv)
|
||||
|
||||
criteria = 0;
|
||||
debug_opt = 0;
|
||||
pidfromfile = -1;
|
||||
pidfile = NULL;
|
||||
pidfilelock = 0;
|
||||
execf = coref = _PATH_DEVNULL;
|
||||
|
||||
while ((ch = getopt(argc, argv, "DF:G:M:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1)
|
||||
while ((ch = getopt(argc, argv, "DF:G:LM:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1)
|
||||
switch (ch) {
|
||||
case 'D':
|
||||
debug_opt++;
|
||||
break;
|
||||
case 'F':
|
||||
pidfromfile = takepid(optarg);
|
||||
pidfile = optarg;
|
||||
criteria = 1;
|
||||
break;
|
||||
case 'G':
|
||||
makelist(&rgidlist, LT_GROUP, optarg);
|
||||
criteria = 1;
|
||||
break;
|
||||
case 'L':
|
||||
pidfilelock = 1;
|
||||
break;
|
||||
case 'M':
|
||||
coref = optarg;
|
||||
break;
|
||||
@ -274,6 +278,13 @@ main(int argc, char **argv)
|
||||
usage();
|
||||
if (newest && oldest)
|
||||
errx(STATUS_ERROR, "-n and -o are mutually exclusive");
|
||||
if (pidfile != NULL)
|
||||
pidfromfile = takepid(pidfile, pidfilelock);
|
||||
else {
|
||||
if (pidfilelock)
|
||||
errx(STATUS_ERROR, "-L doesn't make sense without -F");
|
||||
pidfromfile = -1;
|
||||
}
|
||||
|
||||
mypid = getpid();
|
||||
|
||||
@ -497,9 +508,9 @@ usage(void)
|
||||
const char *ustr;
|
||||
|
||||
if (pgrep)
|
||||
ustr = "[-Sfilnovx] [-d delim]";
|
||||
ustr = "[-LSfilnovx] [-d delim]";
|
||||
else
|
||||
ustr = "[-signal] [-finovx]";
|
||||
ustr = "[-signal] [-Lfinovx]";
|
||||
|
||||
fprintf(stderr,
|
||||
"usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n"
|
||||
@ -631,7 +642,7 @@ makelist(struct listhead *head, enum listtype type, char *src)
|
||||
}
|
||||
|
||||
int
|
||||
takepid(const char *pidfile)
|
||||
takepid(const char *pidfile, int pidfilelock)
|
||||
{
|
||||
char *endp, line[BUFSIZ];
|
||||
FILE *fh;
|
||||
@ -641,6 +652,22 @@ takepid(const char *pidfile)
|
||||
if (fh == NULL)
|
||||
err(STATUS_ERROR, "can't open pid file `%s'", pidfile);
|
||||
|
||||
if (pidfilelock) {
|
||||
/*
|
||||
* If we can lock pidfile, this means that daemon is not
|
||||
* running, so would be better not to kill some random process.
|
||||
*/
|
||||
if (flock(fileno(fh), LOCK_EX | LOCK_NB) == 0) {
|
||||
(void)fclose(fh);
|
||||
errx(STATUS_ERROR, "file '%s' can be locked", pidfile);
|
||||
} else {
|
||||
if (errno != EWOULDBLOCK) {
|
||||
errx(STATUS_ERROR,
|
||||
"error while locking file '%s'", pidfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fgets(line, sizeof(line), fh) == NULL) {
|
||||
if (feof(fh)) {
|
||||
(void)fclose(fh);
|
||||
|
@ -46,6 +46,7 @@ static void usage __P((void)),
|
||||
|
||||
static time_t last_time = 0;
|
||||
static int dst_enabled = 0;
|
||||
struct pidfh *pfh;
|
||||
|
||||
static void
|
||||
usage() {
|
||||
@ -63,6 +64,28 @@ usage() {
|
||||
exit(ERROR_EXIT);
|
||||
}
|
||||
|
||||
static void
|
||||
open_pidfile(void)
|
||||
{
|
||||
char pidfile[MAX_FNAME];
|
||||
char buf[MAX_TEMPSTR];
|
||||
int otherpid;
|
||||
|
||||
(void) snprintf(pidfile, sizeof(pidfile), PIDFILE, PIDDIR);
|
||||
pfh = pidfile_open(pidfile, 0600, &otherpid);
|
||||
if (pfh == NULL) {
|
||||
if (errno == EEXIST) {
|
||||
snprintf(buf, sizeof(buf),
|
||||
"cron already running, pid: %d", otherpid);
|
||||
} else {
|
||||
snprintf(buf, sizeof(buf),
|
||||
"can't open or create %s: %s", pidfile,
|
||||
strerror(errno));
|
||||
}
|
||||
log_it("CRON", getpid(), "DEATH", buf);
|
||||
errx(ERROR_EXIT, "%s", buf);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(argc, argv)
|
||||
@ -87,7 +110,7 @@ main(argc, argv)
|
||||
#endif
|
||||
(void) signal(SIGHUP, sighup_handler);
|
||||
|
||||
acquire_daemonlock(0);
|
||||
open_pidfile();
|
||||
set_cron_uid();
|
||||
set_cron_cwd();
|
||||
|
||||
@ -105,12 +128,13 @@ main(argc, argv)
|
||||
(void) fprintf(stderr, "[%d] cron started\n", getpid());
|
||||
} else {
|
||||
if (daemon(1, 0) == -1) {
|
||||
pidfile_remove(pfh);
|
||||
log_it("CRON",getpid(),"DEATH","can't become daemon");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
acquire_daemonlock(0);
|
||||
pidfile_write(pfh);
|
||||
database.head = NULL;
|
||||
database.tail = NULL;
|
||||
database.mtime = (time_t) 0;
|
||||
|
@ -34,6 +34,7 @@
|
||||
#include <ctype.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <libutil.h>
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
@ -210,7 +211,6 @@ void set_cron_uid __P((void)),
|
||||
env_free __P((char **)),
|
||||
unget_char __P((int, FILE *)),
|
||||
free_entry __P((entry *)),
|
||||
acquire_daemonlock __P((int)),
|
||||
skip_comments __P((FILE *)),
|
||||
log_it __P((char *, int, char *, char *)),
|
||||
log_close __P((void));
|
||||
@ -289,6 +289,7 @@ extern int LineNumber;
|
||||
extern unsigned Jitter,
|
||||
RootJitter;
|
||||
extern time_t TargetTime;
|
||||
extern struct pidfh *pfh;
|
||||
# if DEBUGGING
|
||||
extern int DebugFlags;
|
||||
extern char *DebugFlagNames[];
|
||||
|
@ -59,7 +59,7 @@ do_command(e, u)
|
||||
break;
|
||||
case 0:
|
||||
/* child process */
|
||||
acquire_daemonlock(1);
|
||||
pidfile_close(pfh);
|
||||
child_process(e, u);
|
||||
Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
|
||||
_exit(OK_EXIT);
|
||||
|
@ -222,65 +222,6 @@ set_cron_cwd()
|
||||
}
|
||||
|
||||
|
||||
/* acquire_daemonlock() - write our PID into /etc/cron.pid, unless
|
||||
* another daemon is already running, which we detect here.
|
||||
*
|
||||
* note: main() calls us twice; once before forking, once after.
|
||||
* we maintain static storage of the file pointer so that we
|
||||
* can rewrite our PID into the PIDFILE after the fork.
|
||||
*
|
||||
* it would be great if fflush() disassociated the file buffer.
|
||||
*/
|
||||
void
|
||||
acquire_daemonlock(closeflag)
|
||||
int closeflag;
|
||||
{
|
||||
static FILE *fp = NULL;
|
||||
|
||||
if (closeflag && fp) {
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!fp) {
|
||||
char pidfile[MAX_FNAME];
|
||||
char buf[MAX_TEMPSTR];
|
||||
int fd, otherpid;
|
||||
|
||||
(void) sprintf(pidfile, PIDFILE, PIDDIR);
|
||||
if ((-1 == (fd = open(pidfile, O_RDWR|O_CREAT, 0644)))
|
||||
|| (NULL == (fp = fdopen(fd, "r+")))
|
||||
) {
|
||||
sprintf(buf, "can't open or create %s: %s",
|
||||
pidfile, strerror(errno));
|
||||
log_it("CRON", getpid(), "DEATH", buf);
|
||||
errx(ERROR_EXIT, "%s", buf);
|
||||
}
|
||||
|
||||
if (flock(fd, LOCK_EX|LOCK_NB) < OK) {
|
||||
int save_errno = errno;
|
||||
|
||||
fscanf(fp, "%d", &otherpid);
|
||||
sprintf(buf, "can't lock %s, otherpid may be %d: %s",
|
||||
pidfile, otherpid, strerror(save_errno));
|
||||
log_it("CRON", getpid(), "DEATH", buf);
|
||||
errx(ERROR_EXIT, "%s", buf);
|
||||
}
|
||||
|
||||
(void) fcntl(fd, F_SETFD, 1);
|
||||
}
|
||||
|
||||
rewind(fp);
|
||||
fprintf(fp, "%d\n", getpid());
|
||||
fflush(fp);
|
||||
(void) ftruncate(fileno(fp), ftell(fp));
|
||||
|
||||
/* abandon fd and fp even though the file is open. we need to
|
||||
* keep it open and locked, but we don't need the handles elsewhere.
|
||||
*/
|
||||
}
|
||||
|
||||
/* get_char(file) : like getc() but increment LineNumber on newlines
|
||||
*/
|
||||
int
|
||||
|
@ -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]);
|
||||
|
@ -275,6 +275,7 @@ extern struct biltin biltins[];
|
||||
|
||||
const char *CONFIG = _PATH_INETDCONF;
|
||||
const char *pid_file = _PATH_INETDPID;
|
||||
struct pidfh *pfh = NULL;
|
||||
|
||||
struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
|
||||
|
||||
@ -464,7 +465,18 @@ main(int argc, char **argv)
|
||||
if (access(CONFIG, R_OK) < 0)
|
||||
syslog(LOG_ERR, "Accessing %s: %m, continuing anyway.", CONFIG);
|
||||
if (debug == 0) {
|
||||
FILE *fp;
|
||||
pid_t otherpid;
|
||||
|
||||
pfh = pidfile_open(pid_file, 0600, &otherpid);
|
||||
if (pfh == NULL) {
|
||||
if (errno == EEXIST) {
|
||||
syslog(LOG_ERR, "%s already running, pid: %d",
|
||||
getprogname(), otherpid);
|
||||
exit(EX_OSERR);
|
||||
}
|
||||
syslog(LOG_WARNING, "pidfile_open() failed: %m");
|
||||
}
|
||||
|
||||
if (daemon(0, 0) < 0) {
|
||||
syslog(LOG_WARNING, "daemon(0,0) failed: %m");
|
||||
}
|
||||
@ -480,13 +492,8 @@ main(int argc, char **argv)
|
||||
syslog(LOG_WARNING, "cannot clear logname: %m");
|
||||
/* no big deal if it fails.. */
|
||||
}
|
||||
pid = getpid();
|
||||
fp = fopen(pid_file, "w");
|
||||
if (fp) {
|
||||
fprintf(fp, "%ld\n", (long)pid);
|
||||
fclose(fp);
|
||||
} else {
|
||||
syslog(LOG_WARNING, "%s: %m", pid_file);
|
||||
if (pfh != NULL && pidfile_write(pfh) == -1) {
|
||||
syslog(LOG_WARNING, "pidfile_write(): %m");
|
||||
}
|
||||
}
|
||||
|
||||
@ -721,6 +728,7 @@ main(int argc, char **argv)
|
||||
}
|
||||
sigsetmask(0L);
|
||||
if (pid == 0) {
|
||||
pidfile_close(pfh);
|
||||
if (dofork) {
|
||||
sigaction(SIGALRM, &saalrm, (struct sigaction *)0);
|
||||
sigaction(SIGCHLD, &sachld, (struct sigaction *)0);
|
||||
|
@ -6,4 +6,7 @@ MAN= exports.5 netgroup.5 mountd.8
|
||||
|
||||
WARNS?= 2
|
||||
|
||||
DPADD= ${LIBUTIL}
|
||||
LDADD= -lutil
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
@ -73,6 +73,7 @@ __FBSDID("$FreeBSD$");
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <grp.h>
|
||||
#include <libutil.h>
|
||||
#include <limits.h>
|
||||
#include <netdb.h>
|
||||
#include <pwd.h>
|
||||
@ -87,10 +88,6 @@ __FBSDID("$FreeBSD$");
|
||||
#include <stdarg.h>
|
||||
#endif
|
||||
|
||||
#ifndef MOUNTDLOCK
|
||||
#define MOUNTDLOCK "/var/run/mountd.lock"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Structures for keeping the mount list and export list
|
||||
*/
|
||||
@ -228,7 +225,7 @@ int got_sighup = 0;
|
||||
int opt_flags;
|
||||
static int have_v6 = 1;
|
||||
|
||||
int mountdlockfd;
|
||||
struct pidfh *pfh = NULL;
|
||||
/* Bits for opt_flags above */
|
||||
#define OP_MAPROOT 0x01
|
||||
#define OP_MAPALL 0x02
|
||||
@ -266,6 +263,7 @@ main(argc, argv)
|
||||
char *endptr;
|
||||
SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
|
||||
struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
|
||||
pid_t otherpid;
|
||||
int udpsock, tcpsock, udp6sock, tcp6sock;
|
||||
int xcreated = 0, s;
|
||||
int maxrec = RPC_MAXDATASIZE;
|
||||
@ -277,11 +275,13 @@ main(argc, argv)
|
||||
udp6sock = tcp6sock = 0;
|
||||
|
||||
/* Check that another mountd isn't already running. */
|
||||
if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
|
||||
err(1, "%s", MOUNTDLOCK);
|
||||
pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
|
||||
if (pfh == NULL) {
|
||||
if (errno == EEXIST)
|
||||
errx(1, "mountd already running, pid: %d.", otherpid);
|
||||
warn("cannot open or create pidfile");
|
||||
}
|
||||
|
||||
if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
|
||||
errx(1, "another mountd is already running. Aborting");
|
||||
s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (s < 0)
|
||||
have_v6 = 0;
|
||||
@ -346,12 +346,9 @@ main(argc, argv)
|
||||
}
|
||||
signal(SIGHUP, huphandler);
|
||||
signal(SIGTERM, terminate);
|
||||
{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
|
||||
if (pidfile != NULL) {
|
||||
fprintf(pidfile, "%d\n", getpid());
|
||||
fclose(pidfile);
|
||||
}
|
||||
}
|
||||
|
||||
pidfile_write(pfh);
|
||||
|
||||
rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
|
||||
rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
|
||||
udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
@ -2531,8 +2528,7 @@ huphandler(int sig)
|
||||
void terminate(sig)
|
||||
int sig;
|
||||
{
|
||||
close(mountdlockfd);
|
||||
unlink(MOUNTDLOCK);
|
||||
pidfile_remove(pfh);
|
||||
rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
|
||||
rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
|
||||
exit (0);
|
||||
|
@ -3,6 +3,9 @@
|
||||
PROG= moused
|
||||
MAN= moused.8
|
||||
|
||||
DPADD= ${LIBUTIL}
|
||||
LDADD= -lutil
|
||||
|
||||
#BINMODE=4555
|
||||
#PRECIOUSPROG=
|
||||
|
||||
|
@ -61,6 +61,7 @@ __FBSDID("$FreeBSD$");
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <libutil.h>
|
||||
#include <limits.h>
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
@ -161,6 +162,7 @@ int background = FALSE;
|
||||
int identify = ID_NONE;
|
||||
int extioctl = FALSE;
|
||||
char *pidfile = "/var/run/moused.pid";
|
||||
struct pidfh *pfh;
|
||||
|
||||
#define SCROLL_NOTSCROLLING 0
|
||||
#define SCROLL_PREPARE 1
|
||||
@ -940,7 +942,7 @@ moused(void)
|
||||
struct timeval timeout;
|
||||
fd_set fds;
|
||||
u_char b;
|
||||
FILE *fp;
|
||||
pid_t mpid;
|
||||
int flags;
|
||||
int c;
|
||||
int i;
|
||||
@ -949,15 +951,20 @@ moused(void)
|
||||
logerr(1, "cannot open /dev/consolectl");
|
||||
|
||||
if (!nodaemon && !background) {
|
||||
pfh = pidfile_open(pidfile, 0600, &mpid);
|
||||
if (pfh == NULL) {
|
||||
if (errno == EEXIST)
|
||||
logerrx(1, "moused already running, pid: %d", mpid);
|
||||
logwarn("cannot open pid file");
|
||||
}
|
||||
if (daemon(0, 0)) {
|
||||
int saved_errno = errno;
|
||||
pidfile_remove(pfh);
|
||||
errno = saved_errno;
|
||||
logerr(1, "failed to become a daemon");
|
||||
} else {
|
||||
background = TRUE;
|
||||
fp = fopen(pidfile, "w");
|
||||
if (fp != NULL) {
|
||||
fprintf(fp, "%d\n", getpid());
|
||||
fclose(fp);
|
||||
}
|
||||
pidfile_write(pfh);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,4 +4,7 @@ PROG= powerd
|
||||
MAN= powerd.8
|
||||
WARNS?= 6
|
||||
|
||||
DPADD= ${LIBUTIL}
|
||||
LDADD= -lutil
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
@ -37,6 +37,7 @@
|
||||
.Op Fl i Ar percent
|
||||
.Op Fl n Ar mode
|
||||
.Op Fl p Ar ival
|
||||
.Op Fl P Ar pidfile
|
||||
.Op Fl r Ar percent
|
||||
.Op Fl v
|
||||
.Sh DESCRIPTION
|
||||
@ -82,6 +83,10 @@ to use normally when the AC line state is unknown.
|
||||
Specifies a different polling interval (in milliseconds) for AC line state
|
||||
and system idle levels.
|
||||
The default is 500 ms.
|
||||
.It Fl P
|
||||
Specify an alternative file in which to store the process ID.
|
||||
The default is
|
||||
.Pa /var/run/powerd.pid .
|
||||
.It Fl r Ar percent
|
||||
Specifies the CPU idle percent level where
|
||||
adaptive
|
||||
|
@ -28,9 +28,15 @@
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <libutil.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -41,10 +47,6 @@ __FBSDID("$FreeBSD$");
|
||||
#include <machine/apm_bios.h>
|
||||
#endif
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
#define DEFAULT_ACTIVE_PERCENT 65
|
||||
#define DEFAULT_IDLE_PERCENT 90
|
||||
#define DEFAULT_POLL_INTERVAL 500 /* Poll interval in milliseconds */
|
||||
@ -247,13 +249,15 @@ usage(void)
|
||||
{
|
||||
|
||||
fprintf(stderr,
|
||||
"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%]\n");
|
||||
"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%] [-P pidfile]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char * argv[])
|
||||
{
|
||||
struct pidfh *pfh = NULL;
|
||||
const char *pidfile = NULL;
|
||||
long idle, total;
|
||||
int curfreq, *freqs, i, *mwatts, numfreqs;
|
||||
int ch, mode_ac, mode_battery, mode_none, acline, mode, vflag;
|
||||
@ -273,7 +277,7 @@ main(int argc, char * argv[])
|
||||
if (geteuid() != 0)
|
||||
errx(1, "must be root to run");
|
||||
|
||||
while ((ch = getopt(argc, argv, "a:b:i:n:p:r:v")) != EOF)
|
||||
while ((ch = getopt(argc, argv, "a:b:i:n:p:P:r:v")) != EOF)
|
||||
switch (ch) {
|
||||
case 'a':
|
||||
parse_mode(optarg, &mode_ac, ch);
|
||||
@ -299,6 +303,9 @@ main(int argc, char * argv[])
|
||||
usage();
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
pidfile = optarg;
|
||||
break;
|
||||
case 'r':
|
||||
cpu_running_mark = atoi(optarg);
|
||||
if (cpu_running_mark < 0 || cpu_running_mark > 100) {
|
||||
@ -344,8 +351,20 @@ main(int argc, char * argv[])
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
/* Run in the background unless in verbose mode. */
|
||||
if (!vflag)
|
||||
if (!vflag) {
|
||||
pid_t otherpid;
|
||||
|
||||
pfh = pidfile_open(pidfile, 0600, &otherpid);
|
||||
if (pfh == NULL) {
|
||||
if (errno == EEXIST) {
|
||||
errx(1, "powerd already running, pid: %d",
|
||||
otherpid);
|
||||
}
|
||||
warn("cannot open pid file");
|
||||
}
|
||||
daemon(0, 0);
|
||||
pidfile_write(pfh);
|
||||
}
|
||||
|
||||
/* Decide whether to use ACPI or APM to read the AC line status. */
|
||||
acline_init();
|
||||
@ -477,6 +496,8 @@ main(int argc, char * argv[])
|
||||
}
|
||||
free(freqs);
|
||||
free(mwatts);
|
||||
if (!vflag)
|
||||
pidfile_remove(pfh);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
@ -297,6 +297,7 @@ static int LogFacPri; /* Put facility and priority in log message: */
|
||||
/* 0=no, 1=numeric, 2=names */
|
||||
static int KeepKernFac; /* Keep remotely logged kernel facility */
|
||||
static int needdofsync = 0; /* Are any file(s) waiting to be fsynced? */
|
||||
static struct pidfh *pfh;
|
||||
|
||||
volatile sig_atomic_t MarkSet, WantDie;
|
||||
|
||||
@ -339,14 +340,13 @@ main(int argc, char *argv[])
|
||||
struct sockaddr_un sunx, fromunix;
|
||||
struct sockaddr_storage frominet;
|
||||
fd_set *fdsr = NULL;
|
||||
FILE *fp;
|
||||
char line[MAXLINE + 1];
|
||||
const char *bindhostname, *hname;
|
||||
struct timeval tv, *tvp;
|
||||
struct sigaction sact;
|
||||
struct funix *fx, *fx1;
|
||||
sigset_t mask;
|
||||
pid_t ppid = 1;
|
||||
pid_t ppid = 1, spid;
|
||||
socklen_t len;
|
||||
|
||||
bindhostname = NULL;
|
||||
@ -456,10 +456,20 @@ main(int argc, char *argv[])
|
||||
if ((argc -= optind) != 0)
|
||||
usage();
|
||||
|
||||
pfh = pidfile_open(PidFile, 0600, &spid);
|
||||
if (pfh == NULL) {
|
||||
if (errno == EEXIST)
|
||||
errx(1, "syslogd already running, pid: %d", spid);
|
||||
warn("cannot open pid file");
|
||||
}
|
||||
|
||||
if (!Debug) {
|
||||
ppid = waitdaemon(0, 0, 30);
|
||||
if (ppid < 0)
|
||||
err(1, "could not become daemon");
|
||||
if (ppid < 0) {
|
||||
warn("could not become daemon");
|
||||
pidfile_remove(pfh);
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
setlinebuf(stdout);
|
||||
}
|
||||
@ -542,11 +552,7 @@ main(int argc, char *argv[])
|
||||
dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
|
||||
|
||||
/* tuck my process id away */
|
||||
fp = fopen(PidFile, "w");
|
||||
if (fp != NULL) {
|
||||
fprintf(fp, "%d\n", getpid());
|
||||
(void)fclose(fp);
|
||||
}
|
||||
pidfile_write(pfh);
|
||||
|
||||
dprintf("off & running....\n");
|
||||
|
||||
@ -1476,6 +1482,7 @@ die(int signo)
|
||||
}
|
||||
STAILQ_FOREACH(fx, &funixes, next)
|
||||
(void)unlink(fx->name);
|
||||
pidfile_remove(pfh);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ LINKS= ${BINDIR}/watchdogd ${BINDIR}/watchdog
|
||||
MAN= watchdogd.8 watchdog.8
|
||||
WARNS?= 6
|
||||
|
||||
LDADD= -lm
|
||||
DPADD= ${LIBM}
|
||||
LDADD= -lm -lutil
|
||||
DPADD= ${LIBM} ${LIBUTIL}
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include <sys/types.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/rtprio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
@ -39,6 +40,7 @@ __FBSDID("$FreeBSD$");
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <libutil.h>
|
||||
#include <math.h>
|
||||
#include <paths.h>
|
||||
#include <signal.h>
|
||||
@ -75,7 +77,8 @@ int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct rtprio rtp;
|
||||
FILE *fp;
|
||||
struct pidfh *pfh;
|
||||
pid_t otherpid;
|
||||
|
||||
if (getuid() != 0)
|
||||
errx(EX_SOFTWARE, "not super user");
|
||||
@ -94,8 +97,18 @@ main(int argc, char *argv[])
|
||||
if (watchdog_onoff(1) == -1)
|
||||
exit(EX_SOFTWARE);
|
||||
|
||||
pfh = pidfile_open(pidfile, 0600, &otherpid);
|
||||
if (pfh == NULL) {
|
||||
if (errno == EEXIST) {
|
||||
errx(EX_SOFTWARE, "%s already running, pid: %d",
|
||||
getprogname(), otherpid);
|
||||
}
|
||||
warn("Cannot open or create pidfile");
|
||||
}
|
||||
|
||||
if (debugging == 0 && daemon(0, 0) == -1) {
|
||||
watchdog_onoff(0);
|
||||
pidfile_remove(pfh);
|
||||
err(EX_OSERR, "daemon");
|
||||
}
|
||||
|
||||
@ -103,17 +116,13 @@ main(int argc, char *argv[])
|
||||
signal(SIGINT, sighandler);
|
||||
signal(SIGTERM, sighandler);
|
||||
|
||||
fp = fopen(pidfile, "w");
|
||||
if (fp != NULL) {
|
||||
fprintf(fp, "%d\n", getpid());
|
||||
fclose(fp);
|
||||
}
|
||||
pidfile_write(pfh);
|
||||
|
||||
watchdog_loop();
|
||||
|
||||
/* exiting */
|
||||
watchdog_onoff(0);
|
||||
unlink(pidfile);
|
||||
pidfile_remove(pfh);
|
||||
return (EX_OK);
|
||||
} else {
|
||||
if (passive)
|
||||
|
Loading…
x
Reference in New Issue
Block a user