| 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 | /* The VERY basics of execute in place support. These are buggy, leaky
|
|---|
| 30 | * and not nearly done. Only here for beta testing!! You were warned!!
|
|---|
| 31 | * TODO:
|
|---|
| 32 | * Hash command lookups to save time
|
|---|
| 33 | * Create a running pointer to **path and advance/rewind it as we go */
|
|---|
| 34 |
|
|---|
| 35 | #include <stdio.h>
|
|---|
| 36 | #include <stdlib.h>
|
|---|
| 37 | #include <unistd.h>
|
|---|
| 38 | #include <str.h>
|
|---|
| 39 | #include <fcntl.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "config.h"
|
|---|
| 44 | #include "util.h"
|
|---|
| 45 | #include "exec.h"
|
|---|
| 46 | #include "errors.h"
|
|---|
| 47 |
|
|---|
| 48 | /* FIXME: Just have find_command() return an allocated string */
|
|---|
| 49 | static char *found;
|
|---|
| 50 |
|
|---|
| 51 | static char *find_command(char *);
|
|---|
| 52 | static int try_access(const char *);
|
|---|
| 53 |
|
|---|
| 54 | /* work-around for access() */
|
|---|
| 55 | static int try_access(const char *f)
|
|---|
| 56 | {
|
|---|
| 57 | int fd;
|
|---|
| 58 |
|
|---|
| 59 | fd = open(f, O_RDONLY);
|
|---|
| 60 | if (fd > -1) {
|
|---|
| 61 | close(fd);
|
|---|
| 62 | return 0;
|
|---|
| 63 | } else
|
|---|
| 64 | return -1;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /* Returns the full path of "cmd" if cmd is found, else just hand back
|
|---|
| 68 | * cmd as it was presented */
|
|---|
| 69 | static char *find_command(char *cmd)
|
|---|
| 70 | {
|
|---|
| 71 | char *path_tok;
|
|---|
| 72 | char *path[PATH_MAX];
|
|---|
| 73 | int n = 0, i = 0;
|
|---|
| 74 | size_t x = str_size(cmd) + 2;
|
|---|
| 75 |
|
|---|
| 76 | found = (char *)malloc(PATH_MAX);
|
|---|
| 77 |
|
|---|
| 78 | /* The user has specified a full or relative path, just give it back. */
|
|---|
| 79 | if (-1 != try_access(cmd)) {
|
|---|
| 80 | return (char *) cmd;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | path_tok = str_dup(PATH);
|
|---|
| 84 |
|
|---|
| 85 | /* Extract the PATH env to a path[] array */
|
|---|
| 86 | path[n] = strtok(path_tok, PATH_DELIM);
|
|---|
| 87 | while (NULL != path[n]) {
|
|---|
| 88 | if ((str_size(path[n]) + x ) > PATH_MAX) {
|
|---|
| 89 | cli_error(CL_ENOTSUP,
|
|---|
| 90 | "Segment %d of path is too large, search ends at segment %d",
|
|---|
| 91 | n, n-1);
|
|---|
| 92 | break;
|
|---|
| 93 | }
|
|---|
| 94 | path[++n] = strtok(NULL, PATH_DELIM);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /* We now have n places to look for the command */
|
|---|
| 98 | for (i=0; path[i]; i++) {
|
|---|
| 99 | memset(found, 0, sizeof(found));
|
|---|
| 100 | snprintf(found, PATH_MAX, "%s/%s", path[i], cmd);
|
|---|
| 101 | if (-1 != try_access(found)) {
|
|---|
| 102 | free(path_tok);
|
|---|
| 103 | return (char *) found;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /* We didn't find it, just give it back as-is. */
|
|---|
| 108 | free(path_tok);
|
|---|
| 109 | return (char *) cmd;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | unsigned int try_exec(char *cmd, char **argv, iostate_t *io)
|
|---|
| 113 | {
|
|---|
| 114 | task_id_t tid;
|
|---|
| 115 | task_exit_t texit;
|
|---|
| 116 | char *tmp;
|
|---|
| 117 | int rc, retval, i;
|
|---|
| 118 | fdi_node_t file_nodes[3];
|
|---|
| 119 | fdi_node_t *file_nodes_p[4];
|
|---|
| 120 | FILE *files[3];
|
|---|
| 121 |
|
|---|
| 122 | tmp = str_dup(find_command(cmd));
|
|---|
| 123 | free(found);
|
|---|
| 124 |
|
|---|
| 125 | files[0] = io->stdin;
|
|---|
| 126 | files[1] = io->stdout;
|
|---|
| 127 | files[2] = io->stderr;
|
|---|
| 128 |
|
|---|
| 129 | for (i = 0; i < 3 && files[i] != NULL; i++) {
|
|---|
| 130 | if (fnode(files[i], &file_nodes[i]) == EOK) {
|
|---|
| 131 | file_nodes_p[i] = &file_nodes[i];
|
|---|
| 132 | }
|
|---|
| 133 | else {
|
|---|
| 134 | file_nodes_p[i] = NULL;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | file_nodes_p[i] = NULL;
|
|---|
| 138 |
|
|---|
| 139 | rc = task_spawnvf(&tid, tmp, (const char **) argv, file_nodes_p);
|
|---|
| 140 | free(tmp);
|
|---|
| 141 |
|
|---|
| 142 | if (rc != 0) {
|
|---|
| 143 | cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
|
|---|
| 144 | str_error(rc));
|
|---|
| 145 | return 1;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | rc = task_wait(tid, &texit, &retval);
|
|---|
| 149 | if (rc != EOK) {
|
|---|
| 150 | printf("%s: Failed waiting for command (%s)\n", progname,
|
|---|
| 151 | str_error(rc));
|
|---|
| 152 | } else if (texit != TASK_EXIT_NORMAL) {
|
|---|
| 153 | printf("%s: Command failed (unexpectedly terminated)\n", progname);
|
|---|
| 154 | } else if (retval != 0) {
|
|---|
| 155 | printf("%s: Command failed (exit code %d)\n",
|
|---|
| 156 | progname, retval);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | return 0;
|
|---|
| 160 | }
|
|---|