Changeset View
Changeset View
Standalone View
Standalone View
stand/userboot/test/test.c
| /*- | /*- | ||||||||
| * Copyright (c) 2011 Google, Inc. | * Copyright (c) 2011 Google, Inc. | ||||||||
| * Copyright (c) 2023-2024 Juniper Networks, Inc. | |||||||||
| * All rights reserved. | * All rights reserved. | ||||||||
| * | * | ||||||||
| * Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||||||
| * modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||||||
| * are met: | * are met: | ||||||||
| * 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||||||
| * notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | ||||||||
| * 2. Redistributions in binary form must reproduce the above copyright | * 2. Redistributions in binary form must reproduce the above copyright | ||||||||
| Show All 19 Lines | |||||||||
| #include <sys/stat.h> | #include <sys/stat.h> | ||||||||
| #include <dirent.h> | #include <dirent.h> | ||||||||
| #include <dlfcn.h> | #include <dlfcn.h> | ||||||||
| #include <err.h> | #include <err.h> | ||||||||
| #include <errno.h> | #include <errno.h> | ||||||||
| #include <fcntl.h> | #include <fcntl.h> | ||||||||
| #include <getopt.h> | #include <getopt.h> | ||||||||
| #include <inttypes.h> | #include <inttypes.h> | ||||||||
| #include <libgen.h> | |||||||||
| #include <limits.h> | #include <limits.h> | ||||||||
| #include <stdbool.h> | |||||||||
| #include <stdio.h> | #include <stdio.h> | ||||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||||
| #include <string.h> | #include <string.h> | ||||||||
| #include <termios.h> | #include <termios.h> | ||||||||
| #include <unistd.h> | #include <unistd.h> | ||||||||
| #include <userboot.h> | #include <userboot.h> | ||||||||
| char **vars; | |||||||||
| char *host_base = NULL; | char *host_base = NULL; | ||||||||
| struct termios term, oldterm; | struct termios term, oldterm; | ||||||||
| char *image; | char *image; | ||||||||
| size_t image_size; | size_t image_size; | ||||||||
| uint64_t regs[16]; | uint64_t regs[16]; | ||||||||
| uint64_t pc; | uint64_t pc; | ||||||||
| int *disk_fd; | int *disk_fd; | ||||||||
| ▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | struct test_file { | ||||||||
| size_t tf_size; | size_t tf_size; | ||||||||
| struct stat tf_stat; | struct stat tf_stat; | ||||||||
| union { | union { | ||||||||
| int fd; | int fd; | ||||||||
| DIR *dir; | DIR *dir; | ||||||||
| } tf_u; | } tf_u; | ||||||||
| }; | }; | ||||||||
| int | static int | ||||||||
| test_open(void *arg, const char *filename, void **h_return) | 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 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 (!host_base) | if (depth++ >= MAXSYMLINKS) | ||||||||
jhb: Do you still want this case? I think in the new code open() will fail with `EFAULT` due to the… | |||||||||
Done Inline ActionsThat case is in the test_open() function. stevek: That case is in the test_open() function. | |||||||||
| return (ENOENT); | return (ELOOP); | ||||||||
| strlcpy(path, host_base, PATH_MAX); | openbase = false; | ||||||||
| if (path[strlen(path) - 1] == '/') | error = EINVAL; | ||||||||
| path[strlen(path) - 1] = 0; | if (tf == NULL) { | ||||||||
| strlcat(path, filename, PATH_MAX); | tf = calloc(1, sizeof(struct test_file)); | ||||||||
| tf = malloc(sizeof(struct test_file)); | if (tf == NULL) | ||||||||
| if (stat(path, &tf->tf_stat) < 0) { | return (error); | ||||||||
| free(tf); | openbase = true; | ||||||||
| return (errno); | } else if (tf->tf_isdir) { | ||||||||
| if (filename[0] == '/') { | |||||||||
| closedir(tf->tf_u.dir); | |||||||||
| openbase = true; | |||||||||
| } | } | ||||||||
| } else | |||||||||
| return (error); | |||||||||
| tf->tf_size = st.st_size; | if (openbase) { | ||||||||
| if (S_ISDIR(tf->tf_stat.st_mode)) { | dir_fd = open(host_base, O_RDONLY); | ||||||||
| if (dir_fd < 0) | |||||||||
| goto out; | |||||||||
| tf->tf_isdir = 1; | tf->tf_isdir = 1; | ||||||||
| tf->tf_u.dir = opendir(path); | tf->tf_u.dir = fdopendir(dir_fd); | ||||||||
| if (!tf->tf_u.dir) | |||||||||
| if (fstat(dir_fd, &tf->tf_stat) < 0) { | |||||||||
| error = errno; | |||||||||
| goto out; | goto out; | ||||||||
| *h_return = tf; | |||||||||
| return (0); | |||||||||
| } | } | ||||||||
| if (S_ISREG(tf->tf_stat.st_mode)) { | tf->tf_size = tf->tf_stat.st_size; | ||||||||
| 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; | |||||||||
| } | |||||||||
| 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; | 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; | *h_return = tf; | ||||||||
| return (0); | return (0); | ||||||||
| } | |||||||||
| out: | out: | ||||||||
| /* Failure of some sort, clean up */ | |||||||||
| if (tf->tf_isdir) | |||||||||
| closedir(tf->tf_u.dir); | |||||||||
| else | |||||||||
| close(tf->tf_u.fd); | |||||||||
| free(tf); | 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 | int | ||||||||
| test_close(void *arg, void *h) | test_close(void *arg, void *h) | ||||||||
| { | { | ||||||||
| struct test_file *tf = h; | struct test_file *tf = h; | ||||||||
| if (tf->tf_isdir) | if (tf->tf_isdir) | ||||||||
| ▲ Show 20 Lines • Show All 235 Lines • ▼ Show 20 Lines | test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem) | ||||||||
| *lowmem = 128*1024*1024; | *lowmem = 128*1024*1024; | ||||||||
| *highmem = 0; | *highmem = 0; | ||||||||
| } | } | ||||||||
| char * | char * | ||||||||
| test_getenv(void *arg, int idx) | test_getenv(void *arg, int idx) | ||||||||
| { | { | ||||||||
| static char *vars[] = { | static char *myvars[] = { | ||||||||
| "foo=bar", | "USERBOOT=1" | ||||||||
| "bar=barbar", | |||||||||
| NULL | |||||||||
| }; | }; | ||||||||
| static const int num_myvars = nitems(myvars); | |||||||||
| return (vars[idx]); | if (idx < num_myvars) | ||||||||
| return (myvars[idx]); | |||||||||
Not Done Inline Actions
jhb: | |||||||||
| else | |||||||||
| return (vars[idx - num_myvars]); | |||||||||
| } | } | ||||||||
| struct loader_callbacks cb = { | struct loader_callbacks cb = { | ||||||||
| .putc = test_putc, | .putc = test_putc, | ||||||||
| .getc = test_getc, | .getc = test_getc, | ||||||||
| .poll = test_poll, | .poll = test_poll, | ||||||||
| .open = test_open, | .open = test_open, | ||||||||
| Show All 27 Lines | |||||||||
| usage() | usage() | ||||||||
| { | { | ||||||||
| printf("usage: [-b <userboot shared object>] [-d <disk image path>] [-h <host filesystem path>\n"); | printf("usage: [-b <userboot shared object>] [-d <disk image path>] [-h <host filesystem path>\n"); | ||||||||
| exit(1); | exit(1); | ||||||||
| } | } | ||||||||
| int | int | ||||||||
| main(int argc, char** argv) | main(int argc, char** argv, char ** environment) | ||||||||
| { | { | ||||||||
| void *h; | void *h; | ||||||||
| void (*func)(struct loader_callbacks *, void *, int, int) __dead2; | void (*func)(struct loader_callbacks *, void *, int, int) __dead2; | ||||||||
| int opt; | int opt; | ||||||||
| const char *userboot_obj = "/boot/userboot.so"; | const char *userboot_obj = "/boot/userboot.so"; | ||||||||
| int oflag = O_RDONLY; | int oflag = O_RDONLY; | ||||||||
| vars = environment; | |||||||||
| while ((opt = getopt(argc, argv, "wb:d:h:")) != -1) { | while ((opt = getopt(argc, argv, "wb:d:h:")) != -1) { | ||||||||
| switch (opt) { | switch (opt) { | ||||||||
| case 'b': | case 'b': | ||||||||
| userboot_obj = optarg; | userboot_obj = optarg; | ||||||||
| break; | break; | ||||||||
| case 'd': | case 'd': | ||||||||
| disk_index++; | disk_index++; | ||||||||
| ▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines | |||||||||
Do you still want this case? I think in the new code open() will fail with EFAULT due to the NULL path pointer, but ENOENT does seem like it might be the better error code if there is no host base path.