1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef DEVICE_UDEV_LINUX_UDEV_LOADER_H_
6 #define DEVICE_UDEV_LINUX_UDEV_LOADER_H_
7 
8 #include <stdarg.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 
12 #if !defined(USE_UDEV)
13 #error "USE_UDEV not defined"
14 #endif
15 
16 struct udev;
17 struct udev_device;
18 struct udev_enumerate;
19 struct udev_list_entry;
20 struct udev_monitor;
21 
22 namespace device {
23 
24 // Interface to libudev. Accessed through the static Get() function, which
25 // will try to load libudev1 first and then libudev0 on first use. If neither
26 // libraries load successfully, the program will fail with a crash.
27 //
28 // All the methods have the same signatures as libudev's functions. e.g.
29 // udev_monitor_get_fd(mon) simply becomes device::udev_monitor_get_fd(mon).
30 class UdevLoader {
31  public:
32   static UdevLoader* Get();
33 
34   // Allows to set a particular implementation of the loader.
35   // Given the shape of the existing API bad things will happen if one gets
36   // a udev_device instance, switches to a new loader, and tries to use that
37   // udev_instance again. The expectation is that when running unit tests,
38   // before we switch to fake udev, all attempts to interact with real udev
39   // will fail.
40   static void SetForTesting(UdevLoader* loader, bool delete_previous = true);
41 
42   virtual ~UdevLoader();
43 
44   virtual bool Init() = 0;
45 
46   virtual const char* udev_device_get_action(udev_device* udev_device) = 0;
47   virtual const char* udev_device_get_devnode(udev_device* udev_device) = 0;
48   virtual const char* udev_device_get_devtype(udev_device* udev_device) = 0;
49   virtual udev_device* udev_device_get_parent(udev_device* udev_device) = 0;
50   virtual udev_device* udev_device_get_parent_with_subsystem_devtype(
51       udev_device* udev_device,
52       const char* subsystem,
53       const char* devtype) = 0;
54   virtual const char* udev_device_get_property_value(udev_device* udev_device,
55                                                      const char* key) = 0;
56   virtual const char* udev_device_get_subsystem(udev_device* udev_device) = 0;
57   virtual const char* udev_device_get_sysattr_value(udev_device* udev_device,
58                                                     const char* sysattr) = 0;
59   virtual const char* udev_device_get_sysname(udev_device* udev_device) = 0;
60   virtual const char* udev_device_get_syspath(udev_device* udev_device) = 0;
61   virtual udev_device* udev_device_new_from_devnum(udev* udev,
62                                                    char type,
63                                                    dev_t devnum) = 0;
64   virtual udev_device* udev_device_new_from_subsystem_sysname(
65       udev* udev,
66       const char* subsystem,
67       const char* sysname) = 0;
68   virtual udev_device* udev_device_new_from_syspath(udev* udev,
69                                                     const char* syspath) = 0;
70   virtual void udev_device_unref(udev_device* udev_device) = 0;
71   virtual int udev_enumerate_add_match_subsystem(udev_enumerate* udev_enumerate,
72                                                  const char* subsystem) = 0;
73   virtual udev_list_entry* udev_enumerate_get_list_entry(
74       udev_enumerate* udev_enumerate) = 0;
75   virtual udev_enumerate* udev_enumerate_new(udev* udev) = 0;
76   virtual int udev_enumerate_scan_devices(udev_enumerate* udev_enumerate) = 0;
77   virtual void udev_enumerate_unref(udev_enumerate* udev_enumerate) = 0;
78   virtual udev_list_entry* udev_list_entry_get_next(
79       udev_list_entry* list_entry) = 0;
80   virtual const char* udev_list_entry_get_name(udev_list_entry* list_entry) = 0;
81   virtual int udev_monitor_enable_receiving(udev_monitor* udev_monitor) = 0;
82   virtual int udev_monitor_filter_add_match_subsystem_devtype(
83       udev_monitor* udev_monitor,
84       const char* subsystem,
85       const char* devtype) = 0;
86   virtual int udev_monitor_get_fd(udev_monitor* udev_monitor) = 0;
87   virtual udev_monitor* udev_monitor_new_from_netlink(udev* udev,
88                                                       const char* name) = 0;
89   virtual udev_device* udev_monitor_receive_device(
90       udev_monitor* udev_monitor) = 0;
91   virtual void udev_monitor_unref(udev_monitor* udev_monitor) = 0;
92   virtual udev* udev_new() = 0;
93   virtual void udev_set_log_fn(
94       struct udev* udev,
95       void (*log_fn)(struct udev* udev, int priority,
96                      const char* file, int line,
97                      const char* fn, const char* format, va_list args)) = 0;
98   virtual void udev_set_log_priority(struct udev* udev, int priority) = 0;
99   virtual void udev_unref(udev* udev) = 0;
100 };
101 
102 }  // namespace device
103 
104 #endif  // DEVICE_UDEV_LINUX_UDEV_LOADER_H_
105