<commit_before>#include <sys/cdefs.h>
#include <sys/queue.h>
#include <arpa/inet.h>

#include <assert.h>
#include <err.h>
#include <event.h>
#include <fcntl.h>
#include <grp.h>
#include <paths.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <syslog.h>
#include <unistd.h>

#include "dyndnsd.h"
#include "parser.h"
#include "rtm.h"

inline static void _free(char **);
static void usage(void);
static void sig_handler(int, short, void *);
static void process_event(int, short, void *);
static void drop_privilege(char *, char *);
static pid_t spawn(const char *, int, char *, char *, char *);

int
main(int argc, char *argv[])
{
	int 		opt;
	unsigned 	opts;
	char           *filename;
	struct event	ev_route, ev_signal;
	struct dyndnsd	this;

	opts = 0;
	filename = DYNDNSD_CONF_PATH;

	openlog(getprogname(), LOG_PERROR | LOG_PID, LOG_DAEMON);

	/* allocate route(4) socket before pledge(2) */
	this.routefd = rtm_socket();
	if (-1 == this.routefd)
		errx(EX_UNAVAILABLE, "cannot create route(4) socket");

	if (-1 == pledge("stdio rpath proc exec id getpw inet", NULL))
		err(EX_OSERR, "pledge");

	while (-1 != (opt = getopt(argc, argv, "hdnvf:"))) {
		switch (opt) {
		case 'd':
			opts |= DYNDNSD_DEBUG_MODE;
			break;
		case 'h':
			usage();
			exit(EX_OK);
		case 'f':
			filename = optarg;
			break;
		case 'n':
			opts |= DYNDNSD_VALID_MODE;
			break;
		case 'v':
			puts(DYNDNSD_VERSION);
			exit(EX_OK);
		default:
			usage();
			exit(EX_USAGE);
		}
	}

	if (-1 == (this.devnull = open(_PATH_DEVNULL, O_WRONLY|O_CLOEXEC)))
		err(EX_OSFILE, "open");

	if (NULL == (this.ast = parse(filename)))
		errx(EX_DATAERR, "%s\n", parser_error);

	if (opts & DYNDNSD_VALID_MODE)
		exit(EX_OK);

	if (0 == getuid())
		drop_privilege(DYNDNSD_USER, DYNDNSD_GROUP);

	if (!(opts & DYNDNSD_DEBUG_MODE) && -1 == daemon(0, 0))
		err(EX_OSERR, "daemon");

	if (-1 == pledge("stdio proc exec", NULL)) {
		syslog(LOG_ERR, "pledge: %m");
		exit(EX_OSERR);
	}

	if (-1 == sigaction(SIGCHLD, &(struct sigaction){SIG_IGN, 0, SA_NOCLDWAIT}, NULL))
		syslog(LOG_WARNING, "sigaction: %m");

	event_init();

	event_set(&ev_route, this.routefd, EV_READ|EV_PERSIST, process_event, &this);
	event_add(&ev_route, NULL);<commit_msg>log pledge promises once in daemon mode<commit_after>#include <sys/cdefs.h>
#include <sys/queue.h>
#include <arpa/inet.h>

#include <assert.h>
#include <err.h>
#include <event.h>
#include <fcntl.h>
#include <grp.h>
#include <paths.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <syslog.h>
#include <unistd.h>

#include "dyndnsd.h"
#include "parser.h"
#include "rtm.h"

inline static void _free(char **);
static void usage(void);
static void sig_handler(int, short, void *);
static void process_event(int, short, void *);
static void drop_privilege(char *, char *);
static pid_t spawn(const char *, int, char *, char *, char *);

int
main(int argc, char *argv[])
{
	int 		opt;
	unsigned 	opts;
	char           *filename;
	struct event	ev_route, ev_signal;
	struct dyndnsd	this;

	opts = 0;
	filename = DYNDNSD_CONF_PATH;

	openlog(getprogname(), LOG_PERROR | LOG_PID, LOG_DAEMON);

	/* allocate route(4) socket before pledge(2) */
	this.routefd = rtm_socket();
	if (-1 == this.routefd)
		errx(EX_UNAVAILABLE, "cannot create route(4) socket");

	if (-1 == pledge("stdio rpath proc exec id getpw inet", NULL))
		err(EX_OSERR, "pledge");

	while (-1 != (opt = getopt(argc, argv, "hdnvf:"))) {
		switch (opt) {
		case 'd':
			opts |= DYNDNSD_DEBUG_MODE;
			break;
		case 'h':
			usage();
			exit(EX_OK);
		case 'f':
			filename = optarg;
			break;
		case 'n':
			opts |= DYNDNSD_VALID_MODE;
			break;
		case 'v':
			puts(DYNDNSD_VERSION);
			exit(EX_OK);
		default:
			usage();
			exit(EX_USAGE);
		}
	}

	if (-1 == (this.devnull = open(_PATH_DEVNULL, O_WRONLY|O_CLOEXEC)))
		err(EX_OSFILE, "open");

	if (NULL == (this.ast = parse(filename)))
		errx(EX_DATAERR, "%s\n", parser_error);

	if (opts & DYNDNSD_VALID_MODE)
		exit(EX_OK);

	if (0 == getuid())
		drop_privilege(DYNDNSD_USER, DYNDNSD_GROUP);

	if (!(opts & DYNDNSD_DEBUG_MODE) && -1 == daemon(0, 0))
		err(EX_OSERR, "daemon");

	if (-1 == pledge("stdio proc exec", NULL)) {
		syslog(LOG_ERR, "pledge: %m");
		exit(EX_OSERR);
	}

	syslog(LOG_DEBUG, "pledge: stdio proc exec");

	if (-1 == sigaction(SIGCHLD, &(struct sigaction){SIG_IGN, 0, SA_NOCLDWAIT}, NULL))
		syslog(LOG_WARNING, "sigaction: %m");

	event_init();

	event_set(&ev_route, this.routefd, EV_READ|EV_PERSIST, process_event, &this);
	event_add(&ev_route, NULL);
{'fraction_non_alphanumeric': 0.1726139294926913, 'fraction_numerical': 0.006448839208942391, 'mean_word_length': 4.4357476635514015, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2693441', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1907, 'n_words': 444}