| [36ab7c7] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Tim Post
|
|---|
| [216d6fc] | 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| [36ab7c7] | 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| [216d6fc] | 8 | *
|
|---|
| [36ab7c7] | 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.
|
|---|
| [216d6fc] | 16 | *
|
|---|
| [36ab7c7] | 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.
|
|---|
| [216d6fc] | 27 | */
|
|---|
| 28 |
|
|---|
| [7c3fb9b] | 29 | /*
|
|---|
| 30 | * The VERY basics of execute in place support. These are buggy, leaky
|
|---|
| [216d6fc] | 31 | * and not nearly done. Only here for beta testing!! You were warned!!
|
|---|
| 32 | * TODO:
|
|---|
| 33 | * Hash command lookups to save time
|
|---|
| [7c3fb9b] | 34 | * Create a running pointer to **path and advance/rewind it as we go
|
|---|
| 35 | */
|
|---|
| [216d6fc] | 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| [19f857a] | 39 | #include <str.h>
|
|---|
| [2721a75] | 40 | #include <str_error.h>
|
|---|
| [0485135] | 41 | #include <errno.h>
|
|---|
| [df02460] | 42 | #include <vfs/vfs.h>
|
|---|
| [216d6fc] | 43 |
|
|---|
| 44 | #include "config.h"
|
|---|
| 45 | #include "util.h"
|
|---|
| 46 | #include "exec.h"
|
|---|
| 47 | #include "errors.h"
|
|---|
| 48 |
|
|---|
| [587867a] | 49 | static errno_t find_command(char *, char **);
|
|---|
| [af4eda50] | 50 | static int try_access(const char *);
|
|---|
| [88944695] | 51 |
|
|---|
| [31872f7] | 52 | const char *search_dir[] = { "/app", NULL };
|
|---|
| [9be9c4d] | 53 |
|
|---|
| [216d6fc] | 54 | /* work-around for access() */
|
|---|
| [af4eda50] | 55 | static int try_access(const char *f)
|
|---|
| [216d6fc] | 56 | {
|
|---|
| 57 | int fd;
|
|---|
| 58 |
|
|---|
| [b7fd2a0] | 59 | errno_t rc = vfs_lookup_open(f, WALK_REGULAR, MODE_READ, &fd);
|
|---|
| [f77c1c9] | 60 | if (rc == EOK) {
|
|---|
| [9c4cf0d] | 61 | vfs_put(fd);
|
|---|
| [216d6fc] | 62 | return 0;
|
|---|
| 63 | } else
|
|---|
| 64 | return -1;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| [587867a] | 67 | /** Returns EOK if no internal failure or else ENOMEM
|
|---|
| [7c3fb9b] | 68 | *
|
|---|
| [587867a] | 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
|
|---|
| [7c3fb9b] | 72 | */
|
|---|
| [587867a] | 73 | static errno_t find_command(char *cmd, char **found)
|
|---|
| [216d6fc] | 74 | {
|
|---|
| 75 | /* The user has specified a full or relative path, just give it back. */
|
|---|
| [1c481ee] | 76 | if (is_path(cmd)) {
|
|---|
| 77 | if (-1 != try_access(cmd)) {
|
|---|
| [587867a] | 78 | *found = str_dup(cmd);
|
|---|
| 79 | return EOK;
|
|---|
| [1c481ee] | 80 | }
|
|---|
| [587867a] | 81 |
|
|---|
| 82 | *found = NULL;
|
|---|
| 83 | return EOK;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | *found = (char *)malloc(PATH_MAX);
|
|---|
| 87 | if (*found == NULL) {
|
|---|
| 88 | return ENOMEM;
|
|---|
| [216d6fc] | 89 | }
|
|---|
| [e2ea8d7e] | 90 |
|
|---|
| [216d6fc] | 91 | /* We now have n places to look for the command */
|
|---|
| [c56a3eb] | 92 | size_t i;
|
|---|
| [587867a] | 93 | size_t cmd_length = str_length(cmd);
|
|---|
| [9be9c4d] | 94 | for (i = 0; search_dir[i] != NULL; i++) {
|
|---|
| [587867a] | 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;
|
|---|
| [216d6fc] | 104 | }
|
|---|
| 105 | }
|
|---|
| [587867a] | 106 | free(*found);
|
|---|
| 107 | *found = NULL;
|
|---|
| [216d6fc] | 108 |
|
|---|
| [587867a] | 109 | /* We didn't find it, return NULL */
|
|---|
| 110 | return EOK;
|
|---|
| [216d6fc] | 111 | }
|
|---|
| 112 |
|
|---|
| [6ea9a1d] | 113 | unsigned int try_exec(char *cmd, char **argv, iostate_t *io)
|
|---|
| [216d6fc] | 114 | {
|
|---|
| 115 | task_id_t tid;
|
|---|
| [1c635d6] | 116 | task_wait_t twait;
|
|---|
| [adb49f58] | 117 | task_exit_t texit;
|
|---|
| [216d6fc] | 118 | char *tmp;
|
|---|
| [b7fd2a0] | 119 | errno_t rc;
|
|---|
| [d5c1051] | 120 | int retval, i;
|
|---|
| [bb9ec2d] | 121 | int file_handles[3] = { -1, -1, -1 };
|
|---|
| [6ea9a1d] | 122 | FILE *files[3];
|
|---|
| [216d6fc] | 123 |
|
|---|
| [587867a] | 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 | }
|
|---|
| [a35b458] | 134 |
|
|---|
| [6ea9a1d] | 135 | files[0] = io->stdin;
|
|---|
| 136 | files[1] = io->stdout;
|
|---|
| 137 | files[2] = io->stderr;
|
|---|
| [a35b458] | 138 |
|
|---|
| [ae45201] | 139 | for (i = 0; i < 3 && files[i] != NULL; i++) {
|
|---|
| [bb9ec2d] | 140 | vfs_fhandle(files[i], &file_handles[i]);
|
|---|
| [ae45201] | 141 | }
|
|---|
| [216d6fc] | 142 |
|
|---|
| [bb9ec2d] | 143 | rc = task_spawnvf(&tid, &twait, tmp, (const char **) argv,
|
|---|
| 144 | file_handles[0], file_handles[1], file_handles[2]);
|
|---|
| [216d6fc] | 145 | free(tmp);
|
|---|
| 146 |
|
|---|
| [a53ed3a] | 147 | if (rc != EOK) {
|
|---|
| [d9fae235] | 148 | cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
|
|---|
| [0485135] | 149 | str_error(rc));
|
|---|
| [216d6fc] | 150 | return 1;
|
|---|
| 151 | }
|
|---|
| [a35b458] | 152 |
|
|---|
| [1c635d6] | 153 | rc = task_wait(&twait, &texit, &retval);
|
|---|
| [0485135] | 154 | if (rc != EOK) {
|
|---|
| [7e752b2] | 155 | printf("%s: Failed waiting for command (%s)\n", progname,
|
|---|
| 156 | str_error(rc));
|
|---|
| [c4a8e4a] | 157 | return 1;
|
|---|
| [0485135] | 158 | } else if (texit != TASK_EXIT_NORMAL) {
|
|---|
| [a397f1d] | 159 | printf("%s: Command failed (unexpectedly terminated)\n", progname);
|
|---|
| [c4a8e4a] | 160 | return 1;
|
|---|
| [adb49f58] | 161 | } else if (retval != 0) {
|
|---|
| [0485135] | 162 | printf("%s: Command failed (exit code %d)\n",
|
|---|
| 163 | progname, retval);
|
|---|
| [c4a8e4a] | 164 | return 1;
|
|---|
| [adb49f58] | 165 | }
|
|---|
| [7114d83] | 166 |
|
|---|
| [73878c1] | 167 | return 0;
|
|---|
| [216d6fc] | 168 | }
|
|---|