File: ifctrstat-linux.c

package info (click to toggle)
ifupdown-ng 0.12.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: ansic: 3,572; sh: 980; makefile: 233
file content (100 lines) | stat: -rw-r--r-- 2,184 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * cmd/ifctrstat-linux.c
 * Purpose: Implement ifctrstat system-specific routines for Linux
 *
 * 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.
 */

#define _GNU_SOURCE
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "cmd/multicall.h"
#include "cmd/ifctrstat-linux.h"

struct counter_desc {
	const char *name;
	const void *data;
} avail_counters[] = {
	{"rx.discard", "rx_dropped"},
	{"rx.errors", "rx_errors"},
	{"rx.octets", "rx_bytes"},
	{"rx.packets", "rx_packets"},
	{"tx.discard", "tx_dropped"},
	{"tx.errors", "tx_errors"},
	{"tx.octets", "tx_bytes"},
	{"tx.packets", "tx_packets"}
};

size_t avail_counters_count = ARRAY_SIZE(avail_counters);

static int
counter_compare(const void *key, const void *candidate)
{
	return strcasecmp((const char *)key, ((struct counter_desc *)candidate)->name);
}

const char *
read_counter(const char *interface, const char *counter)
{
	FILE *fp;
	const char *path;
	char *full_path;
	char buffer[1024];
	size_t in_count;
	struct counter_desc *ctrdata;

	errno = 0;

	ctrdata = bsearch(counter, avail_counters, avail_counters_count, sizeof(struct counter_desc), counter_compare);
	if (ctrdata) {
		path = (const char *)ctrdata->data;
	} else {
		errno = ENOSYS;
		return NULL;
	}

	if (asprintf(&full_path, "/sys/class/net/%s/statistics/%s", interface, path) < 0)
	{
		return NULL;
	}

	fp = fopen(full_path, "r");
	if (!fp)
	{
		return NULL;
	}

	free(full_path);
	full_path = 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);
}