From 95bad38c5b073f8e44e525de3846e5538373eb10 Mon Sep 17 00:00:00 2001 From: Stefan Tauner Date: Sun, 21 May 2017 23:42:06 +0200 Subject: [PATCH] Canonicalize symlinks in portnames This allows users to supply symlinks created e.g. by udev rules instead of the actual device names. --- configure.ac | 3 +++ libserialport_internal.h | 2 +- serialport.c | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 19ab3aa..3e19f41 100644 --- a/configure.ac +++ b/configure.ac @@ -126,6 +126,9 @@ AC_CHECK_DECLS([BOTHER],,, [[#include ]]) # Check for serial_struct. AC_CHECK_TYPES([struct serial_struct],,, [[#include ]]) +# Check for realpath(). +AC_CHECK_FUNC([realpath], [AC_DEFINE(HAVE_REALPATH, 1, [realpath is available.])], []) + AC_CACHE_CHECK([for visibility control], [sp_cv_visibility_control], [ sp_saved_CFLAGS=$CFLAGS CFLAGS="$CFLAGS -Werror" diff --git a/libserialport_internal.h b/libserialport_internal.h index 308b20b..83f0090 100644 --- a/libserialport_internal.h +++ b/libserialport_internal.h @@ -23,7 +23,7 @@ #ifdef __linux__ -/* For timeradd, timersub, timercmp. */ +/* For timeradd, timersub, timercmp, realpath. */ #define _BSD_SOURCE 1 /* for glibc < 2.19 */ #define _DEFAULT_SOURCE 1 /* for glibc >= 2.20 */ #endif diff --git a/serialport.c b/serialport.c index d271478..db2aa43 100644 --- a/serialport.c +++ b/serialport.c @@ -75,6 +75,20 @@ SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port * DEBUG_FMT("Building structure for port %s", portname); +#if !defined(_WIN32) && defined(HAVE_REALPATH) + /* + * get_port_details() below tries to be too smart and figure out + * some transport properties from the port name which breaks with + * symlinks. Therefore we canonicalize the portname first. + */ + char pathbuf[PATH_MAX + 1]; + char *res = realpath(portname, pathbuf); + if (!res) + RETURN_ERROR(SP_ERR_ARG, "Could not retrieve realpath behind port name"); + + portname = pathbuf; +#endif + if (!(port = malloc(sizeof(struct sp_port)))) RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed");