Ensure port still exists when trying to gain elevated access

This commit is contained in:
Will Hedgecock 2022-01-28 14:23:46 -06:00
parent bc2ec3e8c4
commit 93011c9aa8
28 changed files with 36 additions and 51 deletions

View File

@ -1640,10 +1640,13 @@ int verifyAndSetUserPortGroup(const char *portFile)
// Attempt to acquire access if not available
if (!userCanAccess)
{
// Check if the user is part of the group that owns the port
// Ensure that the port still exists
struct stat fileStats;
if (stat(portFile, &fileStats) == 0)
{
// Check if the user is part of the group that owns the port
gid_t *userGroups = (gid_t*)malloc(numGroups * sizeof(gid_t));
if ((stat(portFile, &fileStats) == 0) && (getgroups(numGroups, userGroups) >= 0))
if (getgroups(numGroups, userGroups) >= 0)
for (int i = 0; i < numGroups; ++i)
if (userGroups[i] == fileStats.st_gid)
{
@ -1672,6 +1675,7 @@ int verifyAndSetUserPortGroup(const char *portFile)
free(addUserToGroupCmd);
free(userGroups);
}
}
// Return whether the user can currently access the serial port
return userCanAccess;

View File

@ -268,18 +268,22 @@ public class SerialPortTest
ubxPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() { return SerialPort.LISTENING_EVENT_PARITY_ERROR | SerialPort.LISTENING_EVENT_DATA_WRITTEN | SerialPort.LISTENING_EVENT_BREAK_INTERRUPT |
SerialPort.LISTENING_EVENT_CARRIER_DETECT | SerialPort.LISTENING_EVENT_CTS | SerialPort.LISTENING_EVENT_DSR | SerialPort.LISTENING_EVENT_RING_INDICATOR |
SerialPort.LISTENING_EVENT_CARRIER_DETECT | SerialPort.LISTENING_EVENT_CTS | SerialPort.LISTENING_EVENT_DSR | SerialPort.LISTENING_EVENT_RING_INDICATOR | SerialPort.LISTENING_EVENT_PORT_DISCONNECTED |
SerialPort.LISTENING_EVENT_FRAMING_ERROR | SerialPort.LISTENING_EVENT_FIRMWARE_OVERRUN_ERROR | SerialPort.LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR | SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
@Override
public void serialEvent(SerialPortEvent event)
{
System.out.println("Received event type: " + event.getEventType());
if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
{
System.out.println("Received event type: LISTENING_EVENT_DATA_AVAILABLE");
byte[] buffer = new byte[event.getSerialPort().bytesAvailable()];
event.getSerialPort().readBytes(buffer, buffer.length);
System.out.println(" Reading " + buffer.length + " bytes");
}
else if (event.getEventType() == SerialPort.LISTENING_EVENT_PORT_DISCONNECTED)
System.out.println("Received event type: LISTENING_EVENT_PORT_DISCONNECTED");
else
System.out.println("Received event type: " + event.getEventType());
}
});
try { Thread.sleep(5000); } catch (Exception e) {}
@ -311,29 +315,6 @@ public class SerialPortTest
ubxPort.removeDataListener();
System.out.println("\nClosing " + ubxPort.getDescriptivePortName() + ": " + ubxPort.closePort());
/*System.out.println("\nPhysically unplug device within the next 10 seconds to see if the disconnect event fires...");
ubxPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() { return SerialPort.LISTENING_EVENT_PORT_DISCONNECTED | SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
@Override
public void serialEvent(SerialPortEvent event)
{
if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
{
System.out.println("Received event type: LISTENING_EVENT_DATA_AVAILABLE");
byte[] buffer = new byte[event.getSerialPort().bytesAvailable()];
event.getSerialPort().readBytes(buffer, buffer.length);
System.out.println(" Reading " + buffer.length + " bytes");
}
else
{
System.out.println("Received event type: LISTENING_EVENT_PORT_DISCONNECTED");
}
}
});
try { Thread.sleep(10000); } catch (Exception e) {}
ubxPort.closePort();*/
/*System.out.println("\n\nAttempting to read from two serial ports simultaneously\n");
System.out.println("\nAvailable Ports:\n");
for (int i = 0; i < ports.length; ++i)