ndhc/ndhc/leasefile.c

89 lines
2.8 KiB
C
Raw Normal View History

2011-04-20 02:07:43 +05:30
/* leasefile.c - functions for writing the lease file
*
* Copyright (c) 2011 Nicholas J. Kain <njkain at gmail dot com>
* All rights reserved.
2011-04-20 02:07:43 +05:30
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2011-04-20 02:07:43 +05:30
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
2011-04-20 02:07:43 +05:30
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
2011-04-20 02:07:43 +05:30
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <limits.h>
#include "log.h"
#include "strl.h"
#include "io.h"
#include "defines.h"
static char leasefile[PATH_MAX] = "\0";
static int leasefilefd = -1;
void set_leasefile(char *lf)
{
2013-05-06 16:37:54 +05:30
strnkcpy(leasefile, lf, sizeof leasefile);
2011-04-20 02:07:43 +05:30
}
void open_leasefile()
{
if (strlen(leasefile) > 0) {
leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (leasefilefd < 0) {
log_line("Failed to create lease file (%s).", leasefile);
2011-04-20 02:07:43 +05:30
exit(EXIT_FAILURE);
}
}
}
void write_leasefile(struct in_addr ipnum)
{
char ip[INET_ADDRSTRLEN+2];
int ret;
if (leasefilefd < 0)
return;
inet_ntop(AF_INET, &ipnum, ip, sizeof ip);
2013-05-06 16:37:54 +05:30
strnkcat(ip, "\n", sizeof ip);
2011-04-20 02:07:43 +05:30
retry_trunc:
ret = ftruncate(leasefilefd, 0);
switch (ret) {
default: break;
case -1:
if (errno == EINTR)
goto retry_trunc;
log_warning("Failed to truncate lease file.");
2011-04-20 02:07:43 +05:30
return;
}
lseek(leasefilefd, 0, SEEK_SET);
2011-04-20 02:07:43 +05:30
ret = safe_write(leasefilefd, ip, strlen(ip));
if (ret == -1)
log_warning("Failed to write ip to lease file.");
else
fsync(leasefilefd);
2011-04-20 02:07:43 +05:30
}
2013-05-06 16:37:54 +05:30