From patchwork Sun Jul 4 15:09:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Hilliard X-Patchwork-Id: 1500442 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=2607:f8b0:4864:20::337; helo=mail-ot1-x337.google.com; envelope-from=swupdate+bncbcl4hcw73qcbbnm6q6dqmgqeox5dneq@googlegroups.com; receiver=) 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=EHIsJ3/f; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=G2b3rT52; dkim-atps=neutral Received: from mail-ot1-x337.google.com (mail-ot1-x337.google.com [IPv6:2607:f8b0:4864:20::337]) (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 4GHsgG63SDz9sWX for ; Mon, 5 Jul 2021 01:09:45 +1000 (AEST) Received: by mail-ot1-x337.google.com with SMTP id w29-20020a056830411db029047c55b508f8sf6990360ott.21 for ; Sun, 04 Jul 2021 08:09:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1625411382; cv=pass; d=google.com; s=arc-20160816; b=t8i8m0a3xT7b8aYPLYwO+Xy3ikk9DD+gi8EHO93CFWv/Egzx80/yuBTOrZEmbxmZ+Y 3v0FvgneaeVa4GmbbImuaq/vOkRrEdcwWFFWh5N6BrYVIYuKo0IkBe8xHe8GNCDq6CKZ ItQZkhr2b0B1HYaXrVuw1tvKpOm+sy4icXKIHgMYZLrKVpOXEdwaSq9mhFlVDO3+OhDC lz1Q8CbEdTsqGH6fswYELsOIg9gGBi3hqkRQB9xj73/RqGQR8gxIFO0lCd/y5frSQbUV LFgaciLVbhh2FjSilDmrQ/oZ0+YWvu1d8By04mwx3dAFaXLc6QszqIl6vm8mf7vg6Ie4 CalA== 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:dkim-signature; bh=OgVlXxJRLWTXq4fyVrhlzo5+D85/A/7+RpOEAnSJMfU=; b=QbTWKdtXPNoXXCp81abtnr4IpzrZKfwDBxvnsge/ABMezQVmVH+BiJmDixRPgchMPs 0QDzAwxLLJzjGYB6nxPIZlHD/XgYnfoXT4RiU18Qt12zplMhgNuyo1ENzf6DGw1Fgb1+ U8MqcPHbjnTQHAB2IeJ/Xl66fZKkU0HSvOBHRnADuONDO1R7gtpG28gIBQ1bVFWsesop UWOxQ4JNTlzu/hjigUw6hunOsMuGbgo1+thVKTiJ7UuVZZFn8qowFEP4mIbB0HBIsBea d9TdpXDr5/QW0eJybzq/n9EiaR/CbLYrhFyoq8YqsSlWWxFEvwTGGQ8rHjWgSQF3KJse Q3zA== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Txq+pVKb; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d36 as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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=OgVlXxJRLWTXq4fyVrhlzo5+D85/A/7+RpOEAnSJMfU=; b=EHIsJ3/f7MoZKJ+VEQR/8oR1FO3VQ5GWI4+b2w1XVlzVcG0n4kg/pORPWSSKMGcOsN WvLl0TLtcs8BDYe1wul1N6f1zy7KqyTxbYSafulKyMkxUlv9/qtQb+ayuQyfqjJ15S0u rQbmfUATKnvjZ3LflkT6OmC/A+U6gOki0keLljpUxrpY+PAeSxpYRXAiBeTr03Vn09NK U2pN/8ctevI8vt+2n1dEMbqGMWP2UvSjSCgdal9Xd4ku8g5h/NHv/vTVrohXH31iKFdS yXwkYfJk0ubVdgAaAP7i4ueBw+rl+6QE/QqQVHYBiIT0/rGrnIOW5gRvmrC15bM6T0h2 65QQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=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=OgVlXxJRLWTXq4fyVrhlzo5+D85/A/7+RpOEAnSJMfU=; b=G2b3rT52uUYmadXj0KnGHfOmsXiiLXUtjVZvVDD9Bxq8N7WfjR0Nhy94z4r56wE7Rn Ffx6ELI/oDH8wFq8tTTK3Qd/m16t94DYSt794EiFFfUYdRhmqi0HJ2snewqVPC6kOHeb jOsWIuGIROT1/eav7a0hK8sv15kw6q7DpeklH8GxAASSIyHKWhdEouVlE4CPWl05NO41 HE9UAYOpeJKAK0etMHxJJ7myKsQ2JuX3EaOr7p1RAh4GZTbXBVgd8CtL6QWjNttYFC/0 Hh8j8uRVRK5gKNqPmU81zcnm3axn0CjN6mN1TJKKE0Aqcsn8mB041gY2PaEEp1Sjnob5 35LQ== 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=OgVlXxJRLWTXq4fyVrhlzo5+D85/A/7+RpOEAnSJMfU=; b=mKZKj0n+joif77Or+eQtw+Ler4HfLwuQQqy2xGvjTHqrZQsze7ssmh5ZKHXLUuFvWF 25XBOw78571LBvoaYmwrFQzCq9GN7FDpyr+IopFErbPof8W/l1cKzRfKzG3bjGxycO99 sJWAKtO4QATnKevxxIK9Z+cj+zFFKc4uRgS3m/DarJKm+KFrGI7W4g1IkDbhLEKiUK8h 8e7pcPHslWjbneOlTw0ZVvG1sKJIstXNgGadL4ZfPkXevoGKnj0Xw+kUqivIVDrxmlnE Nb02wE4lnwl965V4KJsHnMqmDgVjJD7/iEl//UauptDIIRtJ9hNEq7XEsRQkZeCqZqBQ J0bw== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM531ZY7ORPMtQD2muVXYno3bsddBFX5klfi4LpbroeC8h4Bwe+QeR KikG8wQ2HaoHE3NxHLCpKcQ= X-Google-Smtp-Source: ABdhPJwGkkzinTYoo/cUT94j/earSScgnNFJcV40OCMt0yz0m4BeWe3P+l395aF69MgYEtpQCeR2VA== X-Received: by 2002:a9d:3d3:: with SMTP id f77mr7588198otf.43.1625411382174; Sun, 04 Jul 2021 08:09:42 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a9d:70c4:: with SMTP id w4ls1905968otj.3.gmail; Sun, 04 Jul 2021 08:09:41 -0700 (PDT) X-Received: by 2002:a05:6830:1684:: with SMTP id k4mr7662611otr.359.1625411381665; Sun, 04 Jul 2021 08:09:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625411381; cv=none; d=google.com; s=arc-20160816; b=c8Wp9j1Bw/xgAIs9B4OcPwfGfW52c3AiVrl/M3u4Zsz6z3FWOI0drq2KoaUHjAPjfC Gu2nGgMWC3llxr9EE1Xp4+V8H4TKA7yb0lpThQqdRlo3IwGCs97bHTsLMgLF8w7VwhLh 0rrR4dB4aiK/nt1pfvmFlUSxV1M3woOGJIrnu+B/vseHYeIUNmdrQEF1PpCMma3JXiQd WLfK+WPiPcwT8s88b2sU4scAxs+fWH9jS57pT73pKSgmpsZnI7s65jxZGJEZ97b1M3rh xRh29ESG3rI62Ltqc/STQAoGg9uxsFMSuv3G1bbTEo1taTCuMOevS6/0nb3Q+sPhJmrT tITA== 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:dkim-signature; bh=stkEvBvDfKnlH8C7mnfqLyarXN+iC+FS3vbqJ8g8kfg=; b=aWKQi5J6poesRI6meeTNZOcUgN20B3ph8WnZS5xJwYCKQpKO7QCmNHfF9fyWApKEIl XkyRlwZpM82F6zc0IqKlrEFIxskgg2MbqVGwWFcaLc5UeKkumDVFMBKvFpdNl1fcZjUe ofjx5zVljcIlZwGpn+hB4fIc2RIhcH7ComUZF1ppvguE4b9HtHCqAcM4lfUq3dPjDFGx 7IgAcdLANZTt5xGRMvdwRA47Alp/vRC0EAIDjEYxj4ba2foqg1Cp/R9TyxBTWqboLlud 5HrNrMfCm1nfAWHojDfIfcuzEnh6k3f+Vi+7jrfeFuas4w2xHNnfrjFXioZ8QFFs/xg9 9YHQ== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Txq+pVKb; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d36 as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com. [2607:f8b0:4864:20::d36]) by gmr-mx.google.com with ESMTPS id j26si1573334ooj.0.2021.07.04.08.09.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 04 Jul 2021 08:09:41 -0700 (PDT) Received-SPF: pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d36 as permitted sender) client-ip=2607:f8b0:4864:20::d36; Received: by mail-io1-xd36.google.com with SMTP id q2so2830089iot.11 for ; Sun, 04 Jul 2021 08:09:41 -0700 (PDT) X-Received: by 2002:a02:2547:: with SMTP id g68mr8287530jag.24.1625411380883; Sun, 04 Jul 2021 08:09:40 -0700 (PDT) Received: from MacBook-Pro-2.localdomain (71-33-158-86.hlrn.qwest.net. [71.33.158.86]) by smtp.gmail.com with ESMTPSA id u20sm5106610ilj.77.2021.07.04.08.09.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Jul 2021 08:09:40 -0700 (PDT) From: James Hilliard To: swupdate@googlegroups.com Cc: James Hilliard Subject: [swupdate] [PATCH 1/1] diskpart: lock the device before writing the disklabel Date: Sun, 4 Jul 2021 09:09:37 -0600 Message-Id: <20210704150937.46996-1-james.hilliard1@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Original-Sender: james.hilliard1@gmail.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Txq+pVKb; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d36 as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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: , This should help make sure it is safe to modify the disk before we modify the partition table. Adapted from: https://github.com/karelzak/util-linux/blob/v2.37/lib/blkdev.c#L365-L413 Signed-off-by: James Hilliard --- handlers/diskpart_handler.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c index 5eba2bd..09832ec 100644 --- a/handlers/diskpart_handler.c +++ b/handlers/diskpart_handler.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include "swupdate.h" @@ -634,14 +635,44 @@ static int diskpart_compare_tables(struct diskpart_table *tb, struct diskpart_ta return ret; } +static int diskpart_blkdev_lock(struct fdisk_context *cxt) +{ + int oper = LOCK_EX; + int ret = 0; + + if (fdisk_device_is_used(cxt)) { + ERROR("%s: device is in use", fdisk_get_devname(cxt)); + return -EBUSY; + } + + if (!fdisk_is_readonly(cxt)) { + ret = flock(fdisk_get_devfd(cxt), oper); + if (ret) { + switch (errno) { + case EWOULDBLOCK: + ERROR("%s: device already locked", fdisk_get_devname(cxt)); + break; + default: + ERROR("%s: failed to get lock", fdisk_get_devname(cxt)); + } + return -EBUSY; + } + } + return ret; +} + static int diskpart_write_table(struct fdisk_context *cxt, struct create_table *createtable) { int ret = 0; - if (createtable->parent || createtable->child) + if (createtable->parent || createtable->child) { TRACE("Partitions on disk differ, write to disk;"); - else + ret = diskpart_blkdev_lock(PARENT(cxt)); + if (ret) + return ret; + } else { TRACE("Same partition table on disk, do not touch partition table !"); + } if (createtable->child) { if (!IS_HYBRID(cxt)) {