mirror of
https://github.com/dennypage/dpinger.git
synced 2024-05-19 06:50:01 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
919fad77a2 | ||
|
|
2e0430edea | ||
|
|
31432284dc | ||
|
|
e1d00b4210 | ||
|
|
1ec615486b | ||
|
|
6789e90a38 |
43
dpinger.c
43
dpinger.c
@@ -52,6 +52,18 @@
|
||||
#include <pthread.h>
|
||||
#include <syslog.h>
|
||||
|
||||
// TODO:
|
||||
//
|
||||
// After December 31st, 2016, review use of fcntl() for setting non blocking
|
||||
// and close on exec. It would be preferable to use accept4(), SOCK_CLOEXEC
|
||||
// and SOCK_NONBLOCK. These are currently avoided to allow use on older
|
||||
// systems such as FreeBSD 9.3, Linux 2.6.26.
|
||||
// For Linux accept4() currently requires defining _GNU_SOURCE which we would
|
||||
// like to avoid.
|
||||
// For FreeBSD, these definitions were introduced with FreeBSD 10.0 and are
|
||||
// not present in 9.3 which is supported through 2016.
|
||||
|
||||
|
||||
// Who we are
|
||||
static const char * progname;
|
||||
|
||||
@@ -68,7 +80,7 @@ static unsigned int flag_syslog = 0;
|
||||
static char dest_str[ADDR_STR_MAX];
|
||||
|
||||
// Time period over which we are averaging results in ms
|
||||
static unsigned long time_period_msec = 25000;
|
||||
static unsigned long time_period_msec = 30000;
|
||||
|
||||
// Interval between sends in ms
|
||||
static unsigned long send_interval_msec = 250;
|
||||
@@ -665,6 +677,7 @@ usocket_thread(
|
||||
while (1)
|
||||
{
|
||||
sock_fd = accept(usocket_fd, NULL, NULL);
|
||||
(void) fcntl(sock_fd, F_SETFL, FD_CLOEXEC);
|
||||
(void) fcntl(sock_fd, F_SETFL, fcntl(sock_fd, F_GETFL, 0) | O_NONBLOCK);
|
||||
|
||||
report(&average_latency_usec, &latency_deviation, &average_loss_percent);
|
||||
@@ -775,8 +788,8 @@ usage(void)
|
||||
fprintf(stderr, " -S log warnings via syslog\n");
|
||||
fprintf(stderr, " -B bind (source) address\n");
|
||||
fprintf(stderr, " -s time interval between echo requests (default 250ms)\n");
|
||||
fprintf(stderr, " -l time interval before packets are treated as lost (default 2x send interval)\n");
|
||||
fprintf(stderr, " -t time period over which results are averaged (default 25s)\n");
|
||||
fprintf(stderr, " -l time interval before packets are treated as lost (default 5x send interval)\n");
|
||||
fprintf(stderr, " -t time period over which results are averaged (default 30s)\n");
|
||||
fprintf(stderr, " -r time interval between reports (default 1s)\n");
|
||||
fprintf(stderr, " -o output file for reports (default stdout)\n");
|
||||
fprintf(stderr, " -A time interval between alerts (default 1s)\n");
|
||||
@@ -787,11 +800,13 @@ usage(void)
|
||||
fprintf(stderr, " -u unix socket name for polling\n");
|
||||
fprintf(stderr, " -p process id file name\n\n");
|
||||
fprintf(stderr, " notes:\n");
|
||||
fprintf(stderr, " IP addresses can be in either IPv4 or IPv6 format\n\n");
|
||||
fprintf(stderr, " time values can be expressed with a suffix of 'm' (milliseconds) or 's' (seconds)\n");
|
||||
fprintf(stderr, " if no suffix is specified, milliseconds is the default\n\n");
|
||||
fprintf(stderr, " IP addresses can be in either IPv4 or IPv6 format\n\n");
|
||||
fprintf(stderr, " the output format is \"latency_avg latency_stddev loss_pct\"\n");
|
||||
fprintf(stderr, " latency values are output in microseconds\n\n");
|
||||
fprintf(stderr, " latency values are output in microseconds\n");
|
||||
fprintf(stderr, " loss percentage is reported in whole numbers of 0-100\n");
|
||||
fprintf(stderr, " resolution of loss calculation is: 100 * send_interval / (time_period - loss_interval)\n\n");
|
||||
fprintf(stderr, " the alert_cmd is invoked as \"alert_cmd dest_addr alarm_flag latency_avg loss_avg\"\n");
|
||||
fprintf(stderr, " alarm_flag is set to 1 if either latency or loss is in alarm state\n");
|
||||
fprintf(stderr, " alarm_flag will return to 0 when both have have cleared alarm state\n\n");
|
||||
@@ -1038,7 +1053,7 @@ main(
|
||||
char *argv[])
|
||||
{
|
||||
char bind_str[ADDR_STR_MAX] = "(none)";
|
||||
int pidfile_fd;
|
||||
int pidfile_fd = -1;
|
||||
pthread_t thread;
|
||||
struct sigaction act;
|
||||
int r;
|
||||
@@ -1053,12 +1068,15 @@ main(
|
||||
perror("socket");
|
||||
fatal("cannot create send socket\n");
|
||||
}
|
||||
(void) fcntl(send_sock, F_SETFL, FD_CLOEXEC);
|
||||
|
||||
recv_sock = socket(af_family, SOCK_RAW, ip_proto);
|
||||
if (recv_sock == -1)
|
||||
{
|
||||
perror("socket");
|
||||
fatal("cannot create recv socket\n");
|
||||
}
|
||||
(void) fcntl(recv_sock, F_SETFL, FD_CLOEXEC);
|
||||
|
||||
// Bind our sockets to an address if requested
|
||||
if (bind_addr_len)
|
||||
@@ -1084,7 +1102,7 @@ main(
|
||||
// Create report file
|
||||
if (report_name)
|
||||
{
|
||||
report_fd = open(report_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
report_fd = open(report_name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (report_fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
@@ -1106,12 +1124,13 @@ main(
|
||||
fatal("socket name too large\n");
|
||||
}
|
||||
|
||||
usocket_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
usocket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (usocket_fd == -1)
|
||||
{
|
||||
perror("socket");
|
||||
fatal("cannot create unix domain socket\n");
|
||||
}
|
||||
(void) fcntl(usocket_fd, F_SETFL, FD_CLOEXEC);
|
||||
|
||||
(void) unlink(usocket_name);
|
||||
|
||||
@@ -1143,7 +1162,7 @@ main(
|
||||
// Create pid file
|
||||
if (pidfile_name)
|
||||
{
|
||||
pidfile_fd = open(pidfile_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
pidfile_fd = open(pidfile_name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (pidfile_fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
@@ -1179,7 +1198,7 @@ main(
|
||||
sigaction(SIGINT, &act, NULL);
|
||||
|
||||
// Write pid file
|
||||
if (pidfile_name)
|
||||
if (pidfile_fd != -1)
|
||||
{
|
||||
char buf[64];
|
||||
int len;
|
||||
@@ -1197,7 +1216,7 @@ main(
|
||||
fatal("error writing pidfile\n");
|
||||
}
|
||||
|
||||
r= close(pidfile_fd);
|
||||
r = close(pidfile_fd);
|
||||
if (r == -1)
|
||||
{
|
||||
perror("close");
|
||||
@@ -1216,7 +1235,7 @@ main(
|
||||
// Set the default loss interval
|
||||
if (loss_interval_msec == 0)
|
||||
{
|
||||
loss_interval_msec = send_interval_msec * 4;
|
||||
loss_interval_msec = send_interval_msec * 5;
|
||||
}
|
||||
loss_interval_usec = loss_interval_msec * 1000;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user