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

buses/uart: Use 'lockf()' for serial locking #637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions libnfc/buses/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ const char *serial_ports_device_radix[] = { "ttyUSB", "ttyS", "ttyACM", "ttyAMA"
# define FIONREAD TIOCINQ
#endif

// Work-around to claim uart interface using the c_iflag (software input processing) from the termios struct
# define CCLAIMED 0x80000000

struct serial_port_unix {
int fd; // Serial port file descriptor
struct termios termios_backup; // Terminal info before using the port
Expand Down Expand Up @@ -124,15 +121,15 @@ uart_open(const char *pcPortName)
return INVALID_SERIAL_PORT;
}
// Make sure the port is not claimed already
if (sp->termios_backup.c_iflag & CCLAIMED) {
if (lockf(sp->fd, F_TLOCK, 0)) {
uart_close_ext(sp, false);
return CLAIMED_SERIAL_PORT;
}
// Copy the old terminal info struct
sp->termios_new = sp->termios_backup;

sp->termios_new.c_cflag = CS8 | CLOCAL | CREAD;
sp->termios_new.c_iflag = CCLAIMED | IGNPAR;
sp->termios_new.c_iflag = IGNPAR;
sp->termios_new.c_oflag = 0;
sp->termios_new.c_lflag = 0;

Expand Down Expand Up @@ -282,6 +279,7 @@ uart_close_ext(const serial_port sp, const bool restore_termios)
if (UART_DATA(sp)->fd >= 0) {
if (restore_termios)
tcsetattr(UART_DATA(sp)->fd, TCSANOW, &UART_DATA(sp)->termios_backup);
lockf(UART_DATA(sp)->fd, F_ULOCK, 0);
close(UART_DATA(sp)->fd);
}
free(sp);
Expand Down