From patchwork Tue Aug 16 00:50:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Amiantov X-Patchwork-Id: 9282453 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 724EC6086A for ; Tue, 16 Aug 2016 00:58:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 647B128ED6 for ; Tue, 16 Aug 2016 00:58:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5962F28EDC; Tue, 16 Aug 2016 00:58:18 +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 0C5CE28EDF for ; Tue, 16 Aug 2016 00:58:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752365AbcHPA6Q (ORCPT ); Mon, 15 Aug 2016 20:58:16 -0400 Received: from fmap.me ([91.92.66.84]:33942 "EHLO smtp.fmap.me" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752409AbcHPA6O (ORCPT ); Mon, 15 Aug 2016 20:58:14 -0400 From: Nikolay Amiantov DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=fmap.me; s=mail; t=1471308642; bh=prBMtak1s3GBtfMI2uHVusn4PUHsl68uBedzhKp/iiM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=p1Y78x+kchmF88sIJdOK4oJMqTLX4YoGd4SZvho9UmujcAeQw6F2oOL3+JlVHj+X3 wklzM87SaPNxyILWFFY/jAAgvE79VKc1zGk7KkeE3j+4ZiC5PYEr9XVPobWKDLDM/d lRGCTl5LxtcftuxJ7yngIRfF5ibLQfEzbum3jELw= To: linux-modules@vger.kernel.org Cc: Shea Levy , Nikolay Amiantov Subject: [PATCH 3/4] static-nodes: use kmod to get modules directory Date: Tue, 16 Aug 2016 03:50:31 +0300 Message-Id: <20160816005032.28881-4-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 static-nodes has just used /lib/modules/`uname -r` before with no way to specify another directory. Instead, make it get the path via kmod, which has a more sophisticated algorithm for searching the modules directory. As a side effect, cleanup error messages printing a little. --- tools/static-nodes.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tools/static-nodes.c b/tools/static-nodes.c index 8d2356d..2ed306d 100644 --- a/tools/static-nodes.c +++ b/tools/static-nodes.c @@ -29,10 +29,11 @@ #include #include #include -#include #include +#include + #include "kmod.h" struct static_nodes_format { @@ -154,8 +155,8 @@ static void help(void) static int do_static_nodes(int argc, char *argv[]) { - struct utsname kernel; char modules[PATH_MAX], buf[4096]; + struct kmod_ctx *ctx; const char *output = "/dev/stdout"; FILE *in = NULL, *out = NULL; const struct static_nodes_format *format = &static_nodes_format_human; @@ -206,22 +207,25 @@ static int do_static_nodes(int argc, char *argv[]) } } - if (uname(&kernel) < 0) { - fputs("Error: uname failed!\n", stderr); + ctx = kmod_new(NULL, NULL); + if (ctx == NULL) { + fprintf(stderr, "Error: failed to create kmod context\n"); ret = EXIT_FAILURE; goto finish; } - - snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release); + if (snprintf(modules, sizeof(modules), "%s/modules.devname", kmod_get_dirname(ctx)) < 0) { + fprintf(stderr, "Error: path to modules.devname is too long\n"); + ret = EXIT_FAILURE; + goto finish; + } + kmod_unref(ctx); in = fopen(modules, "re"); if (in == NULL) { if (errno == ENOENT) { - fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n", - kernel.release); + fprintf(stderr, "Warning: %s not found - ignoring\n", modules); ret = EXIT_SUCCESS; } else { - fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n", - kernel.release); + fprintf(stderr, "Error: could not open %s - %m\n", modules); ret = EXIT_FAILURE; } goto finish;