| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Tim Post
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /*
|
|---|
| 30 | * The VERY basics of execute in place support. These are buggy, leaky
|
|---|
| 31 | * and not nearly done. Only here for beta testing!! You were warned!!
|
|---|
| 32 | * TODO:
|
|---|
| 33 | * Hash command lookups to save time
|
|---|
| 34 | * Create a running pointer to **path and advance/rewind it as we go
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <vfs/vfs.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "config.h"
|
|---|
| 45 | #include "util.h"
|
|---|
| 46 | #include "exec.h"
|
|---|
| 47 | #include "errors.h"
|
|---|
| 48 |
|
|---|
| 49 | static errno_t find_command(char *, char **);
|
|---|
| 50 | static int try_access(const char *);
|
|---|
| 51 |
|
|---|
| 52 | const char *search_dir[] = { "/app", NULL };
|
|---|
| 53 |
|
|---|
| 54 | /* work-around for access() */
|
|---|
| 55 | static int try_access(const char *f)
|
|---|
| 56 | {
|
|---|
| 57 | int fd;
|
|---|
| 58 |
|
|---|
| 59 | errno_t rc = vfs_lookup_open(f, WALK_REGULAR, MODE_READ, &fd);
|
|---|
| 60 | if (rc == EOK) {
|
|---|
| 61 | vfs_put(fd);
|
|---|
| 62 | return 0;
|
|---|
| 63 | } else
|
|---|
| 64 | return -1;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /** Returns EOK if no internal failure or else ENOMEM
|
|---|
| 68 | *
|
|---|
| 69 | * When the parameter `cmd` can be found then the absolute path will be set in `found`.
|
|---|
| 70 | * Or else `found` will be NULL.
|
|---|
| 71 | * `found` will be newly allocated and must be freed by the caller
|
|---|
| 72 | */
|
|---|
| 73 | static errno_t find_command(char *cmd, char **found)
|
|---|
| 74 | {
|
|---|
| 75 | /* The user has specified a full or relative path, just give it back. */
|
|---|
| 76 | if (is_path(cmd)) {
|
|---|
| 77 | if (-1 != try_access(cmd)) {
|
|---|
| 78 | *found = str_dup(cmd);
|
|---|
| 79 | return EOK;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | *found = NULL;
|
|---|
| 83 | return EOK;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | *found = (char *)malloc(PATH_MAX);
|
|---|
| 87 | if (*found == NULL) {
|
|---|
| 88 | return ENOMEM;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /* We now have n places to look for the command */
|
|---|
| 92 | size_t i;
|
|---|
| 93 | size_t cmd_length = str_length(cmd);
|
|---|
| 94 | for (i = 0; search_dir[i] != NULL; i++) {
|
|---|
| 95 | if (str_length(search_dir[i]) + cmd_length + 2 > PATH_MAX) {
|
|---|
| 96 | free(*found);
|
|---|
| 97 | return ENOMEM;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | memset(*found, 0, PATH_MAX);
|
|---|
| 101 | snprintf(*found, PATH_MAX, "%s/%s", search_dir[i], cmd);
|
|---|
| 102 | if (-1 != try_access(*found)) {
|
|---|
| 103 | return EOK;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | free(*found);
|
|---|
| 107 | *found = NULL;
|
|---|
| 108 |
|
|---|
| 109 | /* We didn't find it, return NULL */
|
|---|
| 110 | return EOK;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | unsigned int try_exec(char *cmd, char **argv, iostate_t *io)
|
|---|
| 114 | {
|
|---|
| 115 | task_id_t tid;
|
|---|
| 116 | task_wait_t twait;
|
|---|
| 117 | task_exit_t texit;
|
|---|
| 118 | char *tmp;
|
|---|
| 119 | errno_t rc;
|
|---|
| 120 | int retval, i;
|
|---|
| 121 | int file_handles[3] = { -1, -1, -1 };
|
|---|
| 122 | FILE *files[3];
|
|---|
| 123 |
|
|---|
| 124 | rc = find_command(cmd, &tmp);
|
|---|
| 125 | if (rc != EOK) {
|
|---|
| 126 | cli_error(CL_ENOMEM, "%s: failure executing find_command()", progname);
|
|---|
| 127 | return 1;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | if (tmp == NULL) {
|
|---|
| 131 | cli_error(CL_EEXEC, "%s: Command not found '%s'", progname, cmd);
|
|---|
| 132 | return 1;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | files[0] = io->stdin;
|
|---|
| 136 | files[1] = io->stdout;
|
|---|
| 137 | files[2] = io->stderr;
|
|---|
| 138 |
|
|---|
| 139 | for (i = 0; i < 3 && files[i] != NULL; i++) {
|
|---|
| 140 | vfs_fhandle(files[i], &file_handles[i]);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | rc = task_spawnvf(&tid, &twait, tmp, (const char **) argv,
|
|---|
| 144 | file_handles[0], file_handles[1], file_handles[2]);
|
|---|
| 145 | free(tmp);
|
|---|
| 146 |
|
|---|
| 147 | if (rc != EOK) {
|
|---|
| 148 | cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
|
|---|
| 149 | str_error(rc));
|
|---|
| 150 | return 1;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | rc = task_wait(&twait, &texit, &retval);
|
|---|
| 154 | if (rc != EOK) {
|
|---|
| 155 | printf("%s: Failed waiting for command (%s)\n", progname,
|
|---|
| 156 | str_error(rc));
|
|---|
| 157 | return 1;
|
|---|
| 158 | } else if (texit != TASK_EXIT_NORMAL) {
|
|---|
| 159 | printf("%s: Command failed (unexpectedly terminated)\n", progname);
|
|---|
| 160 | return 1;
|
|---|
| 161 | } else if (retval != 0) {
|
|---|
| 162 | printf("%s: Command failed (exit code %d)\n",
|
|---|
| 163 | progname, retval);
|
|---|
| 164 | return 1;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | return 0;
|
|---|
| 168 | }
|
|---|