| 163 | } |
| 164 | |
| 165 | void UMockdevDeviceManager::umockdevInit() |
| 166 | { |
| 167 | const char* const envval = ::getenv("USBGUARD_UMOCKDEV_DEVICEDIR"); |
| 168 | |
| 169 | if (envval == nullptr) { |
| 170 | throw Exception("UMockdevDeviceManager", "USBGUARD_UMOCKDEV_DEVICEDIR", "Environment variable not set."); |
| 171 | } |
| 172 | |
| 173 | _umockdev_deviceroot = std::string(envval); |
| 174 | _testbed.reset(umockdev_testbed_new()); |
| 175 | USBGUARD_LOG(Info) << "umockdev device directory set to " << _umockdev_deviceroot; |
| 176 | const auto lambdaUMockdevFilterEntry = [](const std::string& fullpath, const struct dirent* dirent) -> std::string { |
| 177 | (void)dirent; |
| 178 | struct stat st = {}; |
| 179 | |
| 180 | if (::stat(fullpath.c_str(), &st) != 0) |
| 181 | { |
| 182 | USBGUARD_LOG(Warning) << "stat() failed: " << fullpath << ": Skipping file!"; |
| 183 | return std::string(); |
| 184 | } |
| 185 | |
| 186 | if (S_ISREG(st.st_mode)) |
| 187 | { |
| 188 | return fullpath; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | return std::string(); |
| 193 | } |
| 194 | }; |
| 195 | const auto lambdaUMockdevAddFromFile = [this](const std::string& fullpath, const std::string& loadpath) -> int { |
| 196 | (void)fullpath; |
| 197 | |
| 198 | for (const auto& device_path : umockdevLoadFromFile(loadpath)) |
| 199 | { |
| 200 | umockdevAdd(_sysfs_path_map.at(device_path)); |
| 201 | } |
| 202 | |
| 203 | return 1; |
| 204 | }; |
| 205 | loadFiles(_umockdev_deviceroot, lambdaUMockdevFilterEntry, lambdaUMockdevAddFromFile); |
| 206 | USBGUARD_SYSCALL_THROW("UMockdevDeviceManager", (_inotify_fd = inotify_init1(IN_NONBLOCK)) < 0); |
| 207 | USBGUARD_SYSCALL_THROW("UMockdevDeviceManager", (inotify_add_watch(_inotify_fd, _umockdev_deviceroot.c_str(), |
| 208 | IN_CREATE|IN_DELETE)) < 0); |
| 209 | } |
| 210 | |
| 211 | void UMockdevDeviceManager::umockdevAdd(const std::shared_ptr<UMockdevDeviceDefinition>& definition) |
| 212 | { |