[216d6fc] | 1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
|
---|
| 2 | * All rights reserved.
|
---|
| 3 | *
|
---|
| 4 | * Redistribution and use in source and binary forms, with or without
|
---|
| 5 | * modification, are permitted provided that the following conditions are met:
|
---|
| 6 | *
|
---|
| 7 | * Redistributions of source code must retain the above copyright notice, this
|
---|
| 8 | * list of conditions and the following disclaimer.
|
---|
| 9 | *
|
---|
| 10 | * Redistributions in binary form must reproduce the above copyright notice,
|
---|
| 11 | * this list of conditions and the following disclaimer in the documentation
|
---|
| 12 | * and/or other materials provided with the distribution.
|
---|
| 13 | *
|
---|
| 14 | * Neither the name of the original program's authors nor the names of its
|
---|
| 15 | * contributors may be used to endorse or promote products derived from this
|
---|
| 16 | * software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
---|
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
| 28 | * POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | /* The VERY basics of execute in place support. These are buggy, leaky
|
---|
| 32 | * and not nearly done. Only here for beta testing!! You were warned!!
|
---|
| 33 | * TODO:
|
---|
| 34 | * Hash command lookups to save time
|
---|
| 35 | * Create a running pointer to **path and advance/rewind it as we go */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <unistd.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[216d6fc] | 41 | #include <fcntl.h>
|
---|
[2721a75] | 42 | #include <str_error.h>
|
---|
[0485135] | 43 | #include <errno.h>
|
---|
[216d6fc] | 44 |
|
---|
| 45 | #include "config.h"
|
---|
| 46 | #include "util.h"
|
---|
| 47 | #include "exec.h"
|
---|
| 48 | #include "errors.h"
|
---|
| 49 |
|
---|
| 50 | /* FIXME: Just have find_command() return an allocated string */
|
---|
[af4eda50] | 51 | static char *found;
|
---|
[216d6fc] | 52 |
|
---|
[88944695] | 53 | static char *find_command(char *);
|
---|
[af4eda50] | 54 | static int try_access(const char *);
|
---|
[88944695] | 55 |
|
---|
[216d6fc] | 56 | /* work-around for access() */
|
---|
[af4eda50] | 57 | static int try_access(const char *f)
|
---|
[216d6fc] | 58 | {
|
---|
| 59 | int fd;
|
---|
| 60 |
|
---|
| 61 | fd = open(f, O_RDONLY);
|
---|
| 62 | if (fd > -1) {
|
---|
| 63 | close(fd);
|
---|
| 64 | return 0;
|
---|
| 65 | } else
|
---|
| 66 | return -1;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /* Returns the full path of "cmd" if cmd is found, else just hand back
|
---|
| 70 | * cmd as it was presented */
|
---|
[88944695] | 71 | static char *find_command(char *cmd)
|
---|
[216d6fc] | 72 | {
|
---|
[e2ea8d7e] | 73 | char *path_tok;
|
---|
[216d6fc] | 74 | char *path[PATH_MAX];
|
---|
| 75 | int n = 0, i = 0;
|
---|
[92fd52d7] | 76 | size_t x = str_size(cmd) + 2;
|
---|
[216d6fc] | 77 |
|
---|
| 78 | found = (char *)malloc(PATH_MAX);
|
---|
| 79 |
|
---|
| 80 | /* The user has specified a full or relative path, just give it back. */
|
---|
| 81 | if (-1 != try_access(cmd)) {
|
---|
| 82 | return (char *) cmd;
|
---|
| 83 | }
|
---|
[e2ea8d7e] | 84 |
|
---|
[095003a8] | 85 | path_tok = str_dup(PATH);
|
---|
[216d6fc] | 86 |
|
---|
| 87 | /* Extract the PATH env to a path[] array */
|
---|
[4cc0c9ee] | 88 | path[n] = strtok(path_tok, PATH_DELIM);
|
---|
[216d6fc] | 89 | while (NULL != path[n]) {
|
---|
[92fd52d7] | 90 | if ((str_size(path[n]) + x ) > PATH_MAX) {
|
---|
[216d6fc] | 91 | cli_error(CL_ENOTSUP,
|
---|
| 92 | "Segment %d of path is too large, search ends at segment %d",
|
---|
| 93 | n, n-1);
|
---|
| 94 | break;
|
---|
| 95 | }
|
---|
[4cc0c9ee] | 96 | path[++n] = strtok(NULL, PATH_DELIM);
|
---|
[216d6fc] | 97 | }
|
---|
| 98 |
|
---|
| 99 | /* We now have n places to look for the command */
|
---|
| 100 | for (i=0; path[i]; i++) {
|
---|
| 101 | memset(found, 0, sizeof(found));
|
---|
| 102 | snprintf(found, PATH_MAX, "%s/%s", path[i], cmd);
|
---|
| 103 | if (-1 != try_access(found)) {
|
---|
| 104 | free(path_tok);
|
---|
| 105 | return (char *) found;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /* We didn't find it, just give it back as-is. */
|
---|
| 110 | free(path_tok);
|
---|
| 111 | return (char *) cmd;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | unsigned int try_exec(char *cmd, char **argv)
|
---|
| 115 | {
|
---|
| 116 | task_id_t tid;
|
---|
[adb49f58] | 117 | task_exit_t texit;
|
---|
[216d6fc] | 118 | char *tmp;
|
---|
[0485135] | 119 | int rc, retval;
|
---|
[216d6fc] | 120 |
|
---|
[095003a8] | 121 | tmp = str_dup(find_command(cmd));
|
---|
[216d6fc] | 122 | free(found);
|
---|
| 123 |
|
---|
[0485135] | 124 | rc = task_spawnv(&tid, tmp, (const char **) argv);
|
---|
[216d6fc] | 125 | free(tmp);
|
---|
| 126 |
|
---|
[0485135] | 127 | if (rc != 0) {
|
---|
[d9fae235] | 128 | cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
|
---|
[0485135] | 129 | str_error(rc));
|
---|
[216d6fc] | 130 | return 1;
|
---|
| 131 | }
|
---|
[73878c1] | 132 |
|
---|
[0485135] | 133 | rc = task_wait(tid, &texit, &retval);
|
---|
| 134 | if (rc != EOK) {
|
---|
[7e752b2] | 135 | printf("%s: Failed waiting for command (%s)\n", progname,
|
---|
| 136 | str_error(rc));
|
---|
[0485135] | 137 | } else if (texit != TASK_EXIT_NORMAL) {
|
---|
[a397f1d] | 138 | printf("%s: Command failed (unexpectedly terminated)\n", progname);
|
---|
[adb49f58] | 139 | } else if (retval != 0) {
|
---|
[0485135] | 140 | printf("%s: Command failed (exit code %d)\n",
|
---|
| 141 | progname, retval);
|
---|
[adb49f58] | 142 | }
|
---|
[7114d83] | 143 |
|
---|
[73878c1] | 144 | return 0;
|
---|
[216d6fc] | 145 | }
|
---|