From patchwork Fri Dec 2 16:41:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 17939 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E72D8BE08B for ; Fri, 2 Dec 2022 16:41:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4DA4363336; Fri, 2 Dec 2022 17:41:54 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1669999314; bh=c5ik94Nur3PWLQReTYSA1lR2gzW8n14wXIWHAS2qIqg=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KbslqjJz7G4EYERgwbUHijykioNyRtjyk2TTJw9UNLsCqHw2ipEYKbvw86GzzPyZj PUcxd0mUZ1FDlBjLG/kctJRnGEYWYizvSWnvojaYNSgztiP9uV28lcvIj3dkdzVXa1 ODutINBJKgeOSLVjpxkpvv/dqpdkMJteOMKOxyqjhmyy5SmV8MR9VmYWMYvE8Ao1zC lAtLTU+cyfsp0c55psIekYjnbN4TffnZfFcP/MuqovnhotLuDbFK1ycIrt/VNwy4Cb RR+l3l6vaCknKzaT5f8cp2DCbpzN4gXxdR84jnOexXcnFqZmCPvD80Ar+w0tj4jU7e wBPLp7Vg7IzjA== Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 55EA460483 for ; Fri, 2 Dec 2022 17:41:52 +0100 (CET) Received: (Authenticated sender: jacopo@jmondi.org) by mail.gandi.net (Postfix) with ESMTPSA id C5EC54000B; Fri, 2 Dec 2022 16:41:51 +0000 (UTC) To: libcamera-devel@lists.libcamera.org Date: Fri, 2 Dec 2022 17:41:44 +0100 Message-Id: <20221202164145.30603-1-jacopo@jmondi.org> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] libcamera: utils: Wrap usage of lockf() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jacopo Mondi via libcamera-devel From: Jacopo Mondi Reply-To: Jacopo Mondi Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The lockf() function is not implemented in the Bionic standard C library. As an alternative, since Linux v2.0 the flock() function is available as a direct system call instead of being emulated in the C library. As lockf() is instead usually implemented as an interface to fcntl() locking, locks placed by flock() and lockf() might not be detected. As reported by the flock() function documentation: Since kernel 2.0, flock() is implemented as a system call in its own right rather than being emulated in the GNU C library as a call to fcntl(2). With this implementation, there is no interaction between the types of lock placed by flock() and fc‐ ntl(2), and flock() does not detect deadlock. (Note, however, that on some systems, such as the modern BSDs, flock() and fc‐ ntl(2) locks do interact with one another.) To avoid risks of undetected deadlock, provide an wrapper function utils, which in case flock() is not available, deflects all calls to lockf() instead. Signed-off-by: Jacopo Mondi --- src/libcamera/media_device.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 52c8e66e9e99..bffb241efa7c 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -20,6 +20,34 @@ #include +/* + * Android NDK workaround. + * + * Bionic does not implement the lockf function before API release 24. + * If we're building for a recent enough Android release include the + * correct header, if we're building for an older release, deflect + * flock() on the lockf() system call. + * + * A note on flock()/lockf() co-existency: it would be easier to change + * all usages of lockf() in libcamera to flock(). However lockf() is + * implemented as an interface on fcntl() while flock() is a system call + * since Linux v2.0. Locks set with lockf() won't be detected by flock() + * and vice-versa, hence mixing the two is highly undesirable. For this + * reason if lockf() is available prefer it, assuming all other + * applications in the system will do the same. Only deflect on flock() + * as last resort and only on Android systems. + */ +#if __ANDROID_API__ >= 24 + #include +#elif defined(__ANDROID_API__) + #undef F_TLOCK + #undef F_ULOCK + #include + #define F_TLOCK (LOCK_EX | LOCK_NB) + #define F_ULOCK LOCK_UN + #define lockf(f, c, o) flock(f, c) +#endif + /** * \file media_device.h * \brief Provide a representation of a Linux kernel Media Controller device From patchwork Fri Dec 2 16:41:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 17940 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id A92D6C3284 for ; Fri, 2 Dec 2022 16:41:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BEE6A63342; Fri, 2 Dec 2022 17:41:54 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1669999314; bh=CpvkOAi3RnVI942NlfvRwJgcBoRlsmxDztZ20i7M/W8=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=rhOMpQZpNER/8zU4NlqO+5b7hiQCa+AFE7mL/eLjLE4SyNO18eUqWtQ9zWLlwV73t Cdxg14FSTVghhAAdGJ7I4J5rFU0Lkclz0Te5ejcueeUasxnJAcvnIpEFWcSeGwxWmX wpfxgCOivwIlElaeSQSdumpCwwyHsl3o+wqZmGoYjjuZehzihCqtIEcuFPcKBG5EWX cqmCDJofevyERRAR6d5UCtuvzO34i0UK3wCp35vk3nj5zjQSRHX/PtJcG4p7T0p6cV ehBKtublAgp6/bo+HY5qbhgF10xbKVbudx97WzqD2XxelK5AXQj9rfmcIbz3TwlcXj lEA8UdX3FUR+Q== Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C8B9360483 for ; Fri, 2 Dec 2022 17:41:52 +0100 (CET) Received: (Authenticated sender: jacopo@jmondi.org) by mail.gandi.net (Postfix) with ESMTPSA id 63F8D4000B; Fri, 2 Dec 2022 16:41:52 +0000 (UTC) To: libcamera-devel@lists.libcamera.org Date: Fri, 2 Dec 2022 17:41:45 +0100 Message-Id: <20221202164145.30603-2-jacopo@jmondi.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221202164145.30603-1-jacopo@jmondi.org> References: <20221202164145.30603-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/2] libcamera: utils: Provide strchrnul() wrapper X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jacopo Mondi via libcamera-devel From: Jacopo Mondi Reply-To: Jacopo Mondi Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The strchrnul function is a GNU-specific extension to string.h and it's not availalable on all C libraries (in example, Android's Bionic). Provide an implementation of the function based around the generally available strchr() function and use it in the Logger class. Open code the same wrapper in the application's option parser, where we cannot use the internal utils namespace. Signed-off-by: Jacopo Mondi --- include/libcamera/base/utils.h | 1 + src/apps/common/options.cpp | 5 ++++- src/libcamera/base/log.cpp | 2 +- src/libcamera/base/utils.cpp | 19 +++++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index eb7bcdf4c173..b7064ec6253b 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -36,6 +36,7 @@ namespace libcamera { namespace utils { const char *basename(const char *path); +const char *strchrnul(const char *s, int c); char *secure_getenv(const char *name); std::string dirname(const std::string &path); diff --git a/src/apps/common/options.cpp b/src/apps/common/options.cpp index 4f7e869144c8..1bbbec7fa25c 100644 --- a/src/apps/common/options.cpp +++ b/src/apps/common/options.cpp @@ -363,7 +363,10 @@ KeyValueParser::Options KeyValueParser::parse(const char *arguments) Options options; for (const char *pair = arguments; *arguments != '\0'; pair = arguments) { - const char *comma = strchrnul(arguments, ','); + const char *comma = strchr(arguments, ','); + if (!comma) + comma = &arguments[strlen(arguments)]; + size_t len = comma - pair; /* Skip over the comma. */ diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 55fbd7b03438..cc30681fd67f 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -629,7 +629,7 @@ void Logger::parseLogLevels() return; for (const char *pair = debug; *debug != '\0'; pair = debug) { - const char *comma = strchrnul(debug, ','); + const char *comma = utils::strchrnul(debug, ','); size_t len = comma - pair; /* Skip over the comma. */ diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index 6a307940448e..f829a8106b76 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -39,6 +39,25 @@ const char *basename(const char *path) return base ? base + 1 : path; } +/** + * \brief Implement strchnul wrapper + * \param[in] s The string to seach on + * \param[in] c The character to search + * + * The strchrnul function is a GNU-specific extension to string.h and it's not + * available on all C libraries (in example, Android's Bionic). This + * implementation realizes strchrnul() on strchr() which is instead more + * generally available. + * + * \return A pointer to the first occurrence of \a c in \a s, or a pointer to + * the null byte at the end of \a s if \a c is not found + */ +const char *strchrnul(const char *s, int c) +{ + const char *p = strchr(s, c); + return p ? : s + strlen(s); +} + /** * \brief Get an environment variable * \param[in] name The name of the variable to return