From patchwork Mon Mar 16 12:36:22 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 6018131 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: X-Original-To: patchwork-dm-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 17F7E9FB24 for ; Mon, 16 Mar 2015 12:44:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0DBDA204E0 for ; Mon, 16 Mar 2015 12:44:19 +0000 (UTC) Received: from mx6-phx2.redhat.com (mx6-phx2.redhat.com [209.132.183.39]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B31E9204E1 for ; Mon, 16 Mar 2015 12:44:17 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx6-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t2GCcKPF005321; Mon, 16 Mar 2015 08:38:20 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id t2GCbHQH028826 for ; Mon, 16 Mar 2015 08:37:17 -0400 Received: from mx1.redhat.com (ext-mx14.extmail.prod.ext.phx2.redhat.com [10.5.110.19]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t2GCbH97004240; Mon, 16 Mar 2015 08:37:17 -0400 Received: from mx2.suse.de (cantor2.suse.de [195.135.220.15]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t2GCbEJZ031029 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=FAIL); Mon, 16 Mar 2015 08:37:15 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 4E82DADDB; Mon, 16 Mar 2015 12:37:09 +0000 (UTC) From: Hannes Reinecke To: Christophe Varoqui Date: Mon, 16 Mar 2015 13:36:22 +0100 Message-Id: <1426509425-15978-36-git-send-email-hare@suse.de> In-Reply-To: <1426509425-15978-1-git-send-email-hare@suse.de> References: <1426509425-15978-1-git-send-email-hare@suse.de> X-RedHat-Spam-Score: -7.309 (BAYES_00, DCC_REPUT_00_12, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, URIBL_BLOCKED) 195.135.220.15 cantor2.suse.de 195.135.220.15 cantor2.suse.de X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Scanned-By: MIMEDefang 2.68 on 10.5.110.19 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com Subject: [dm-devel] [PATCH 35/78] Separate out uevent parsing functions X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk Reply-To: device-mapper development 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-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Move the uevent parsing into separate functions. No functional change. Signed-off-by: Hannes Reinecke --- libmultipath/uevent.c | 254 +++++++++++++++++++++++++++----------------------- 1 file changed, 136 insertions(+), 118 deletions(-) diff --git a/libmultipath/uevent.c b/libmultipath/uevent.c index 9ee3ade..7e41c6f 100644 --- a/libmultipath/uevent.c +++ b/libmultipath/uevent.c @@ -182,6 +182,80 @@ int uevent_dispatch(int (*uev_trigger)(struct uevent *, void * trigger_data), return 0; } +struct uevent *uevent_from_buffer(char *buf, ssize_t buflen) +{ + struct uevent *uev; + char *buffer; + size_t bufpos; + int i; + char *pos; + + uev = alloc_uevent(); + if (!uev) { + condlog(1, "lost uevent, oom"); + return NULL; + } + + if ((size_t)buflen > sizeof(buf)-1) + buflen = sizeof(buf)-1; + + /* + * Copy the shared receive buffer contents to buffer private + * to this uevent so we can immediately reuse the shared buffer. + */ + memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE); + buffer = uev->buffer; + buffer[buflen] = '\0'; + + /* save start of payload */ + bufpos = strlen(buffer) + 1; + + /* action string */ + uev->action = buffer; + pos = strchr(buffer, '@'); + if (!pos) { + condlog(3, "bad action string '%s'", buffer); + FREE(uev); + return NULL; + } + pos[0] = '\0'; + + /* sysfs path */ + uev->devpath = &pos[1]; + + /* hotplug events have the environment attached - reconstruct envp[] */ + for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) { + int keylen; + char *key; + + key = &buffer[bufpos]; + keylen = strlen(key); + uev->envp[i] = key; + /* Filter out sequence number */ + if (strncmp(key, "SEQNUM=", 7) == 0) { + char *eptr; + + uev->seqnum = strtoul(key + 7, &eptr, 10); + if (eptr == key + 7) + uev->seqnum = -1; + } + bufpos += keylen + 1; + } + uev->envp[i] = NULL; + + condlog(3, "uevent %ld '%s' from '%s'", uev->seqnum, + uev->action, uev->devpath); + uev->kernel = strrchr(uev->devpath, '/'); + if (uev->kernel) + uev->kernel++; + + /* print payload environment */ + for (i = 0; uev->envp[i] != NULL; i++) + condlog(5, "%s", uev->envp[i]); + + return uev; +} + int failback_listen(void) { int sock; @@ -266,12 +340,9 @@ int failback_listen(void) } while (1) { - int i; - char *pos; size_t bufpos; ssize_t buflen; struct uevent *uev; - char *buffer; struct msghdr smsg; struct iovec iov; char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; @@ -324,69 +395,9 @@ int failback_listen(void) buflen = sizeof(buf)-1; } - uev = alloc_uevent(); - - if (!uev) { - condlog(1, "lost uevent, oom"); - continue; - } - - if ((size_t)buflen > sizeof(buf)-1) - buflen = sizeof(buf)-1; - - /* - * Copy the shared receive buffer contents to buffer private - * to this uevent so we can immediately reuse the shared buffer. - */ - memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE); - buffer = uev->buffer; - buffer[buflen] = '\0'; - - /* save start of payload */ - bufpos = strlen(buffer) + 1; - - /* action string */ - uev->action = buffer; - pos = strchr(buffer, '@'); - if (!pos) { - condlog(3, "bad action string '%s'", buffer); + uev = uevent_from_buffer(buf, buflen); + if (!uev) continue; - } - pos[0] = '\0'; - - /* sysfs path */ - uev->devpath = &pos[1]; - - /* hotplug events have the environment attached - reconstruct envp[] */ - for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) { - int keylen; - char *key; - - key = &buffer[bufpos]; - keylen = strlen(key); - uev->envp[i] = key; - /* Filter out sequence number */ - if (strncmp(key, "SEQNUM=", 7) == 0) { - char *eptr; - - uev->seqnum = strtoul(key + 7, &eptr, 10); - if (eptr == key + 7) - uev->seqnum = -1; - } - bufpos += keylen + 1; - } - uev->envp[i] = NULL; - - condlog(3, "uevent %ld '%s' from '%s'", uev->seqnum, - uev->action, uev->devpath); - uev->kernel = strrchr(uev->devpath, '/'); - if (uev->kernel) - uev->kernel++; - - /* print payload environment */ - for (i = 0; uev->envp[i] != NULL; i++) - condlog(5, "%s", uev->envp[i]); - /* * Queue uevent and poke service pthread. */ @@ -401,6 +412,62 @@ exit: return 1; } +struct uevent *uevent_from_udev_device(struct udev_device *dev) +{ + struct uevent *uev; + int i = 0; + char *pos, *end; + struct udev_list_entry *list_entry; + + uev = alloc_uevent(); + if (!uev) { + udev_device_unref(dev); + condlog(1, "lost uevent, oom"); + return NULL; + } + pos = uev->buffer; + end = pos + HOTPLUG_BUFFER_SIZE + OBJECT_SIZE - 1; + udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev)) { + const char *name, *value; + int bytes; + + name = udev_list_entry_get_name(list_entry); + if (!name) + name = "(null)"; + value = udev_list_entry_get_value(list_entry); + if (!value) + value = "(null)"; + bytes = snprintf(pos, end - pos, "%s=%s", name, value); + if (pos + bytes >= end) { + condlog(2, "buffer overflow for uevent"); + break; + } + uev->envp[i] = pos; + pos += bytes; + *pos = '\0'; + pos++; + if (strcmp(name, "DEVPATH") == 0) + uev->devpath = uev->envp[i] + 8; + if (strcmp(name, "ACTION") == 0) + uev->action = uev->envp[i] + 7; + i++; + if (i == HOTPLUG_NUM_ENVP - 1) + break; + } + uev->udev = dev; + uev->envp[i] = NULL; + + condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath); + uev->kernel = strrchr(uev->devpath, '/'); + if (uev->kernel) + uev->kernel++; + + /* print payload environment */ + for (i = 0; uev->envp[i] != NULL; i++) + condlog(5, "%s", uev->envp[i]); + return uev; +} + int uevent_listen(struct udev *udev) { int err; @@ -456,69 +523,20 @@ int uevent_listen(struct udev *udev) goto out; } while (1) { - int i = 0; - char *pos, *end; struct uevent *uev; struct udev_device *dev; - struct udev_list_entry *list_entry; dev = udev_monitor_receive_device(monitor); if (!dev) { condlog(0, "failed getting udev device"); continue; } - - uev = alloc_uevent(); - if (!uev) { - udev_device_unref(dev); - condlog(1, "lost uevent, oom"); + uev = uevent_from_udev_device(dev); + if (!uev) continue; - } - pos = uev->buffer; - end = pos + HOTPLUG_BUFFER_SIZE + OBJECT_SIZE - 1; - udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev)) { - const char *name, *value; - int bytes; - - name = udev_list_entry_get_name(list_entry); - if (!name) - name = "(null)"; - value = udev_list_entry_get_value(list_entry); - if (!value) - value = "(null)"; - bytes = snprintf(pos, end - pos, "%s=%s", name, - value); - if (pos + bytes >= end) { - condlog(2, "buffer overflow for uevent"); - break; - } - uev->envp[i] = pos; - pos += bytes; - *pos = '\0'; - pos++; - if (strcmp(name, "DEVPATH") == 0) - uev->devpath = uev->envp[i] + 8; - if (strcmp(name, "ACTION") == 0) - uev->action = uev->envp[i] + 7; - i++; - if (i == HOTPLUG_NUM_ENVP - 1) - break; - } - uev->udev = dev; - uev->envp[i] = NULL; - - condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath); - uev->kernel = strrchr(uev->devpath, '/'); - if (uev->kernel) - uev->kernel++; - - /* print payload environment */ - for (i = 0; uev->envp[i] != NULL; i++) - condlog(5, "%s", uev->envp[i]); - /* - * Queue uevent and poke service pthread. - */ + * Queue uevent and poke service pthread. + */ pthread_mutex_lock(uevq_lockp); list_add_tail(&uev->node, &uevq); pthread_cond_signal(uev_condp);