From patchwork Tue May 17 15:42:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jo-Philipp Wich X-Patchwork-Id: 623179 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r8M8p07f9z9t0t for ; Wed, 18 May 2016 01:44:58 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b2h9R-0006w7-00; Tue, 17 May 2016 15:43:17 +0000 Received: from mxout01.bytecamp.net ([212.204.60.217]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b2h9O-0006li-Pa for lede-dev@lists.infradead.org; Tue, 17 May 2016 15:43:15 +0000 Received: by mxout01.bytecamp.net (Postfix, from userid 1001) id 216E130FE41; Tue, 17 May 2016 17:42:30 +0200 (CEST) Received: from mail.bytecamp.net (mailstore.bytecamp.net [212.204.60.20]) by mxout01.bytecamp.net (Postfix) with ESMTP id CC36E30FD55 for ; Tue, 17 May 2016 17:42:29 +0200 (CEST) Received: (qmail 24872 invoked by uid 89); 17 May 2016 17:42:29 +0200 Received: from unknown (HELO localhost.localdomain) (jo%wwsnet.net@213.61.250.148) by mail.bytecamp.net with AES128-SHA encrypted SMTP; 17 May 2016 17:42:29 +0200 From: Jo-Philipp Wich To: lede-dev@lists.infradead.org Date: Tue, 17 May 2016 17:42:10 +0200 Message-Id: <8fd3a1a125a2321e0fe7a10b41f137e489d66378.1463499516.git.jo@mein.io> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160517_084315_025168_401A4015 X-CRM114-Status: GOOD ( 10.52 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [212.204.60.217 listed in list.dnswl.org] -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Subject: [LEDE-DEV] [PATCH 1/2] inittab: use more robust dev_exist() implementation X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jo-Philipp Wich , john@phrozen.org MIME-Version: 1.0 Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Rework the dev_exist() function to use openat() in order to resolve the device file relative to the "/dev" directory. Drop the now unused dev_open() function. Signed-off-by: Jo-Philipp Wich --- inittab.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/inittab.c b/inittab.c index 622601a..528396e 100644 --- a/inittab.c +++ b/inittab.c @@ -65,30 +65,23 @@ static char *ask = "/sbin/askfirst"; static LIST_HEAD(actions); -static int dev_open(const char *dev) +static int dev_exist(const char *dev) { - int fd = -1; - - if (dev) { - if (chdir("/dev")) - ERROR("failed to change dir to /dev\n"); - fd = open(dev, O_RDWR); - if (chdir("/")) - ERROR("failed to change dir to /\n"); - } + int dfd, fd; - return fd; -} + dfd = open("/dev", O_PATH|O_DIRECTORY); -static int dev_exist(const char *dev) -{ - int res; + if (dfd < 0) + return 0; + + fd = openat(dfd, dev, O_RDONLY); + close(dfd); - res = dev_open(dev); - if (res != -1) - close(res); + if (fd < 0) + return 0; - return (res != -1); + close(fd); + return 1; } static void fork_worker(struct init_action *a)