diff options
author | Henning Brauer <henning@cvs.openbsd.org> | 2004-04-15 23:20:24 +0000 |
---|---|---|
committer | Henning Brauer <henning@cvs.openbsd.org> | 2004-04-15 23:20:24 +0000 |
commit | 871809898c2097e2fdba9b1a4f8fbd462aaeabbe (patch) | |
tree | 9c0e7f27091d9fb77bedafc69ad54ad29ecfa112 | |
parent | 61e786809c15717b49e8bb7142788a83ce2c4499 (diff) |
do not close and re-open the leases file all the time, instead open it once
and rewind as neccessary
-rw-r--r-- | usr.sbin/dhcpd/db.c | 49 |
1 files changed, 16 insertions, 33 deletions
diff --git a/usr.sbin/dhcpd/db.c b/usr.sbin/dhcpd/db.c index 638e2584363..6a59abe89ad 100644 --- a/usr.sbin/dhcpd/db.c +++ b/usr.sbin/dhcpd/db.c @@ -1,4 +1,4 @@ -/* $OpenBSD: db.c,v 1.4 2004/04/15 21:57:04 henning Exp $ */ +/* $OpenBSD: db.c,v 1.5 2004/04/15 23:20:23 henning Exp $ */ /* * Persistent database management routines for DHCPD. @@ -204,34 +204,27 @@ commit_leases(void) void db_startup(void) { + int db_fd; + + /* open lease file. once we dropped privs it has to stay open */ + db_fd = open(path_dhcpd_db, O_WRONLY|O_TRUNC|O_CREAT, 0664); + if (db_fd == -1) + error("Can't create new lease file: %m"); + if ((db_file = fdopen(db_fd, "w")) == NULL) + error("Can't fdopen new lease file!"); + /* Read in the existing lease file... */ read_leases(); time(&write_time); + new_lease_file(); } void new_lease_file() { - char newfname [MAXPATHLEN]; - char backfname [MAXPATHLEN]; - time_t t; - int db_fd; - - /* If we already have an open database, close it. */ - if (db_file) - fclose(db_file); - - /* Make a temporary lease file... */ - time(&t); - snprintf(newfname, sizeof(newfname), "%s.%d", path_dhcpd_db, - (int)t); - - db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664); - if (db_fd == -1) - error("Can't create new lease file: %m"); - if ((db_file = fdopen(db_fd, "w")) == NULL) - error("Can't fdopen new lease file!"); + fflush(db_file); + rewind(db_file); /* * Write an introduction so people don't complain about time being off. @@ -245,19 +238,9 @@ new_lease_file() counting = 0; write_leases(); - /* Get the old database out of the way... */ - snprintf(backfname, sizeof(backfname), "%s~", path_dhcpd_db); - if (unlink(backfname) == -1 && errno != ENOENT) - error("Can't remove old lease database backup %s: %m", - backfname); - if (link(path_dhcpd_db, backfname) == -1) - error("Can't backup lease database %s to %s: %m", - path_dhcpd_db, backfname); - - /* Move in the new file... */ - if (rename(newfname, path_dhcpd_db) == -1) - error("Can't install new lease database %s to %s: %m", - newfname, path_dhcpd_db); + fflush(db_file); + ftruncate(fileno(db_file), ftell(db_file)); + fsync(fileno(db_file)); counting = 1; } |