1
0
mirror of git://sigrok.org/libserialport synced 2023-08-10 21:13:24 +03:00

Use O_CLOEXEC where available

Ensures that the file descriptor is (by default) not passed to
subprocesses spawned by applications using libserialport.

This fixes bug #1051.
This commit is contained in:
Christian Seiler 2017-10-15 19:28:05 +02:00 committed by Uwe Hermann
parent bd0fb6094f
commit fa106ef155
3 changed files with 33 additions and 11 deletions

View File

@ -85,6 +85,16 @@
#define TIOCOUTQ FIONWRITE #define TIOCOUTQ FIONWRITE
#endif #endif
/*
* O_CLOEXEC is not available everywhere, fallback to not setting the
* flag on those systems.
*/
#ifndef _WIN32
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#endif
/* Non-standard baudrates are not available everywhere. */ /* Non-standard baudrates are not available everywhere. */
#if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER #if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER
#define USE_TERMIOS_SPEED #define USE_TERMIOS_SPEED

32
linux.c
View File

@ -22,6 +22,18 @@
#include "libserialport.h" #include "libserialport.h"
#include "libserialport_internal.h" #include "libserialport_internal.h"
/*
* The 'e' modifier for O_CLOEXEC is glibc >= 2.7 only, hence not
* portable, so provide an own wrapper for this functionality.
*/
static FILE *fopen_cloexec_rdonly(const char *pathname)
{
int fd;
if ((fd = open(pathname, O_RDONLY | O_CLOEXEC)) < 0)
return NULL;
return fdopen(fd, "r");
}
SP_PRIV enum sp_return get_port_details(struct sp_port *port) SP_PRIV enum sp_return get_port_details(struct sp_port *port)
{ {
/* /*
@ -62,7 +74,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
strcat(sub_dir, "../"); strcat(sub_dir, "../");
snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum"); snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "busnum");
if (!(file = fopen(file_name, "r"))) if (!(file = fopen_cloexec_rdonly(file_name)))
continue; continue;
count = fscanf(file, "%d", &bus); count = fscanf(file, "%d", &bus);
fclose(file); fclose(file);
@ -70,7 +82,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
continue; continue;
snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum"); snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "devnum");
if (!(file = fopen(file_name, "r"))) if (!(file = fopen_cloexec_rdonly(file_name)))
continue; continue;
count = fscanf(file, "%d", &address); count = fscanf(file, "%d", &address);
fclose(file); fclose(file);
@ -78,7 +90,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
continue; continue;
snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor"); snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idVendor");
if (!(file = fopen(file_name, "r"))) if (!(file = fopen_cloexec_rdonly(file_name)))
continue; continue;
count = fscanf(file, "%4x", &vid); count = fscanf(file, "%4x", &vid);
fclose(file); fclose(file);
@ -86,7 +98,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
continue; continue;
snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct"); snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "idProduct");
if (!(file = fopen(file_name, "r"))) if (!(file = fopen_cloexec_rdonly(file_name)))
continue; continue;
count = fscanf(file, "%4x", &pid); count = fscanf(file, "%4x", &pid);
fclose(file); fclose(file);
@ -99,7 +111,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
port->usb_pid = pid; port->usb_pid = pid;
snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product"); snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
if ((file = fopen(file_name, "r"))) { if ((file = fopen_cloexec_rdonly(file_name))) {
if ((ptr = fgets(description, sizeof(description), file))) { if ((ptr = fgets(description, sizeof(description), file))) {
ptr = description + strlen(description) - 1; ptr = description + strlen(description) - 1;
if (ptr >= description && *ptr == '\n') if (ptr >= description && *ptr == '\n')
@ -112,7 +124,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
port->description = strdup(dev); port->description = strdup(dev);
snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "manufacturer"); snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "manufacturer");
if ((file = fopen(file_name, "r"))) { if ((file = fopen_cloexec_rdonly(file_name))) {
if ((ptr = fgets(manufacturer, sizeof(manufacturer), file))) { if ((ptr = fgets(manufacturer, sizeof(manufacturer), file))) {
ptr = manufacturer + strlen(manufacturer) - 1; ptr = manufacturer + strlen(manufacturer) - 1;
if (ptr >= manufacturer && *ptr == '\n') if (ptr >= manufacturer && *ptr == '\n')
@ -123,7 +135,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
} }
snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product"); snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
if ((file = fopen(file_name, "r"))) { if ((file = fopen_cloexec_rdonly(file_name))) {
if ((ptr = fgets(product, sizeof(product), file))) { if ((ptr = fgets(product, sizeof(product), file))) {
ptr = product + strlen(product) - 1; ptr = product + strlen(product) - 1;
if (ptr >= product && *ptr == '\n') if (ptr >= product && *ptr == '\n')
@ -134,7 +146,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
} }
snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "serial"); snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "serial");
if ((file = fopen(file_name, "r"))) { if ((file = fopen_cloexec_rdonly(file_name))) {
if ((ptr = fgets(serial, sizeof(serial), file))) { if ((ptr = fgets(serial, sizeof(serial), file))) {
ptr = serial + strlen(serial) - 1; ptr = serial + strlen(serial) - 1;
if (ptr >= serial && *ptr == '\n') if (ptr >= serial && *ptr == '\n')
@ -160,7 +172,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
if (port->transport == SP_TRANSPORT_BLUETOOTH) { if (port->transport == SP_TRANSPORT_BLUETOOTH) {
snprintf(file_name, sizeof(file_name), dir_name, dev, "", "address"); snprintf(file_name, sizeof(file_name), dir_name, dev, "", "address");
if ((file = fopen(file_name, "r"))) { if ((file = fopen_cloexec_rdonly(file_name))) {
if ((ptr = fgets(baddr, sizeof(baddr), file))) { if ((ptr = fgets(baddr, sizeof(baddr), file))) {
ptr = baddr + strlen(baddr) - 1; ptr = baddr + strlen(baddr) - 1;
if (ptr >= baddr && *ptr == '\n') if (ptr >= baddr && *ptr == '\n')
@ -215,7 +227,7 @@ SP_PRIV enum sp_return list_ports(struct sp_port ***list)
* is to try to open them and make an ioctl call. * is to try to open them and make an ioctl call.
*/ */
DEBUG("serial8250 device, attempting to open"); DEBUG("serial8250 device, attempting to open");
if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) { if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY | O_CLOEXEC)) < 0) {
DEBUG("Open failed, skipping"); DEBUG("Open failed, skipping");
continue; continue;
} }

View File

@ -563,7 +563,7 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
RETURN_CODEVAL(ret); RETURN_CODEVAL(ret);
} }
#else #else
int flags_local = O_NONBLOCK | O_NOCTTY; int flags_local = O_NONBLOCK | O_NOCTTY | O_CLOEXEC;
/* Map 'flags' to the OS-specific settings. */ /* Map 'flags' to the OS-specific settings. */
if ((flags & SP_MODE_READ_WRITE) == SP_MODE_READ_WRITE) if ((flags & SP_MODE_READ_WRITE) == SP_MODE_READ_WRITE)