-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
327 lines (280 loc) · 7.69 KB
/
main.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <libudev.h>
#include <signal.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fmt/core.h>
#include <fmt/color.h>
#include <fmt/chrono.h>
#include <fmt/format.h>
#include <fmt/printf.h>
#define PROC_NAME "usb-mon"
#define PROC_VERSION "1.0"
#define MAX_EVENTS 5
static bool global_exit = false;
//
// Signal Callback
//
void signalsHandler(int signum)
{
if (signum == SIGINT || signum == SIGTERM)
{
fmt::print(stdout, fg(fmt::color::red) | fmt::emphasis::bold,"\n** Signal Triggered **");
fmt::print(stdout, "\n");
global_exit = true;
}
}
static void printDevice(struct udev_device *device, const char *source, int prop)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
printf("%-6s[%llu.%06u] %-8s %s (%s)\n",
source,
(unsigned long long)ts.tv_sec, (unsigned int)ts.tv_nsec / 1000,
udev_device_get_action(device),
udev_device_get_devpath(device),
udev_device_get_subsystem(device));
if (prop)
{
struct udev_list_entry *list_entry;
udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
printf("%s=%s\n",
udev_list_entry_get_name(list_entry),
udev_list_entry_get_value(list_entry));
printf("\n");
}
}
static void printDevice(struct udev_device *dev)
{
const char *action = udev_device_get_action(dev);
if (!action)
action = "exists";
const char *vendor = udev_device_get_sysattr_value(dev, "idVendor");
if (!vendor)
vendor = "0000";
const char *product = udev_device_get_sysattr_value(dev, "idProduct");
if (!product)
product = "0000";
const char *devnum = udev_device_get_sysattr_value(dev, "devnum");
if (!devnum)
devnum = "";
const char *busnum = udev_device_get_sysattr_value(dev, "busnum");
if (!busnum)
busnum = "";
const char *manufacturer = udev_device_get_sysattr_value(dev, "manufacturer");
if (!manufacturer)
manufacturer = "";
const char *dev_path = udev_device_get_devnode(dev);
if (!dev_path)
dev_path = "---";
fmt::print(stdout, "{:12} {:15} {:8} {:4} {:4} {:4}:{:7} {:40} {:30}\n",
udev_device_get_subsystem(dev),
udev_device_get_devtype(dev),
action,
devnum,
busnum,
vendor,
product,
manufacturer,
dev_path);
}
static void processDevice(struct udev_device *dev)
{
if (dev)
{
if (udev_device_get_devnode(dev))
printDevice(dev);
udev_device_unref(dev);
}
}
static void monitorDevices(struct udev *udev)
{
int fd_ep = -1;
int fd_udev = -1;
struct epoll_event ep_udev;
struct udev_monitor *udev_monitor;
fd_ep = epoll_create1(EPOLL_CLOEXEC);
if (fd_ep < 0)
{
fmt::print(stderr, "error creating epoll fd: %m\n");
return;
}
//
// Create Link udev Monitor events 'udev'
//
udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
if (udev_monitor == nullptr)
{
fmt::print(stderr, "error: unable to create netlink socket\n");
if (fd_ep >= 0)
close(fd_ep);
return;
}
//
// Set size buffer udev_monitor
//
udev_monitor_set_receive_buffer_size(udev_monitor, 128 * 1024 * 1024);
//
// Create file descriptor udev_monitor
//
fd_udev = udev_monitor_get_fd(udev_monitor);
if (fd_udev <= 0)
{
fmt::print(stderr, "error: udev_monitor_get_fd");
udev_monitor_unref(udev_monitor);
if (fd_ep >= 0)
close(fd_ep);
return;
}
if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", nullptr) < 0)
{
fmt::print(stderr, "error: unable to apply subsystem filter 'usb'\n");
udev_monitor_unref(udev_monitor);
if (fd_ep >= 0)
close(fd_ep);
return;
}
//
// Check enable receiving events udev
//
if (udev_monitor_enable_receiving(udev_monitor) < 0)
{
fmt::print(stderr, "error: unable to subscribe to udev events\n");
udev_monitor_unref(udev_monitor);
if (fd_ep >= 0)
close(fd_ep);
return;
}
//
// Set epoll FD udev
//
ep_udev.events = EPOLLIN | EPOLLET;
ep_udev.data.fd = fd_udev;
if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0)
{
fmt::print(stderr, "Fail to add fd to epoll\n");
udev_monitor_unref(udev_monitor);
if (fd_ep >= 0)
close(fd_ep);
return;
}
fmt::print(stdout, fmt::emphasis::bold, "\nStarted USB Monitor\n");
fmt::print(stdout, "\n{:12} {:15} {:8} {:4} {:4} {:12} {:40} {:30}\n",
"SUBSYSTEM", "TYPE", "ACTION", "DEV", "BUS", "VID:PID",
"MANUFACTURER", "PATH");
while (!global_exit)
{
int fdcount;
struct epoll_event ev[4];
int i;
fdcount = epoll_wait(fd_ep, ev, MAX_EVENTS, -1);
if (fdcount < 0)
{
if (errno != EINTR)
{
fmt::print(stderr, "error receiving uevent message: %m\n");
}
continue;
}
for (i = 0; i < fdcount; i++)
{
if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN)
{
struct udev_device *device;
device = udev_monitor_receive_device(udev_monitor);
if (device == nullptr)
continue;
// printDevice(device, "UDEV", true);
printDevice(device);
udev_device_unref(device);
}
}
}
if (fd_ep >= 0)
close(fd_ep);
}
static void enumerateDevices(struct udev *udev)
{
struct udev_enumerate *enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "usb");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry;
fmt::print(stdout, fmt::emphasis::bold, "\nListing USB Devices\n");
fmt::print(stdout, "\n{:12} {:15} {:8} {:4} {:4} {:12} {:40} {:30}\n",
"SUBSYSTEM", "TYPE", "ACTION", "DEV", "BUS", "VID:PID",
"MANUFACTURER", "PATH");
udev_list_entry_foreach(entry, devices)
{
const char *path = udev_list_entry_get_name(entry);
struct udev_device *dev = udev_device_new_from_syspath(udev, path);
processDevice(dev);
}
udev_enumerate_unref(enumerate);
}
int main()
{
struct udev *udev;
struct sigaction act;
sigset_t mask;
fmt::print(stdout, "Name : {}\n", PROC_NAME);
fmt::print(stdout, "Version : {}\n", PROC_VERSION);
fmt::print(stdout, "PID: : {}\n", getpid());
//
// Register Date Time Started Application
//
std::time_t dt = std::time(nullptr);
fmt::print(stdout, "Started : {:%Y-%m-%d %H:%M:%S}\n", fmt::localtime(dt));
//
// Set Signal Handlers
//
std::memset(&act, 0x00, sizeof(struct sigaction));
act.sa_handler = signalsHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
//
// Create udev object
//
udev = udev_new();
if (!udev)
{
fmt::print(stderr, "udev_new() failed\n");
return 1;
}
enumerateDevices(udev);
monitorDevices(udev);
//
// Free Object udev
//
udev_unref(udev);
//
// Register Date Time Finished Application
//
dt = std::time(nullptr);
fmt::print(stdout, "Finished : {:%Y-%m-%d %H:%M:%S}\n", fmt::localtime(dt));
return EXIT_SUCCESS;
}