-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathudev_example1.c
72 lines (60 loc) · 1.99 KB
/
udev_example1.c
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
/* udev_example1.c
*
* Copyright (C) 2014-2021 Robert Milasan <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* This example will get basic information about a specified network
* device using libudev API.
*
*/
#include <stdio.h>
#include <libudev.h>
#define SYSPATH "/sys/class/net"
int main(int argc, char *argv[])
{
struct udev *udev;
struct udev_device *dev, *dev_parent;
char device[128];
/* verify that we have an argument, like eth0, otherwise fail */
if (!argv[1]) {
fprintf(stderr, "Missing network interface name.\nexample: %s eth0\n", argv[0]);
return 1;
}
/* build device path out of SYSPATH macro and argv[1] */
snprintf(device, sizeof(device), "%s/%s", SYSPATH, argv[1]);
/* create udev object */
udev = udev_new();
if (!udev) {
fprintf(stderr, "Cannot create udev context.\n");
return 1;
}
/* get device based on path */
dev = udev_device_new_from_syspath(udev, device);
if (!dev) {
fprintf(stderr, "Failed to get device.\n");
return 1;
}
printf("I: DEVNAME=%s\n", udev_device_get_sysname(dev));
printf("I: DEVPATH=%s\n", udev_device_get_devpath(dev));
printf("I: MACADDR=%s\n", udev_device_get_sysattr_value(dev, "address"));
dev_parent = udev_device_get_parent(dev);
if (dev_parent)
printf("I: DRIVER=%s\n", udev_device_get_driver(dev_parent));
/* free dev */
udev_device_unref(dev);
/* free udev */
udev_unref(udev);
return 0;
}