1

I wrote a C++ program in Linux that notifies me whenever an external device is connected or disconnected from my system. I used netlink for the purpose and poll function call is used to wait for events. But now I am facing a major problem:

When I insert a pendrive to my system, the poll() is called many times(I happened to see it 4 times). I am storing the values to a std::map for later use. Now when I connect another device, I want the map to be cleared for storing the new information. How can I understand whether the output is related to a second device or not?

if(*prev_tok == "ACTION")
{
  cout << "Action = "<< *curr_tok << "\n";
  DeviceDetails["Action"] = *curr_tok;
}
else if(*prev_tok == "ID_FS_LABEL")
{
  cout << "LABEL = "<< *curr_tok << "\n";
  DeviceDetails["LABEL"] = *curr_tok;
}
else if(*prev_tok == "ID_FS_VERSION")
{
  cout << "FileSystem Version = "<< *curr_tok << "\n";
  DeviceDetails["FileSystem Version"] = *curr_tok;
}
else if(*prev_tok == "ID_FS_UUID")
{
  cout << "UUID = "<< *curr_tok << "\n";
  DeviceDetails["UUID"] = *curr_tok;
}
else if(*prev_tok == "ID_MODEL_ID")
{
  cout << "Model ID = "<< *curr_tok << "\n";
  DeviceDetails["Model ID"] = *curr_tok;
}
else if(*prev_tok == "ID_MODEL")
{
  cout << "Model Name = "<< *curr_tok << "\n";
  DeviceDetails["Model Name"] = *curr_tok;
}
else if(*prev_tok == "ID_SERIAL_SHORT")
{
  cout << "Serial Number = "<< *curr_tok << "\n";
  DeviceDetails["Serial Number"] = *curr_tok;
}
else if(*prev_tok == "ID_REVISION")
{
  cout << "Version = "<< *curr_tok << "\n";
  DeviceDetails["Version"] = *curr_tok;
}
else if(*prev_tok == "ID_VENDOR")
{
  cout << "Vendor Name = "<< *curr_tok << "\n";
  DeviceDetails["Vendor Name"] = *curr_tok;
}
else if(*prev_tok == "ID_VENDOR_ID")
{
  cout << "Vendor ID = "<< *curr_tok << "\n";
  DeviceDetails["Vendor ID"] = *curr_tok;
}
else if(*prev_tok == "MODALIAS")
{
  cout << "Description = "<< *curr_tok << "\n";
  DeviceDetails["Description"] = *curr_tok;
}
else if(*prev_tok == "DEVPATH")
{
  cout << "Device Path = "<< *curr_tok << "\n";
  DeviceDetails["Device Path"] = *curr_tok;
}
else if(*prev_tok == "DEVLINKS")
{
  cout << "Device Links = "<< *curr_tok << "\n";
  DeviceDetails["Device LINk"] = *curr_tok;
}
else if(*prev_tok == "ID_CDROM_MEDIA")
{
  cout << "CD INSERTED\n";
  DeviceDetails["CDROM_MEDIA"] = *curr_tok;
}
else if(*prev_tok == "ID_CDROM_DVD")
{
  cout << "DVD\n\n";
  DeviceDetails["CDROM_DVD"] = *curr_tok;
}
else if(*prev_tok == "ID_TYPE")
{
  cout << "Device Type = "<< *curr_tok << "\n";
  DeviceDetails["Device Type"] = *curr_tok;
}
Jackzz
  • 1,417
  • 4
  • 24
  • 53
  • You should filter notifications and pick out only the most important one. Overview to get you started: http://free-electrons.com/doc/udev.pdf Typically you'd see a chain of events, e.g. "new usb device", "new disk drive", "partitions detected", "filesystem mounted", etc... – Dima Tisnek Jun 12 '15 at 06:57
  • @qarma : I am picking only some details as edited in the question. But I want to know whether the polling was performed by a second device or not. – Jackzz Jun 12 '15 at 07:02
  • `PHYSDEVPATH` seems like the attribute you want. Assuming names didn't change in last 5~10 years :) – Dima Tisnek Jun 12 '15 at 07:04
  • @qarma : But I dont get any `PHYSDEVPATH` from data read using netlink socket. – Jackzz Jun 12 '15 at 08:30
  • I see you have `DEVPATH`, perhaps that's same? – Dima Tisnek Jun 12 '15 at 09:17
  • no..DEVPATH is different for the same device itself.. – Jackzz Jun 12 '15 at 09:20

0 Answers0