ndhc/src/leasefile.c

107 lines
3.8 KiB
C
Raw Normal View History

2011-04-20 02:07:43 +05:30
/* leasefile.c - functions for writing the lease file
*
* Copyright 2011-2018 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 <stdio.h>
2011-04-20 02:07:43 +05:30
#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>
2014-03-31 02:32:48 +05:30
#include "nk/log.h"
#include "nk/io.h"
2014-03-11 05:12:52 +05:30
#include "leasefile.h"
#include "ndhc.h"
2011-04-20 02:07:43 +05:30
static int leasefilefd = -1;
static void get_leasefile_path(char *leasefile, size_t dlen, char *ifname)
2011-04-20 02:07:43 +05:30
{
int splen = snprintf(leasefile, dlen, "%s/LEASE-%s",
state_dir, ifname);
if (splen < 0)
suicide("%s: (%s) snprintf failed; return=%d",
client_config.interface, __func__, splen);
if ((size_t)splen >= dlen)
suicide("%s: (%s) snprintf dest buffer too small %d >= %u",
client_config.interface, __func__, splen, sizeof dlen);
2011-04-20 02:07:43 +05:30
}
2014-03-11 05:12:52 +05:30
void open_leasefile(void)
2011-04-20 02:07:43 +05:30
{
char leasefile[PATH_MAX];
get_leasefile_path(leasefile, sizeof leasefile, client_config.interface);
leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (leasefilefd < 0)
suicide("%s: Failed to create lease file '%s': %s",
client_config.interface, leasefile, strerror(errno));
2011-04-20 02:07:43 +05:30
}
void write_leasefile(struct in_addr ipnum)
{
char ip[INET_ADDRSTRLEN];
char out[INET_ADDRSTRLEN*2];
ssize_t ret;
if (leasefilefd < 0) {
log_error("%s: (%s) leasefile fd < 0; no leasefile will be written",
client_config.interface, __func__);
2011-04-20 02:07:43 +05:30
return;
}
2011-04-20 02:07:43 +05:30
inet_ntop(AF_INET, &ipnum, ip, sizeof ip);
ssize_t olen = snprintf(out, sizeof out, "%s\n", ip);
if (olen < 0 || (size_t)olen >= sizeof ip) {
log_error("%s: (%s) snprintf failed; return=%d",
client_config.interface, __func__, olen);
return;
}
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("%s: Failed to truncate lease file: %s",
client_config.interface, strerror(errno));
2011-04-20 02:07:43 +05:30
return;
}
lseek(leasefilefd, 0, SEEK_SET);
size_t outlen = strlen(out);
ret = safe_write(leasefilefd, out, outlen);
if (ret < 0 || (size_t)ret != outlen)
log_warning("%s: Failed to write ip to lease file.",
client_config.interface);
else
fsync(leasefilefd);
2011-04-20 02:07:43 +05:30
}
2013-05-06 16:37:54 +05:30