From patchwork Sat Aug 12 12:04:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Sutter X-Patchwork-Id: 800892 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xV0z638w1z9t3C for ; Sat, 12 Aug 2017 22:09:06 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752641AbdHLMJE (ORCPT ); Sat, 12 Aug 2017 08:09:04 -0400 Received: from orbyte.nwl.cc ([151.80.46.58]:34277 "EHLO mail.nwl.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752018AbdHLMJC (ORCPT ); Sat, 12 Aug 2017 08:09:02 -0400 Received: from mail.nwl.cc (orbyte.nwl.cc [127.0.0.1]) by mail.nwl.cc (Postfix) with ESMTP id D65EF68252; Sat, 12 Aug 2017 14:09:01 +0200 (CEST) Received: from xsao (localhost [IPv6:::1]) by mail.nwl.cc (Postfix) with ESMTP id B93E9681D7; Sat, 12 Aug 2017 14:09:01 +0200 (CEST) From: Phil Sutter To: Stephen Hemminger Cc: netdev@vger.kernel.org Subject: [iproute PATCH 19/51] lib/fs: Fix and simplify make_path() Date: Sat, 12 Aug 2017 14:04:38 +0200 Message-Id: <20170812120510.28750-20-phil@nwl.cc> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20170812120510.28750-1-phil@nwl.cc> References: <20170812120510.28750-1-phil@nwl.cc> X-Virus-Scanned: ClamAV using ClamSMTP Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Calling stat() before mkdir() is racey: The entry might change in between. Also, the call to stat() seems to exist only to check if the directory exists already. So simply call mkdir() unconditionally and catch only errors other than EEXIST. Signed-off-by: Phil Sutter --- lib/fs.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/lib/fs.c b/lib/fs.c index 1ff881ecfcd8c..ebe05cd44e11b 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -102,7 +102,6 @@ out: int make_path(const char *path, mode_t mode) { char *dir, *delim; - struct stat sbuf; int rc = -1; delim = dir = strdup(path); @@ -120,20 +119,11 @@ int make_path(const char *path, mode_t mode) if (delim) *delim = '\0'; - if (stat(dir, &sbuf) != 0) { - if (errno != ENOENT) { - fprintf(stderr, - "stat failed for %s: %s\n", - dir, strerror(errno)); - goto out; - } - - if (mkdir(dir, mode) != 0) { - fprintf(stderr, - "mkdir failed for %s: %s\n", - dir, strerror(errno)); - goto out; - } + rc = mkdir(dir, mode); + if (mkdir(dir, mode) != 0 && errno != EEXIST) { + fprintf(stderr, "mkdir failed for %s: %s\n", + dir, strerror(errno)); + goto out; } if (delim == NULL)