From patchwork Tue Dec 17 07:50:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Storm, Christian" X-Patchwork-Id: 1211179 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::13d; helo=mail-lf1-x13d.google.com; envelope-from=swupdate+bncbdd6bwv65qpbbsur4lxqkgqeikku4ia@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="AZelQ8re"; dkim-atps=neutral Received: from mail-lf1-x13d.google.com (mail-lf1-x13d.google.com [IPv6:2a00:1450:4864:20::13d]) (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 47cVgM4NYdz9sRX for ; Tue, 17 Dec 2019 18:50:38 +1100 (AEDT) Received: by mail-lf1-x13d.google.com with SMTP id c16sf584474lfm.10 for ; Mon, 16 Dec 2019 23:50:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1576569035; cv=pass; d=google.com; s=arc-20160816; b=vH+VtmtA6ox6HSUG0DKVvClgrNsuqLOAzaSJnOUAtOiXdFBrnkFXHvlYP7i+FxdwWq cUt3+7Vn+kKN6gYrHar+R/uX/73PgGfvIxiNPzHmnjBO/aDLoSt7FI0RaV2vAIMczdan fdmUBE7918ARhNZYqaGZtI/0L6j8j0qDvbJLz55RT3nMthzrgtKb1XzJpi6k/6e3mqNb RMasRbe/C19Cw+nGXDWjVPUluIfv9CxcT9YUUq/8kU5ZKmph+aE5ltJc5rA4GIWxjgwo kDY1I2XDA1GQKalrI7VVjMqwGWWbLO1YK7XETiAxNR1SgVC45hJmCYZbyrRP4Yh9OIZa G0sA== 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:message-id:date :subject:cc:to:from:sender:dkim-signature; bh=So9Z6JV37IBx1EwurSIerHQp6FlArkLZqZYv3yB3mKg=; b=jr+5Xz4tmMy5g5vSJRuNGfnueTYiiFQuDr7WEDC3gxDcgOPvv7AdIabF+wTsn2nrG4 sTybhlvtkoekbH6e9hnrqHGJZjpyVwRSouL89xpJHrunSFAbyi6vM+ce3xcJw4KEPWhg b5HznCWldlmPzdE1Il2bKEtxAJLSNmlUUM0xwml8rymMQCTZVGyKkIvSwtDfFwGWlomM IkWSr0zOTXPLbKHhH0p1SOvS+GDnhJH8O/qL1+In9O7tq+aaoZ8akK1oRB0Id6x2M7jI 0CkCyFdbAtR9kJH5r+Q2wW8Tyyoe7CMZSQI+owr9dwMrzQm5kfqKnCS703ZQTLtLX3MO uS+Q== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.14 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: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=So9Z6JV37IBx1EwurSIerHQp6FlArkLZqZYv3yB3mKg=; b=AZelQ8rezbAVj4D9OFxL3JSTumwzWlBlavvCXxmDkwstOeQANxPG8TEVrs93gsGb1K OyVdxVJ7owuqDcvE0Jvfn257LG1Z+n9MxQ0aY6CNduI73BHTyJaCSvEfZFMQLEZk+LdI DgQtUinmd3Co+6xOIW/px93Boe3RUJMsly3069QKpNDIgYYd6/R4wpIKL/FQeElX5yZk ETryMFHDA2x6zT2dC+FPu0g5WezLOqu9sz3fXG9dPhzC9LOroJUugNUl5t8VUTNB4JN6 DTVoTRPE5pUkgzm3zD3KiKaBZYs1YSTeGnyBa8oTodRDVJUIphwB67lZ7Pv74y93PSYd RuJQ== 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 :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=So9Z6JV37IBx1EwurSIerHQp6FlArkLZqZYv3yB3mKg=; b=lwYQFqWeyw6vR4nIgfnekp+mwOmhY3nOst+61+bj76zoGDyumsTbAT2K0Crprz3bJ7 SPgLLlQOW1nwkAaBbY2R8i7PpHSLNcsHYDAIJkiws6V8XX0acilPvyOMR/C/1UJHgrLm vcKfXu5Jm6dhwjfk9d23L4Yew2pyJc8WlE+NWi9dCvmBeVi5nTa54rJTxKgA2hhj9sAz stPe+H+zTBhu1vx227Vb29rxjQs/0krQE7EUnFJK8Svg02VWh4XBivsBKpV/Ll+K29hp J1NfQHNcgZeHEW0vg1Q/+iZRojtEbGq3uLV6eosK2KCnRphbKsR2JrBJpZ1eH7hxf08R MbsA== Sender: swupdate@googlegroups.com X-Gm-Message-State: APjAAAVPC8h961U+pDdKS779MLeSkJiPOMe5S24OapHLvn2mRw8FebLv Y1FMisJFU2jBSvsyV+hmnJA= X-Google-Smtp-Source: APXvYqyU0uJJ7KV9UthiCDlc7HCMsWRTTfhb7lMkYC7rDtN1dCZfTDdcq+HgfJq0E7XWx6ASRxRavQ== X-Received: by 2002:ac2:4422:: with SMTP id w2mr1925988lfl.178.1576569034848; Mon, 16 Dec 2019 23:50:34 -0800 (PST) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a2e:9912:: with SMTP id v18ls2653400lji.5.gmail; Mon, 16 Dec 2019 23:50:34 -0800 (PST) X-Received: by 2002:a2e:b0db:: with SMTP id g27mr2276585ljl.74.1576569034206; Mon, 16 Dec 2019 23:50:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576569034; cv=none; d=google.com; s=arc-20160816; b=g41qt0Lr+O6kcc/i1LXioh9I7PMu56DOIQ3fBjK+usdsPuktZRDWW3X6cyRZr4ZiEK 4MgTuugq5sw3j2GUBEFivGZF7d1t/+ysNFgg/tUpx2qstFlE9Uy4yXRhPt1qkRcjugIS d6Og8BLKvGVgZiGi3hcIb6Z3/yi5zCfQA2ekPiBqhF+9Qx3ERZazGQkTt36k1MkTKNRW TZdeQIoeUoQf2LGNbGsrUbUIb/IJOmM3eXv6rqqh7arc/mDBllJ6lzSCQSTYZubnZBHn qg9Y5ARZsfkiQgnIRnNFXn22RsP3Z0M94rcS7eLgzClCT7nWGs9BhwA5OO9zDpma1Egy LXCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from; bh=g8VzLkS1Q5LeUeVZ2q59wAjxtfzm29CFK7wsOz54swk=; b=sxAnit0xBWF28ghR52GwwfFUWW2pCqKDcBxzZAWa5nBZvbjaFUZqFUo29azCHTwMdI MgVIUzXUlcz3C1CfQl+fTuCN8M0+ZbB1cj8NjPbmAxcUQJM0MV59R90P7k0m4GoHfIw9 FrL0PknyHcsB8XiHrXoNd6XNglQw09KXREv2EQAUv1wKfrSwErreCGw2Um1EXwuZ2HQ5 A/7vWWAgY57BJjPEwOnMaSsMLhGFoQSCdBmdpjd3iE0nnDKDw8GC9UoZDeJnTZLsLOhr 9M2K+VSZPnSkFeoXTlFMmTg7Wj0kNR2PioKTgSGM4QfDCX9TlJDaW+kt905qGIHnIoxG Fi8A== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.14 as permitted sender) smtp.mailfrom=christian.storm@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Received: from david.siemens.de (david.siemens.de. [192.35.17.14]) by gmr-mx.google.com with ESMTPS id b5si871695ljo.0.2019.12.16.23.50.34 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Dec 2019 23:50:34 -0800 (PST) Received-SPF: pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.14 as permitted sender) client-ip=192.35.17.14; Received: from mail3.siemens.de (mail3.siemens.de [139.25.208.14]) by david.siemens.de (8.15.2/8.15.2) with ESMTPS id xBH7oXOk031566 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 17 Dec 2019 08:50:33 +0100 Received: from MD1ZFJVC.ad001.siemens.net ([139.25.69.82]) by mail3.siemens.de (8.15.2/8.15.2) with ESMTP id xBH7oX17022025; Tue, 17 Dec 2019 08:50:33 +0100 From: Christian Storm To: swupdate@googlegroups.com Cc: Christian Storm Subject: [swupdate] [PATCH 1/3] IPC: factor out pthread-based methods Date: Tue, 17 Dec 2019 08:50:53 +0100 Message-Id: <20191217075055.3172-1-christian.storm@siemens.com> X-Mailer: git-send-email 2.24.1 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.14 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 | 146 ++++++++++++++++++++++++++++++++++++++++++ ipc/network_ipc.c | 147 ------------------------------------------- 3 files changed, 147 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..fcfad6e --- /dev/null +++ b/ipc/network_ipc-if.c @@ -0,0 +1,146 @@ +/* + * Author: Christian Storm + * Copyright (C) 2019, Siemens AG + * + * 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();