diff --git a/usr.sbin/moused/Makefile b/usr.sbin/moused/Makefile index 07a389267ab5..014bf6fc3edb 100644 --- a/usr.sbin/moused/Makefile +++ b/usr.sbin/moused/Makefile @@ -3,6 +3,9 @@ PROG= moused MAN= moused.8 +DPADD= ${LIBUTIL} +LDADD= -lutil + #BINMODE=4555 #PRECIOUSPROG= diff --git a/usr.sbin/moused/moused.c b/usr.sbin/moused/moused.c index 617ae371283c..9fa514558dc9 100644 --- a/usr.sbin/moused/moused.c +++ b/usr.sbin/moused/moused.c @@ -61,6 +61,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -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 @@ -907,7 +909,7 @@ moused(void) struct timeval timeout; fd_set fds; u_char b; - FILE *fp; + pid_t mpid; int flags; int c; int i; @@ -916,15 +918,20 @@ moused(void) logerr(1, "cannot open /dev/consolectl"); if (!nodaemon && !background) { + pfh = pidfile_open(pidfile, 0644, &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); } }