From patchwork Mon Mar 9 10:15:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1251422 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=209.85.167.60; helo=mail-lf1-f60.google.com; envelope-from=swupdate+bncbcxploxj6ikrbv5otdzqkgqeahlikyq@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=Y02S1pWf; dkim-atps=neutral Received: from mail-lf1-f60.google.com (mail-lf1-f60.google.com [209.85.167.60]) (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 48bYzy0frkz9sPJ for ; Mon, 9 Mar 2020 21:17:01 +1100 (AEDT) Received: by mail-lf1-f60.google.com with SMTP id o25sf2692560lfb.12 for ; Mon, 09 Mar 2020 03:17:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1583748951; cv=pass; d=google.com; s=arc-20160816; b=b0Gi6dSJnf/uBL2y5Bd+Yx5zB/fT9qjoPRAaQn7UYjFGs2P/f5zu17wkbc1YZOb3t6 +MOk7r8Q1oBqj5xj9LXQBkzoseuqwOkLRGMpVFgioG2l+IU8iJ+8vPJVIzJaHifkjYKJ sfr9yLThI2gEtLZZ2Kx05pvx66EXx4pzkokjbp4dx/23qaCzaCrBXBJ7EM9hM2iBscJG B67q8nDOolHEAeVVw7o+V7VlM6KOYQUAHBTMQ04UdHwBZmae+kIfdFFNMIKd4N5uL6Li 3aNpAzDtB3jknJpWKyYRLeiTHVo/WqSpJjQvN/UTfE4LnuZq3mGX+Fq3ehLfHHCNGeLL ZE1w== 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=tN2P1/qe572AaW2H23I1jPpCl4wq/V3m5kguZ1g7tUw=; b=wYu7QUWJLWXRoHNzxWoHYWfu8tXQaDxvERe7eBOf/bGcE6ugYiKDb5Z8f8yef6+yhC RYT75KPlMqfkhynCJWvfi8bZOmfShyAL2pnMZzkIbG453NxbNWAGQNHcgrgsRDCca9SU 4HcU97scsM3fLmeU91dEfO69KXz2cXHmnsPPYM8VQKFYB2Ya6B78V1GoSQS13sCiPLji +GUazvGA5Usp7/yNWTOs1HLEDnlRF47t26eNQ/bjLTJErl79N7NxqUDZ7X4/A4PQ+h8f mGftpfnlJwnV5XT8+ecW+XxhQss9h4q8/VcsgtQL2Ktmkz6d3VGTMczwfx6WSHppGlyB ryWA== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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=tN2P1/qe572AaW2H23I1jPpCl4wq/V3m5kguZ1g7tUw=; b=Y02S1pWfEi7UK2DWPLQTpUtllk5z9+Rum/zkfGdtYGt/O1eX86m7phlsjh4+w6sVD5 8e3rKdeFUVVDHcAtziZGL9XVEfLHmt2cxOXcANq5s7pYiRAZJZ19onZl3GNWLZopzeM9 M8AsqStUFthWZNeaeTbBUYd2EAALyTxBIApsrfEHnnomNGbu1W/YIQTugLmHraQAD2Zb BqOlZbn0k6YM9un99DQckuDbqY7tKrLcr0CRPGf8Ob59xjCZMXE6UnmDJQrMajT+xE6v AYgrUH61VDyIve5gxYdJXtqadJvU+CjWTriwY7XXmdZe0/cfXZqY43FfpzyKI1Z1L5hm XtcQ== 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=tN2P1/qe572AaW2H23I1jPpCl4wq/V3m5kguZ1g7tUw=; b=Pmnx54PUJn/HF6juoPF0R3+ZY/zhdcLx6nlE31yf1upCUlTgox5x3fZZGdh9GxTprk eT1Bmy2HFLNqAQCXqsXt/DnOHy1lTlYHWzfRMSieM+6oQKum0ppuAxpGQenY2eRvBi6g AuYJV0He7VLuQkR6BD8FrYRlwxxf0xc8rSrA0A5t7E/jCyLgsJRqXtgLkK2rvxpsRcyd +cUDOxysUVublfN0Y3McI5tUsD8ECoNP6p7quYH8wLGjRpKqQRGbqpMqGjr8+4Do3pkV +YzLF5eFIM0yliE7/hvvMJUSl6KihvPnXC5RTVO63gnOGM1wXme+mbXIX0K/8QQ37tyo 4owA== Sender: swupdate@googlegroups.com X-Gm-Message-State: ANhLgQ1jfnK7SRy7W5Nht5s95iCybo2/E7u+pLWImGn0hdFh8Kte+YTr Z0QDjA06eqC3ssgU8yxYU/w= X-Google-Smtp-Source: ADFU+vuRVb5D6wrW5TJHv0kVRBUE/CNMOS6gZZkK60oW8hcdzbZzkJTQ0sBhB6rbXbNC+yVjkj7tuQ== X-Received: by 2002:a05:651c:94:: with SMTP id 20mr9424338ljq.17.1583748951751; Mon, 09 Mar 2020 03:15:51 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:651c:550:: with SMTP id q16ls562557ljp.1.gmail; Mon, 09 Mar 2020 03:15:50 -0700 (PDT) X-Received: by 2002:a2e:b521:: with SMTP id z1mr9513792ljm.19.1583748950612; Mon, 09 Mar 2020 03:15:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583748950; cv=none; d=google.com; s=arc-20160816; b=bs2glokDBd0W7mWJpv/XF28l5usZuLZJbo2BTNaoyvW58QFE+1cuZjtXGKWscAjBPz zXI+6qmgblrU3IXBqPa2xmuUKcmafAEfkUVZIirr/zdnNjWVe2FQ7OuTO7hboUpEWlXz vMDnXjVfKi8CavbtSTlJv2faKUe4SAhQkV/A5C0w0TEEZA931kjC+x3l3/7fhdZ6ywwb 5VTEkZzGQrlNmwrODsKYtQc9//fTMw3HH6qjPisYSVCEkFDJY3gamD8uijPewIDWdPkp KsNHrEvB1EzqdxDtXOEwXLRphzGhT8+ouNIfwFgIi6Z23WQPNFcOlCw1jkOCBNrU/s8K ReAg== 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=Qa++H8aizQSGCFqxmTuxkqiTgVWNfR5Pb6WmTMGsIZs=; b=VsepOhgKXe7tGkUeELZGXIW5iED+aPqyrgrfduA+3IdU6GC1iCTus8noYdNtN18uxz KxjVX8jWBeCYMl+99zfPF3l93H2uFKLyoMtnlO9X9MrlbHp+DqVaFe1DqUWM6wklJhpz R15E+5XaJtMj12ksidkh46LmjYa74V8mnn1KeJwJU/8cG6tptplQN2Non8ityAaPdkOl ismvsiURSFXjdFuTPUcqc3ulDtSNloBp9EMuDkxajJw2M9VFVF6leGY0S53l8eHDUdIp +KFfJSN9P3Vjue8Bzuv4yJBgmrX4NG+sjNnQXBuwa5hATAH4ULZJfcSdqlI8R+9/IVqg 8Agw== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de Received: from mail-out.m-online.net (mail-out.m-online.net. [212.18.0.9]) by gmr-mx.google.com with ESMTPS id u8si393460lfu.3.2020.03.09.03.15.50 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Mar 2020 03:15:50 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) client-ip=212.18.0.9; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 48bYyZ08P5z1qqkL; Mon, 9 Mar 2020 11:15:50 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 48bYyY74Wnz1qwyf; Mon, 9 Mar 2020 11:15:49 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id OgLy-UN6xW17; Mon, 9 Mar 2020 11:15:49 +0100 (CET) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Mon, 9 Mar 2020 11:15:49 +0100 (CET) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id C8F8A45401BA; Mon, 9 Mar 2020 11:15:49 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([IPv6:::1]) by localhost (mail.babic.homelinux.org [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id AaVJwoAirDBl; Mon, 9 Mar 2020 11:15:45 +0100 (CET) Received: from paperino.fritz.box (paperino.fritz.box [IPv6:2001:a61:513e:9501:3d60:f61:f29a:9d00]) by babic.homelinux.org (Postfix) with ESMTP id B15AF4540494; Mon, 9 Mar 2020 11:15:45 +0100 (CET) From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 1/5] Factorize calls of system() Date: Mon, 9 Mar 2020 11:15:38 +0100 Message-Id: <20200309101542.27373-2-sbabic@denx.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200309101542.27373-1-sbabic@denx.de> References: <20200309101542.27373-1-sbabic@denx.de> MIME-Version: 1.0 X-Original-Sender: sbabic@denx.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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: , There are just two places where a system() is called: - during pre- and post- update command - in the shell script handler Part of code is duplicate. Factorize it in the run_system_cmd() already used by the installed and expose it to be used globally. This is laso in preparation for a better support of shell commands. Signed-off-by: Stefano Babic --- core/installer.c | 27 ++++++--------------------- core/pctl.c | 20 ++++++++++++++++++++ handlers/shell_scripthandler.c | 10 ++-------- include/pctl.h | 1 + 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/core/installer.c b/core/installer.c index cc3c1ea..61dc06d 100644 --- a/core/installer.c +++ b/core/installer.c @@ -32,6 +32,7 @@ #include "parsers.h" #include "bootloader.h" #include "progress.h" +#include "pctl.h" /* * function returns: @@ -458,28 +459,11 @@ void cleanup_files(struct swupdate_cfg *software) { #endif } -static int run_system_cmd(const char *cmd, const char *desc) -{ - int ret = 0; - if ((strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) > 0) - && (strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) < SWUPDATE_GENERAL_STRING_SIZE)) { - DEBUG("Running %s command '%s' ...", desc, cmd); - ret = system(cmd); - if (WIFEXITED(ret)) { - DEBUG("%s command returned %d", desc, WEXITSTATUS(ret)); - } else { - ERROR("%s command returned %d: '%s'", desc, ret, strerror(errno)); - return -1; - } - } - - return ret; -} - int preupdatecmd(struct swupdate_cfg *swcfg) { if (swcfg) { - return run_system_cmd(swcfg->globals.preupdatecmd, "Pre-update"); + DEBUG("Running Pre-update command"); + return run_system_cmd(swcfg->globals.preupdatecmd); } return 0; @@ -489,8 +473,9 @@ int postupdate(struct swupdate_cfg *swcfg, const char *info) { swupdate_progress_done(info); - if ((swcfg) && (run_system_cmd(swcfg->globals.postupdatecmd, "Post-update") == -1)) { - return -1; + if (swcfg) { + DEBUG("Running Post-update command"); + return run_system_cmd(swcfg->globals.postupdatecmd); } return 0; diff --git a/core/pctl.c b/core/pctl.c index d8b2c76..134dd10 100644 --- a/core/pctl.c +++ b/core/pctl.c @@ -14,6 +14,7 @@ #include #endif #include +#include #include #include #include @@ -202,6 +203,25 @@ void start_subprocess(sourcetype type, const char *name, const char *cfgfile, start_swupdate_subprocess(type, name, cfgfile, argc, argv, start, NULL); } +int run_system_cmd(const char *cmd) +{ + int ret = 0; + + if ((strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) > 0) + && (strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) < SWUPDATE_GENERAL_STRING_SIZE)) { + DEBUG("Running %s command...", cmd); + ret = system(cmd); + if (WIFEXITED(ret)) { + TRACE("%s command returned %d", cmd, WEXITSTATUS(ret)); + } else { + ERROR("%s command returned %d: '%s'", cmd, ret, strerror(errno)); + return -1; + } + } + + return ret; +} + /* * The handler supervises the subprocesses * (Downloader, Webserver, Suricatta) diff --git a/handlers/shell_scripthandler.c b/handlers/shell_scripthandler.c index ca0fc34..fc6ff06 100644 --- a/handlers/shell_scripthandler.c +++ b/handlers/shell_scripthandler.c @@ -19,6 +19,7 @@ #include "swupdate.h" #include "handler.h" #include "util.h" +#include "pctl.h" static void shell_handler(void); static void shell_preinstall_handler(void); @@ -41,14 +42,7 @@ static int execute_shell_script(struct img_type *img, const char *fnname) snprintf(shellscript, sizeof(shellscript), "%s%s %s %s", tmp, img->fname, fnname, img->type_data); - ret = system(shellscript); - if (WIFEXITED(ret)) { - ret = WEXITSTATUS(ret); - TRACE("Calling shell script %s: return with %d", - shellscript, ret); - } else { - ERROR("%s returns '%s'", img->fname, strerror(errno)); - } + ret = run_system_cmd(shellscript); return ret; } diff --git a/include/pctl.h b/include/pctl.h index 0518531..de5f3b5 100644 --- a/include/pctl.h +++ b/include/pctl.h @@ -40,5 +40,6 @@ void sigchld_handler (int __attribute__ ((__unused__)) signum); int pctl_getfd_from_type(sourcetype s); const char *pctl_getname_from_type(sourcetype s); +int run_system_cmd(const char *cmd); #endif From patchwork Mon Mar 9 10:15:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1251420 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=209.85.167.57; helo=mail-lf1-f57.google.com; envelope-from=swupdate+bncbcxploxj6ikrbwfotdzqkgqexyph24y@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=LAJkaUyd; dkim-atps=neutral Received: from mail-lf1-f57.google.com (mail-lf1-f57.google.com [209.85.167.57]) (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 48bYzw74Q3z9sPJ for ; Mon, 9 Mar 2020 21:17:00 +1100 (AEDT) Received: by mail-lf1-f57.google.com with SMTP id u20sf2690828lfu.18 for ; Mon, 09 Mar 2020 03:17:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1583748953; cv=pass; d=google.com; s=arc-20160816; b=B9B42ManNIxOJya3GrN9W2HPRmY2WvAnnINfZonQowwDRERYsuN4W2iZeWgQzgioGJ mMg/nxQ36Qao0pVWvdu0JFBMZlEVqwzw/i3Gf7Y+JpVIsGPC3TlTRCs0rQqlvICiRt/V Sko5IekQpHjldhTYNFi6O7XVi41/DCCIy9NU2MZ/YCQGswJEVzt7feqcE4vIC+emacOg h2KqtKa9+hEr6MAzDNsJ3xmr5eulO5SDRjCQIJn7ad7T1hQH5CbyEj5NuMN4eVOE1swD jJLrtrFVyRUZi59AAe1YijG+tW7nmzy/lhevbGQYzrEZGDqdG5N2/6BkoSd0S3lLFk61 CqhA== 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=Wm79BiQw3dFuwz6Eq2yTsgJb5zNbNEQiKSFWryofZ0o=; b=XpdLdCwt7mg8U5kyLAsm1hhusGy5QYxtJcyFog1aIADwfxRJZOBoNrH97iAmWA2DI6 SMBrSPMmliHx2NUFDDKIiTFYos6UERmoDAti6IWsGWg8fa+xaUPF20FjpPRp9mzSZEBl OOfEzlTMuiOpfZdI/h19I/y2k7nYHVCLRIJ/uZBTszFVhIKFLPjDm/Hqmx4B6NDy9/PK jJ8H42ZvDAeP0gY0zfBQpeoPDhpHMD18/fhhTtfw2qSxayrcWfWpOT4Wcksl46bndoy+ tjNzSD/HoUlGQj+Sa7lioSi2S/neUW0rhcH4VDnwvIT0lgSEgV8Hd19ZMOWhiAA1FCai XgiQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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=Wm79BiQw3dFuwz6Eq2yTsgJb5zNbNEQiKSFWryofZ0o=; b=LAJkaUydG6+dEQKtUCiVCUWkc4QSfdAlyuUdgCg+LvG5gRwuptraOc7k41tGvnzU7Z eXlUiK3Y+iv+z1K8DNY1O8AffSL28X8+GJ8+FzEz5H6DUZGksxsfHjWpxwqurGO9gjPc DJscVnQ9CzzKP0XgFU13FkM+BHdAGWOZDfGIyA5/YRe6SEJ8ujEhcWsodYyWPE9W/9JX aYBlwuCDldqk3OnqEYDWQgFP5XLwfPv/4/GVWfjyUUuw3q+nSkg0aAhU3LZOWeXRSDwZ baSu8aCclJfCvuDucxkQrBUe0kZhlDdPwlw/42kz9oCsKR4b2w9vjmsoH6NMudPdM5n+ HIHA== 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=Wm79BiQw3dFuwz6Eq2yTsgJb5zNbNEQiKSFWryofZ0o=; b=TWuXslfnpHTC+YqGW4pthifjgy9KqnMzFri+3yHMP4dwm/LpYZ+GDj4tMYPB3VpM2j ZkyDOLTQH1LqymA7ifgsMbA0bQ9uRx5KcoAODOfqM4UgjCYo8tLd0a5Egqjy4vtSNMub ZUNnVy1st/sNkkhr14n469il3/nwJClUb7I/wfXcsZUNzqJ3TCbfq4izUrwkSHrYkxKu fE5hbNG7Z5DmqZ8QYxYn8QTJbGDJCgFNz5zZMemwg7jE/i1cJb0Vx2gVVj4ouFWUHIos W+cgutdrIo3Urp3ON0lyu5MNBHu3MGV2US3172SDcqVzeM4t0epNTYBvtGrIzp37p+XH 0yiA== Sender: swupdate@googlegroups.com X-Gm-Message-State: ANhLgQ2GsC6KutzKKPPfwWb6+O3oGUmfutiA63Oaawmj5HWQrriFaCQo qPg5MXuEp2UBl6IdTVADaOs= X-Google-Smtp-Source: ADFU+vuaLHR8XTe0+GZyWXcu2lm+NyvZnK9OO64JU0EVLCCUDrOAUU2tlRMXV5e5Y7r+yhlrS1EpHw== X-Received: by 2002:a05:651c:1072:: with SMTP id y18mr9513856ljm.243.1583748953078; Mon, 09 Mar 2020 03:15:53 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a2e:8647:: with SMTP id i7ls1649352ljj.11.gmail; Mon, 09 Mar 2020 03:15:52 -0700 (PDT) X-Received: by 2002:a2e:804b:: with SMTP id p11mr2893994ljg.50.1583748952180; Mon, 09 Mar 2020 03:15:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583748952; cv=none; d=google.com; s=arc-20160816; b=n9SevvjxI4A1ohEicAoHX/8T2kN2tKYi+kvYGo2iKIloLB6PGBrD+tZixDMyRORGt8 Bsy08mN7q1XFiDIQrbmYw+l0tp+IQDA+mQalglLG2g53R45xXuMsXMPDYBQfMzdu+gGx KsUPq1jpwGzNYNBZU3wxS/BICJym1j96877Y+/WA5I896paWuJgmGdQy4NP7J3+neYwo 7cXGW2YsL+pjz983HRsCVzrnCuRcWrziE8gSXbZ+hnHusBVm4T8/sPY7djLI/CcZyOcu vMgib2F0UGEG0ggddBKRP7FkPNZ47NjJcHNH4NjxpzG4qz1Pnf8+Uv/baa0O5rpfmWhv MB2Q== 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=CsAJjWY0gt9yvhwPdxdGkL8OgpKJ0vax1vnu/8W3knw=; b=pRF7Zf5/Ib5qR1k1SUPlOyBnUvev5ijmnhPVw8ZNr0Te45Eh5lnUnDtHYV0Xuum8Gn ZLZU8KmcSpZD8U0h5lH20mN0pyzNLbQ3e+OiF3nLFwz9LtFWFryAOHMqRlWsmdRX2Xqh PzlNaqqUhZo9BmkV/IIEIVb6HM4vi0W6oIMJ0J9ovvFRsgMM34/uOGCBRLx/a6NJQxFC rElWYMuWbWtWebUkOVDXr1o7OvcbLEB4WA6hxSV0DsB/6AQGtfrg+QXPM+F5Oo/3uTe9 ZGbQsSnB6LZ4R8f8YmUzyH1ukyvK6i57Ph75oBU2xFSM3eAD2i7cxRlKnWvYNMY2NwLP o3TA== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de Received: from mail-out.m-online.net (mail-out.m-online.net. [212.18.0.9]) by gmr-mx.google.com with ESMTPS id p14si155199lfo.0.2020.03.09.03.15.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Mar 2020 03:15:52 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) client-ip=212.18.0.9; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 48bYyb4lYpz1qqkL; Mon, 9 Mar 2020 11:15:51 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 48bYyb4dLKz1qwyW; Mon, 9 Mar 2020 11:15:51 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id O5fdPa1LqY5X; Mon, 9 Mar 2020 11:15:50 +0100 (CET) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Mon, 9 Mar 2020 11:15:50 +0100 (CET) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id C0DD34540494; Mon, 9 Mar 2020 11:15:50 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([127.0.0.1]) by localhost (mail.babic.homelinux.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JrMZwTGAN42U; Mon, 9 Mar 2020 11:15:48 +0100 (CET) Received: from paperino.fritz.box (paperino.fritz.box [IPv6:2001:a61:513e:9501:3d60:f61:f29a:9d00]) by babic.homelinux.org (Postfix) with ESMTP id CABA845404D8; Mon, 9 Mar 2020 11:15:45 +0100 (CET) From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 2/5] Intercepts the output of shell scripts Date: Mon, 9 Mar 2020 11:15:39 +0100 Message-Id: <20200309101542.27373-3-sbabic@denx.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200309101542.27373-1-sbabic@denx.de> References: <20200309101542.27373-1-sbabic@denx.de> MIME-Version: 1.0 X-Original-Sender: sbabic@denx.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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: , Output of Shell scripts called by shell script's handler or as pre- and post install scripts is lost because it does not go into the common tracing facility in SWUpdate. SWUpdate checks the error code from the scripts, but important information to debug an error cause is simply lost. Rework how shell scripts are called to get the output: - a process is created for the script - stdout and stderr are redirected to a pipe - SWUpdate (parent process) reads the pipe, and use TRACE and ERROR to store output and errors from the scripts. Signed-off-by: Stefano Babic --- core/pctl.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 179 insertions(+), 11 deletions(-) diff --git a/core/pctl.c b/core/pctl.c index 134dd10..8b1c667 100644 --- a/core/pctl.c +++ b/core/pctl.c @@ -10,6 +10,7 @@ #include #include #include +#include #if defined(__linux__) #include #endif @@ -35,7 +36,7 @@ static int nprocs = 0; /* * The global pid is used to identify if context is - * the main process (SWUpdate, pid=0) or it is + * the main process (SWUpdate, pid=0) or it is * a child process. * This is required when using internal libraries and can be * decided between a direct call or (in case of child process) @@ -90,7 +91,6 @@ static int spawn_process(struct swupdate_task *task, int process_id; int sockfd[2]; - /* * Create the pipe to exchange data with the child */ @@ -116,7 +116,7 @@ static int spawn_process(struct swupdate_task *task, task->pipe = sockfd[0]; return 0; } - + /* Child closes [0] */ close(sockfd[0]); @@ -203,19 +203,187 @@ void start_subprocess(sourcetype type, const char *name, const char *cfgfile, start_swupdate_subprocess(type, name, cfgfile, argc, argv, start, NULL); } +/* + * run_system_cmd executes a shell script in background and intercepts + * stdout and stderr of the script, writing then to TRACE and ERROR + * This let the output of the scripts to be collected by SWUpdate + * tracing capabilities. + */ int run_system_cmd(const char *cmd) { int ret = 0; + int stdoutpipe[2]; + int stderrpipe[2]; + pid_t process_id; + int const PIPE_READ = 0; + int const PIPE_WRITE = 1; + int wstatus; + + if (!strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE)) + return 0; + + if (strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) > SWUPDATE_GENERAL_STRING_SIZE) { + ERROR("Command string too long, skipping.."); + /* do nothing */ + return -EINVAL; + } + + /* + * Creates pipes to intercept stdout and stderr of the + * child process + */ + if (pipe(stdoutpipe) < 0) { + ERROR("stdout pipe cannot be created, exiting..."); + return -EFAULT; + } + if (pipe(stderrpipe) < 0) { + ERROR("stderr pipe cannot be created, exiting..."); + close(stdoutpipe[0]); + close(stdoutpipe[1]); + return -EFAULT; + } + + process_id = fork(); + /* Child process, this runs the shell command */ + if (process_id == 0) { + if (dup2(stdoutpipe[PIPE_WRITE], STDOUT_FILENO) < 0) + exit(errno); + if (dup2(stderrpipe[PIPE_WRITE], STDERR_FILENO) < 0) + exit(errno); + /* close all pipes, not used anymore */ + close(stdoutpipe[PIPE_READ]); + close(stdoutpipe[PIPE_WRITE]); + close(stderrpipe[PIPE_READ]); + close(stderrpipe[PIPE_WRITE]); + + ret = execl("/bin/sh", "sh", "-c", cmd, (char *)NULL); + if (ret) { + ERROR("Process %s cannot be started: %s", cmd, strerror(errno)); + exit(1); + } + } else { + int fds[2]; + + close(stdoutpipe[PIPE_WRITE]); + close(stderrpipe[PIPE_WRITE]); + + fds[0] = stdoutpipe[PIPE_READ]; + fds[1] = stderrpipe[PIPE_READ]; - if ((strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) > 0) - && (strnlen(cmd, SWUPDATE_GENERAL_STRING_SIZE) < SWUPDATE_GENERAL_STRING_SIZE)) { - DEBUG("Running %s command...", cmd); - ret = system(cmd); - if (WIFEXITED(ret)) { - TRACE("%s command returned %d", cmd, WEXITSTATUS(ret)); + /* + * Now waits until the child process exits and checks + * for the output. Forward data from stdout as TRACE + * and from stderr (of the child process) as ERROR + */ + do { + pid_t w; + int n1 = 0; + struct timeval tv; + fd_set readfds; + int n, i; + char buf[2][SWUPDATE_GENERAL_STRING_SIZE]; + char *pbuf; + int cindex[2] = {0, 0}; /* this is the first free char inside buffer */ + + w = waitpid(process_id, &wstatus, WNOHANG | WUNTRACED | WCONTINUED); + if (w == -1) { + ERROR("Error from waitpid() !!"); + close(stdoutpipe[PIPE_READ]); + close(stderrpipe[PIPE_READ]); + return -EFAULT; + } + + /* + * Use buffers (for stdout and stdin) to collect data from + * the cmd. Data can contain multiple lines or just a part + * of a line and must be parsed + */ + memset(buf[0], 0, SWUPDATE_GENERAL_STRING_SIZE); + memset(buf[1], 0, SWUPDATE_GENERAL_STRING_SIZE); + + tv.tv_sec = 1; + tv.tv_usec = 0; + + /* Check if the child has sent something */ + do { + FD_ZERO(&readfds); + FD_SET(fds[0], &readfds); + FD_SET(fds[1], &readfds); + + n1 = 0; + ret = select(max(fds[0], fds[1]) + 1, &readfds, NULL, NULL, &tv); + if (ret <= 0) + break; + + for (i = 0; i < 2 ; i++) { + pbuf = buf[i]; + int c = cindex[i]; + + if (FD_ISSET(fds[i], &readfds)) { + int last; + + n = read(fds[i], &pbuf[c], SWUPDATE_GENERAL_STRING_SIZE - c); + if (n < 0) + continue; + n += c; /* add previous data, if any */ + n1 += n; + if (n > 0) { + + /* check if just a part of a line was sent. In that + * case, search for the last line and then copy the rest + * to the begin of the buffer for next read + */ + last = n - 1; + while (last > 0 && pbuf[last] != '\0' && pbuf[last] != '\n') + last--; + pbuf[last] = '\0'; + /* + * compute the truncate line that should be + * parsed next time when the rest is received + */ + int left = n - 1 - last; + char **lines = string_split(pbuf, '\n'); + int nlines = count_string_array((const char **)lines); + + if (last < SWUPDATE_GENERAL_STRING_SIZE - 1) { + last++; + memcpy(pbuf, &pbuf[last], left); + if (last + left < SWUPDATE_GENERAL_STRING_SIZE) { + memset(&pbuf[left], 0, SWUPDATE_GENERAL_STRING_SIZE - left); + } + cindex[i] = left; + } else { /* no need to copy, reuse all buffer */ + memset(pbuf, 0, SWUPDATE_GENERAL_STRING_SIZE); + cindex[i] = 0; + } + + for (unsigned int index = 0; index < nlines; index++) { + switch (i) { + case 0: + TRACE("%s", lines[index]); + break; + case 1: + ERROR("%s", lines[index]); + break; + } + } + + free_string_array(lines); + } + } + } + } while (ret > 0 && n1 > 0); + } while (!WIFEXITED(wstatus)); + + close(stdoutpipe[PIPE_READ]); + close(stderrpipe[PIPE_READ]); + + if (WIFEXITED(wstatus)) { + ret = WEXITSTATUS(wstatus); + TRACE("%s command returned %d", cmd, ret); } else { - ERROR("%s command returned %d: '%s'", cmd, ret, strerror(errno)); - return -1; + TRACE("(%s) killed by signal %d\n", cmd, WTERMSIG(wstatus)); + ret = -1; } } From patchwork Mon Mar 9 10:15:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1251419 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=209.85.221.57; helo=mail-wr1-f57.google.com; envelope-from=swupdate+bncbcxploxj6ikrbwnotdzqkgqemxmwawi@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=LM5ZSdes; dkim-atps=neutral Received: from mail-wr1-f57.google.com (mail-wr1-f57.google.com [209.85.221.57]) (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 48bYzr0mX1z9sPJ for ; Mon, 9 Mar 2020 21:16:55 +1100 (AEDT) Received: by mail-wr1-f57.google.com with SMTP id u8sf4970608wrq.13 for ; Mon, 09 Mar 2020 03:16:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1583748953; cv=pass; d=google.com; s=arc-20160816; b=f+coMBDdSWWIKHx1UFx3C0aBjmzl2jISO0iVNhG8MrhRsE0a+EkKAbPdEM7QnYcg3L Xzk+70qysKM6x7owJbcHq5jj3F0ta3/riCeAuHsPAfVgjdGii2o6592H4RZ3sj9Pos6J +CtfH/9WA4C6zedGUZ7yCKaPLLzIuthL8jBAEEvn3RCb+uRNrlt6gIV+N82wCO5UVul0 2yLhEmLoE6uXQ7rXNgfZ03//6k2NAV7rzEKOF1ke5DsIeUuiiIgxalpTyRilGFYtl2E4 NfJUkUN0qmQlDMM2K9TVV62yZkAw8RNJ6d+YZmkBnruS1F3j2Z/xSNXUtS6mPm3hVv9L 0l/A== 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=yE42acpdXSq0lD57i1bzwkZ1qlfsWMLZGRCru1MewVY=; b=OyKGlg1wMJ4132kXRfSuDaPj2QpHBdwRrgU5eJWjx0yVpK6QhToGAhClqJj9Q5lKZN rWh600en3K1qoIbMJaA3dc8ZHRABMsarV8tFMBBReNEUH6nqeNQQgQRgJ5EMTTCxzIk+ BB0EDxkTiSgiwKeWPTn2vLGOf3ryr1WF+4Co/6CScxbpfZSYRW3XKtjAuBErPGwJFMze xv5djA9g8O+ZAL6pgwLL7Ar2e14dqQx1E3IqaDtd66Z2P+VZ4C0AVaNGJPnDFP+sdy5g 5OvasnRoCZBTIxQ+A+CM1bvTPk0clOk4ufyI7BhWb2YcGgO3W9U5Bnzy401CK9BV+onh Ku/g== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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=yE42acpdXSq0lD57i1bzwkZ1qlfsWMLZGRCru1MewVY=; b=LM5ZSdes77JdtAyb0HuTM8NWWS4XqgSQmuvikFyov/SGQbrv5eY0pjc97qrf0IB4eK XhdXjnI1NUGaYftGFXvGrTUpiAHoL3akcfMaBj8BEoaXS89SzSGyAEVp6d2DwaX1Qle/ Yr922JmRnG3AKUHlq2dE3jEvr1XLqTudWT1bUGZzIJ5SyXdd5BkiDck4wsmOaVM3TE+Z b9Liad61mQi4uYxdqC6w1k3KajqlTkXHlYT0l1ROl8k4UAEjN56GsaeTzm/vhz3TLaj/ wqHCc/o2l4uivnT/PkceoIP10Qta1XDRvKyJL+w8b+zPpxFXr5JRShWcDYRUjxgj2yHv NdVQ== 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=yE42acpdXSq0lD57i1bzwkZ1qlfsWMLZGRCru1MewVY=; b=naJzWt1RVNnI1WNscojeT9fc4l/NXSGuniFmNRTAtcx6QEzQ6Ore5J7QXxJgb15kom s0pu6UY6vjCz5Xu3wfKDCT/8oqbZ5xQXzqYDF8ouocGe7d2fCdgVCawgb4TWx1DEjxYB AF4/8cVk8HbmuviFUGfYCz1Kl0ejwfGjguYevAhG2RGRNandnsnzKx+8KD9JoOox/pDv 2QuhINy8C0BrKnIxoGk4MOrAwm84mIW1wKmnV0nDvuWh0H/bJqNrWuLonniwZM/jdfXt uhsn9hP3jINkoFfzX7XodU1RS7izns/j41NPvCrZ6STQROJE+4ezYU15nQrqt4YXT6Il nUVQ== Sender: swupdate@googlegroups.com X-Gm-Message-State: ANhLgQ0vYNfW/SxRdW82uIiRmAyWK/XasrcTKY2xoV3LGn0x5CxSEs9C b8dZhXLKmbyE7Ff52rPD/u4= X-Google-Smtp-Source: ADFU+vuSeh2m4Xz+0HuDjXGWclbQKVdCXZIjn0HSIPSQSGtNVGAJTZNG/eG48TE5ih7q6MzaXAosRA== X-Received: by 2002:a7b:cc6a:: with SMTP id n10mr19483289wmj.170.1583748953528; Mon, 09 Mar 2020 03:15:53 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:adf:a799:: with SMTP id j25ls3629163wrc.7.gmail; Mon, 09 Mar 2020 03:15:52 -0700 (PDT) X-Received: by 2002:a5d:6588:: with SMTP id q8mr3416227wru.170.1583748952755; Mon, 09 Mar 2020 03:15:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583748952; cv=none; d=google.com; s=arc-20160816; b=R6eBrwtulSWxZcr/fvUqZUA8+NDpZZWegKGhHlSRbvE+T4RPNcLz897CkM6KQ8KKEJ r+O/mjGrIC/YeVwvg3zyJdfRtGb0N4AUbzAFWYiGFQx50kdAJ0UlQp3bfbMTmQn+ToTd JlvYNjwvraX9VFo6I5QQygmpj90hd87a1CzwMpEktCc6zUARRkqmymVFjdasNPIukN9D iL37I2/cSu0mI3Ieet7t800o/9bE3lf6l+JuX6L0qCYkG/LeB+AmVXyK8+EIlD92811h yvg1nqo8/gZeoi9O2E6SynAdGoSaITAIAJpMlEx9KrQHvAKYe2EQHbdx63oyZts+8vG3 zHwQ== 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=TxvInokNHg6/G0lRsCBqZ1bZ9GfxPX9AGExU/nxGJUE=; b=iGjYqnnvHhQfJQnRXLRITHpEWHqVQLZxs6eQoz2eEX1WccA2MSjd3FxGK6veo326o0 ZiQfKQ/hkzXYXcM3n5gcaeZL1/8kQKwfFj4WKcWFmvkt6o5SAlP2zxebiCtNLt03vKXB OAPtJLhD3+74w64HW4yhIUjggEJpQiS9kCLhFQt497yjOcU8Gct4hK0gaSS9GvNBC5hN +d3oR4ozAecy0xo/mFOdABv2iuR1QIpxVXHKca7IFpnxTTmxweczEAo0Ex/FSEKz4/x6 2QGSSpxnS+sROt2krvDgANEyxWIk02gLXSLIywNNdr9bSq9aAGY99OtoYyg3uHuTmJju ocvw== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de Received: from mail-out.m-online.net (mail-out.m-online.net. [212.18.0.9]) by gmr-mx.google.com with ESMTPS id n7si372833wmc.1.2020.03.09.03.15.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Mar 2020 03:15:52 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) client-ip=212.18.0.9; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 48bYyc44lnz1qqkL; Mon, 9 Mar 2020 11:15:52 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 48bYyc3ttbz1qwyW; Mon, 9 Mar 2020 11:15:52 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id ls9he7ldv7qI; Mon, 9 Mar 2020 11:15:51 +0100 (CET) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Mon, 9 Mar 2020 11:15:51 +0100 (CET) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id 5C99645401BA; Mon, 9 Mar 2020 11:15:52 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([127.0.0.1]) by localhost (mail.babic.homelinux.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ntLM5dm_sjRS; Mon, 9 Mar 2020 11:15:49 +0100 (CET) Received: from paperino.fritz.box (paperino.fritz.box [IPv6:2001:a61:513e:9501:3d60:f61:f29a:9d00]) by babic.homelinux.org (Postfix) with ESMTP id E8B6B45405E9; Mon, 9 Mar 2020 11:15:45 +0100 (CET) From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 3/5] IPC: add get_status_timeout Date: Mon, 9 Mar 2020 11:15:40 +0100 Message-Id: <20200309101542.27373-4-sbabic@denx.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200309101542.27373-1-sbabic@denx.de> References: <20200309101542.27373-1-sbabic@denx.de> MIME-Version: 1.0 X-Original-Sender: sbabic@denx.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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: , Add a not blocking function (get_status_timeout) to retrieve notification about an update. This allows to not block while reading the output from the installer. Signed-off-by: Stefano Babic --- include/network_ipc.h | 1 + ipc/network_ipc.c | 54 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/include/network_ipc.h b/include/network_ipc.h index d99b30f..844f2d0 100644 --- a/include/network_ipc.h +++ b/include/network_ipc.h @@ -76,6 +76,7 @@ int ipc_inst_start_ext(sourcetype source, size_t len, const char *info, bool dry int ipc_send_data(int connfd, char *buf, int size); void ipc_end(int connfd); int ipc_get_status(ipc_message *msg); +int ipc_get_status_timeout(ipc_message *msg, unsigned int timeout_ms); int ipc_postupdate(ipc_message *msg); int ipc_send_cmd(ipc_message *msg); diff --git a/ipc/network_ipc.c b/ipc/network_ipc.c index bcdcfad..382f1d0 100644 --- a/ipc/network_ipc.c +++ b/ipc/network_ipc.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "network_ipc.h" #include "compat.h" @@ -98,9 +99,11 @@ int ipc_postupdate(ipc_message *msg) { return 0; } -static int __ipc_get_status(int connfd, ipc_message *msg) +static int __ipc_get_status(int connfd, ipc_message *msg, unsigned int timeout_ms) { ssize_t ret; + fd_set fds; + struct timeval tv; memset(msg, 0, sizeof(*msg)); msg->magic = IPC_MAGIC; @@ -109,14 +112,51 @@ static int __ipc_get_status(int connfd, ipc_message *msg) if (ret != sizeof(*msg)) return -1; - ret = read(connfd, msg, sizeof(*msg)); - if (ret <= 0) + if (!timeout_ms) { + ret = read(connfd, msg, sizeof(*msg)); + } else { + FD_ZERO(&fds); + FD_SET(connfd, &fds); + + /* + * Invalid the message + * Caller should check it + */ + msg->magic = 0; + + tv.tv_sec = 0; + tv.tv_usec = timeout_ms * 1000; + ret = select(connfd + 1, &fds, NULL, NULL, &tv); + if (ret <= 0 || !FD_ISSET(connfd, &fds)) + return 0; + ret = read(connfd, msg, sizeof(*msg)); + } + return ret; +} + +int ipc_get_status(ipc_message *msg) +{ + int ret; + int connfd; + + connfd = prepare_ipc(); + if (connfd < 0) { return -1; + } + ret = __ipc_get_status(connfd, msg, 0); + close(connfd); - return 0; + if (ret > 0) + return 0; + return -1; } -int ipc_get_status(ipc_message *msg) +/* + * @return : 0 = TIMEOUT + * -1 : error + * else data read + */ +int ipc_get_status_timeout(ipc_message *msg, unsigned int timeout_ms) { int ret; int connfd; @@ -125,7 +165,7 @@ int ipc_get_status(ipc_message *msg) if (connfd < 0) { return -1; } - ret = __ipc_get_status(connfd, msg); + ret = __ipc_get_status(connfd, msg, timeout_ms); close(connfd); return ret; @@ -224,7 +264,7 @@ int ipc_wait_for_complete(getstatus callback) fd = prepare_ipc(); if (fd < 0) break; - ret = __ipc_get_status(fd, &message); + ret = __ipc_get_status(fd, &message, 0); ipc_end(fd); if (ret < 0) { From patchwork Mon Mar 9 10:15:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1251423 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=209.85.208.183; helo=mail-lj1-f183.google.com; envelope-from=swupdate+bncbcxploxj6ikrbwvotdzqkgqebzpjosa@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=jRFOMx9J; dkim-atps=neutral Received: from mail-lj1-f183.google.com (mail-lj1-f183.google.com [209.85.208.183]) (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 48bYzz4HSWz9sPJ for ; Mon, 9 Mar 2020 21:17:03 +1100 (AEDT) Received: by mail-lj1-f183.google.com with SMTP id y7sf1423391ljm.13 for ; Mon, 09 Mar 2020 03:17:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1583748955; cv=pass; d=google.com; s=arc-20160816; b=PkeeHLJj1Nm2KQX6MGLXPPx96QzSFuEY5r8Pyc6b6h8nlgt2PHwgrU1VFwm8V4Ttkg jM03C/w8M1y0lDm2BCaGctADW0k8m3Nr92FWF+brCDaMigiVvxF6MZsU3KzbhucKY1IF f2nVzqLQIu6gY4/O9Jb5ZK5J34zYkd8i7jXuMephWNwpl3rzNu57NzSaT1NDxPeHaxE3 J5Qq7WkOrlBUaz7Dh0bZnfOtLbNZWELe3C++yelhgLPmvqWTHGNc231hbetxnCxxMr72 HNTmU4Kt3qo6xb9Y0S1IlYuQeFSe8SuX+/JbY7Wo9zdL/ElpXTpd2CvKp38k6JeU9DFF zLHQ== 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=Ad/fdHEuSjkNv8wITwRJrkEX5NvyV8gVt1V1Ru2rAr4=; b=CPAJkdgTPFJMByAb3uUezlFdcHmbsLYFb38niKz21vxpgKn60rSJ1dB2NIiVJDFGcw aCBw3hODeqJFz5kynIm5/eKOgLBCPiC46sdiwudrxZP9gWgE4PR/RBrHEZhVL+p/w30b cw1sMgXOdic5t/8PktKJDnzuqLZSTwv3m/g6d/qHF+FU2R1aIpfOTcbOSVp4pVoLOX/K U/AotndHMNhJ+fslh8h2q4Lmj9gqRCI/cph81W/yLkQ0AbxLPrIvV0FECBwZW8jpB8q4 xlEoZDSinlCkybHUdUtod8KXPmRfw2ies5gqzjTCy2Jx2fJKrDufv/wn5YY4u0hO4o6c rGqw== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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=Ad/fdHEuSjkNv8wITwRJrkEX5NvyV8gVt1V1Ru2rAr4=; b=jRFOMx9JE2uE3UeNQS45wp/iJuVy3kJ2u6PGT3MtW4nPCbJjarJwAw6Z/xIhZHSNDH DKf+Q4OqjRNaj810W0mfVjeq8lpirWcquDD9wAHfGT+JsP/nBQ4+WkH/jPwAL3DVk5W1 YReaIzYAM/m+UJKe3DXgLz02NLSMEuxOrkvP3ac1N7gUDv0geoWohmw/0wji9uD+p8xk D1YrDHdymNcPnUYXEpgVy33e5jOq/K3c7FBaApT4/GalQrVPG/oBV2skGGg1LW63fjLV 5f9pXPqZPa2DFJNFId4LOIeE93nwVAbYKkcJh08gEVoWS7PUIOQVS78KHlmC+ruAkV/Y xnXw== 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=Ad/fdHEuSjkNv8wITwRJrkEX5NvyV8gVt1V1Ru2rAr4=; b=I5oEJ8krFIvwTiNH4sRzjMm7krR8wGMJQaszRKWod9t8m0QmMfpMoCy9E9cOy/GSny 13sP/bm+IXwuHARDeIfqQLE8+/3Sr4Yial5ppXnnh/Myj4vNTlCx4qNZHWEH260C741s jSE/IPfK6VtRmZ14VsrY0SZ7lizopo3ft7rp78zONo2X/eOncO9oGlSswn5stBSVyxQZ TFKJDQ/d/03HR5YeoAHsO7yYlLBUNZwry/+rXo0h+WRKHmaCSrJ3I6lnrtClR8w+KBwt Oj/SuDQY7tv0OKSgIW/6mUAwmH0PMhZUWBkySCbS9jM+EDxPyXUCftxfDjsFiShgNBBa 3D2w== Sender: swupdate@googlegroups.com X-Gm-Message-State: ANhLgQ0ukn8hOWYBaTk8bvN7ZM+Nk4bKNSRw7o7o/KWHyE4YRSseo+w1 mYJtHuMfHJ20fqFkF5zdrEw= X-Google-Smtp-Source: ADFU+vuwLo97N9J3qx1km9IlxorIZ1BJzahxV20PWrslD6uObQ9ZjjWITKvbIelJ9Zi0787D/rKSmA== X-Received: by 2002:a05:6512:3e5:: with SMTP id n5mr9038080lfq.55.1583748955152; Mon, 09 Mar 2020 03:15:55 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:ac2:5096:: with SMTP id f22ls1218164lfm.2.gmail; Mon, 09 Mar 2020 03:15:54 -0700 (PDT) X-Received: by 2002:a19:c20b:: with SMTP id l11mr9013421lfc.135.1583748954372; Mon, 09 Mar 2020 03:15:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583748954; cv=none; d=google.com; s=arc-20160816; b=yWUhN6wLZieET6jIuCjYM4dA652hsZDsusHKgUytWikJ3Z2UYxgtqGmp2cwbsz6Buu 3RiZhAGrm8WLu1rhjGBgoo58Ui697Hm1jQ3F+TzD6jaIp/E89yrsq2bWtRE2gKfU+xh4 1RGdfWk4BClM0nG9EdCEMVPYafM3yDcRmEf9fQ2kAj+0BweLpGcvUdLqKhLM/pPoi+XS Q32A2wNRzCHkjyqLNsYCnXltqzacXug+6W070+GMf8T/C0dnNj+0xQwQBQoc5rqjk4HD G5JqJw63n3olyu8OMdvuNoV1XhpRnr9W3gi1thnT+FOry342+wBPZoOrSfwjFptgKVj0 VX/Q== 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=/2HS4OrLR1SYU/Tt1l+h/nDf8xgoeBmfgd1EZytBeu4=; b=NRs0YuR9PRxELSkfmGqZayEJn2+ZARAu0xf+/nPkSjIXmpEIhG6hMAEMPa7ElNVyX/ uDRLvOoQO/if/Ko1JwFEU2bAwmYb6sCFQ1C6EfHpM62oXrMCHSjYL2QCYUJqT/7MXZ9V /EayvVJM2yok6b32kYmcU2LtpoKtcq+rUNh5hk/jRl7zL/+jApQSPj7eHJHeDOo7IeUS KGE4rKJ/jJs6+khgbJHIg2zIw3WPrDxtBfieQmfnZpy+LVaJJZOuUCltKgSjEvxcpiQ1 gSicvueBNRlS59toUC6SRfmWpXP7N3J2nRAmOWHMGt+7HDQLYSPc3voSHcBcppsQg/Tj e9wQ== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de Received: from mail-out.m-online.net (mail-out.m-online.net. [212.18.0.10]) by gmr-mx.google.com with ESMTPS id p14si155207lfo.0.2020.03.09.03.15.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Mar 2020 03:15:54 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) client-ip=212.18.0.10; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 48bYyd65c1z1rpF3; Mon, 9 Mar 2020 11:15:53 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 48bYyd5xPyz1qwyg; Mon, 9 Mar 2020 11:15:53 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id W8MMgKs-TG3N; Mon, 9 Mar 2020 11:15:53 +0100 (CET) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Mon, 9 Mar 2020 11:15:53 +0100 (CET) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id 87AE745401BA; Mon, 9 Mar 2020 11:15:53 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([127.0.0.1]) by localhost (mail.babic.homelinux.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1sEGI0mHkjQ5; Mon, 9 Mar 2020 11:15:50 +0100 (CET) Received: from paperino.fritz.box (paperino.fritz.box [IPv6:2001:a61:513e:9501:3d60:f61:f29a:9d00]) by babic.homelinux.org (Postfix) with ESMTP id 066F64540601; Mon, 9 Mar 2020 11:15:46 +0100 (CET) From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 4/5] Factorize snescape function Date: Mon, 9 Mar 2020 11:15:41 +0100 Message-Id: <20200309101542.27373-5-sbabic@denx.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200309101542.27373-1-sbabic@denx.de> References: <20200309101542.27373-1-sbabic@denx.de> MIME-Version: 1.0 X-Original-Sender: sbabic@denx.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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: , Function escape double quote from a string - used by mongoose to send JSON formatted text to the browser and can be reused by other subsystems. Signed-off-by: Stefano Babic --- core/util.c | 25 +++++++++++++++++++++++++ include/util.h | 1 + mongoose/mongoose_interface.c | 24 ------------------------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/core/util.c b/core/util.c index 3c7db79..c2ac822 100644 --- a/core/util.c +++ b/core/util.c @@ -754,3 +754,28 @@ int swupdate_file_setnonblock(int fd, bool block) return fcntl(fd, F_SETFL, flags); } +/* Write escaped output to sized buffer */ +size_t snescape(char *dst, size_t n, const char *src) +{ + size_t len = 0; + + if (n < 3) + return 0; + + memset(dst, 0, n); + + for (int i = 0; src[i] != '\0'; i++) { + if (src[i] == '\\' || src[i] == '\"') { + if (len < n - 2) + dst[len] = '\\'; + len++; + } + if (len < n - 1) + dst[len] = src[i]; + len++; + } + + return len; +} + + diff --git a/include/util.h b/include/util.h index 906be9a..b5af974 100644 --- a/include/util.h +++ b/include/util.h @@ -189,6 +189,7 @@ char **splitargs(char *args, int *argc); char *mstrcat(const char **nodes, const char *delim); char** string_split(const char* a_str, const char a_delim); char *substring(const char *src, int first, int len); +size_t snescape(char *dst, size_t n, const char *src); void freeargs (char **argv); int get_hw_revision(struct hw_type *hw); void get_sw_versions(char *cfgfname, struct swupdate_cfg *sw); diff --git a/mongoose/mongoose_interface.c b/mongoose/mongoose_interface.c index 55ebad2..4510d48 100644 --- a/mongoose/mongoose_interface.c +++ b/mongoose/mongoose_interface.c @@ -106,30 +106,6 @@ static const char *get_source_string(unsigned int source) return str[source]; } -/* Write escaped output to sized buffer */ -static size_t snescape(char *dst, size_t n, const char *src) -{ - size_t len = 0; - - if (n < 3) - return 0; - - memset(dst, 0, n); - - for (int i = 0; src[i] != '\0'; i++) { - if (src[i] == '\\' || src[i] == '\"') { - if (len < n - 2) - dst[len] = '\\'; - len++; - } - if (len < n - 1) - dst[len] = src[i]; - len++; - } - - return len; -} - static void restart_handler(struct mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; From patchwork Mon Mar 9 10:15:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1251421 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=209.85.128.62; helo=mail-wm1-f62.google.com; envelope-from=swupdate+bncbcxploxj6ikrbxfotdzqkgqeciqn2yi@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=djgIJf+c; dkim-atps=neutral Received: from mail-wm1-f62.google.com (mail-wm1-f62.google.com [209.85.128.62]) (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 48bYzx6hkmz9sPg for ; Mon, 9 Mar 2020 21:17:01 +1100 (AEDT) Received: by mail-wm1-f62.google.com with SMTP id i16sf2329799wmd.9 for ; Mon, 09 Mar 2020 03:17:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1583748956; cv=pass; d=google.com; s=arc-20160816; b=JuEaYSCzs9PuC6xFibLZnlsoOQB8EBO+yjPM3kECxTjYkjdylyE3rY4g5/NQxNBQQG 0d7IxB8rXf0/iC//+KjMFrWPMuzJPjO//9bpI1tfxaA7NzFESnvoHztkaSvPGCjIIulr 50fbvlrdUUvKP0X1Khqr6zOnVYukUgkPUpItDdg0snRptyhIw87yQXs4d7D0spmO4n4H NAsvPav+A+Dm66O5FjwLlGJXeslcKOiYdUIUKP7rtjr3LKKN8HaRbPU9ks+ceBnIK6cg gkmLU3hYb2GVQ3R4yyYWsFMuO8Iv68XcQhE8ZUdpqskKpbURrOJYbPUIGa19mOVgTsLC 4XnQ== 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=BFK/aNS3w4lGPWxbwkU/ZkuP960TPnraWWTypvSXp08=; b=kN3AWDAirTohsiCET3mTbByzKHp8JIV2Vk+cT+VULIgwNb1HJsMjfCs08k5EmO07bI Fmp9YI9qJqR5YvlaOjcdvhBv2ZdsTnWIlw8/zbCxGuOoS6b4PRe60sJCFdOxpzdo1H12 vZ0w0uic8F5TxNZtTJtu4Yj+QZdHQeLPgCZvORXtg6jZz0Q1oWlyt5dsG2UvnDQuExmZ FrHluUH2gDwn/nv2ksciMiUEayASFnwT70I+HGOcJ6wSxMqSJPyp+xo0GVs0TMcAw1Wb PjfHkPOHTnVVBlo0pWyxeGTE6+Em3njpRAQLIDlxX+4lgp2VZ0i1vcDK4O5WQ7FSyuTJ JKGQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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=BFK/aNS3w4lGPWxbwkU/ZkuP960TPnraWWTypvSXp08=; b=djgIJf+cwyZi0q956STkJPdNVGtFa8rV+WpyakIXdfXSsjvTGsJaSiereq5+/8Z8SJ WikJVX8naQrA3Tq2sEeQoHNw9De17juEJ0rOYBLI9g8soPpojSYBiuQ66OoX5f/cBTI0 gmlqXbFDgJPjn4u7IXj9OsUqJ7ukPAG+VKJeyt26Ojld//qGNZvrP1qsbFenkmddmljC YcD64sDHsuFqa6/Vyhdh6rUgre0MFt1DkvuHVKlcaSPF+TD8khK5GoF9Y7jkq1xC8lUj 4ayXFkSyxUsaGjb5xh3ndqTh3MXWLw99kGjzaLaidw6sOeynLjzvtMfjwkLZ0oAnWzuf 1fsQ== 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=BFK/aNS3w4lGPWxbwkU/ZkuP960TPnraWWTypvSXp08=; b=mUeZWcLIW3x2tHjIEviXuFb5iZfhrCCcYc/7bPaOPLPVTNz1/vc3E4oGXDCiosFmdm Yfe6f+pYYNDU/xrHex59yfVspafUEfqZc3gqNGJEoX1TR1RDZ1N2y+7b/yqqzPfqkzNc m8c/mgOEm7dJtqkl/FEkwtkku63cAVO+OuR/k4pzAB25UQASFXx7qzgdYuA3QrTvK42/ j4rPrWH1rw/M30uLIOoRn6bW2SMB6umry5oFf8Tuc3CjpTmJnQOzkDttakTrQPMkJpAX HP8GAB+o1MO82WV3E77gXzRLg2oXZJxWiBnp0X56Y1y1C/4HTFaf8xD/9iwoHeN20FAl qi2Q== Sender: swupdate@googlegroups.com X-Gm-Message-State: ANhLgQ1aWhxK5hwZDzUZRcwI4O5r7JIvKK4K/1urkbm6rOgyUOn14KxR shsKc2UsL6SevhBZOX/JPUM= X-Google-Smtp-Source: ADFU+vugPUV9xZVMLiypER9WOnhKGWn6GQbZUWpnyiRfc4dg077+80swmEle1MOs0x7x7HNFk9XYQw== X-Received: by 2002:adf:df0f:: with SMTP id y15mr2319470wrl.420.1583748956652; Mon, 09 Mar 2020 03:15:56 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a5d:4d42:: with SMTP id a2ls5346972wru.9.gmail; Mon, 09 Mar 2020 03:15:55 -0700 (PDT) X-Received: by 2002:a5d:628c:: with SMTP id k12mr20373091wru.237.1583748955897; Mon, 09 Mar 2020 03:15:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583748955; cv=none; d=google.com; s=arc-20160816; b=Je+EkLlPAVA2kb/E2gTqOKBmhE2wrn5CGTeNSaTzD8WAHjItMA4KmUR2DfMgeqRbjc 2fhJUme5qVL+fnceqpwrbrQmQVwnR/IdNcI/U6xiCHMB+d3sRGEDblbOyDEBvJCX9B0u +4qr8oMHN0dArDUNAl1EypTEKWpSocpIvdgsxu71qcIjTsUt66h3KROy56vtzQv1vqla WpkXWyD2upPWPRvhUskSNeLgFQEgdkD8e1OxcEHJsStQ6/YFsES3aMVoBt2J5Ikiibit DQWr4BW0yS6YIMowVdC26xg7lTaeH69ezJMXE2o+njEaWi+53Fh9tZSkj0CNeBHVUyJG Dgpg== 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=i43uE0ukvmXd4oEFq7tojvhh9yZ3iKM2Fn8jyfVhMjw=; b=pU4ZiR+7jsgkQ1nqTJrt9uSYMnR3i26WyEADOBCf2uV/5/QGiAYZCOVslSyQRjFYhs 2umZ+H26UrFGdRnoMhUIWmkWZriTEPAAeNmzetdhIbwlVvJy0kPSL016OMM6JPzOR/1G IomL3nDzBjoQ1ddn9+C8riRvtZc5tKw9fY2pjZz118QDjbVkfHvOFVa9W9SAJyLCz+s6 xVlAts6Le1VSjjYR3QAVNpBC3Z6cqQX1s8dRr5xei32KhpzaaJBOubxATR5GPmmLIZNn KoePI8Mul2vsEr4HRQMq7sUitMFOgD4rqF2jqU2crGVUFNmcCTLNENXmc/RKRzTC1QK4 v+WQ== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de Received: from mail-out.m-online.net (mail-out.m-online.net. [212.18.0.10]) by gmr-mx.google.com with ESMTPS id i19si649239wml.0.2020.03.09.03.15.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Mar 2020 03:15:55 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) client-ip=212.18.0.10; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 48bYyg57Nvz1rpFQ; Mon, 9 Mar 2020 11:15:55 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 48bYyg4wkqz1qwyf; Mon, 9 Mar 2020 11:15:55 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id LT9d5ja1E3dH; Mon, 9 Mar 2020 11:15:54 +0100 (CET) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Mon, 9 Mar 2020 11:15:54 +0100 (CET) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id 1256245401BA; Mon, 9 Mar 2020 11:15:55 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([127.0.0.1]) by localhost (mail.babic.homelinux.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fDK4TT-i6346; Mon, 9 Mar 2020 11:15:52 +0100 (CET) Received: from paperino.fritz.box (paperino.fritz.box [IPv6:2001:a61:513e:9501:3d60:f61:f29a:9d00]) by babic.homelinux.org (Postfix) with ESMTP id 1E138454063B; Mon, 9 Mar 2020 11:15:46 +0100 (CET) From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 5/5] hawkbit: send log of the update to the server Date: Mon, 9 Mar 2020 11:15:42 +0100 Message-Id: <20200309101542.27373-6-sbabic@denx.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200309101542.27373-1-sbabic@denx.de> References: <20200309101542.27373-1-sbabic@denx.de> MIME-Version: 1.0 X-Original-Sender: sbabic@denx.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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: , No log is sent to the server - in case of failure, the reson is stored on the device and cannot be seen on the server. This makes difficult to identify the cause of failure. Add a temporary thread that collects all traces and errors and sends them to the Hawkbit server. The thread exists for the time a SWU is installed, and then exits gracefully. Signed-off-by: Stefano Babic --- suricatta/server_hawkbit.c | 151 ++++++++++++++++++++++++++++++++++--- 1 file changed, 141 insertions(+), 10 deletions(-) diff --git a/suricatta/server_hawkbit.c b/suricatta/server_hawkbit.c index e08cdaa..1d98c55 100644 --- a/suricatta/server_hawkbit.c +++ b/suricatta/server_hawkbit.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "suricatta/suricatta.h" #include "suricatta_private.h" #include "parselib.h" @@ -50,6 +51,7 @@ static struct option long_options[] = { {NULL, 0, NULL, 0}}; static unsigned short mandatory_argument_count = 0; +static pthread_mutex_t notifylock = PTHREAD_MUTEX_INITIALIZER; /* * See Hawkbit's API for an explanation @@ -95,7 +97,8 @@ void server_print_help(void); server_op_res_t server_set_polling_interval_json(json_object *json_root); server_op_res_t server_set_config_data(json_object *json_root); server_op_res_t -server_send_deployment_reply(const int action_id, const int job_cnt_max, +server_send_deployment_reply(channel_t *channel, + const int action_id, const int job_cnt_max, const int job_cnt_cur, const char *finished, const char *execution_status, int numdetails, const char *details[]); server_op_res_t server_send_cancel_reply(channel_t *channel, const int action_id); @@ -347,8 +350,11 @@ static char *server_create_details(int numdetails, const char *details[]) char *prev = NULL; char *next = NULL; + /* + * Note: NEVER call TRACE / ERROR inside this function + * because it generates a recursion + */ for (i = 0; i < numdetails; i++) { - TRACE("Detail %d %s", i, details[i]); if (i == 0) { ret = asprintf(&next, "\"%s\"", details[i]); } else { @@ -360,29 +366,26 @@ static char *server_create_details(int numdetails, const char *details[]) prev = next; } - TRACE("Final details: %s", next); - return next; } server_op_res_t -server_send_deployment_reply(const int action_id, const int job_cnt_max, +server_send_deployment_reply(channel_t *channel, + const int action_id, const int job_cnt_max, const int job_cnt_cur, const char *finished, const char *execution_status, int numdetails, const char *details[]) { - channel_t *channel = server_hawkbit.channel; assert(channel != NULL); assert(finished != NULL); assert(execution_status != NULL); assert(details != NULL); assert(server_hawkbit.url != NULL); + assert(channel != NULL); assert(server_hawkbit.tenant != NULL); assert(server_hawkbit.device_id != NULL); char *detail = server_create_details(numdetails, details); - TRACE("Reporting Installation progress for ID %d: %s / %s / %s", - action_id, finished, execution_status, detail); /* clang-format off */ static const char* const json_hawkbit_deployment_feedback = STRINGIFY( { @@ -427,7 +430,6 @@ server_send_deployment_reply(const int action_id, const int job_cnt_max, } channel_data.url = url; channel_data.request_body = json_reply_string; - TRACE("PUTing to %s: %s", channel_data.url, channel_data.request_body); channel_data.method = CHANNEL_POST; result = map_channel_retcode(channel->put(channel, (void *)&channel_data)); @@ -811,7 +813,7 @@ static server_op_res_t handle_feedback(int action_id, server_op_res_t result, break; } - if (server_send_deployment_reply(action_id, 0, 0, reply_result, + if (server_send_deployment_reply(server_hawkbit.channel, action_id, 0, 0, reply_result, reply_execution, numdetails, details) != SERVER_OK) { ERROR("Error while reporting installation status to server."); @@ -898,6 +900,107 @@ static int server_update_status_callback(ipc_message *msg) return 0; } +static void *process_notification_thread(void *data) +{ + const int action_id = *(int *)data; + const int MAX_DETAILS = 48; + const char *details[MAX_DETAILS]; + unsigned int numdetails = 0; + bool stop = false; + channel_data_t channel_data = channel_data_defaults; + unsigned int percent = 0; + unsigned int step = 0; + + + /* + * Create a new channel to the server. The opened channel is + * used to download the SWU + */ + channel_t *channel = channel_new(); + if (channel->open(channel, &channel_data) != CHANNEL_OK) { + free(channel); + return NULL; + } + + for (;;) { + ipc_message msg; + bool data_avail = false; + int ret = ipc_get_status_timeout(&msg, 100); + + data_avail = ret > 0 && (strlen(msg.data.status.desc) != 0); + + /* + * Mutex used to synchronize end of the thread + * The suricatta thread locks the mutex at the beginning + * and release it when this task should terminate. + * By getting the lock, this task loads know it has to finish + */ + if (!pthread_mutex_trylock(¬ifylock)) { + stop = true; + } + + if (msg.data.status.current == PROGRESS) + continue; + /* + * If there is a message + * ret > 0: data available + * ret == 0: TIMEOUT, no more messages + * ret < 0 : ERROR, exit + */ + if (data_avail && numdetails < MAX_DETAILS) { + for (int c = 0; c < strlen(msg.data.status.desc); c++) { + switch (msg.data.status.desc[c]) { + case '"': + case '\'': + case '\\': + case '\n': + case '\r': + msg.data.status.desc[c] = ' '; + break; + } + } + details[numdetails++] = strdup(msg.data.status.desc); + } + + /* + * Flush to the server + */ + if (numdetails == MAX_DETAILS || (stop && !data_avail)) { + TRACE("Update log to server from thread"); + if (server_send_deployment_reply( + channel, + action_id, step, percent, + reply_status_result_finished.none, + reply_status_execution.proceeding, numdetails, + &details[0]) != SERVER_OK) { + ERROR("Error while sending log to server."); + } + for (unsigned int count = 0; count < numdetails; count++) { + free((void *)details[count]); + } + numdetails = 0; + percent++; + if (percent > 100) { + percent = 0; + step++; + } + } + + if (stop && !data_avail) + break; + } + + pthread_mutex_unlock(¬ifylock); + + /* + * Now close the channel for feedback + */ + channel->close(channel); + free(channel); + + return NULL; +} + server_op_res_t server_process_update_artifact(int action_id, json_object *json_data_artifact, const char *update_action, @@ -910,6 +1013,7 @@ server_op_res_t server_process_update_artifact(int action_id, assert(json_data_artifact != NULL); assert(json_object_get_type(json_data_artifact) == json_type_array); server_op_res_t result = SERVER_OK; + pthread_t notify_to_hawkbit_thread; /* Initialize list of errors */ for (int i = 0; i < HAWKBIT_MAX_REPORTED_ERRORS; i++) @@ -925,6 +1029,7 @@ server_op_res_t server_process_update_artifact(int action_id, for (int json_data_artifact_count = 0; json_data_artifact_count < json_data_artifact_max; json_data_artifact_count++) { + int thread_ret = -1; json_data_artifact_item = array_list_get_idx( json_data_artifact_array, json_data_artifact_count); TRACE("Iterating over JSON, key=%s", @@ -1045,6 +1150,20 @@ server_op_res_t server_process_update_artifact(int action_id, server_get_current_time(&server_time); + /* + * Start background task to collect logs and + * send to Hawkbit server + */ + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + pthread_mutex_init(¬ifylock, NULL); + pthread_mutex_lock(¬ifylock); + + thread_ret = pthread_create(¬ify_to_hawkbit_thread, &attr, + process_notification_thread, &action_id); + channel_op_res_t cresult = channel->get_file(channel, (void *)&channel_data); if ((result = map_channel_retcode(cresult)) != SERVER_OK) { @@ -1086,6 +1205,13 @@ server_op_res_t server_process_update_artifact(int action_id, } cleanup_loop: + pthread_mutex_unlock(¬ifylock); + if (!thread_ret) { + if (pthread_join(notify_to_hawkbit_thread, NULL)) { + ERROR("return code from pthread_join()"); + } + } + pthread_mutex_destroy(¬ifylock); if (channel_data.url != NULL) { free(channel_data.url); } @@ -1136,6 +1262,7 @@ server_op_res_t server_install_update(void) if (server_hawkbit.update_action == deployment_update_action.skip) { const char *details = "Skipped Update."; if (server_send_deployment_reply( + server_hawkbit.channel, action_id, 0, 0, reply_status_result_finished.success, reply_status_execution.closed, 1, &details) != SERVER_OK) { @@ -1214,6 +1341,7 @@ server_op_res_t server_install_update(void) } if (server_send_deployment_reply( + server_hawkbit.channel, action_id, json_data_chunk_max, json_data_chunk_count, reply_status_result_finished.none, reply_status_execution.proceeding, 1, @@ -1250,6 +1378,7 @@ server_op_res_t server_install_update(void) json_object_get_string(json_data_chunk_version), json_object_get_string(json_data_chunk_part)); (void)server_send_deployment_reply( + server_hawkbit.channel, action_id, json_data_chunk_max, json_data_chunk_count, reply_status_result_finished.failure, @@ -1259,6 +1388,7 @@ server_op_res_t server_install_update(void) goto cleanup; } if (server_send_deployment_reply( + server_hawkbit.channel, action_id, json_data_chunk_max, json_data_chunk_count + 1, reply_status_result_finished.none, @@ -1276,6 +1406,7 @@ server_op_res_t server_install_update(void) } if (server_send_deployment_reply( + server_hawkbit.channel, action_id, json_data_chunk_max, json_data_chunk_count, reply_status_result_finished.none, reply_status_execution.proceeding, 1,