From patchwork Tue Aug 16 00:50:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Amiantov X-Patchwork-Id: 9282447 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0DD29600CB for ; Tue, 16 Aug 2016 00:58:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F288628ED3 for ; Tue, 16 Aug 2016 00:58:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E674628EDC; Tue, 16 Aug 2016 00:58:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A38428ED3 for ; Tue, 16 Aug 2016 00:58:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752576AbcHPA6P (ORCPT ); Mon, 15 Aug 2016 20:58:15 -0400 Received: from fmap.me ([91.92.66.84]:33938 "EHLO smtp.fmap.me" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750796AbcHPA6O (ORCPT ); Mon, 15 Aug 2016 20:58:14 -0400 X-Greylist: delayed 452 seconds by postgrey-1.27 at vger.kernel.org; Mon, 15 Aug 2016 20:58:14 EDT From: Nikolay Amiantov DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=fmap.me; s=mail; t=1471308641; bh=PIW8MUpJwNQeVbPdfJcYoHzFlJmfaRoeScV1RVLYaLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=K9GWwyLq2eeTgyJshxPrE+pteZpdanvbbWQq4hDaVaTcQVBMYDIhgKdiYsZcUy3aA eKMjvgZ/ZNf8engKeAaI+ab2ykbnS+zRqg4QMdM46wzZ/9B2wCeMDi5FKMOd0KC5Jn 6GVihMonxdVq4CrtwZqGPKrzoOJKTwfr5AgR8moE= To: linux-modules@vger.kernel.org Cc: Shea Levy , Nikolay Amiantov Subject: [PATCH 2/4] libkmod: allow hardcoding array of dirname prefixes Date: Tue, 16 Aug 2016 03:50:30 +0300 Message-Id: <20160816005032.28881-3-ab@fmap.me> In-Reply-To: <20160816005032.28881-1-ab@fmap.me> References: <20160816005032.28881-1-ab@fmap.me> Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Directories in the array are searched until the first directory with `uname -r` subdirectory is found. As a fallback last item in the array is used unconditionally. --- libkmod/libkmod.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 7b0247f..be9358d 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -206,7 +206,10 @@ static int log_priority(const char *priority) return 0; } -static const char *dirname_default_prefix = "/lib/modules"; +static const char *dirname_default_prefixes[] = { + "/lib/modules", + NULL +}; static char *get_kernel_release(const char *dirname) { @@ -219,11 +222,43 @@ static char *get_kernel_release(const char *dirname) if (uname(&u) < 0) return NULL; - if ((dirname_prefix = getenv("MODULE_DIR")) == NULL) - dirname_prefix = dirname_default_prefix; + if ((dirname_prefix = getenv("MODULE_DIR")) != NULL) { + if(asprintf(&p, "%s/%s", dirname_prefix, u.release) < 0) + return NULL; + } else { + size_t i; + char buf[PATH_MAX]; + + for (i = 0; dirname_default_prefixes[i] != NULL; i++) { + int plen; + + plen = snprintf(buf, sizeof(buf), "%s/%s", dirname_default_prefixes[i], u.release); + if (plen < 0) + return NULL; + else if (plen >= PATH_MAX) + continue; + + if (dirname_default_prefixes[i + 1] != NULL) { + struct stat dirstat; + + if (stat(buf, &dirstat) < 0) { + if (errno == ENOENT) + continue; + else + return NULL; + } + + if (!S_ISDIR(dirstat.st_mode)) + continue; + } - if (asprintf(&p, "%s/%s", dirname_prefix, u.release) < 0) - return NULL; + p = malloc(plen + 1); + if (p == NULL) + return NULL; + memcpy(p, buf, plen + 1); + break; + } + } return p; }