From patchwork Thu Jun 9 02:20:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yousong Zhou X-Patchwork-Id: 632602 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.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 3rQ8Df3tcZz9sds for ; Thu, 9 Jun 2016 12:21:06 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=M3k7Y9lU; dkim-atps=neutral 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 1bApad-0003Co-9T; Thu, 09 Jun 2016 02:20:59 +0000 Received: from mail-pa0-x244.google.com ([2607:f8b0:400e:c03::244]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1bApaa-0003BO-Vx for lede-dev@lists.infradead.org; Thu, 09 Jun 2016 02:20:57 +0000 Received: by mail-pa0-x244.google.com with SMTP id gp3so1625690pac.2 for ; Wed, 08 Jun 2016 19:20:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=LDsjS1gCEWrFVPFef/Mz3YQBhzCwkmghud8cv0yyWzk=; b=M3k7Y9lUVWrk3m7jYrgwpIjBPoSCX5Oeuh9uIbNt56ZFVQ4s/u5eJEUzqEWxe72PhM r/Fvm5Z52L8XiK+il+24M21f9eR1EzE4YRaiOpH7ikdQOsoFZ91KNmhlz0POJOvot0+b 3gBXaVl1THiWYUuOrj/T9z151tf8DDgHte1PPxhkTJi2hueilq65+3fWyEAsHHpCwBcc 3BSve1NDUs62B06AoBtEQDrsVEX91UmDK3kxyC+E9csK1XsUrEvaROMiAhS81uKm6/oQ Tm5nwZrwtN2vKDlOrKG/T5F4Vqi1ahUPYYcFF3nLYe6UWltEu3Uq9zcBnpmNlRn2KGmt NxSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=LDsjS1gCEWrFVPFef/Mz3YQBhzCwkmghud8cv0yyWzk=; b=PwFYExGVmPJwjLkj6bOCb+ZB5bmfpBo4S1sFkxM4QHIZQ5s9UT/EHT2AsL+fn9Rern zHVh1YwhgdYYWVfTYP/pw8+l/yI/8v9m3zTvgziHC5rMiJiyjtGgkRHxvBdjCp7JyoPC o45Pv6Iojt07Grk3TtclyD2UqJ5duMLTvedEVsXj21G8WFLNcQ54ViO/Sc7BevWbyHM2 zFOqJFtvt5uaySJRY3HZLFVMLk1B/43vhWaaC4KPYvAhH91vIrZROdfjw+srSdu8fAzs UhGVeqnhOYij9FYsZ7qWyNy+uBMuLcEvqqusZ7P8cpP5sXSqeuZfYHE3h/+21IgYgdYB zURg== X-Gm-Message-State: ALyK8tJa02HFh7lV0fc+9jexhBt/V4Zszq2049GiHV04flgSJWbR9hDQiNkKAXmrdYtuwQ== X-Received: by 10.66.121.197 with SMTP id lm5mr9280316pab.143.1465438836218; Wed, 08 Jun 2016 19:20:36 -0700 (PDT) Received: from titan.office.mos ([103.29.140.58]) by smtp.gmail.com with ESMTPSA id u78sm5286615pfi.78.2016.06.08.19.20.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 08 Jun 2016 19:20:35 -0700 (PDT) From: Yousong Zhou To: nbd@nbd.name Date: Thu, 9 Jun 2016 10:20:32 +0800 Message-Id: <1465438832-20133-1-git-send-email-yszhou4tech@gmail.com> X-Mailer: git-send-email 2.6.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160608_192057_075181_4EF9BDCB X-CRM114-Status: GOOD ( 13.94 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2607:f8b0:400e:c03:0:0:0:244 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (yszhou4tech[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid Subject: [LEDE-DEV] [PATCH] uloop: use a waker for notifying sigchld and loop cancel events 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: Yousong Zhou , lede-dev@lists.infradead.org MIME-Version: 1.0 Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Fix a race condition when do_sigchld, uloop_cancelled were set just before epoll_wait(timeout=-1), resulting the loop stuck in the syscall without noticing the events just happened Signed-off-by: Yousong Zhou --- uloop-epoll.c | 2 +- uloop-kqueue.c | 2 +- uloop.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/uloop-epoll.c b/uloop-epoll.c index bb652fd..6014bea 100644 --- a/uloop-epoll.c +++ b/uloop-epoll.c @@ -23,7 +23,7 @@ #define EPOLLRDHUP 0x2000 #endif -int uloop_init(void) +static int uloop_init_pollfd(void) { if (poll_fd >= 0) return 0; diff --git a/uloop-kqueue.c b/uloop-kqueue.c index 0cb1c14..ba5595b 100644 --- a/uloop-kqueue.c +++ b/uloop-kqueue.c @@ -15,7 +15,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -int uloop_init(void) +static int uloop_init_pollfd(void) { struct timespec timeout = { 0, 0 }; struct kevent ev = {}; diff --git a/uloop.c b/uloop.c index cd3de85..b59c31f 100644 --- a/uloop.c +++ b/uloop.c @@ -63,6 +63,11 @@ static bool do_sigchld = false; static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS]; static int cur_fd, cur_nfds; +static int waker_pipe[2] = {-1, -1}; +static struct uloop_fd waker_fd; + +int uloop_fd_add(struct uloop_fd *sock, unsigned int flags); + #ifdef USE_KQUEUE #include "uloop-kqueue.c" #endif @@ -71,6 +76,45 @@ static int cur_fd, cur_nfds; #include "uloop-epoll.c" #endif +static void waker_consume(struct uloop_fd *fd, unsigned int events) +{ + char buf[4]; + + while (read(fd->fd, buf, 4) > 0) + ; +} + +static int waker_init(void) +{ + if (waker_pipe[0] >= 0 && waker_pipe[1] >= 0) + return 0; + + if (pipe(waker_pipe) < 0) + return -1; + + fcntl(waker_pipe[0], F_SETFD, fcntl(waker_pipe[0], F_GETFD) | O_NONBLOCK); + fcntl(waker_pipe[1], F_SETFD, fcntl(waker_pipe[1], F_GETFD) | O_NONBLOCK); + + waker_fd.fd = waker_pipe[0]; + waker_fd.cb = waker_consume; + uloop_fd_add(&waker_fd, ULOOP_READ); + + return 0; +} + +int uloop_init(void) +{ + if (uloop_init_pollfd() < 0) + return -1; + + if (waker_init() < 0) { + close(poll_fd); + poll_fd = -1; + return -1; + } + return 0; +} + static bool uloop_fd_stack_event(struct uloop_fd *fd, int events) { struct uloop_fd_stack *cur; @@ -330,12 +374,18 @@ static void uloop_handle_processes(void) static void uloop_handle_sigint(int signo) { + char buf[1] = {'w'}; + uloop_cancelled = true; + write(waker_pipe[1], buf, 1); } static void uloop_sigchld(int signo) { + char buf[1] = {'w'}; + do_sigchld = true; + write(waker_pipe[1], buf, 1); } static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add) @@ -477,11 +527,18 @@ void uloop_run(void) void uloop_done(void) { - if (poll_fd < 0) - return; + int i; - close(poll_fd); - poll_fd = -1; + if (poll_fd >= 0) { + close(poll_fd); + poll_fd = -1; + } + for (i = 0; i < ARRAY_SIZE(waker_pipe); i++) { + if (waker_pipe[i] >= 0) { + close(waker_pipe[i]); + waker_pipe[i] = -1; + } + } uloop_clear_timeouts(); uloop_clear_processes();