Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor device enumeration for unprivileged containers #11900

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 73 additions & 5 deletions src/linux/backend-v4l2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,64 @@ namespace librealsense
}
}

bool get_devname_from_video_path(const std::string& video_path, std::string& dev_name)
{
std::ifstream uevent_file(video_path + "/uevent");
if (!uevent_file)
{
LOG_ERROR("Cannot access " + video_path + "/uevent");
return false;
}

std::string uevent_line, major_string, minor_string;
while (std::getline(uevent_file, uevent_line) && (major_string.empty() || minor_string.empty()))
{
if (uevent_line.find("MAJOR=") != std::string::npos)
{
major_string = uevent_line.substr(uevent_line.find_last_of('=') + 1);
}
else if (uevent_line.find("MINOR=") != std::string::npos)
{
minor_string = uevent_line.substr(uevent_line.find_last_of('=') + 1);
}
}
uevent_file.close();

if (major_string.empty() || minor_string.empty())
{
LOG_ERROR("No Major or Minor number found for " + video_path);
return false;
}

DIR * dir = opendir("/dev");
if (!dir)
{
LOG_ERROR("Cannot access /dev");
return false;
}
while (dirent * entry = readdir(dir))
{
std::string name = entry->d_name;
std::string path = "/dev/" + name;

struct stat st = {};
if (stat(path.c_str(), &st) < 0)
{
continue;
}

if (std::stoi(major_string) == major(st.st_rdev) && std::stoi(minor_string) == minor(st.st_rdev))
{
dev_name = path;
closedir(dir);
return true;
}
}
closedir(dir);

return false;
}

std::vector<std::string> v4l_uvc_device::get_video_paths()
{
std::vector<std::string> video_paths;
Expand Down Expand Up @@ -593,8 +651,11 @@ namespace librealsense
//LOG_INFO("Skipping Video4Linux entry " << real_path << " - not a device");
continue;
}
if (get_devname_from_video_path(real_path, name))
{
video_paths.push_back(real_path);
}
}
video_paths.push_back(real_path);
}
closedir(dir);

Expand Down Expand Up @@ -657,8 +718,11 @@ namespace librealsense

uvc_device_info v4l_uvc_device::get_info_from_usb_device_path(const std::string& video_path, const std::string& name)
{
std::string busnum, devnum, devpath;
auto dev_name = "/dev/" + name;
std::string busnum, devnum, devpath, dev_name;
if (!get_devname_from_video_path(video_path, dev_name))
{
throw linux_backend_exception(rsutils::string::from() << "Unable to find dev_name from video_path: " << video_path);
}

if (!is_usb_path_valid(video_path, dev_name, busnum, devnum, devpath))
{
Expand Down Expand Up @@ -923,8 +987,12 @@ namespace librealsense
{
continue;
}
auto dev_name = "/dev/" + name;
uvc_nodes.emplace_back(info, dev_name);

std::string dev_name;
if (get_devname_from_video_path(video_path, dev_name))
{
uvc_nodes.emplace_back(info, dev_name);
}
}
catch(const std::exception & e)
{
Expand Down
Loading