Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp929084pxb; Fri, 22 Apr 2022 14:35:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwZb3CdPayn80fgWQImXC0NrMDAEsh5tnx8A9FwFjXwebBSXcHqfPu1wL43Ppsu03W+8Cru X-Received: by 2002:a05:6a00:b52:b0:508:31e1:7d35 with SMTP id p18-20020a056a000b5200b0050831e17d35mr7084523pfo.33.1650663309200; Fri, 22 Apr 2022 14:35:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650663309; cv=none; d=google.com; s=arc-20160816; b=RZIqA9VC5VQ5B28g9/ko0WYeK5SX+9OvJ46Zl/OmDJ5sV2ulabVGTN1Iw21s89B3uX 5lwe91HpyVT0WqxcU2ew9xUt1El3EIarKPXvEjmcwcvrdextCzvl67b0G/uoRsbPGGxH 9wZKzCjxQpv3z1movt3AO5sq0vW9cLamdRvRJNZe2YCRSquz1Pbz7OheRZQGM1yZP9Hb gK94gZ/68fHWZQYZttzmUfKd9SfJeQNUjSNcS6yS3G+LhFBlVUXWIzktJubDQ95PeKQi 32XBrOdr3qq/PrHB5IGa/RJBwhxgqrzzK0X+LJy+KNy7ySLKiy3tpP5/Fw3zdQKpgV42 IczA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=o1jm2ZxrobVfc+EDXZbKA8synQY3urRJFo8ts6M9Cos=; b=MZn4gjRRVqaxrGah5Wj5XhbLaZeufDlnjBTqhv3iIEOxp+i6r5FgRV2+Oll21rTSsD C07V2Ro2x8Gw4YDG0wJBd/HcwQVzv79TNraYIfWBkNq/r9AhGAjo7AYXTo5YauotSL8l E8oh29JeNW5XaXc73FgVk/I0UiYFrhJCtZQDK/ESvYUcuNVmF7XwxOiO1lBT6coigln1 ocn34t3umAX57KWlOkd2vyM5C6EFHUZfLB6KerdI1lhdEqqqel9MhvRUq7GuzSCKgKmR 0NLs1EclqSeAUlZ5PYfE2U7vKtxbGigD84IlEris8MJHQCAbJ5Kqk78dbXSIRNQte2wD BdGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XNmIj+vh; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id bf2-20020a170902b90200b0015696c5bda6si9107837plb.37.2022.04.22.14.35.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 14:35:09 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XNmIj+vh; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 12C5A16C17E; Fri, 22 Apr 2022 12:42:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442744AbiDUXsA (ORCPT + 99 others); Thu, 21 Apr 2022 19:48:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442735AbiDUXrh (ORCPT ); Thu, 21 Apr 2022 19:47:37 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3385E3EBB5 for ; Thu, 21 Apr 2022 16:44:46 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id bj12-20020a056a02018c00b003a9eebaad34so3591298pgb.10 for ; Thu, 21 Apr 2022 16:44:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=o1jm2ZxrobVfc+EDXZbKA8synQY3urRJFo8ts6M9Cos=; b=XNmIj+vh/zg85pOZ7hddsPc4rZoBWgPyUKppuibPQ8HpozXmQF4+KbncIhebNDLW+A OaXTK2I3Y4FSfd6Gk2xjKvidVm8LAaC0kUVGEo9TUpGjqdno7WSghhitFdSbSpl49CsV tN/y2CDjCmmhUJ5bWM3jDcayzcK/VRUFAN/fq1h0P/eGY5Hy+hXEJyzvjDolAnIyIh8w 7uodhfP1m0/4SgHnVku88XUlvSW+55VzyEWrQhAUPNeD6F+CC6uOkZE3RyW5PPK9/T9U kH6ybVBPt6zTUU/GcTYAtffLbYejeX7CoHq85fV8abFrT+jgJziiU3RQIdVNZSFNsFws rRVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=o1jm2ZxrobVfc+EDXZbKA8synQY3urRJFo8ts6M9Cos=; b=GMiKGALrVC4MREUTVbd4Pn8eXNkqdM/zTjvf7MBhbVpxNh9dop/MmeSbcdJSoIPFst 3mOSRdCRIM4FYWK6jFgZcnjFZ8IGqN8idfD9MSIi6cfuJLBLEuAKTolR+7GlBb6vbQPh zBfe38wQa3UOqNbwBTf8mOWhLnJFF+K9OGlVG/0VtGK0lpPo5jSQJRtFC3nOMREjNdD+ cX290awbDeM2JeWr0wcCPdfGrs6kkbKvD3at69wbVnDgyCnNDkH0amTztimQg3c/+wJl bbHzYQQwyLPgNumPl9t0AsuNtD4doQPPX3EuqJSDMJyZ02L69+k8pON1Z6adRvV0RUwn Osjw== X-Gm-Message-State: AOAM5313ZxLzKoBKNX15XyZFSXtdYIUH87S44PnxMyOczN/tLj3mNij4 U2RFo25ieKGnHveooGj1//9nUhDeYQRf6eWU X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a17:90b:4c85:b0:1d4:bc0b:90eb with SMTP id my5-20020a17090b4c8500b001d4bc0b90ebmr2179433pjb.171.1650584685622; Thu, 21 Apr 2022 16:44:45 -0700 (PDT) Date: Thu, 21 Apr 2022 23:44:24 +0000 In-Reply-To: <20220421234426.3494842-1-yosryahmed@google.com> Message-Id: <20220421234426.3494842-3-yosryahmed@google.com> Mime-Version: 1.0 References: <20220421234426.3494842-1-yosryahmed@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v4 2/4] selftests: cgroup: return -errno from cg_read()/cg_write() on failure From: Yosry Ahmed To: Johannes Weiner , Michal Hocko , Shakeel Butt , Andrew Morton , Roman Gushchin Cc: David Rientjes , Tejun Heo , Zefan Li , Jonathan Corbet , Shuah Khan , Yu Zhao , Dave Hansen , Wei Xu , Greg Thelen , Chen Wandun , Vaibhav Jain , "=?UTF-8?q?Michal=20Koutn=C3=BD?=" , Tim Chen , Dan Schatzberg , cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Yosry Ahmed Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, cg_read()/cg_write() returns 0 on success and -1 on failure. Modify them to return the -errno on failure. Signed-off-by: Yosry Ahmed --- tools/testing/selftests/cgroup/cgroup_util.c | 44 +++++++++----------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c index dbaa7aabbb4a..e6f3679cdcc0 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/cgroup_util.c @@ -19,6 +19,7 @@ #include "cgroup_util.h" #include "../clone3/clone3_selftests.h" +/* Returns read len on success, or -errno on failure. */ static ssize_t read_text(const char *path, char *buf, size_t max_len) { ssize_t len; @@ -26,35 +27,29 @@ static ssize_t read_text(const char *path, char *buf, size_t max_len) fd = open(path, O_RDONLY); if (fd < 0) - return fd; + return -errno; len = read(fd, buf, max_len - 1); - if (len < 0) - goto out; - buf[len] = 0; -out: + if (len >= 0) + buf[len] = 0; + close(fd); - return len; + return len < 0 ? -errno : len; } +/* Returns written len on success, or -errno on failure. */ static ssize_t write_text(const char *path, char *buf, ssize_t len) { int fd; fd = open(path, O_WRONLY | O_APPEND); if (fd < 0) - return fd; + return -errno; len = write(fd, buf, len); - if (len < 0) { - close(fd); - return len; - } - close(fd); - - return len; + return len < 0 ? -errno : len; } char *cg_name(const char *root, const char *name) @@ -87,16 +82,16 @@ char *cg_control(const char *cgroup, const char *control) return ret; } +/* Returns 0 on success, or -errno on failure. */ int cg_read(const char *cgroup, const char *control, char *buf, size_t len) { char path[PATH_MAX]; + ssize_t ret; snprintf(path, sizeof(path), "%s/%s", cgroup, control); - if (read_text(path, buf, len) >= 0) - return 0; - - return -1; + ret = read_text(path, buf, len); + return ret >= 0 ? 0 : ret; } int cg_read_strcmp(const char *cgroup, const char *control, @@ -177,17 +172,15 @@ long cg_read_lc(const char *cgroup, const char *control) return cnt; } +/* Returns 0 on success, or -errno on failure. */ int cg_write(const char *cgroup, const char *control, char *buf) { char path[PATH_MAX]; - ssize_t len = strlen(buf); + ssize_t len = strlen(buf), ret; snprintf(path, sizeof(path), "%s/%s", cgroup, control); - - if (write_text(path, buf, len) == len) - return 0; - - return -1; + ret = write_text(path, buf, len); + return ret == len ? 0 : ret; } int cg_find_unified_root(char *root, size_t len) @@ -545,7 +538,8 @@ ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t else snprintf(path, sizeof(path), "/proc/%d/%s", pid, item); - return read_text(path, buf, size); + size = read_text(path, buf, size); + return size < 0 ? -1 : size; } int proc_read_strstr(int pid, bool thread, const char *item, const char *needle) -- 2.36.0.rc2.479.g8af0fa9b8e-goog