From patchwork Fri Sep 8 18:45:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Marzinski X-Patchwork-Id: 9944885 X-Patchwork-Delegate: christophe.varoqui@free.fr 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 BEEB160224 for ; Fri, 8 Sep 2017 18:46:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BBB10288A1 for ; Fri, 8 Sep 2017 18:46:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B095A288A6; Fri, 8 Sep 2017 18:46:14 +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.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 40172288A1 for ; Fri, 8 Sep 2017 18:46:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3A4167E421; Fri, 8 Sep 2017 18:46:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3A4167E421 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=dm-devel-bounces@redhat.com Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1A7B05EDE4; Fri, 8 Sep 2017 18:46:10 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id ECBB61806106; Fri, 8 Sep 2017 18:46:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v88Ik1EW017952 for ; Fri, 8 Sep 2017 14:46:01 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6454953C27; Fri, 8 Sep 2017 18:46:01 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from redhat.com (octiron.msp.redhat.com [10.15.80.209]) by smtp.corp.redhat.com (Postfix) with SMTP id 219B260C14; Fri, 8 Sep 2017 18:45:57 +0000 (UTC) Received: by redhat.com (sSMTP sendmail emulation); Fri, 08 Sep 2017 13:45:57 -0500 From: "Benjamin Marzinski" To: device-mapper development Date: Fri, 8 Sep 2017 13:45:50 -0500 Message-Id: <1504896354-28181-2-git-send-email-bmarzins@redhat.com> In-Reply-To: <1504896354-28181-1-git-send-email-bmarzins@redhat.com> References: <1504896354-28181-1-git-send-email-bmarzins@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: dm-devel@redhat.com Cc: Martin Wilck Subject: [dm-devel] [PATCH 1/5] libmultipath: pull functions into util.c X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 08 Sep 2017 18:46:11 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP This patch just pulls safe_write out of rbd. and the persistent reservation key parsing code out of dict.c and puts them in util.c, so that other functions can make use of them. Signed-off-by: Benjamin Marzinski Reviewed-by: Martin Wilck --- libmultipath/checkers/rbd.c | 16 +--------------- libmultipath/dict.c | 26 +++++--------------------- libmultipath/util.c | 33 +++++++++++++++++++++++++++++++++ libmultipath/util.h | 4 ++++ 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/libmultipath/checkers/rbd.c b/libmultipath/checkers/rbd.c index 9ea0572..2c18009 100644 --- a/libmultipath/checkers/rbd.c +++ b/libmultipath/checkers/rbd.c @@ -28,6 +28,7 @@ #include "../libmultipath/debug.h" #include "../libmultipath/util.h" #include "../libmultipath/time-util.h" +#include "../libmultipath/util.h" struct rbd_checker_context; typedef int (thread_fn)(struct rbd_checker_context *ct, char *msg); @@ -356,21 +357,6 @@ static int rbd_check(struct rbd_checker_context *ct, char *msg) return PATH_UP; } -static int safe_write(int fd, const void *buf, size_t count) -{ - while (count > 0) { - ssize_t r = write(fd, buf, count); - if (r < 0) { - if (errno == EINTR) - continue; - return -errno; - } - count -= r; - buf = (char *)buf + r; - } - return 0; -} - static int sysfs_write_rbd_bus(const char *which, const char *buf, size_t buf_len) { diff --git a/libmultipath/dict.c b/libmultipath/dict.c index 9dc1090..680b2c5 100644 --- a/libmultipath/dict.c +++ b/libmultipath/dict.c @@ -19,6 +19,7 @@ #include "blacklist.h" #include "defaults.h" #include "prio.h" +#include "util.h" #include #include #include "mpath_cmd.h" @@ -963,32 +964,15 @@ set_reservation_key(vector strvec, void *ptr) { unsigned char **uchar_ptr = (unsigned char **)ptr; char *buff; - char *tbuff; - int j, k; - int len; + int j; uint64_t prkey; buff = set_value(strvec); if (!buff) return 1; - tbuff = buff; - - if (!memcmp("0x",buff, 2)) - buff = buff + 2; - - len = strlen(buff); - - k = strspn(buff, "0123456789aAbBcCdDeEfF"); - - if (len != k) { - FREE(tbuff); - return 1; - } - - if (1 != sscanf (buff, "%" SCNx64 "", &prkey)) - { - FREE(tbuff); + if (parse_prkey(buff, &prkey) != 0) { + FREE(buff); return 1; } @@ -1002,7 +986,7 @@ set_reservation_key(vector strvec, void *ptr) prkey >>= 8; } - FREE(tbuff); + FREE(buff); return 0; } diff --git a/libmultipath/util.c b/libmultipath/util.c index dff2ed3..0800da5 100644 --- a/libmultipath/util.c +++ b/libmultipath/util.c @@ -11,6 +11,7 @@ #include #include +#include "util.h" #include "debug.h" #include "memory.h" #include "checkers.h" @@ -416,3 +417,35 @@ int get_linux_version_code(void) pthread_once(&_lvc_initialized, _set_linux_version_code); return _linux_version_code; } + +int parse_prkey(char *ptr, uint64_t *prkey) +{ + if (!ptr) + return 1; + if (*ptr == '0') + ptr++; + if (*ptr == 'x' || *ptr == 'X') + ptr++; + if (*ptr == '\0' || strlen(ptr) > 16) + return 1; + if (strlen(ptr) != strspn(ptr, "0123456789aAbBcCdDeEfF")) + return 1; + if (sscanf(ptr, "%" SCNx64 "", prkey) != 1) + return 1; + return 0; +} + +int safe_write(int fd, const void *buf, size_t count) +{ + while (count > 0) { + ssize_t r = write(fd, buf, count); + if (r < 0) { + if (errno == EINTR) + continue; + return -errno; + } + count -= r; + buf = (char *)buf + r; + } + return 0; +} diff --git a/libmultipath/util.h b/libmultipath/util.h index 45291be..3dc048e 100644 --- a/libmultipath/util.h +++ b/libmultipath/util.h @@ -2,6 +2,7 @@ #define _UTIL_H #include +#include size_t strchop(char *); int basenamecpy (const char * src, char * dst, int); @@ -16,6 +17,9 @@ char *parse_uid_attribute_by_attrs(char *uid_attrs, char *path_dev); void setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached); int systemd_service_enabled(const char *dev); int get_linux_version_code(void); +int parse_prkey(char *ptr, uint64_t *prkey); +int safe_write(int fd, const void *buf, size_t count); + #define KERNEL_VERSION(maj, min, ptc) ((((maj) * 256) + (min)) * 256 + (ptc)) #define safe_sprintf(var, format, args...) \