<commit_before>/*
 * copy diverted (or tee'd) packets to a file in 'tcpdump' format
 * (ie. this uses the '-lpcap' routines).
 *
 * example usage:
 *	# ipfwpcap -r 8091 divt.log &
 *	# ipfw add 2864 divert 8091 ip from 128.432.53.82 to any
 *	# ipfw add 2864 divert 8091 ip from any to 128.432.53.82
 *
 *   the resulting dump file can be read with ...
 *	# tcpdump -nX -r divt.log
 */
/*
 * Written by <NAME> { pkern [AT] cns.utoronto.ca }
 *
 * Copyright (c) 2004 University of Toronto. All rights reserved.
 * Anyone may use or copy this software except that this copyright
 * notice remain intact and that credit is given where it is due.
 * The University of Toronto and the author make no warranty and
 * accept no liability for this software.
 *
 * From: Header: /local/src/local.lib/SRC/ipfwpcap/RCS/ipfwpcap.c,v 1.4 2004/01/15 16:19:07 pkern Exp
 *
 * $FreeBSD$
 */

#include <stdio.h>
#include <errno.h>
#include <paths.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>		/* for MAXPATHLEN */
#include <sys/socket.h>
#include <netinet/in.h>

#include <netinet/in_systm.h>	/* for IP_MAXPACKET */
#include <netinet/ip.h>		/* for IP_MAXPACKET */

/* XXX normally defined in config.h */
#define HAVE_STRLCPY 1
#define HAVE_SNPRINTF 1
#define HAVE_VSNPRINTF 1
#include <pcap-int.h>	/* see pcap(3) and /usr/src/contrib/libpcap/. */

#ifdef IP_MAXPACKET
#define BUFMAX	IP_MAXPACKET
#else
#define BUFMAX	65535
#endif

#ifndef MAXPATHLEN
#define MAXPATHLEN	1024
#endif

static int debug = 0;
static int reflect = 0;		/* 1 == write packet back to socket. */

static ssize_t totbytes = 0, maxbytes = 0;
static ssize_t totpkts = 0, maxpkts = 0;

char *prog = NULL;
char pidfile[MAXPATHLEN] = { '\0' };

/*
 * tidy up.
 */
void
quit(sig)
int sig;
{
	(void) unlink(pidfile);
	exit(sig);
}

/*
 * do the "paper work"
 *	- save my own pid in /var/run/$0.{port#}.pid
 */
okay(pn)
int pn;
{
	FILE *fp;
	int fd, numlen, n;
	char *p, numbuf[80];

	numlen = sizeof(numbuf);
	bzero(numbuf, numlen);
	snprintf(numbuf, numlen-1, "%ld\n", getpid());
	numlen = strlen(numbuf);

	if (pidfile[0] == '\0') {
		p = (char *)rindex(prog, '/');
		p = (p == NULL) ? prog : p+1 ;

		snprintf(pidfile, sizeof(pidfile)-1,
			"%s%s.%d.pid", _PATH_VARRUN, p, pn);
	}

	fd = open(pidfile, O_WRONLY|O_CREAT|O_EXCL, 0644);
	if (fd < 0) { perror(pidfile); exit(21); }

        siginterrupt(SIGTERM, 1);
        siginterrupt(SIGHUP, 1);
        signal (SIGTERM, quit);
        signal (SIGHUP, quit);

	n = write(fd, numbuf, numlen);
	if (n < 0) { perror(pidfile); quit(23); }
	(void) close(fd);
}

usage()
{<commit_msg>Add a signal handler for SIGINT to make sure that the PID file gets cleaned up upon receiving SIGINT.  This un-breaks subsequent executions of ipfwpcap and helps when debugging network/divert issues like this:<commit_after>/*
 * copy diverted (or tee'd) packets to a file in 'tcpdump' format
 * (ie. this uses the '-lpcap' routines).
 *
 * example usage:
 *	# ipfwpcap -r 8091 divt.log &
 *	# ipfw add 2864 divert 8091 ip from 128.432.53.82 to any
 *	# ipfw add 2864 divert 8091 ip from any to 128.432.53.82
 *
 *   the resulting dump file can be read with ...
 *	# tcpdump -nX -r divt.log
 */
/*
 * Written by <NAME> { pkern [AT] cns.utoronto.ca }
 *
 * Copyright (c) 2004 University of Toronto. All rights reserved.
 * Anyone may use or copy this software except that this copyright
 * notice remain intact and that credit is given where it is due.
 * The University of Toronto and the author make no warranty and
 * accept no liability for this software.
 *
 * From: Header: /local/src/local.lib/SRC/ipfwpcap/RCS/ipfwpcap.c,v 1.4 2004/01/15 16:19:07 pkern Exp
 *
 * $FreeBSD$
 */

#include <stdio.h>
#include <errno.h>
#include <paths.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>		/* for MAXPATHLEN */
#include <sys/socket.h>
#include <netinet/in.h>

#include <netinet/in_systm.h>	/* for IP_MAXPACKET */
#include <netinet/ip.h>		/* for IP_MAXPACKET */

/* XXX normally defined in config.h */
#define HAVE_STRLCPY 1
#define HAVE_SNPRINTF 1
#define HAVE_VSNPRINTF 1
#include <pcap-int.h>	/* see pcap(3) and /usr/src/contrib/libpcap/. */

#ifdef IP_MAXPACKET
#define BUFMAX	IP_MAXPACKET
#else
#define BUFMAX	65535
#endif

#ifndef MAXPATHLEN
#define MAXPATHLEN	1024
#endif

static int debug = 0;
static int reflect = 0;		/* 1 == write packet back to socket. */

static ssize_t totbytes = 0, maxbytes = 0;
static ssize_t totpkts = 0, maxpkts = 0;

char *prog = NULL;
char pidfile[MAXPATHLEN] = { '\0' };

/*
 * tidy up.
 */
void
quit(sig)
int sig;
{
	(void) unlink(pidfile);
	exit(sig);
}

/*
 * do the "paper work"
 *	- save my own pid in /var/run/$0.{port#}.pid
 */
okay(pn)
int pn;
{
	FILE *fp;
	int fd, numlen, n;
	char *p, numbuf[80];

	numlen = sizeof(numbuf);
	bzero(numbuf, numlen);
	snprintf(numbuf, numlen-1, "%ld\n", getpid());
	numlen = strlen(numbuf);

	if (pidfile[0] == '\0') {
		p = (char *)rindex(prog, '/');
		p = (p == NULL) ? prog : p+1 ;

		snprintf(pidfile, sizeof(pidfile)-1,
			"%s%s.%d.pid", _PATH_VARRUN, p, pn);
	}

	fd = open(pidfile, O_WRONLY|O_CREAT|O_EXCL, 0644);
	if (fd < 0) { perror(pidfile); exit(21); }

        siginterrupt(SIGTERM, 1);
        siginterrupt(SIGHUP, 1);
        signal (SIGTERM, quit);
        signal (SIGHUP, quit);
        signal (SIGINT, quit);

	n = write(fd, numbuf, numlen);
	if (n < 0) { perror(pidfile); quit(23); }
	(void) close(fd);
}

usage()
{
{'fraction_non_alphanumeric': 0.15193519351935195, 'fraction_numerical': 0.03636363636363636, 'mean_word_length': 3.430622009569378, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 33, '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': '492476', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2181, 'n_words': 653}