<commit_before>/*
 * dhcpcd - DHCP client daemon
 * Copyright (c) 2006-2015 <NAME> <<EMAIL>>
 * All rights reserved

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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 AUTHOR 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 AUTHOR 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.
 */

/* Needed define to get at getline for glibc and FreeBSD */
#ifndef _GNU_SOURCE
#  define _GNU_SOURCE
#endif

#ifdef __APPLE__
#  include <mach/mach_time.h>
#  include <mach/kern_return.h>
#endif

#include <sys/param.h>
#include <sys/time.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#ifdef BSD
#  include <paths.h>
#endif
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

#include "common.h"
#include "dhcpcd.h"
#include "if-options.h"

#ifndef _PATH_DEVNULL
#  define _PATH_DEVNULL "/dev/null"
#endif

const char *
get_hostname(char *buf, size_t buflen, int short_hostname)
{
	char *p;

	if (gethostname(buf, buflen) != 0)
		return NULL;
	buf[buflen - 1] = '\0';
	if (strcmp(buf, "(none)") == 0 ||
	    strcmp(buf, "localhost") == 0 ||
	    strncmp(buf, "localhost.", strlen("localhost.")) == 0 ||
	    buf[0] == '.')
		return NULL;

	if (short_hostname) {
		p = strchr(buf, '.');
		if (p)
			*p = '\0';
	}

	return buf;
}

/* Handy function to get the time.
 * We only care about time advancements, not the actual time itself
 * Which is why we use CLOCK_MONOTONIC, but it is not available on all
 * platforms.
 */
#define NO_MONOTONIC "host does not support a monotonic clock - timing can skew"
int
get_monotonic(struct timespec *ts)
{

#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)
	return clock_gettime(CLOCK_MONOTONIC, ts);
#elif defined(__APPLE__)
	/* We can use mach kernel functions here.
	 * This is crap though - why can't they implement clock_gettime?*/
	static struct mach_timebase_info info = { 0, 0 };
	static double factor = 0.0;
	uint64_t nano;
	long rem;

	if (!posix_clock_set) {
		if (mach_timebase_info(&info) == KERN_SUCCESS) {
			factor = (double)info.numer / (double)info.denom;
			clock_monotonic = posix_clock_set = 1;
		}
	}
	if (clock_monotonic) {
		nano = mach_absolute_time();
		if ((info.denom != 1 || info.numer != 1) && factor != 0.0)
			nano *= factor;
		ts->tv_sec = nano / NSEC_PER_SEC;
		ts->tv_nsec = nano % NSEC_PER_SEC;
		if (ts->tv_nsec < 0) {
			ts->tv_sec--;
			ts->tv_nsec += NSEC_PER_SEC;
		}
		return 0;
	}
#endif

#if 0
	/* Something above failed, so fall back to gettimeofday */
	if (!posix_clock_set) {
		logger(NULL, LOG_WARNING, NO_MONOTONIC);
		posix_clock_set = 1;
	}
#endif
	{
		struct timeval tv;
		if (gettimeofday(&tv, NULL) == 0) {
			TIMEVAL_TO_TIMESPEC(&tv, ts);
			return 0;
		}
	}

	return -1;
}

#if USE_LOGFILE
void
logger_open(struct dhcpcd_ctx *ctx)
{

	if (ctx->logfile) {
		int f = O_CREAT | O_APPEND | O_TRUNC;

#ifdef O_CLOEXEC
		f |= O_CLOEXEC;
#endif
		ctx->log_fd = open(ctx->logfile, O_WRONLY | f, 0644);
		if (ctx->log_fd == -1)
			warn("open: %s", ctx->logfile);
#ifndef O_CLOEXEC
		else {
			if (fcntl(ctx->log_fd, F_GETFD, &f) == -1 ||
			    fcntl(ctx->log_fd, F_SETFD, f | FD_CLOEXEC) == -1)
				warn("fcntl: %s", ctx->logfile);
		}
#endif
	} else
		openlog(PACKAGE, LOG_PID, LOG_DAEMON);
}

void
logger_close(struct dhcpcd_ctx *ctx)
{

	if (ctx->log_fd != -1) {
		close(ctx->log_fd);
		ctx->log_fd = -1;
	}
	closelog();
}

void
logger(struct dhcpcd_ctx *ctx, int pri, const char *fmt, ...)
{
	va_list va;
	int serrno;
#ifndef HAVE_PRINTF_M
	char fmt_cpy[1024];
#endif

	serrno = errno;
	va_start(va, fmt);

	if (pri >= LOG_DEBUG && ctx && !(ctx->options & DHCPCD_DEBUG))
		return;

#ifndef HAVE_PRINTF_M
	/* Print strerrno(errno) in place of %m */
	if (ctx == NULL || !(ctx->options & DHCPCD_QUIET) || ctx->log_fd != -1)
	{
		const char *p;
		char *fp = fmt_cpy, *serr = NULL;
		size_t fmt_left = sizeof(fmt_cpy) - 1, fmt_wrote;

		for (p = fmt; *p != '\0'; p++) {
			if (p[0] == '%' && p[1] == '%') {
				if (fmt_left < 2)
					break;
				*fp++ = '%';
				*fp++ = '%';
				fmt_left -= 2;
				p++;
			} else if (p[0] == '%' && p[1] == 'm') {
				if (serr == NULL)
					serr = strerror(serrno);
				fmt_wrote = strlcpy(fp, serr, fmt_left);
				if (fmt_wrote > fmt_left)
					break;
				fp += fmt_wrote;
				fmt_left -= fmt_wrote;
				p++;
			} else {
				*fp++ = *p;
				--fmt_left;
			}<commit_msg>CID 1292550: Missing call to va_end().<commit_after>/*
 * dhcpcd - DHCP client daemon
 * Copyright (c) 2006-2015 <NAME> <<EMAIL>>
 * All rights reserved

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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 AUTHOR 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 AUTHOR 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.
 */

/* Needed define to get at getline for glibc and FreeBSD */
#ifndef _GNU_SOURCE
#  define _GNU_SOURCE
#endif

#ifdef __APPLE__
#  include <mach/mach_time.h>
#  include <mach/kern_return.h>
#endif

#include <sys/param.h>
#include <sys/time.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#ifdef BSD
#  include <paths.h>
#endif
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

#include "common.h"
#include "dhcpcd.h"
#include "if-options.h"

#ifndef _PATH_DEVNULL
#  define _PATH_DEVNULL "/dev/null"
#endif

const char *
get_hostname(char *buf, size_t buflen, int short_hostname)
{
	char *p;

	if (gethostname(buf, buflen) != 0)
		return NULL;
	buf[buflen - 1] = '\0';
	if (strcmp(buf, "(none)") == 0 ||
	    strcmp(buf, "localhost") == 0 ||
	    strncmp(buf, "localhost.", strlen("localhost.")) == 0 ||
	    buf[0] == '.')
		return NULL;

	if (short_hostname) {
		p = strchr(buf, '.');
		if (p)
			*p = '\0';
	}

	return buf;
}

/* Handy function to get the time.
 * We only care about time advancements, not the actual time itself
 * Which is why we use CLOCK_MONOTONIC, but it is not available on all
 * platforms.
 */
#define NO_MONOTONIC "host does not support a monotonic clock - timing can skew"
int
get_monotonic(struct timespec *ts)
{

#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)
	return clock_gettime(CLOCK_MONOTONIC, ts);
#elif defined(__APPLE__)
	/* We can use mach kernel functions here.
	 * This is crap though - why can't they implement clock_gettime?*/
	static struct mach_timebase_info info = { 0, 0 };
	static double factor = 0.0;
	uint64_t nano;
	long rem;

	if (!posix_clock_set) {
		if (mach_timebase_info(&info) == KERN_SUCCESS) {
			factor = (double)info.numer / (double)info.denom;
			clock_monotonic = posix_clock_set = 1;
		}
	}
	if (clock_monotonic) {
		nano = mach_absolute_time();
		if ((info.denom != 1 || info.numer != 1) && factor != 0.0)
			nano *= factor;
		ts->tv_sec = nano / NSEC_PER_SEC;
		ts->tv_nsec = nano % NSEC_PER_SEC;
		if (ts->tv_nsec < 0) {
			ts->tv_sec--;
			ts->tv_nsec += NSEC_PER_SEC;
		}
		return 0;
	}
#endif

#if 0
	/* Something above failed, so fall back to gettimeofday */
	if (!posix_clock_set) {
		logger(NULL, LOG_WARNING, NO_MONOTONIC);
		posix_clock_set = 1;
	}
#endif
	{
		struct timeval tv;
		if (gettimeofday(&tv, NULL) == 0) {
			TIMEVAL_TO_TIMESPEC(&tv, ts);
			return 0;
		}
	}

	return -1;
}

#if USE_LOGFILE
void
logger_open(struct dhcpcd_ctx *ctx)
{

	if (ctx->logfile) {
		int f = O_CREAT | O_APPEND | O_TRUNC;

#ifdef O_CLOEXEC
		f |= O_CLOEXEC;
#endif
		ctx->log_fd = open(ctx->logfile, O_WRONLY | f, 0644);
		if (ctx->log_fd == -1)
			warn("open: %s", ctx->logfile);
#ifndef O_CLOEXEC
		else {
			if (fcntl(ctx->log_fd, F_GETFD, &f) == -1 ||
			    fcntl(ctx->log_fd, F_SETFD, f | FD_CLOEXEC) == -1)
				warn("fcntl: %s", ctx->logfile);
		}
#endif
	} else
		openlog(PACKAGE, LOG_PID, LOG_DAEMON);
}

void
logger_close(struct dhcpcd_ctx *ctx)
{

	if (ctx->log_fd != -1) {
		close(ctx->log_fd);
		ctx->log_fd = -1;
	}
	closelog();
}

void
logger(struct dhcpcd_ctx *ctx, int pri, const char *fmt, ...)
{
	va_list va;
	int serrno;
#ifndef HAVE_PRINTF_M
	char fmt_cpy[1024];
#endif

	if (pri >= LOG_DEBUG && ctx && !(ctx->options & DHCPCD_DEBUG))
		return;

	serrno = errno;
	va_start(va, fmt);

#ifndef HAVE_PRINTF_M
	/* Print strerrno(errno) in place of %m */
	if (ctx == NULL || !(ctx->options & DHCPCD_QUIET) || ctx->log_fd != -1)
	{
		const char *p;
		char *fp = fmt_cpy, *serr = NULL;
		size_t fmt_left = sizeof(fmt_cpy) - 1, fmt_wrote;

		for (p = fmt; *p != '\0'; p++) {
			if (p[0] == '%' && p[1] == '%') {
				if (fmt_left < 2)
					break;
				*fp++ = '%';
				*fp++ = '%';
				fmt_left -= 2;
				p++;
			} else if (p[0] == '%' && p[1] == 'm') {
				if (serr == NULL)
					serr = strerror(serrno);
				fmt_wrote = strlcpy(fp, serr, fmt_left);
				if (fmt_wrote > fmt_left)
					break;
				fp += fmt_wrote;
				fmt_left -= fmt_wrote;
				p++;
			} else {
				*fp++ = *p;
				--fmt_left;
			}
{'fraction_non_alphanumeric': 0.13859255939040788, 'fraction_numerical': 0.01102644554011654, 'mean_word_length': 3.5815195071868584, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2379426', 'n_tokens_mistral': 4619, 'n_tokens_neox': 4137, 'n_words': 1257}