From patchwork Wed Dec 18 08:15:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Storm, Christian" X-Patchwork-Id: 1212049 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::540; helo=mail-ed1-x540.google.com; envelope-from=swupdate+bncbdd6bwv65qpbbg6a47xqkgqeywjgucy@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=siemens.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b="ctFQyNlP"; dkim-atps=neutral Received: from mail-ed1-x540.google.com (mail-ed1-x540.google.com [IPv6:2a00:1450:4864:20::540]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47d79Z32dpz9sS6 for ; Wed, 18 Dec 2019 19:15:28 +1100 (AEDT) Received: by mail-ed1-x540.google.com with SMTP id w9sf857880eds.4 for ; Wed, 18 Dec 2019 00:15:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1576656923; cv=pass; d=google.com; s=arc-20160816; b=vbC5KCUZ/MB3HrWIvx1d1I9vzRLsyDew2TW0migguHhXjphnuuJwQtyU4RWl+g6RcX ud+iNWf+nlCfp9tE4qyJtZJebkIVVun4gclF+dRoVxnpfJiMOhWZsfCTrntu1Flvm8MF 4hvHkJE/OmSk36yxkkLgAC/dtCTAJzf/1YwHpbQBk6WWe2IrQlyXJyRf858s28YVkFvh 8UrqGqRHl9KQuOSOiVIbO6/PQPpTmv3R0RGTQPgzxxeJmd0Ft7pnvxi/rF7MP0HVjBiz ziTMv7tQuRGDE65KVmXj4E/yCbakzd8OLATvkVAPasxUxbKl5rHNtRH82jRSxODKP2+F 5CDg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=FzQ5B8ewaSJg4tbk7OG+TTpR+bWd28i2SD7C+HHGbKg=; b=XjOf7ZfRen9hQPjTWlHgFmdIyGFazX1FVdrowTjL/1irl/ViN9iIvSY/it120EzPUK 2J3pKjrbppW5xOsTTNK01na4xN4u1OWd0tWYW9ZB3z3hy/hJ91zxsMU+B1LKOKJuwA8l WGPwc68ND6vZ3tuoSTRpl7lFdzAJ+RNykmRZxbBcAui+22n138RpdFEvz+8jcJ8cg3hw KtBLkf9dF8j77biU6QAbg5WTQZv9wshCsP/2dRyovrh9GSdEyPXyWktL0fxioSWhQzaY qvRT1p5BM4Nrpb76QqzJ8RCzvqUMmnc+jrQRoapBBMgtv55aTePAv9gGTLzz8BJxomr4 ShVg== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) smtp.mailfrom=christian.storm@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=FzQ5B8ewaSJg4tbk7OG+TTpR+bWd28i2SD7C+HHGbKg=; b=ctFQyNlPztYikyO1P2f7idyV42sPGB1DE4XVJHlljCsvkoZ+9mM7l3aoMVgetXDJU7 ENuny83pxjnCfmQMimXhlz7MXxbvSDrWxTP5zrgJjYA74B95gmXWJLsKHNjydE8tQdMi JVt/GtUBLwmihuwMfsBxwQLiDA+1A7Ot6xP3BwDjYVuJVaWMLqO2jjLFgJHUdIVtN5es gyumqSoFehNZ7EWLG9l/vyHJJsiXyoRlHWGDoUzOj214MZqaI6589sjiIyudMzcpECXX MNg8CJaAmPAgN7G+kM4pbj7mx1Y3ItxoB4059ifo07R697m4vipatPEn1a+6+porldRT GoTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=FzQ5B8ewaSJg4tbk7OG+TTpR+bWd28i2SD7C+HHGbKg=; b=DiIhPoajc0dAAuR49Imx0/EDyHsP9V/R7qtKjHD+vb0tVTIrZq7ptFMR7WTndIktfs iQsrpIaVUQLkkyOpE3aIWbUpBiXAEYSU33t22Cog482uqT1+c0j0ZHQ0eDkcvQW0XW2Y hgm6JzvdNKH3NwgGICpD5WP4HltcYKlfIlOTLfiSpfYDPp1mWfr0VWiQzPQ7natBTnvB vIzGfGlOtMh3jl1q5M3Cs/rHwW7MF/379j9Rchs43UGY9YFaWDVJZ3JMDzSxEc2BfKf1 W6mveiQA8gcPFfefDcP/d7+dKmiPiRBc6Uzz119c6Kx+1mdfVcrWJf8mz/7I8Oaxl93Z UHYg== Sender: swupdate@googlegroups.com X-Gm-Message-State: APjAAAXvUIr7pKjUSiSFKozyW2G4n8NDq9InPkb9JyiaPTnKItVcfQTc IeHbhwsYhqhjBrjeNywgnbY= X-Google-Smtp-Source: APXvYqzCkH2S0JKpUQFmqnMzQaKW0CyEcYhp3oROx1JILOSPuzH7nwe5xwOj//VqKpicTYJ388MnCA== X-Received: by 2002:a50:8522:: with SMTP id 31mr812466edr.237.1576656923324; Wed, 18 Dec 2019 00:15:23 -0800 (PST) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a17:906:c82b:: with SMTP id dd11ls364848ejb.8.gmail; Wed, 18 Dec 2019 00:15:22 -0800 (PST) X-Received: by 2002:a17:906:4bd7:: with SMTP id x23mr1146011ejv.245.1576656922770; Wed, 18 Dec 2019 00:15:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576656922; cv=none; d=google.com; s=arc-20160816; b=G3n97v03ciarKGzEXdduG7kwdoGJk5qIPXcAabtUwBzJxs8bookFG0ADHmyVY2Y77F 5xoJbvVsJlxjT+QuDwsBfFInwcWIJNUFg/XKlP8NUCZrt7+7/4LQuX66cVhqzDnJ6QO0 NdNbwIn3d2wK11XOwOeE40ikvOrf2q/JzmPB/WzcGRKIf4De0OSpMlDbBIfpaV7BmQ9p qZuYIdXUz7PDyosPOLQyEZWBxlZGNpuALBU4m0di1ZA1yxXIDKmGQ6azdrmtY2UCEB0i DhYu8AHpT5GW8AV+bzvAz1wfNMjbp1iYKkqdSvFiwXpJ5GharyXGYaB4pgYx2niz4aeB R0RQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=RQbv2hIIS+r71Qa37O2vwCb2fmj7mvwmkPdnhdx569c=; b=bRoGTr+Aedl5XD5obHkGVR96z7uUn3UkYjf9UPzP9dGGf16WvXmx08f5wq4LS1ShHS OmZsRvoTORfP6xe9ZiKy+u5wesk8/k8tw+67fTN9NXAPWnlGMv3u/wRmM/BJ9IMWDytQ vG+TVcYFb8uE/60ZW9w6EOD+DXVEWKALUFKBZD78mJv+ZlGmeGjZk37JONYwj0ByH2Ix DEWYBcvneRdGFGQUC7LqjiLo94cGWuD4BesuqJC6AYa7i/n2kpaDNMHII82eYWK+v1P6 9mqU4F7vx3g5apFnP5/DGjphSCPxI92vyEu0Z0LxCN/FDX//eN21eD50bZrp6fKPBygz P8uw== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) smtp.mailfrom=christian.storm@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Received: from thoth.sbs.de (thoth.sbs.de. [192.35.17.2]) by gmr-mx.google.com with ESMTPS id w19si42191edr.1.2019.12.18.00.15.22 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Dec 2019 00:15:22 -0800 (PST) Received-SPF: pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) client-ip=192.35.17.2; Received: from mail2.siemens.de (mail2.siemens.de [139.25.208.11]) by thoth.sbs.de (8.15.2/8.15.2) with ESMTPS id xBI8FMBk032452 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 18 Dec 2019 09:15:22 +0100 Received: from MD1ZFJVC.ad001.siemens.net ([139.25.69.207]) by mail2.siemens.de (8.15.2/8.15.2) with ESMTP id xBI8FM0u003350; Wed, 18 Dec 2019 09:15:22 +0100 From: Christian Storm To: swupdate@googlegroups.com Cc: Christian Storm Subject: [swupdate] [PATCH 1/3 v2] IPC: factor out pthread-based methods Date: Wed, 18 Dec 2019 09:15:46 +0100 Message-Id: <20191218081546.2856-1-christian.storm@siemens.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <6dfc2d6c-dc4f-4245-7e57-5aa5364d88f0@denx.de> References: <6dfc2d6c-dc4f-4245-7e57-5aa5364d88f0@denx.de> MIME-Version: 1.0 X-Original-Sender: christian.storm@siemens.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) smtp.mailfrom=christian.storm@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Factor out the pthread-based IPC interface methods described in swupdate-ipc.rst, Section "Client Library", into a separate compilation unit ipc/network_ipc-if.c, leaving the "plumbing" methods in ipc/network_ipc.c. This allows, e.g., the Lua shared library binding to use the "plumbing" methods while not pulling in the pthread dependency. Signed-off-by: Christian Storm --- ipc/Makefile | 2 +- ipc/network_ipc-if.c | 147 +++++++++++++++++++++++++++++++++++++++++++ ipc/network_ipc.c | 147 ------------------------------------------- 3 files changed, 148 insertions(+), 148 deletions(-) create mode 100644 ipc/network_ipc-if.c diff --git a/ipc/Makefile b/ipc/Makefile index e2826bd..5c12d3d 100644 --- a/ipc/Makefile +++ b/ipc/Makefile @@ -1,6 +1,6 @@ # Copyright (C) 2014-2018 Stefano Babic # # SPDX-License-Identifier: GPL-2.0-or-later -lib-y += network_ipc.o progress_ipc.o +lib-y += network_ipc.o network_ipc-if.o progress_ipc.o EXTRA_CFLAGS += -fPIC diff --git a/ipc/network_ipc-if.c b/ipc/network_ipc-if.c new file mode 100644 index 0000000..cd89deb --- /dev/null +++ b/ipc/network_ipc-if.c @@ -0,0 +1,147 @@ +/* + * (C) Copyright 2008-2017 + * Stefano Babic, DENX Software Engineering, sbabic@denx.de. + * on behalf of ifm electronic GmbH + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include +#include +#include +#include +#include +#include "network_ipc.h" + +static pthread_t async_thread_id; + +struct async_lib { + int connfd; + int status; + writedata wr; + getstatus get; + terminated end; +}; + +static int handle = 0; + +static struct async_lib request; + +#define get_request() (&request) + +static void *swupdate_async_thread(void *data) +{ + char *pbuf; + int size; + sigset_t sigpipe_mask; + sigset_t saved_mask; + struct timespec zerotime = {0, 0}; + struct async_lib *rq = (struct async_lib *)data; + int swupdate_result; + + sigemptyset(&sigpipe_mask); + sigaddset(&sigpipe_mask, SIGPIPE); + + if (pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &saved_mask) == -1) { + perror("pthread_sigmask"); + exit(1); + } + /* Start writing the image until end */ + + do { + if (!rq->wr) + break; + + rq->wr(&pbuf, &size); + if (size) + swupdate_image_write(pbuf, size); + } while(size > 0); + + ipc_end(rq->connfd); + printf("Now getting status\n"); + + /* + * Everything sent, ask for status + */ + + swupdate_result = ipc_wait_for_complete(rq->get); + + handle = 0; + + if (sigtimedwait(&sigpipe_mask, 0, &zerotime) == -1) { + // currently ignored + } + + if (pthread_sigmask(SIG_SETMASK, &saved_mask, 0) == -1) { + perror("pthread_sigmask"); + } + + if (rq->end) + rq->end((RECOVERY_STATUS)swupdate_result); + + pthread_exit(NULL); +} + +/* + * This is duplicated from pctl + * to let build the ipc library without + * linking pctl code + */ +static pthread_t start_ipc_thread(void *(* start_routine) (void *), void *arg) +{ + int ret; + pthread_t id; + pthread_attr_t attr; + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + ret = pthread_create(&id, &attr, start_routine, arg); + if (ret) { + exit(1); + } + return id; +} + +/* + * This is part of the library for an external client. + * Only one running request is accepted + */ +int swupdate_async_start(writedata wr_func, getstatus status_func, + terminated end_func, bool dryrun) +{ + struct async_lib *rq; + int connfd; + + if (handle) + return -EBUSY; + + rq = get_request(); + + rq->wr = wr_func; + rq->get = status_func; + rq->end = end_func; + + connfd = ipc_inst_start_ext(SOURCE_UNKNOWN, 0, NULL, dryrun); + + if (connfd < 0) + return connfd; + + rq->connfd = connfd; + + async_thread_id = start_ipc_thread(swupdate_async_thread, rq); + + handle++; + + return handle; +} + +int swupdate_image_write(char *buf, int size) +{ + struct async_lib *rq; + + rq = get_request(); + + return ipc_send_data(rq->connfd, buf, size); +} + diff --git a/ipc/network_ipc.c b/ipc/network_ipc.c index 53e577d..9cfb877 100644 --- a/ipc/network_ipc.c +++ b/ipc/network_ipc.c @@ -9,26 +9,9 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include #include #include -#include -#include -#include -#include -#include -#include -#include -#include - #include "network_ipc.h" -#include "compat.h" #ifdef CONFIG_SOCKET_CTRL_PATH static char* SOCKET_CTRL_PATH = (char*)CONFIG_SOCKET_CTRL_PATH; @@ -38,20 +21,6 @@ static char* SOCKET_CTRL_PATH = NULL; #define SOCKET_CTRL_DEFAULT "sockinstctrl" -struct async_lib { - int connfd; - int status; - writedata wr; - getstatus get; - terminated end; -}; - -static int handle = 0; -static struct async_lib request; -static pthread_t async_thread_id; - -#define get_request() (&request) - char *get_ctrl_socket(void) { if (!SOCKET_CTRL_PATH || !strlen(SOCKET_CTRL_PATH)) { const char *tmpdir = getenv("TMPDIR"); @@ -241,15 +210,6 @@ void ipc_end(int connfd) close(connfd); } -int swupdate_image_write(char *buf, int size) -{ - struct async_lib *rq; - - rq = get_request(); - - return ipc_send_data(rq->connfd, buf, size); -} - int ipc_wait_for_complete(getstatus callback) { int fd; @@ -283,113 +243,6 @@ int ipc_wait_for_complete(getstatus callback) return message.data.status.last_result; } -static void *swupdate_async_thread(void *data) -{ - char *pbuf; - int size; - sigset_t sigpipe_mask; - sigset_t saved_mask; - struct timespec zerotime = {0, 0}; - struct async_lib *rq = (struct async_lib *)data; - int swupdate_result; - - sigemptyset(&sigpipe_mask); - sigaddset(&sigpipe_mask, SIGPIPE); - - if (pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &saved_mask) == -1) { - perror("pthread_sigmask"); - exit(1); - } - /* Start writing the image until end */ - - do { - if (!rq->wr) - break; - - rq->wr(&pbuf, &size); - if (size) - swupdate_image_write(pbuf, size); - } while(size > 0); - - ipc_end(rq->connfd); - printf("Now getting status\n"); - - /* - * Everything sent, ask for status - */ - - swupdate_result = ipc_wait_for_complete(rq->get); - - handle = 0; - - if (sigtimedwait(&sigpipe_mask, 0, &zerotime) == -1) { - // currently ignored - } - - if (pthread_sigmask(SIG_SETMASK, &saved_mask, 0) == -1) { - perror("pthread_sigmask"); - } - - if (rq->end) - rq->end((RECOVERY_STATUS)swupdate_result); - - pthread_exit(NULL); -} - -/* - * This is duplicated from pctl - * to let build the ipc library without - * linking pctl code - */ -static pthread_t start_ipc_thread(void *(* start_routine) (void *), void *arg) -{ - int ret; - pthread_t id; - pthread_attr_t attr; - - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - - ret = pthread_create(&id, &attr, start_routine, arg); - if (ret) { - exit(1); - } - return id; -} - -/* - * This is part of the library for an external client. - * Only one running request is accepted - */ -int swupdate_async_start(writedata wr_func, getstatus status_func, - terminated end_func, bool dryrun) -{ - struct async_lib *rq; - int connfd; - - if (handle) - return -EBUSY; - - rq = get_request(); - - rq->wr = wr_func; - rq->get = status_func; - rq->end = end_func; - - connfd = ipc_inst_start_ext(SOURCE_UNKNOWN, 0, NULL, dryrun); - - if (connfd < 0) - return connfd; - - rq->connfd = connfd; - - async_thread_id = start_ipc_thread(swupdate_async_thread, rq); - - handle++; - - return handle; -} - int ipc_send_cmd(ipc_message *msg) { int connfd = prepare_ipc();