Coverage Report

Created: 2026-02-26 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libusb/libusb/os/linux_udev.c
Line
Count
Source
1
/* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
2
/*
3
 * Linux usbfs backend for libusb
4
 * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
5
 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6
 * Copyright (c) 2012-2013 Nathan Hjelm <hjelmn@mac.com>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#include "libusbi.h"
24
#include "linux_usbfs.h"
25
26
#include <errno.h>
27
#include <fcntl.h>
28
#include <libudev.h>
29
#include <poll.h>
30
#include <pthread.h>
31
#include <string.h>
32
#include <unistd.h>
33
34
/* udev context */
35
static struct udev *udev_ctx = NULL;
36
static int udev_monitor_fd = -1;
37
static usbi_event_t udev_control_event = USBI_INVALID_EVENT;
38
static struct udev_monitor *udev_monitor = NULL;
39
static pthread_t linux_event_thread;
40
41
static void udev_hotplug_event(struct udev_device *udev_dev);
42
static void *linux_udev_event_thread_main(void *arg);
43
44
int linux_udev_start_event_monitor(void)
45
0
{
46
0
  int r;
47
48
0
  assert(udev_ctx == NULL);
49
0
  udev_ctx = udev_new();
50
0
  if (!udev_ctx) {
51
0
    usbi_err(NULL, "could not create udev context");
52
0
    goto err;
53
0
  }
54
55
0
  udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
56
0
  if (!udev_monitor) {
57
0
    usbi_err(NULL, "could not initialize udev monitor");
58
0
    goto err_free_ctx;
59
0
  }
60
61
0
  r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device");
62
0
  if (r) {
63
0
    usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
64
0
    goto err_free_monitor;
65
0
  }
66
67
0
  if (udev_monitor_enable_receiving(udev_monitor)) {
68
0
    usbi_err(NULL, "failed to enable the udev monitor");
69
0
    goto err_free_monitor;
70
0
  }
71
72
0
  udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
73
74
0
#if defined(FD_CLOEXEC)
75
  /* Make sure the udev file descriptor is marked as CLOEXEC */
76
0
  r = fcntl(udev_monitor_fd, F_GETFD);
77
0
  if (r == -1) {
78
0
    usbi_err(NULL, "failed to get udev monitor fd flags, errno=%d", errno);
79
0
    goto err_free_monitor;
80
0
  }
81
0
  if (!(r & FD_CLOEXEC)) {
82
0
    if (fcntl(udev_monitor_fd, F_SETFD, r | FD_CLOEXEC) == -1) {
83
0
      usbi_err(NULL, "failed to set udev monitor fd flags, errno=%d", errno);
84
0
      goto err_free_monitor;
85
0
    }
86
0
  }
87
0
#endif
88
89
  /* Some older versions of udev are not non-blocking by default,
90
   * so make sure this is set */
91
0
  r = fcntl(udev_monitor_fd, F_GETFL);
92
0
  if (r == -1) {
93
0
    usbi_err(NULL, "failed to get udev monitor fd status flags, errno=%d", errno);
94
0
    goto err_free_monitor;
95
0
  }
96
0
  if (!(r & O_NONBLOCK)) {
97
0
    if (fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK) == -1) {
98
0
      usbi_err(NULL, "failed to set udev monitor fd status flags, errno=%d", errno);
99
0
      goto err_free_monitor;
100
0
    }
101
0
  }
102
103
0
  r = usbi_create_event(&udev_control_event);
104
0
  if (r) {
105
0
    usbi_err(NULL, "failed to create udev control event");
106
0
    goto err_free_monitor;
107
0
  }
108
109
0
  r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
110
0
  if (r) {
111
0
    usbi_err(NULL, "failed to create hotplug event thread (%d)", r);
112
0
    goto err_destroy_event;
113
0
  }
114
115
0
  return LIBUSB_SUCCESS;
116
117
0
err_destroy_event:
118
0
  usbi_destroy_event(&udev_control_event);
119
0
  udev_control_event = (usbi_event_t)USBI_INVALID_EVENT;
120
0
err_free_monitor:
121
0
  udev_monitor_unref(udev_monitor);
122
0
  udev_monitor = NULL;
123
0
  udev_monitor_fd = -1;
124
0
err_free_ctx:
125
0
  udev_unref(udev_ctx);
126
0
err:
127
0
  udev_ctx = NULL;
128
0
  return LIBUSB_ERROR_OTHER;
129
0
}
130
131
int linux_udev_stop_event_monitor(void)
132
0
{
133
0
  int r;
134
135
0
  assert(udev_ctx != NULL);
136
0
  assert(udev_monitor != NULL);
137
0
  assert(udev_monitor_fd != -1);
138
139
  /* Signal the control event and wait for the thread to exit */
140
0
  usbi_signal_event(&udev_control_event);
141
142
0
  r = pthread_join(linux_event_thread, NULL);
143
0
  if (r)
144
0
    usbi_warn(NULL, "failed to join hotplug event thread (%d)", r);
145
146
0
  usbi_destroy_event(&udev_control_event);
147
0
  udev_control_event = (usbi_event_t)USBI_INVALID_EVENT;
148
149
  /* Release the udev monitor */
150
0
  udev_monitor_unref(udev_monitor);
151
0
  udev_monitor = NULL;
152
0
  udev_monitor_fd = -1;
153
154
  /* Clean up the udev context */
155
0
  udev_unref(udev_ctx);
156
0
  udev_ctx = NULL;
157
158
0
  return LIBUSB_SUCCESS;
159
0
}
160
161
static void *linux_udev_event_thread_main(void *arg)
162
0
{
163
0
  struct pollfd fds[] = {
164
0
    { .fd = USBI_EVENT_OS_HANDLE(&udev_control_event),
165
0
      .events = USBI_EVENT_POLL_EVENTS },
166
0
    { .fd = udev_monitor_fd,
167
0
      .events = POLLIN },
168
0
  };
169
0
  struct udev_device *udev_dev;
170
0
  int r;
171
172
0
  UNUSED(arg);
173
174
0
#if defined(HAVE_PTHREAD_SETNAME_NP)
175
0
  r = pthread_setname_np(pthread_self(), "libusb_event");
176
0
  if (r)
177
0
    usbi_warn(NULL, "failed to set hotplug event thread name, error=%d", r);
178
0
#endif
179
180
0
  usbi_dbg(NULL, "udev event thread entering");
181
182
0
  while (1) {
183
0
    r = poll(fds, 2, -1);
184
0
    if (r == -1) {
185
      /* check for temporary failure */
186
0
      if (errno == EINTR)
187
0
        continue;
188
0
      usbi_err(NULL, "poll() failed, errno=%d", errno);
189
0
      break;
190
0
    }
191
0
    if (fds[0].revents) {
192
      /* activity on control event, exit */
193
0
      break;
194
0
    }
195
0
    if (fds[1].revents) {
196
0
      usbi_mutex_static_lock(&linux_hotplug_lock);
197
0
      udev_dev = udev_monitor_receive_device(udev_monitor);
198
0
      if (udev_dev)
199
0
        udev_hotplug_event(udev_dev);
200
0
      usbi_mutex_static_unlock(&linux_hotplug_lock);
201
0
    }
202
0
  }
203
204
0
  usbi_dbg(NULL, "udev event thread exiting");
205
206
0
  return NULL;
207
0
}
208
209
static int udev_device_info(struct libusb_context *ctx, int detached,
210
          struct udev_device *udev_dev, uint8_t *busnum,
211
0
          uint8_t *devaddr, const char **sys_name) {
212
0
  const char *dev_node;
213
214
0
  dev_node = udev_device_get_devnode(udev_dev);
215
0
  if (!dev_node) {
216
0
    return LIBUSB_ERROR_OTHER;
217
0
  }
218
219
0
  *sys_name = udev_device_get_sysname(udev_dev);
220
0
  if (!*sys_name) {
221
0
    return LIBUSB_ERROR_OTHER;
222
0
  }
223
224
0
  return linux_get_device_address(ctx, detached, busnum, devaddr,
225
0
          dev_node, *sys_name, -1);
226
0
}
227
228
static void udev_hotplug_event(struct udev_device *udev_dev)
229
0
{
230
0
  const char *udev_action;
231
0
  const char *sys_name = NULL;
232
0
  uint8_t busnum = 0, devaddr = 0;
233
0
  int detached;
234
0
  int r;
235
236
0
  do {
237
0
    udev_action = udev_device_get_action(udev_dev);
238
0
    if (!udev_action) {
239
0
      break;
240
0
    }
241
242
0
    detached = !strncmp(udev_action, "remove", 6);
243
244
0
    r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
245
0
    if (LIBUSB_SUCCESS != r) {
246
0
      break;
247
0
    }
248
249
0
    usbi_dbg(NULL, "udev hotplug event. action: %s.", udev_action);
250
251
0
    if (strncmp(udev_action, "add", 3) == 0) {
252
0
      linux_hotplug_enumerate(busnum, devaddr, sys_name);
253
0
    } else if (detached) {
254
0
      linux_device_disconnected(busnum, devaddr);
255
0
    } else if (strncmp(udev_action, "bind", 4) == 0) {
256
      /* silently ignore "known unhandled" action */
257
0
    } else {
258
0
      usbi_err(NULL, "ignoring udev action %s", udev_action);
259
0
    }
260
0
  } while (0);
261
262
0
  udev_device_unref(udev_dev);
263
0
}
264
265
int linux_udev_scan_devices(struct libusb_context *ctx)
266
0
{
267
0
  struct udev_enumerate *enumerator;
268
0
  struct udev_list_entry *devices, *entry;
269
0
  struct udev_device *udev_dev;
270
0
  const char *sys_name;
271
0
  int r;
272
273
0
  assert(udev_ctx != NULL);
274
275
0
  enumerator = udev_enumerate_new(udev_ctx);
276
0
  if (NULL == enumerator) {
277
0
    usbi_err(ctx, "error creating udev enumerator");
278
0
    return LIBUSB_ERROR_OTHER;
279
0
  }
280
281
0
  udev_enumerate_add_match_subsystem(enumerator, "usb");
282
0
  udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device");
283
0
  udev_enumerate_scan_devices(enumerator);
284
0
  devices = udev_enumerate_get_list_entry(enumerator);
285
286
0
  entry = NULL;
287
0
  udev_list_entry_foreach(entry, devices) {
288
0
    const char *path = udev_list_entry_get_name(entry);
289
0
    uint8_t busnum = 0, devaddr = 0;
290
291
0
    udev_dev = udev_device_new_from_syspath(udev_ctx, path);
292
293
0
    r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
294
0
    if (r) {
295
0
      udev_device_unref(udev_dev);
296
0
      continue;
297
0
    }
298
299
0
    linux_enumerate_device(ctx, busnum, devaddr, sys_name);
300
0
    udev_device_unref(udev_dev);
301
0
  }
302
303
0
  udev_enumerate_unref(enumerator);
304
305
0
  return LIBUSB_SUCCESS;
306
0
}
307
308
void linux_udev_hotplug_poll(void)
309
0
{
310
0
  struct udev_device *udev_dev;
311
312
0
  usbi_mutex_static_lock(&linux_hotplug_lock);
313
0
  do {
314
0
    udev_dev = udev_monitor_receive_device(udev_monitor);
315
0
    if (udev_dev) {
316
0
      usbi_dbg(NULL, "Handling hotplug event from hotplug_poll");
317
0
      udev_hotplug_event(udev_dev);
318
0
    }
319
0
  } while (udev_dev);
320
0
  usbi_mutex_static_unlock(&linux_hotplug_lock);
321
0
}