2020-08-12 22:38:15 +00:00
|
|
|
/*
|
2020-08-14 15:17:01 +00:00
|
|
|
* cmd/ifctrstat-linux.c
|
|
|
|
* Purpose: Implement ifctrstat system-specific routines for Linux
|
2020-08-12 22:38:15 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Adélie Software in the Public Benefit, Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2020-08-14 15:49:45 +00:00
|
|
|
#include <limits.h>
|
2020-08-12 22:38:15 +00:00
|
|
|
#include <stdio.h>
|
2020-08-14 15:49:45 +00:00
|
|
|
#include <string.h>
|
2020-08-14 15:17:01 +00:00
|
|
|
#include "multicall.h"
|
2020-08-12 22:38:15 +00:00
|
|
|
|
|
|
|
const char *avail_counters[] = {
|
|
|
|
"rx.octets",
|
|
|
|
"rx.packets",
|
|
|
|
"rx.discard",
|
|
|
|
"rx.errors",
|
|
|
|
"tx.octets",
|
|
|
|
"tx.packets",
|
|
|
|
"tx.discard",
|
|
|
|
"tx.errors"
|
|
|
|
};
|
|
|
|
|
2020-08-14 15:17:01 +00:00
|
|
|
size_t avail_counters_count = ARRAY_SIZE(avail_counters);
|
2020-08-12 22:38:15 +00:00
|
|
|
|
2020-08-14 15:49:45 +00:00
|
|
|
char *
|
2020-08-12 22:38:15 +00:00
|
|
|
read_counter(const char *interface, const char *counter)
|
|
|
|
{
|
2020-08-14 15:49:45 +00:00
|
|
|
FILE *fp;
|
|
|
|
const char *path;
|
|
|
|
char full_path[PATH_MAX];
|
|
|
|
char buffer[1024];
|
|
|
|
size_t in_count;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (strcasecmp(counter, "rx.octets") == 0)
|
|
|
|
{
|
|
|
|
path = "rx_bytes";
|
|
|
|
} else if (strcasecmp(counter, "rx.packets") == 0) {
|
|
|
|
path = "rx_packets";
|
|
|
|
} else if (strcasecmp(counter, "rx.discard") == 0) {
|
|
|
|
path = "rx_dropped";
|
|
|
|
} else if (strcasecmp(counter, "rx.errors") == 0) {
|
|
|
|
path = "rx_errors";
|
|
|
|
} else if (strcasecmp(counter, "tx.octets") == 0) {
|
|
|
|
path = "tx_bytes";
|
|
|
|
} else if (strcasecmp(counter, "tx.packets") == 0) {
|
|
|
|
path = "tx_packets";
|
|
|
|
} else if (strcasecmp(counter, "tx.discard") == 0) {
|
|
|
|
path = "tx_dropped";
|
|
|
|
} else if (strcasecmp(counter, "tx.errors") == 0) {
|
|
|
|
path = "tx_errors";
|
|
|
|
} else {
|
|
|
|
errno = ENOSYS;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (snprintf(full_path, PATH_MAX, "/sys/class/net/%s/statistics/%s", interface, path) > PATH_MAX)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(full_path, "r");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
in_count = fread(buffer, 1, sizeof(buffer), fp);
|
|
|
|
|
|
|
|
if (in_count == sizeof(buffer))
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ferror(fp))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
/* take away the \n, we add our own */
|
|
|
|
buffer[in_count - 1] = '\0';
|
|
|
|
|
|
|
|
return strdup(buffer);
|
2020-08-12 22:38:15 +00:00
|
|
|
}
|