added random stuff, including checking to make sure we don't fill the disk.

This commit is contained in:
cgd 1993-05-03 03:06:32 +00:00
parent d7f7dfe281
commit 4ad13c0905
2 changed files with 64 additions and 11 deletions

View File

@ -24,7 +24,7 @@
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE. .\" POSSIBILITY OF SUCH DAMAGE.
.\" .\"
.\" $Id: accountant.8,v 1.2 1993/05/03 01:45:34 cgd Exp $ .\" $Id: accountant.8,v 1.3 1993/05/03 03:06:32 cgd Exp $
.\" .\"
.Dd May 2, 1993 .Dd May 2, 1993
.Dt ACCOUNTANT 8 .Dt ACCOUNTANT 8
@ -35,6 +35,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm accountant .Nm accountant
.Op Fl d .Op Fl d
.Op Fl s
.Op Fl f Ar log file .Op Fl f Ar log file
.Op Fl F Ar accounting device .Op Fl F Ar accounting device
.Sh DESCRIPTION .Sh DESCRIPTION
@ -56,6 +57,11 @@ to the terminal.
.El .El
.Pp .Pp
.Bl -tag -width Fl .Bl -tag -width Fl
.It Fl s
Sync the accounting log after every write.
.El
.Pp
.Bl -tag -width Fl
.It Fl f Ar log file .It Fl f Ar log file
Write accounting records to Write accounting records to
.Ar log file .Ar log file

View File

@ -26,15 +26,17 @@
*/ */
#ifndef LINT #ifndef LINT
static char *rcsid = "$Id: accountant.c,v 1.1 1993/05/03 01:02:52 cgd Exp $"; static char *rcsid = "$Id: accountant.c,v 1.2 1993/05/03 03:06:33 cgd Exp $";
#endif #endif
#include <sys/types.h> #include <sys/types.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/acct.h> #include <sys/acct.h>
#include <sys/mount.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <signal.h> #include <signal.h>
#include <syslog.h>
#include "pathnames.h" #include "pathnames.h"
@ -42,6 +44,13 @@ char *acctfile = _PATH_ACCTLOG;
char *acctdev = _PATH_ACCTDEV; char *acctdev = _PATH_ACCTDEV;
int devfd = -1; int devfd = -1;
int filefd = -1; int filefd = -1;
int min_on = 2;
int max_off = 4;
int sleeptime = 15;
int debug;
#define dprintf if (debug) printf
#define dperror if (debug) perror
void usage(char *progname) void usage(char *progname)
{ {
@ -63,12 +72,43 @@ void reinitfiles(int num)
} }
} }
void checkspace()
{
struct statfs fs;
int freespace = 1;
int slept = 0;
do {
if (fstatfs(filefd, &fs) < 0) {
perror("checkspace");
freespace = 0;
} else {
if (fs.f_bavail < fs.f_blocks / (100 / min_on)) {
freespace = 0;
} else if (fs.f_bavail > fs.f_blocks / (100 / max_off)) {
freespace = 1;
}
}
if (!freespace) {
if (!slept) syslog(LOG_NOTICE, "Accounting suspended");
dprintf("going to sleep...\n");
sleep(sleeptime);
slept = 1;
}
} while (!freespace);
if (slept) {
syslog(LOG_NOTICE, "Accounting resumed");
dprintf("awake and happy...\n");
}
}
main(argc, argv) main(argc, argv)
int argc; int argc;
char **argv; char **argv;
{ {
int ch; int ch;
int debug = 0; int dosync = 0;
extern char *optarg; extern char *optarg;
extern int optind; extern int optind;
FILE *pidf; FILE *pidf;
@ -78,6 +118,9 @@ main(argc, argv)
case 'd': /* debug it */ case 'd': /* debug it */
debug = 1; debug = 1;
break; break;
case 's': /* force syncs after writes */
dosync = 1;
break;
case 'f': /* set log file */ case 'f': /* set log file */
acctfile = optarg; acctfile = optarg;
break; break;
@ -93,10 +136,12 @@ main(argc, argv)
if (argc -= optind) if (argc -= optind)
usage(argv[0]); usage(argv[0]);
openlog("accountant", LOG_PERROR, LOG_DAEMON);
if (!debug) if (!debug)
daemon(0, 0); daemon(0, 0);
if (debug) printf("%s: in debugging mode\n", argv[0]); dprintf("%s: in debugging mode\n", argv[0]);
signal(SIGHUP, reinitfiles); signal(SIGHUP, reinitfiles);
reinitfiles(0); reinitfiles(0);
@ -112,22 +157,24 @@ main(argc, argv)
rv = read(devfd, &abuf, sizeof(struct acct)); rv = read(devfd, &abuf, sizeof(struct acct));
if (rv == sizeof(struct acct)) { if (rv == sizeof(struct acct)) {
if (debug) checkspace();
printf("accounting record copied for: %s\n", dprintf("accounting record copied for: %s\n",
abuf.ac_comm); abuf.ac_comm);
rv = write(filefd, &abuf, sizeof(struct acct)); rv = write(filefd, &abuf, sizeof(struct acct));
if (rv != sizeof(struct acct)) { if (rv != sizeof(struct acct)) {
if (rv < 0) { if (rv < 0) {
if (debug) perror("write"); dperror("write");
} else { } else {
if (debug) printf("weird write result: %d\n", rv); dprintf("weird write result: %d\n", rv);
} }
} }
if (dosync)
fsync(filefd);
} else { } else {
if (rv < 0) { if (rv < 0) {
if (debug) perror("read"); dperror("read");
} else { } else {
if (debug) printf("weird read result: %d\n", rv); dprintf("weird read result: %d\n", rv);
} }
} }
} }