Ensure port still exists when trying to gain elevated access
This commit is contained in:
parent
bc2ec3e8c4
commit
93011c9aa8
|
@ -1640,37 +1640,41 @@ int verifyAndSetUserPortGroup(const char *portFile)
|
||||||
// Attempt to acquire access if not available
|
// Attempt to acquire access if not available
|
||||||
if (!userCanAccess)
|
if (!userCanAccess)
|
||||||
{
|
{
|
||||||
// Check if the user is part of the group that owns the port
|
// Ensure that the port still exists
|
||||||
struct stat fileStats;
|
struct stat fileStats;
|
||||||
gid_t *userGroups = (gid_t*)malloc(numGroups * sizeof(gid_t));
|
if (stat(portFile, &fileStats) == 0)
|
||||||
if ((stat(portFile, &fileStats) == 0) && (getgroups(numGroups, userGroups) >= 0))
|
|
||||||
for (int i = 0; i < numGroups; ++i)
|
|
||||||
if (userGroups[i] == fileStats.st_gid)
|
|
||||||
{
|
|
||||||
userPartOfPortGroup = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attempt to add the user to the group that owns the port
|
|
||||||
char *addUserToGroupCmd = (char*)malloc(256);
|
|
||||||
if (!userPartOfPortGroup)
|
|
||||||
{
|
{
|
||||||
struct group *portGroup;
|
// Check if the user is part of the group that owns the port
|
||||||
struct passwd *userDetails;
|
gid_t *userGroups = (gid_t*)malloc(numGroups * sizeof(gid_t));
|
||||||
if ((portGroup = getgrgid(fileStats.st_gid)) && (userDetails = getpwuid(geteuid())))
|
if (getgroups(numGroups, userGroups) >= 0)
|
||||||
|
for (int i = 0; i < numGroups; ++i)
|
||||||
|
if (userGroups[i] == fileStats.st_gid)
|
||||||
|
{
|
||||||
|
userPartOfPortGroup = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt to add the user to the group that owns the port
|
||||||
|
char *addUserToGroupCmd = (char*)malloc(256);
|
||||||
|
if (!userPartOfPortGroup)
|
||||||
{
|
{
|
||||||
snprintf(addUserToGroupCmd, 256, "sudo usermod -a -G %s %s", portGroup->gr_name, userDetails->pw_name);
|
struct group *portGroup;
|
||||||
userCanAccess = (system(addUserToGroupCmd) == 0);
|
struct passwd *userDetails;
|
||||||
|
if ((portGroup = getgrgid(fileStats.st_gid)) && (userDetails = getpwuid(geteuid())))
|
||||||
|
{
|
||||||
|
snprintf(addUserToGroupCmd, 256, "sudo usermod -a -G %s %s", portGroup->gr_name, userDetails->pw_name);
|
||||||
|
userCanAccess = (system(addUserToGroupCmd) == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempt to enable all read/write port permissions
|
||||||
|
snprintf(addUserToGroupCmd, 256, "sudo chmod 666 %s", portFile);
|
||||||
|
userCanAccess = (system(addUserToGroupCmd) == 0) || userCanAccess;
|
||||||
|
|
||||||
|
// Clean up memory
|
||||||
|
free(addUserToGroupCmd);
|
||||||
|
free(userGroups);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to enable all read/write port permissions
|
|
||||||
snprintf(addUserToGroupCmd, 256, "sudo chmod 666 %s", portFile);
|
|
||||||
userCanAccess = (system(addUserToGroupCmd) == 0) || userCanAccess;
|
|
||||||
|
|
||||||
// Clean up memory
|
|
||||||
free(addUserToGroupCmd);
|
|
||||||
free(userGroups);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return whether the user can currently access the serial port
|
// Return whether the user can currently access the serial port
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -268,18 +268,22 @@ public class SerialPortTest
|
||||||
ubxPort.addDataListener(new SerialPortDataListener() {
|
ubxPort.addDataListener(new SerialPortDataListener() {
|
||||||
@Override
|
@Override
|
||||||
public int getListeningEvents() { return SerialPort.LISTENING_EVENT_PARITY_ERROR | SerialPort.LISTENING_EVENT_DATA_WRITTEN | SerialPort.LISTENING_EVENT_BREAK_INTERRUPT |
|
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; }
|
SerialPort.LISTENING_EVENT_FRAMING_ERROR | SerialPort.LISTENING_EVENT_FIRMWARE_OVERRUN_ERROR | SerialPort.LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR | SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
|
||||||
@Override
|
@Override
|
||||||
public void serialEvent(SerialPortEvent event)
|
public void serialEvent(SerialPortEvent event)
|
||||||
{
|
{
|
||||||
System.out.println("Received event type: " + event.getEventType());
|
|
||||||
if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
|
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()];
|
byte[] buffer = new byte[event.getSerialPort().bytesAvailable()];
|
||||||
event.getSerialPort().readBytes(buffer, buffer.length);
|
event.getSerialPort().readBytes(buffer, buffer.length);
|
||||||
System.out.println(" Reading " + buffer.length + " bytes");
|
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) {}
|
try { Thread.sleep(5000); } catch (Exception e) {}
|
||||||
|
@ -311,29 +315,6 @@ public class SerialPortTest
|
||||||
ubxPort.removeDataListener();
|
ubxPort.removeDataListener();
|
||||||
System.out.println("\nClosing " + ubxPort.getDescriptivePortName() + ": " + ubxPort.closePort());
|
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("\n\nAttempting to read from two serial ports simultaneously\n");
|
||||||
System.out.println("\nAvailable Ports:\n");
|
System.out.println("\nAvailable Ports:\n");
|
||||||
for (int i = 0; i < ports.length; ++i)
|
for (int i = 0; i < ports.length; ++i)
|
||||||
|
|
Loading…
Reference in New Issue