From nobody Tue Apr 30 20:40:26 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VTXBz1Jprz5JtKV; Tue, 30 Apr 2024 20:40:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VTXBz0mhLz4PJs; Tue, 30 Apr 2024 20:40:27 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714509627; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UEgZuSG13yQ8XKEr8j09hXGbfkkrEIflveSNPwKmFLA=; b=ErlIa7MI6XMl2jpCnSCKnenJtWgnPyStpHjhlqIMPjyRbNTk4Cfr7M/tTwfKCnC/sBGDFt 6jATJCeW+lt8fiC73kHb3CtbsN+fZNeurfk8I/XSd31vCsRnd4r70SvUgonHIRIlWRyoq6 h3p9lnGm0Q/TjVkIF47q+EL1M1JMyTPr//AKKNBt+at0FsYftWv16a4jCIr12Vx18DxgH9 5veHMryYwMqnHcXbixHKPuBYbYhcZYrdsqu87H2YGU4hmzLH39hGuBsiSVzxPE4l/adWqN gMyx8fOw631WVYXjOUaQdhuDnOzPZRT1sA4nblBROmfQirIoyoAl1nhzyfk3xA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1714509627; a=rsa-sha256; cv=none; b=Vt5Glz2oRVl/LPEPA0cUi0Up7QTAgUEeiU6QSIUq5qabS7d1MSPxrQEb4+LdO9YXAdvpU/ oxwOyRKLoc1m3sAXxSxMJj0L7o7pN83EZjKiEK39sXlrDJlImcq2hUaTK7zNlzsQciJeDY ufPcRWmzXcbJiX3tmafzi93DhmfFXkGvyqoF3v6b2DZCVYxD2GT5LeDa+GVFSSsmGiv2s7 RVY7S4YJemZxYBZ1fIUO0w3DV5iTIttpr7u2RbC1RZ54TzQ/LlV5FiqPCiz4krB1hWrdl4 doc+pJ+Ywi2STwNz40HAdUtUKuuVsb1MCS4AGw27EeUnUsF9+Aw78oMC172bsA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714509627; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UEgZuSG13yQ8XKEr8j09hXGbfkkrEIflveSNPwKmFLA=; b=VDNC1blAwoz/Sk82dOZj5QBM8wtJeG2lRpHpR0s3oDc5l/jf17TZ+3+l0U15MKFhsw3op8 pzO0eXQ1qYxOYNDqqsno8ZkueL13XWhh7F+kMElDjkHRuy8OmF7wryNGZVZrvkgyrjgvAr jqvtrzdHVzO9m3nD/GhhsWuPalsHNPnjKz1q3GRBB/xqvyIwrPMRo7zh9zIgLdSJyEpaSL 4pSR4FN/Fj8aaF8Ov8/I+/X7pkzOWnp7q+0YE52x+N7A4l3Z9ioqNmH/tR27MoVtJirfCm Nn7wKxO+owg1dSqibnTKUb5oVZhFY7JAzhrhuME37PopHc5o4qaeObA/RzSJ1A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VTXBz0J6Pzq9p; Tue, 30 Apr 2024 20:40:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43UKeQqM038223; Tue, 30 Apr 2024 20:40:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43UKeQQ1038220; Tue, 30 Apr 2024 20:40:26 GMT (envelope-from git) Date: Tue, 30 Apr 2024 20:40:26 GMT Message-Id: <202404302040.43UKeQQ1038220@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Stephen J. Kiernan" Subject: git: 46b606c8fd9a - main - userboot: support environment and symlinks in test application List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: stevek X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 46b606c8fd9aabfdc880ca98241df443450b5791 Auto-Submitted: auto-generated The branch main has been updated by stevek: URL: https://cgit.FreeBSD.org/src/commit/?id=46b606c8fd9aabfdc880ca98241df443450b5791 commit 46b606c8fd9aabfdc880ca98241df443450b5791 Author: Stephen J. Kiernan AuthorDate: 2024-04-30 20:40:18 +0000 Commit: Stephen J. Kiernan CommitDate: 2024-04-30 20:40:18 +0000 userboot: support environment and symlinks in test application Pass the environment on to the loader. Also define USERBOOT=1 in the environment varables. Add support for symlinks in the test application open callback. stat the root directory when opening file Without this, running "ls" command on the root directory encounters issues getting the directory listing. Reviewed by: jhb Obtained from: Juniper Networks, Inc. Differential Revision: https://reviews.freebsd.org/D44625 --- stand/userboot/test/test.c | 196 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 161 insertions(+), 35 deletions(-) diff --git a/stand/userboot/test/test.c b/stand/userboot/test/test.c index d4b7ed713336..22aaf8d7785a 100644 --- a/stand/userboot/test/test.c +++ b/stand/userboot/test/test.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 Google, Inc. + * Copyright (c) 2023-2024 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,7 +36,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -44,6 +47,8 @@ #include +char **vars; + char *host_base = NULL; struct termios term, oldterm; char *image; @@ -102,47 +107,164 @@ struct test_file { } tf_u; }; -int -test_open(void *arg, const char *filename, void **h_return) +static int +test_open_internal(void *arg, const char *filename, void **h_return, + struct test_file *tf, int depth) { - struct stat st; - struct test_file *tf; char path[PATH_MAX]; + char linkpath[PATH_MAX]; + char *component, *cp, *linkptr; + ssize_t slen; + int comp_fd, dir_fd, error; + char c; + bool openbase; + + if (depth++ >= MAXSYMLINKS) + return (ELOOP); + + openbase = false; + error = EINVAL; + if (tf == NULL) { + tf = calloc(1, sizeof(struct test_file)); + if (tf == NULL) + return (error); + openbase = true; + } else if (tf->tf_isdir) { + if (filename[0] == '/') { + closedir(tf->tf_u.dir); + openbase = true; + } + } else + return (error); - if (!host_base) - return (ENOENT); - - strlcpy(path, host_base, PATH_MAX); - if (path[strlen(path) - 1] == '/') - path[strlen(path) - 1] = 0; - strlcat(path, filename, PATH_MAX); - tf = malloc(sizeof(struct test_file)); - if (stat(path, &tf->tf_stat) < 0) { - free(tf); - return (errno); - } + if (openbase) { + dir_fd = open(host_base, O_RDONLY); + if (dir_fd < 0) + goto out; - tf->tf_size = st.st_size; - if (S_ISDIR(tf->tf_stat.st_mode)) { tf->tf_isdir = 1; - tf->tf_u.dir = opendir(path); - if (!tf->tf_u.dir) + tf->tf_u.dir = fdopendir(dir_fd); + + if (fstat(dir_fd, &tf->tf_stat) < 0) { + error = errno; goto out; - *h_return = tf; - return (0); + } + tf->tf_size = tf->tf_stat.st_size; } - if (S_ISREG(tf->tf_stat.st_mode)) { - tf->tf_isdir = 0; - tf->tf_u.fd = open(path, O_RDONLY); - if (tf->tf_u.fd < 0) + + strlcpy(path, filename, sizeof(path)); + cp = path; + while (*cp) { + /* + * The test file should be a directory at this point. + * If it is not, then the caller provided an invalid filename. + */ + if (!tf->tf_isdir) + goto out; + + /* Trim leading slashes */ + while (*cp == '/') + cp++; + + /* If we reached the end, we are done */ + if (*cp == '\0') + break; + + /* Get the file descriptor for the directory */ + dir_fd = dirfd(tf->tf_u.dir); + + /* Get the next component path */ + component = cp; + while ((c = *cp) != '\0' && c != '/') + cp++; + if (c == '/') + *cp++ = '\0'; + + /* Get status of the component */ + if (fstatat(dir_fd, component, &tf->tf_stat, + AT_SYMLINK_NOFOLLOW) < 0) { + error = errno; goto out; - *h_return = tf; - return (0); + } + tf->tf_size = tf->tf_stat.st_size; + + /* + * Check that the path component is a directory, regular file, + * or a symlink. + */ + if (!S_ISDIR(tf->tf_stat.st_mode) && + !S_ISREG(tf->tf_stat.st_mode) && + !S_ISLNK(tf->tf_stat.st_mode)) + goto out; + + /* For anything that is not a symlink, open it */ + if (!S_ISLNK(tf->tf_stat.st_mode)) { + comp_fd = openat(dir_fd, component, O_RDONLY); + if (comp_fd < 0) + goto out; + } + + if (S_ISDIR(tf->tf_stat.st_mode)) { + /* Directory */ + + /* close the parent directory */ + closedir(tf->tf_u.dir); + + /* Open the directory from the component descriptor */ + tf->tf_isdir = 1; + tf->tf_u.dir = fdopendir(comp_fd); + if (!tf->tf_u.dir) + goto out; + } else if (S_ISREG(tf->tf_stat.st_mode)) { + /* Regular file */ + + /* close the parent directory */ + closedir(tf->tf_u.dir); + + /* Stash the component descriptor */ + tf->tf_isdir = 0; + tf->tf_u.fd = comp_fd; + } else if (S_ISLNK(tf->tf_stat.st_mode)) { + /* Symlink */ + + /* Read what the symlink points to */ + slen = readlinkat(dir_fd, component, linkpath, + sizeof(linkpath)); + if (slen < 0) + goto out; + /* NUL-terminate the string */ + linkpath[(size_t)slen] = '\0'; + + /* Open the thing that the symlink points to */ + error = test_open_internal(arg, linkpath, NULL, + tf, depth); + if (error != 0) + goto out; + } } + /* Completed the entire path and have a good file/directory */ + if (h_return != NULL) + *h_return = tf; + return (0); + out: + /* Failure of some sort, clean up */ + if (tf->tf_isdir) + closedir(tf->tf_u.dir); + else + close(tf->tf_u.fd); free(tf); - return (EINVAL); + return (error); +} + +int +test_open(void *arg, const char *filename, void **h_return) +{ + if (host_base == NULL) + return (ENOENT); + + return (test_open_internal(arg, filename, h_return, NULL, 0)); } int @@ -394,13 +516,15 @@ test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem) char * test_getenv(void *arg, int idx) { - static char *vars[] = { - "foo=bar", - "bar=barbar", - NULL + static char *myvars[] = { + "USERBOOT=1" }; + static const int num_myvars = nitems(myvars); - return (vars[idx]); + if (idx < num_myvars) + return (myvars[idx]); + else + return (vars[idx - num_myvars]); } struct loader_callbacks cb = { @@ -444,7 +568,7 @@ usage() } int -main(int argc, char** argv) +main(int argc, char** argv, char ** environment) { void *h; void (*func)(struct loader_callbacks *, void *, int, int) __dead2; @@ -452,6 +576,8 @@ main(int argc, char** argv) const char *userboot_obj = "/boot/userboot.so"; int oflag = O_RDONLY; + vars = environment; + while ((opt = getopt(argc, argv, "wb:d:h:")) != -1) { switch (opt) { case 'b':