Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp349692rdh; Tue, 13 Feb 2024 20:56:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUTJZikm6e/vlR7siAL6Wn4FfZQDiyyrcDx4tT1GrZaM1uS8d7vPKiY6urEOFAVaXISgmQ0hh4X9c8MnxEyulkVKbN1xQ9C2HAqPOul1w== X-Google-Smtp-Source: AGHT+IGpLHMAPtj2CeEBaxapPMxXo3CbZr7IaWkgdgmRHbOtU2OWDZhx89BYbOkF7j4Hn0nhXLX/ X-Received: by 2002:a05:622a:1a19:b0:42c:792f:30ed with SMTP id f25-20020a05622a1a1900b0042c792f30edmr1373511qtb.20.1707886565995; Tue, 13 Feb 2024 20:56:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886565; cv=pass; d=google.com; s=arc-20160816; b=RabS7jZ3b9YZHMCikPa2EuhYKr5FmQghC26koglEwZlF/LzA6DSZIxoQWmrYlCwc5+ Y66jqIXhSY9n0jr7PGvmJR46skAF5EZRlqqHib6XpT5UFek1atJIkIRxC9yd7RV9ykpb tqIGxGUeXCXlqzKjDFXcVKpM54yGCHiF3d3u5DekqwjZzaJQDtNjRNdtgF+Mhqj3YiZ7 Vz0c1cZw+YYs0lfWTvPWT9XcH0Om8yJID+dVnBFXhq5jgPRhUiXwdTvE9a8cJo60wg43 GYbOGb4O+nUCDAKHTM8ydkuoprQ4fQAAxthSO+xlN2lcs9tOgkvkI1XOysPnjcAJQU2S 2alg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=mEeFv0uon5WQC9cFYvgD6Kwx90gDUxQUuXbCDfN9dJU=; fh=UapB/cNmupgdTqTS4/DCkWgnEwrGqjLKKIGdudb37hY=; b=qBkQWg+LFUnkqYaJgM06d0i1qubLu7i93KvD5VefLELgrLDKwsSlQam8qAu/SJTXhk qsrXCvx6sMPjxg7l5mKu/exOpqVwJs7feKXOT5YuwBGAw2VC0mm+a2WMrh/IvwN7+ON/ xgw1thPppjd9zKokTOpegMsWOD5TfQewhyoPU0mU8hF5uWttHt0FSIh5kDklawIwoodv mT9mRUcKaEPM+zHjtA+E6uYGVywRMFbFGDLoy5ipvktX9s43aQOW2lDlUTEDdrcVXKR6 Ekjjj+bZXPdiiXQorKYwz3sdTZ/8OauXqecDORKwRd4sAF1MoNxC+5m8Irvoucv81jju yZtA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ACbHuTxZ; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64742-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64742-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXU5uUtEeqPwhcwYZ0uEswBuzpdGrzA0f0jssASlgZ6mfHoui5+20d8t2wG/3iImqipoH57Qfv77/yHO5c0kpJY3vP/6BDYFpmUcfOrBw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f17-20020ac84651000000b0042c7bd6fcbbsi4195618qto.249.2024.02.13.20.56.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:56:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64742-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ACbHuTxZ; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64742-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64742-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C75CE1C22BAA for ; Wed, 14 Feb 2024 04:55:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9568512B98; Wed, 14 Feb 2024 04:54:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ACbHuTxZ" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 919D612B60 for ; Wed, 14 Feb 2024 04:54:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886488; cv=none; b=tVyAMaQ05xeqht6AvOy2PW5IdSmchgRNO8kJI+g2ULDZcq0gCF3ZC3sgyF4hOHpVilE53lPQ5qaz+GqiMgt24E692fbCILcmjmqxCowZqiPEipwi7Kt4Gy5ay5WI6c8TFyZWXbDCWoHPmTZBk4oCFA5ZBCcOGTAJt3tDKYpHNVw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886488; c=relaxed/simple; bh=pokSEbOrag3uWdO1RhTKTOPvPSFp8ZMJI6bQo7DbnNA=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=l37XI9jufytinyp5ogtWh3U56l7/X4gmBnpf6YD/srZWZxGbpRMIzv3MziKK1CP28n5CobLNkqihn9WlSeaWR6AX83lsWaaidrsilyWv3tWuhJ184Xgwxu34OyKTvH0sxXbTgYoXhkZy37koRTJqTgCukfwYN9BltkQkyeZwL5U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ACbHuTxZ; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcd703b721dso1231488276.1 for ; Tue, 13 Feb 2024 20:54:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886485; x=1708491285; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=mEeFv0uon5WQC9cFYvgD6Kwx90gDUxQUuXbCDfN9dJU=; b=ACbHuTxZthtoTd3nlDYDquxbufe2cI9F5DQ5//GukYFAaBixZOoHUFcUW96QapMyk/ ExlAX3e/p1iLUUVQqBhzWUrpzMC264ZWNcMCNht60ypZnBZFojMsAtrbes5k8s/iJpTa CAXmxjchT1drT0DcYEiLT1useD6vL8vHqNH9WcXY7NBpTjUf5nLu/tCoywQKU2rVpd59 S5AhBglynZ1heMcnXQUJhSB4v3XjKRZeAO8eG0+FCVtEDpzDVKMZsQ6D76s+GkpHu/W2 L1HvTtX9pZcd8pwRdt+2LnDKciSGY5Fo+fnmtHEe6AA4z5rPdM40AY/kNKyXn/U/UMO2 wNxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886485; x=1708491285; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mEeFv0uon5WQC9cFYvgD6Kwx90gDUxQUuXbCDfN9dJU=; b=DTYFVfES1XZivrueaslLkOIW0lLxDtoGciaOXFscMaYxMyOHPMHrg+x/jUr5r//haC ktKE2mYfk/g+UHZmwq44j5YGlUSJ7qAi1eEcIkGQmmTvFwkde68LuEQbZQ7Bqjs6iGa7 l407jFMmqBOK0JLbExxJOdJ6OvnbXeODHf1JHySw/ylyGjV/Tyx34TMZvmjHcJbS3Ugi yGW6cULSEHQRGnLJVKb56qHpmlUeXkhB75/GMvTOLMPKPbCdkFo31QiijbEeFWDkp0Ff qACjbQb9SZaU0QfXvepblIhXhDRBM7nHT8K2MUWqLR3KWWWrGeY3LBC901DR7xltZg2K 8vJQ== X-Forwarded-Encrypted: i=1; AJvYcCU9a4dVXnzfjvM6rvodiztwPfA5+ykQ9FITOmpCntr2+hHpoUYME1J5p8heubfQnMNZt4c3rhABIKUHL34bLkUXf4MGZ3FFa+GacihH X-Gm-Message-State: AOJu0YwNsEgXQzCRVTg7uumZo7EvxCyAguxA6cOUzymeN/KhitEgoQLm QHm/+OspL9rVlIY6ivhxMbqAFqR2EGC0y8Vx4N6wIVCpuvzRKIt+EhUbRR+LDRWkTRiDUY0NH5C LMGmzdw== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a25:2e4c:0:b0:dc6:44d4:bee0 with SMTP id b12-20020a252e4c000000b00dc644d4bee0mr74284ybn.7.1707886485640; Tue, 13 Feb 2024 20:54:45 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:54 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-7-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 6/8] perf tests: Use scandirat for shell script finding From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , James Clark , Athira Jajeev , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Avoid filename appending buffers by using openat, faccessat and scandirat more widely. Turn the script's path back to a file name using readlink from /proc//fd/. Read the script's description using api/io.h to avoid fdopen conversions. Whilst reading perform additional sanity checks on the script's contents. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 21 ++--- tools/perf/tests/tests-scripts.c | 144 ++++++++++++++++++------------- tools/perf/tests/tests-scripts.h | 1 - 3 files changed, 95 insertions(+), 71 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index eff3c62e9b47..6d5001daaf63 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -300,22 +300,20 @@ static int test_and_print(struct test_suite *t, int subtest) } struct shell_test { - const char *dir; const char *file; }; static int shell_test__run(struct test_suite *test, int subdir __maybe_unused) { int err; - char script[PATH_MAX]; struct shell_test *st = test->priv; + char *cmd; - path__join(script, sizeof(script) - 3, st->dir, st->file); - - if (verbose > 0) - strncat(script, " -v", sizeof(script) - strlen(script) - 1); - - err = system(script); + asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : ""); + if (!cmd) + return TEST_FAIL; + err = system(cmd); + free(cmd); if (!err) return TEST_OK; @@ -331,7 +329,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width, files = list_script_files(); if (!files) return 0; - for (file = files; file->dir; file++) { + for (file = files; file->file; file++) { int curr = i++; struct test_case test_cases[] = { { @@ -345,13 +343,12 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width, .test_cases = test_cases, .priv = &st, }; - st.dir = file->dir; + st.file = file->file; if (test_suite.desc == NULL || !perf_test__matches(test_suite.desc, curr, argc, argv)) continue; - st.file = file->file; pr_info("%3d: %-*s:", i, width, test_suite.desc); if (intlist__find(skiplist, i)) { @@ -455,7 +452,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i) files = list_script_files(); if (!files) return 0; - for (file = files; file->dir; file++) { + for (file = files; file->file; file++) { int curr = i++; struct test_suite t = { .desc = file->desc diff --git a/tools/perf/tests/tests-scripts.c b/tools/perf/tests/tests-scripts.c index 4ebd841da05b..9b3b66dd5508 100644 --- a/tools/perf/tests/tests-scripts.c +++ b/tools/perf/tests/tests-scripts.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "builtin.h" #include "tests-scripts.h" #include "color.h" @@ -35,55 +36,69 @@ static size_t files_num = 0; static struct script_file *files = NULL; static int files_max_width = 0; -static const char *shell_tests__dir(char *path, size_t size) +static int shell_tests__dir_fd(void) { - const char *devel_dirs[] = { "./tools/perf/tests", "./tests", }; - char *exec_path; - unsigned int i; + char path[PATH_MAX], *exec_path; + static const char * const devel_dirs[] = { "./tools/perf/tests/shell", "./tests/shell", }; - for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { - struct stat st; + for (size_t i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { + int fd = open(devel_dirs[i], O_PATH); - if (!lstat(devel_dirs[i], &st)) { - scnprintf(path, size, "%s/shell", devel_dirs[i]); - if (!lstat(devel_dirs[i], &st)) - return path; - } + if (fd >= 0) + return fd; } /* Then installed path. */ exec_path = get_argv_exec_path(); - scnprintf(path, size, "%s/tests/shell", exec_path); + scnprintf(path, sizeof(path), "%s/tests/shell", exec_path); free(exec_path); - return path; + return open(path, O_PATH); } -static const char *shell_test__description(char *description, size_t size, - const char *path, const char *name) +static char *shell_test__description(int dir_fd, const char *name) { - FILE *fp; - char filename[PATH_MAX]; - int ch; + struct io io; + char buf[128], desc[256]; + int ch, pos = 0; - path__join(filename, sizeof(filename), path, name); - fp = fopen(filename, "r"); - if (!fp) + io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf)); + if (io.fd < 0) return NULL; /* Skip first line - should be #!/bin/sh Shebang */ + if (io__get_char(&io) != '#') + goto err_out; + if (io__get_char(&io) != '!') + goto err_out; do { - ch = fgetc(fp); - } while (ch != EOF && ch != '\n'); - - description = fgets(description, size, fp); - fclose(fp); + ch = io__get_char(&io); + if (ch < 0) + goto err_out; + } while (ch != '\n'); - /* Assume first char on line is omment everything after that desc */ - return description ? strim(description + 1) : NULL; + do { + ch = io__get_char(&io); + if (ch < 0) + goto err_out; + } while (ch == '#' || isspace(ch)); + while (ch > 0 && ch != '\n') { + desc[pos++] = ch; + if (pos >= (int)sizeof(desc) - 1) + break; + ch = io__get_char(&io); + } + while (pos > 0 && isspace(desc[--pos])) + ; + desc[++pos] = '\0'; + close(io.fd); + return strdup(desc); +err_out: + close(io.fd); + return NULL; } /* Is this full file path a shell script */ -static bool is_shell_script(const char *path) +static bool is_shell_script(int dir_fd, const char *path) { const char *ext; @@ -91,20 +106,16 @@ static bool is_shell_script(const char *path) if (!ext) return false; if (!strcmp(ext, ".sh")) { /* Has .sh extension */ - if (access(path, R_OK | X_OK) == 0) /* Is executable */ + if (faccessat(dir_fd, path, R_OK | X_OK, 0) == 0) /* Is executable */ return true; } return false; } /* Is this file in this dir a shell script (for test purposes) */ -static bool is_test_script(const char *path, const char *name) +static bool is_test_script(int dir_fd, const char *name) { - char filename[PATH_MAX]; - - path__join(filename, sizeof(filename), path, name); - if (!is_shell_script(filename)) return false; - return true; + return is_shell_script(dir_fd, name); } /* Duplicate a string and fall over and die if we run out of memory */ @@ -120,12 +131,21 @@ static char *strdup_check(const char *str) return newstr; } -static void append_script(const char *dir, const char *file, const char *desc) +static void append_script(int dir_fd, const char *name, char *desc) { + char filename[PATH_MAX], link[128]; struct script_file *files_tmp; - size_t files_num_tmp; + size_t files_num_tmp, len; int width; + snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd); + len = readlink(link, filename, sizeof(filename)); + if (len < 0) { + pr_err("Failed to readlink %s", link); + return; + } + filename[len++] = '/'; + strcpy(&filename[len], name); files_num_tmp = files_num + 1; if (files_num_tmp >= SIZE_MAX) { pr_err("Too many script files\n"); @@ -142,10 +162,8 @@ static void append_script(const char *dir, const char *file, const char *desc) /* Add file to end and NULL terminate the struct array */ files = files_tmp; files_num = files_num_tmp; - files[files_num - 1].dir = strdup_check(dir); - files[files_num - 1].file = strdup_check(file); - files[files_num - 1].desc = strdup_check(desc); - files[files_num].dir = NULL; + files[files_num - 1].file = strdup_check(filename); + files[files_num - 1].desc = desc; files[files_num].file = NULL; files[files_num].desc = NULL; @@ -154,32 +172,39 @@ static void append_script(const char *dir, const char *file, const char *desc) files_max_width = width; } -static void append_scripts_in_dir(const char *path) +static void append_scripts_in_dir(int dir_fd) { struct dirent **entlist; struct dirent *ent; int n_dirs, i; - char filename[PATH_MAX]; /* List files, sorted by alpha */ - n_dirs = scandir(path, &entlist, NULL, alphasort); + n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort); if (n_dirs == -1) return; for (i = 0; i < n_dirs && (ent = entlist[i]); i++) { + int fd; + if (ent->d_name[0] == '.') continue; /* Skip hidden files */ - if (is_test_script(path, ent->d_name)) { /* It's a test */ - char bf[256]; - const char *desc = shell_test__description - (bf, sizeof(bf), path, ent->d_name); + if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */ + char *desc = shell_test__description(dir_fd, ent->d_name); if (desc) /* It has a desc line - valid script */ - append_script(path, ent->d_name, desc); - } else if (is_directory(path, ent)) { /* Scan the subdir */ - path__join(filename, sizeof(filename), - path, ent->d_name); - append_scripts_in_dir(filename); + append_script(dir_fd, ent->d_name, desc); + continue; + } + if (ent->d_type != DT_DIR) { + struct stat st; + + if (ent->d_type != DT_UNKNOWN) + continue; + fstatat(dir_fd, ent->d_name, &st, 0); + if (!S_ISDIR(st.st_mode)) + continue; } + fd = openat(dir_fd, ent->d_name, O_PATH); + append_scripts_in_dir(fd); } for (i = 0; i < n_dirs; i++) /* Clean up */ zfree(&entlist[i]); @@ -188,14 +213,17 @@ static void append_scripts_in_dir(const char *path) const struct script_file *list_script_files(void) { - char path_dir[PATH_MAX]; - const char *path; + int dir_fd; if (files) return files; /* Singleton - we already know our list */ - path = shell_tests__dir(path_dir, sizeof(path_dir)); /* Walk dir */ - append_scripts_in_dir(path); + dir_fd = shell_tests__dir_fd(); /* Walk dir */ + if (dir_fd < 0) + return NULL; + + append_scripts_in_dir(dir_fd); + close(dir_fd); return files; } diff --git a/tools/perf/tests/tests-scripts.h b/tools/perf/tests/tests-scripts.h index 3a3ec6191848..3508a293aaf9 100644 --- a/tools/perf/tests/tests-scripts.h +++ b/tools/perf/tests/tests-scripts.h @@ -3,7 +3,6 @@ #define TESTS_SCRIPTS_H struct script_file { - char *dir; char *file; char *desc; }; -- 2.43.0.687.g38aa6559b0-goog