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(); From patchwork Tue Dec 17 07:50:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Storm, Christian" X-Patchwork-Id: 1211181 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::13a; helo=mail-lf1-x13a.google.com; envelope-from=swupdate+bncbdd6bwv65qpbbs4r4lxqkgqefcjypea@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="onivTxUE"; dkim-atps=neutral Received: from mail-lf1-x13a.google.com (mail-lf1-x13a.google.com [IPv6:2a00:1450:4864:20::13a]) (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 47cVgN2yjfz9sRl for ; Tue, 17 Dec 2019 18:50:40 +1100 (AEDT) Received: by mail-lf1-x13a.google.com with SMTP id f18sf900326lfm.2 for ; Mon, 16 Dec 2019 23:50:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1576569036; cv=pass; d=google.com; s=arc-20160816; b=fyiwywX2cRJ1kQs+yFUjbkekc73VVsj+RgELFHqJgStG65AUWBO2I61QQ09oCChztQ K2rMR4P9IRH1gmZQCXD56pZm98wG1T60mawIZL5//3T63oA+SAj745TjTC24lViFDVk/ 3BG155wLtrSQ8SEzx4WTJCcEXb8M1pvm2m4yaU+WMQT9p40F8phhEAOZybr+Vxl99RgW YymdtmE4xzEMxLJGhD52lFHR5zzItUpaHz+fPb7ZOm3D6AumUsHEgCT/zlK6fl1dQEiB m3U/UFIM4y29d90r/SQDFlUmfi1H1NHrIDijPWMBhYebEdaYTAMzXagPAxj16IXE6BRx HyIA== 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=hllqz1aoqgrDj0pfuBab1VeqLfKlZvy/gGXasz1OQxg=; b=iiAzjSVo5Hvmaq6o9ve7IEUWLRNmW1ySjLrXyyC5bJ9NAA5ZEQBI7HuhjEa5FCsnZ7 wivNTYOXrEJz1P0OWsAm84JUzdEW4p2qWCTbHKtjQ9XU0NgOalzdwmlScbLEfVge0w9D qWDj6xdRaLIdoss5MSCNDfSO7Xfzx35LtZE7OIfNmVqRyF3W4d8DTTphuAc4MuO3M61Y zdmF72HmkCg2ZaRr6uUUQqb28z0eor0oXyDtMBa8QhCAv9WNEb1Jc/OPtTC0KSyijqA9 t+K1you2fV0X4JNkf24Q8KmftfkU5EheGm9DYvcj3r7Vw7HOKMrno4EbuucBfX/9gPkc ygAw== 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=hllqz1aoqgrDj0pfuBab1VeqLfKlZvy/gGXasz1OQxg=; b=onivTxUE0S13CuA6/0bzLnQD4vd/q+TuqXrWis3vBGpPE3FIwJjACcoWPtQAwZh8Kf ftJTsnzzIIgEhnkMCgdhXtb1HGY6fMzK7mIN7XMYR1F1lekh/sqzqkxcrg/Ck+rnS7ht Pk6YhWAQhVBUO97uYZim8BvXPa/20QSXrpVBPNTxYjt1fP1wz3VW+iPpDX/t5Z2S7kBY 68BGgHxHi+on3IK2D1Ml9MehtBh5BDoF1dD/eHE42hri4ZFwhOrQtQ/HOLxPYxhgTCgk qwDHCQhBLvzxoiFqILv1yYKnLzwqAVlPvzIGiz/Zgkd+FARFzHHhGQwOMR+EQ3PzbFtW yHBA== 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=hllqz1aoqgrDj0pfuBab1VeqLfKlZvy/gGXasz1OQxg=; b=PAjsKQ9iqlDWOUuLr6y7Fr14yiy9vAW+zNjG9ydOtkhRNBBMvEN/yJP/LjZBUo86wi TkmPjvpiitPIXe9//hHzsKAo+zkqZuumqZK0+41fk/o2UvrtjppMexq9fGQu8jydG6Zz 31YQ9rAMCvwERrXW7QBFGCui4fEKxeOzLskDWrTqgo/WfJRIAN4CCfjzFiLaB556sv4m CHdOzaBntjHLGIe8/l4LO2aHFFWZnSluToA0kryNyRh84sYk7CSisAKH2VqFKYkIV6b5 bV49uTRTNCsweDDrJbvwMA4On71beb2dNgM8b4KhiyEAUFgreoQUk5CRrbkbhujhO8op grTg== Sender: swupdate@googlegroups.com X-Gm-Message-State: APjAAAXv744LdZh7YeZKLMhPjjGPPha3Tm+hrLYEOqpuTAzwUF9DEWEt vRMCJYOazqRc70/TWm0Vgog= X-Google-Smtp-Source: APXvYqyXuQBD9JCR8uEFqqG0/UR1a7cW1Mwb5/hx0//odv9RuzptsDx1mTylYuEDwovEXQ/gzlW9ew== X-Received: by 2002:a2e:9015:: with SMTP id h21mr2181810ljg.69.1576569035663; Mon, 16 Dec 2019 23:50:35 -0800 (PST) X-BeenThere: swupdate@googlegroups.com Received: by 2002:ac2:5219:: with SMTP id a25ls1727135lfl.16.gmail; Mon, 16 Dec 2019 23:50:34 -0800 (PST) X-Received: by 2002:ac2:53a8:: with SMTP id j8mr1967490lfh.28.1576569034934; 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=Pjyn4U1MhLVcQBL1jG/iyuY89F8vv+wEpX0BTZ6ZaWAo06gxi00ssvtCXnZ8cHgFKB Ik9B/ag/utlFmLVKl/5792abLMC1gLqTYfFrrvpttrn8GPwnUR6URMlXTx5DaHW0fN/X QbL9PeukAGQZX7+7duvllg75UUq+AyTBfBeLxziyDaig2dpxuLVdEVnep889kD2f0haI vOo67u06KWgjBXfCJqq/G7s2hwz1GuqWky5sJ5jlLAQ1E6E974R/WNfHDRgNDXrqszl4 nu+RKs5GIl/wkBhgvPSOdIHNCpmmIDeJeAf1AYfFnGdpIWmE1adZOKaJXeUBFsCaaqp1 H9vw== 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=7EZjIBx8I9xU0jZn1tk5rcUnCPa2PpcNoRwjLnIAKww=; b=kUR1GZhvFWOVHYfn+yONjJrJ7A6OC5o0tt3O+iVL/EPc8e94g3Ke49VjS9smrYMveX YDiS/u6LIUA98PhJsZYOxG+gCwbgZ3Q9cWcHTVhxpp9fJ5kAL4P6kWsgyyYm2LQ8Hiy5 QeJXIJJJ8tjfAZMpPER/6xiMdl+63yFyYQYidD+F3pHB2XvFJ0JxiIAE2gnZxSsJBLS6 XErSXyK5EIyif1DC2sen5qOGDi+UHlJLmddJmy+0zCWogJKYKt7xPLyuJEreMvIcQ3Ck 3yvd3zAPdsJaIeYBnN5GZkG3O6lhM5vsg863XhYIQui0nGV+P58vCcGW64hGuvfsQ7Vi srNg== 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 x5si793751ljh.5.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.2 as permitted sender) client-ip=192.35.17.2; Received: from mail3.siemens.de (mail3.siemens.de [139.25.208.14]) by thoth.sbs.de (8.15.2/8.15.2) with ESMTPS id xBH7oYmf020697 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 17 Dec 2019 08:50:34 +0100 Received: from MD1ZFJVC.ad001.siemens.net ([139.25.69.82]) by mail3.siemens.de (8.15.2/8.15.2) with ESMTP id xBH7oX18022025; Tue, 17 Dec 2019 08:50:34 +0100 From: Christian Storm To: swupdate@googlegroups.com Cc: Christian Storm Subject: [swupdate] [PATCH 2/3] Bindings: Implement control socket binding for Lua Date: Tue, 17 Dec 2019 08:50:54 +0100 Message-Id: <20191217075055.3172-2-christian.storm@siemens.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191217075055.3172-1-christian.storm@siemens.com> References: <20191217075055.3172-1-christian.storm@siemens.com> 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: , Implements the binding to SWUpdate's control socket for Lua. This allows to operate SWUpdate from within pure Lua along the lines of, e.g., local artifact = io.open("/some/path/to/artifact.swu", "rb" ) local ctrl = swupdate.control() if not ctrl:connect() then -- Deliberately neglecting error message. io.stderr:write("Error connecting to SWUpdate control socket.\n") return end while true do local chunk = artifact:read(1024) if not chunk then break end if not ctrl:write(chunk) then -- Deliberately neglecting error message. io.stderr:write("Error writing to SWUpdate control socket.\n") break end end local res, msg = ctrl:close() if not res then io.stderr:write(string.format("Error finalizing update: %s\n", msg)) end Signed-off-by: Christian Storm --- bindings/lua_swupdate.c | 161 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) diff --git a/bindings/lua_swupdate.c b/bindings/lua_swupdate.c index dd164e3..ce512a7 100644 --- a/bindings/lua_swupdate.c +++ b/bindings/lua_swupdate.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include "auxiliar.h" @@ -54,6 +56,12 @@ static int progress_connect(lua_State *L); static int progress_receive(lua_State *L); static int progress_close(lua_State *L); +static int ctrl(lua_State *L); +static int ctrl_connect(lua_State *L); +static int ctrl_write(lua_State *L); +static int ctrl_close(lua_State *L); +static int ctrl_close_socket(lua_State *L); + int luaopen_lua_swupdate(lua_State *L); /* @@ -99,6 +107,157 @@ static int netif(lua_State *L) return 1; } +struct ctrl_obj { + int socket; +}; + +/* control object methods */ +static luaL_Reg ctrl_methods[] = { + {"__gc", ctrl_close_socket}, + {"__tostring", auxiliar_tostring}, + {"connect", ctrl_connect}, + {"write", ctrl_write}, + {"close", ctrl_close}, + {NULL, NULL} +}; + +/** + * @brief Connect to SWUpdate control socket. + * + * @param [Lua] The swupdate_control class instance. + * @return [Lua] The connection handle (mostly for information), or, + * in case of errors, nil plus an error message. + */ +static int ctrl_connect(lua_State *L) { + struct ctrl_obj *p = (struct ctrl_obj *) auxiliar_checkclass(L, "swupdate_control", 1); + if (p->socket != -1) { + lua_pop(L, 1); + lua_pushnil(L); + lua_pushstring(L, "Already connected to SWUpdate control socket."); + return 2; + } + + int connfd = ipc_inst_start_ext(SOURCE_LOCAL, 0, NULL, false); + if (connfd < 0) { + lua_pop(L, 1); + lua_pushnil(L); + lua_pushstring(L, "Cannot connect to SWUpdate control socket."); + return 2; + } + + p->socket = connfd; + + lua_pop(L, 1); + lua_pushnumber(L, connfd); + lua_pushnil(L); + + return 2; +} + +/** + * @brief Write data chunk to SWUpdate's control socket. + * + * @param [Lua] The swupdate_control class instance. + * @param [Lua] Lua String chunk data to write to SWUpdate's control socket. + * @return [Lua] True, or, in case of errors, nil plus an error message. + */ +static int ctrl_write(lua_State *L) { + struct ctrl_obj *p = (struct ctrl_obj *) auxiliar_checkclass(L, "swupdate_control", 1); + luaL_checktype(L, 2, LUA_TSTRING); + + if (p->socket == -1) { + lua_pushnil(L); + lua_pushstring(L, "Not connected to SWUpdate control socket."); + goto ctrl_write_exit; + } + + size_t len = 0; + const char* buf = lua_tolstring(L, 2, &len); + if (!buf) { + lua_pushnil(L); + lua_pushstring(L, "Error converting Lua chunk data."); + goto ctrl_write_exit; + } + if ((len = ipc_send_data(p->socket, (char *)buf, len)) < 0) { + lua_pushnil(L); + lua_pushstring(L, "Error writing to SWUpdate control socket."); + goto ctrl_write_exit; + } + + lua_pushboolean(L, true); + lua_pushnil(L); + +ctrl_write_exit: + lua_remove(L, 1); + lua_remove(L, 1); + return 2; +} + +static int ctrl_close_socket(lua_State *L) { + struct ctrl_obj *p = (struct ctrl_obj *) auxiliar_checkclass(L, "swupdate_control", 1); + (void)ipc_end(p->socket); + p->socket = -1; + lua_remove(L, 1); + return 0; +} + +static char *ipc_wait_error_msg = NULL; +static int ipc_wait_get_msg(ipc_message *msg) +{ + if (msg->data.status.error != 0 && msg->data.status.current == FAILURE) { + free(ipc_wait_error_msg); + ipc_wait_error_msg = strdup(msg->data.status.desc); + } + return 0; +} + +/** + * @brief Close connection to SWUpdate control socket. + * + * @param [Lua] The swupdate_control class instance. + * @return [Lua] True, or, in case of errors, nil plus an error message. + */ +static int ctrl_close(lua_State *L) { + struct ctrl_obj *p = (struct ctrl_obj *) auxiliar_checkclass(L, "swupdate_control", 1); + if (p->socket == -1) { + lua_pop(L, 1); + lua_pushboolean(L, true); + lua_pushnil(L); + return 2; + } + + (void)ctrl_close_socket(L); + + if ((RECOVERY_STATUS)ipc_wait_for_complete(ipc_wait_get_msg) == FAILURE) { + lua_pushnil(L); + lua_pushstring(L, ipc_wait_error_msg); + free(ipc_wait_error_msg); + ipc_wait_error_msg = NULL; + return 2; + } + + ipc_message msg; + if (ipc_postupdate(&msg) != 0) { + lua_pushnil(L); + lua_pushstring(L, "SWUpdate succeeded but post-update action failed."); + return 2; + } + + lua_pushboolean(L, true); + lua_pushnil(L); + return 2; +} + +static int ctrl(lua_State *L) { + /* allocate control object */ + struct ctrl_obj *p = (struct ctrl_obj *) lua_newuserdata(L, sizeof(*p)); + p->socket = -1; + + /* set its type as master object */ + auxiliar_setclass(L, "swupdate_control", -1); + + return 1; +} struct prog_obj { RECOVERY_STATUS status; @@ -185,6 +344,7 @@ static int progress(lua_State *L) { static const luaL_Reg lua_swupdate[] = { {"progress", progress}, + {"control", ctrl}, {"ipv4", netif}, {NULL, NULL} }; @@ -195,5 +355,6 @@ static const luaL_Reg lua_swupdate[] = { int luaopen_lua_swupdate(lua_State *L){ luaL_newlib(L, lua_swupdate); auxiliar_newclass(L, "swupdate_progress", progress_methods); + auxiliar_newclass(L, "swupdate_control", ctrl_methods); return 1; } From patchwork Tue Dec 17 07:50:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Storm, Christian" X-Patchwork-Id: 1211180 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::239; helo=mail-lj1-x239.google.com; envelope-from=swupdate+bncbdd6bwv65qpbbter4lxqkgqew7zbtjy@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="MDdbAlGE"; dkim-atps=neutral Received: from mail-lj1-x239.google.com (mail-lj1-x239.google.com [IPv6:2a00:1450:4864:20::239]) (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 47cVgN3L5Vz9sRm for ; Tue, 17 Dec 2019 18:50:40 +1100 (AEDT) Received: by mail-lj1-x239.google.com with SMTP id y24sf623885ljc.19 for ; Mon, 16 Dec 2019 23:50:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1576569036; cv=pass; d=google.com; s=arc-20160816; b=wLDlzzH4LkZkGSwbEGpYCqMwzZfcFJKrM++WiHP+9toegDhrjU6nRuIb/11vsV1l92 BEgWP1TVqz2RH3XZQYCS34dgShm1dn/oYlYT4SPKj1fovG8bKKXcpoaWP4VXgJLKko0k c0EygAXjgqxhFcKw3UTM3T7avnUO13C64OGfB+ZwiEnU+bhixnutYCpEKedvOH3rJhLu sqk6glwQmeckOcPQsI5usPcaO8iGuuRcBkVK9PPmC7zTzOa3TgPW18IO6+vmClI/XyDs H0nCXVTn+jZvy0gnOZvzHXmw3pnGJyeuMnCnViLVaDeU53nxb39ZSGQG0bPzFw9TrIJ7 cxeQ== 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=xE+aM9eQQSYuobRitc+CTGWS22njZiwGnTlLnV4/w4U=; b=iU4pScS4ntcigV6Cy08GVGWH+V/VqOHUCH5KJTDXRLDiWu1GhvQhkF3A2QAybzqAlr d6ExZVVYurMuI+3u+iaTPYDUTBCamWiiRc6eArzQrmo1VU6H9Tf/923OY0IQXkQDHJgT Pdusu0cQIAC89I/Vh4BFBOciWWnQFrAhPvxDTxxCbQOOhzLo4mrYhcjFeXl/EujJoqCE EV3VzTrqwzbFPIt3E4QekEqGK5hp5Kd0EPnVer50pITGwcpVDwGCtzD2khTuO9TkafVm x2GJGDMVbhrT2Y4xU1YUOGhIsXTxFBR7HwWbV0wCfqdJrX0u/IferDvDYt0LDG3/vL0y YKcA== 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=xE+aM9eQQSYuobRitc+CTGWS22njZiwGnTlLnV4/w4U=; b=MDdbAlGE07I1LzJIo4P9Qth2eB58A0iFmZ35ZL/+OIy/pT9DyIuP0racuWoX5Z2jFg aBjoSza0fHqPjp5CEft2VxOU56LvuuwKVRnytgTFozBaIS7U1Bm7Q5l1LiQl/69IVpnO OhI7ofxlz1khtCkKexeRgDC946rAT9MduAFGJo9ak35np1//9kS5/lnXyKbwS74iJ0p0 vypfMgTVyAz8bQaRXwJHBBG/JqiLwS09L3Psg4+pec5x+lSndVV9rurjpk85z5Em6W7I TmUGIR1u0PGctpEtsMO+KSKB5hCfxjiquon5yfgqs/m9vdVs2LvZyy9iGsIGM6k5jYoS WtNQ== 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=xE+aM9eQQSYuobRitc+CTGWS22njZiwGnTlLnV4/w4U=; b=sab+wd/4mdbEIGJgdxcBO0XY73wuXoGtIGA7nsEhpIG3e/VSbM0PP77L3z/G5neQYI kkNgQc5XUEXfRX5Op/kxf0TZD7ZYt0pnNU+1+Wb9aZI7ZkBiooCiHgJ8dLquwwQHOyCx 3WhLafnOZD6Gbii4xvhFomyzPgBCFp7XqDnWeRN2PoVxtqvfPuyw+VfNNXYpxSv4hfKg u29QiKNVULfkc0O68RXBOv2kU5LadPuJGYZsErvyZbklFAb45KSb50PdgSSZeNbms+kU hR241OxnrCOi7DaB1+kf4OC4Ptrn3fb5DdD109qR3v74qDurxx4MsfsV2JC0Uk5YX7a+ /ICA== Sender: swupdate@googlegroups.com X-Gm-Message-State: APjAAAUmuF7SnidPMiSkCFvu/9dVNy/afOn+YmpQxcvlv5faN676hHrw gZhVE1dyLejB7R9ve6Wn/FA= X-Google-Smtp-Source: APXvYqw0kc+03H1g+9LuZ+vg5Lq79JdUmuJWxM7uA2xWg8QFFiFdibJnjCNXIMmBtAnJm4PTfbh9Nw== X-Received: by 2002:a19:6a04:: with SMTP id u4mr1991708lfu.62.1576569036417; Mon, 16 Dec 2019 23:50:36 -0800 (PST) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a19:c507:: with SMTP id w7ls1723772lfe.1.gmail; Mon, 16 Dec 2019 23:50:35 -0800 (PST) X-Received: by 2002:ac2:498e:: with SMTP id f14mr1888016lfl.172.1576569035815; Mon, 16 Dec 2019 23:50:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576569035; cv=none; d=google.com; s=arc-20160816; b=dy3vFfWeOnLie++21kV2GUzNuZmT5OG9ge/dEEqW9yMDYbRcgAKQspIZ1wBRnrLJ1T RhRg+OmzaPHUa5oHiwAW7DMGgIjYHKoqGKpwpI1o9bDX7OjU7rpNkJZKG+OuXXoOVHKb LFf9SfiI7J+cYdOvvEl4qOSCIM3HK++z/ho4wbgEkmfWKPGn7MU+fZS4PG0pXxw66TLN xf4TIppP6+bNbxjIXIgACAvzjhT32TsvGlhGF55abboMb3l4GjB6o+YNM0g/pjiIIhkg C7N/UbQbAcT6MW9e7jcBg1yIIcTe3wELW6XIgaD+oIp/SzptWDDvU7F4a+JtjR94lVMS xEJw== 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=RjKB8qKf6BgPb4P/lTH44kTppPjAlSE5gc1gvYkBzDc=; b=R5YANnJCjO1WBmt208u1ypplVQ7yVNkC88Mr1hpJj476OIIG7WpBvn0XyiIcOtUcN7 HRksdjVFShNPXgbQ+Z6a8inDTkHpieVwx0IdwR5Yy+CaeEcBaPf3eonISRKZDNDp3tNd gdvMND3jngcl7MIpI8IjxNEJp07bf0FMajepzN/yMFHougU5H62oDW1WEWIWVLCJyFAZ wPdb3xcEmzq0E89rUBSdx0qsiG0pnkU0Ls4KvlKRu0TLqMKPevuS9PKO/qi+ZdJRAz/H rsPtrIBW0zj7NQGWDvmg5bJosDPYnXOPZVF8XbAGNYtxqL291d+yNmJTfRuIxQT0IrZh jhOg== 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 o193si811433lff.4.2019.12.16.23.50.35 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Dec 2019 23:50:35 -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 mail3.siemens.de (mail3.siemens.de [139.25.208.14]) by thoth.sbs.de (8.15.2/8.15.2) with ESMTPS id xBH7oYi7020717 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 17 Dec 2019 08:50:34 +0100 Received: from MD1ZFJVC.ad001.siemens.net ([139.25.69.82]) by mail3.siemens.de (8.15.2/8.15.2) with ESMTP id xBH7oX19022025; Tue, 17 Dec 2019 08:50:34 +0100 From: Christian Storm To: swupdate@googlegroups.com Cc: Christian Storm Subject: [swupdate] [PATCH 3/3] doc: document language bindings Date: Tue, 17 Dec 2019 08:50:55 +0100 Message-Id: <20191217075055.3172-3-christian.storm@siemens.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191217075055.3172-1-christian.storm@siemens.com> References: <20191217075055.3172-1-christian.storm@siemens.com> 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: , Document (programming) language bindings, in particular the Lua binding. Signed-off-by: Christian Storm --- doc/source/bindings.rst | 112 ++++++++++++++++++++++++++++++++++++++++ doc/source/index.rst | 1 + 2 files changed, 113 insertions(+) create mode 100644 doc/source/bindings.rst diff --git a/doc/source/bindings.rst b/doc/source/bindings.rst new file mode 100644 index 0000000..7bf32bc --- /dev/null +++ b/doc/source/bindings.rst @@ -0,0 +1,112 @@ +============================================= +Language Bindings +============================================= + +Overview +-------- + +In general, SWUpdate is agnostic to a particular language it is operated from, +thanks to :doc:`SWUpdate's socket-based control ` and +:doc:`progress APIs ` for external programs. As long as the language +of choice has proper socket (library) support, SWUpdate can be operated with it. + +However, for convenience, a Lua language binding in terms of a shared library, +currently ``lua_swupdate.so.0.1``, is provided. + + +Lua Language Binding +-------------------- + +The Lua language binding is realized in terms of the ``swupdate`` module that +defines three bindings, namely for the control interface, the progress +interface, and a convenience function yielding a table holding all local +network interfaces including their IP addresses and submasks. + +Note that, depending on the filesystem location of the Lua binding's shared +library, Lua's ``package.cpath`` may have to be adapted by setting the +environment variable ``LUA_CPATH``, modifying ``package.cpath`` prior to +a ``require('lua_swupdate')``, or , as last resort, using ``package.loadlib()`` +instead of ``require('lua_swupdate')``. + + +Control Interface +................. + +The ``swupdate`` module's control interface binding conveniently makes +:doc:`SWUpdate's socket-based control API ` available to pure Lua. + +The binding is captured in the ``swupdate_control`` object that is returned +by a call to ``swupdate.control()``. This object offers the three methods +``connect()``, ``write()``, and ``close()``: + +The ``connect()`` method initializes the connection to SWUpdate's control +socket, sends ``REQ_INSTALL``, and waits for ``ACK`` or ``NACK``, returning the +socket connection file descriptor, mostly for information purposes, or, in case +of an error, ``nil`` plus an error message. + +The artifact's data can then be sent to SWUpdate via the ``write()`` +method, returning ``true``, or, in case of errors, ``nil`` plus an error message. + +Finally, the ``close()`` method closes the connection to SWUpdate's control +socket after which it waits for SWUpdate to complete the update transaction and +executes the post-install command, if given. + +The following example snippet illustrates how to use the control interface binding: + +:: + + local artifact = io.open("/some/path/to/artifact.swu", "rb" ) + swupdate = require('lua_swupdate') + local ctrl = swupdate.control() + if not ctrl:connect() then + -- Deliberately neglecting error message. + io.stderr:write("Error connecting to SWUpdate control socket.\n") + return + end + + while true do + local chunk = artifact:read(1024) + if not chunk then break end + if not ctrl:write(chunk) then + -- Deliberately neglecting error message. + io.stderr:write("Error writing to SWUpdate control socket.\n") + break + end + end + + local res, msg = ctrl:close() + if not res then + io.stderr:write(string.format("Error finalizing update: %s\n", msg)) + end + + +Progress Interface +.................. + +The ``swupdate`` module's progress interface binding conveniently makes +:doc:`SWUpdate's socket-based progress API ` available to pure Lua. + +The binding is captured in the ``swupdate_progress`` object that is returned +by a call to ``swupdate.progress()``. This object offers the three methods +``connect()``, ``receive()``, and ``close()``: + +The ``connect()`` method connects to SWUpdate's progress socket, waiting until +the connection has been established. Note that it is only really required to +explicitly call ``connect()`` to reestablish a broken connection as the +``swupdate_progress`` object's instantiation already initiates the connection. + +The ``receive()`` method returns a table representation of the ``struct +progress_msg`` described in the :doc:`progress interface's API description +`. + +The ``close()`` method deliberately closes the connection to SWUpdate's progress +socket. + + +IPv4 Interface +.............. + +For convenience, the ``swupdate`` module provides the ``ipv4()`` method +returning a table holding the local network interfaces as the table's keys and +their space-separated IP addresses plus subnet masks as respective values. + diff --git a/doc/source/index.rst b/doc/source/index.rst index eb653db..cb6491d 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -43,6 +43,7 @@ SWUpdate Documentation hawkbit-setup.rst swupdate-ipc.rst progress.rst + bindings.rst building-with-yocto.rst ############################################