From patchwork Mon Mar 16 12:36:41 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 6018091 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 8F65F9F399 for ; Mon, 16 Mar 2015 12:44:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 919B0204E0 for ; Mon, 16 Mar 2015 12:44:17 +0000 (UTC) Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com [209.132.183.25]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 11B49204D5 for ; Mon, 16 Mar 2015 12:44:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id t2GCcYYE021200; Mon, 16 Mar 2015 08:38:34 -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 t2GCbKKa028960 for ; Mon, 16 Mar 2015 08:37:20 -0400 Received: from mx1.redhat.com (ext-mx16.extmail.prod.ext.phx2.redhat.com [10.5.110.21]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t2GCbKMt004276; Mon, 16 Mar 2015 08:37:20 -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 t2GCbH2R003109 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=FAIL); Mon, 16 Mar 2015 08:37:18 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 690B8ADEB; Mon, 16 Mar 2015 12:37:10 +0000 (UTC) From: Hannes Reinecke To: Christophe Varoqui Date: Mon, 16 Mar 2015 13:36:41 +0100 Message-Id: <1426509425-15978-55-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.21 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com Subject: [dm-devel] [PATCH 54/78] multipathd: Use standard lists for CLI handling 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=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 We already have standard list handling macros in list.h, so we should be using them for CLI, too. Signed-off-by: Hannes Reinecke --- libmultipath/list.h | 2 ++ libmultipath/uxsock.c | 2 +- multipathd/uxlsnr.c | 40 ++++++++++++++++++++++++++-------------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/libmultipath/list.h b/libmultipath/list.h index 7a3cf16..a0d8184 100644 --- a/libmultipath/list.h +++ b/libmultipath/list.h @@ -8,6 +8,8 @@ #ifndef _LIST_H #define _LIST_H +#include + /** * container_of - cast a member of a structure out to the containing structure * diff --git a/libmultipath/uxsock.c b/libmultipath/uxsock.c index af0798c..300d657 100644 --- a/libmultipath/uxsock.c +++ b/libmultipath/uxsock.c @@ -210,7 +210,7 @@ int recv_packet(int fd, char **buf, size_t *len, unsigned int timeout) } (*buf) = MALLOC(*len); if (!*buf) - return -1; + return -ENOMEM; ret = read_all(fd, *buf, *len, timeout); if (ret != *len) { FREE(*buf); diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c index f20982f..7b13190 100644 --- a/multipathd/uxlsnr.c +++ b/multipathd/uxlsnr.c @@ -39,11 +39,11 @@ struct timespec sleep_time = {5, 0}; struct client { + struct list_head node; int fd; - struct client *next, *prev; }; -static struct client *clients; +LIST_HEAD(clients); static unsigned num_clients; struct pollfd *polls; volatile sig_atomic_t reconfig_sig = 0; @@ -67,10 +67,10 @@ static void new_client(int ux_sock) /* put it in our linked list */ c = (struct client *)MALLOC(sizeof(*c)); memset(c, 0, sizeof(*c)); + INIT_LIST_HEAD(&c->node); c->fd = fd; - c->next = clients; - if (c->next) c->next->prev = c; - clients = c; + + list_add_tail(&c->node, &clients); num_clients++; } @@ -80,9 +80,8 @@ static void new_client(int ux_sock) static void dead_client(struct client *c) { close(c->fd); - if (c->prev) c->prev->next = c->next; - if (c->next) c->next->prev = c->prev; - if (c == clients) clients = c->next; + c->fd = -1; + list_del_init(&c->node); FREE(c); num_clients--; } @@ -151,7 +150,7 @@ void * uxsock_listen(int (*uxsock_trigger)(char *, char **, int *, void *), sigdelset(&mask, SIGHUP); sigdelset(&mask, SIGUSR1); while (1) { - struct client *c; + struct client *c, *tmp; int i, poll_count; /* @@ -168,9 +167,13 @@ void * uxsock_listen(int (*uxsock_trigger)(char *, char **, int *, void *), polls[0].events = POLLIN; /* setup the clients */ - for (i=1, c = clients; c; i++, c = c->next) { + i = 1; + list_for_each_entry(c, &clients, node) { polls[i].fd = c->fd; polls[i].events = POLLIN; + if (i > num_clients) + break; + i++; } /* most of our life is spent in this call */ @@ -191,12 +194,22 @@ void * uxsock_listen(int (*uxsock_trigger)(char *, char **, int *, void *), continue; /* see if a client wants to speak to us */ - for (i=1, c = clients; c; i++) { - struct client *next = c->next; - + for (i = 1; i < num_clients + 1; i++) { if (polls[i].revents & POLLIN) { struct timeval start_time; + c = NULL; + list_for_each_entry(tmp, &clients, node) { + if (tmp->fd == polls[i].fd) { + c = tmp; + break; + } + } + if (!c) { + condlog(3, "cli%d: invalid fd %d", + i, polls[i].fd); + continue; + } if (gettimeofday(&start_time, NULL) != 0) start_time.tv_sec = 0; @@ -223,7 +236,6 @@ void * uxsock_listen(int (*uxsock_trigger)(char *, char **, int *, void *), FREE(inbuf); } } - c = next; } /* see if we got a new client */