[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 |
|
---|
| 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>
|
---|
[19f857a] | 38 | #include <str.h>
|
---|
[216d6fc] | 39 | #include <fcntl.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 |
|
---|
| 49 | /* FIXME: Just have find_command() return an allocated string */
|
---|
[af4eda50] | 50 | static char *found;
|
---|
[216d6fc] | 51 |
|
---|
[88944695] | 52 | static char *find_command(char *);
|
---|
[af4eda50] | 53 | static int try_access(const char *);
|
---|
[88944695] | 54 |
|
---|
[aa8053f1] | 55 | const char *search_dir[] = { "/app", "/srv", NULL };
|
---|
[9be9c4d] | 56 |
|
---|
[216d6fc] | 57 | /* work-around for access() */
|
---|
[af4eda50] | 58 | static int try_access(const char *f)
|
---|
[216d6fc] | 59 | {
|
---|
| 60 | int fd;
|
---|
| 61 |
|
---|
| 62 | fd = open(f, O_RDONLY);
|
---|
[6afc9d7] | 63 | if (fd >= 0) {
|
---|
[216d6fc] | 64 | close(fd);
|
---|
| 65 | return 0;
|
---|
| 66 | } else
|
---|
| 67 | return -1;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /* Returns the full path of "cmd" if cmd is found, else just hand back
|
---|
| 71 | * cmd as it was presented */
|
---|
[88944695] | 72 | static char *find_command(char *cmd)
|
---|
[216d6fc] | 73 | {
|
---|
[9be9c4d] | 74 | size_t i;
|
---|
[216d6fc] | 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 | }
|
---|
[e2ea8d7e] | 82 |
|
---|
[216d6fc] | 83 | /* We now have n places to look for the command */
|
---|
[9be9c4d] | 84 | for (i = 0; search_dir[i] != NULL; i++) {
|
---|
[445e7c0] | 85 | memset(found, 0, PATH_MAX);
|
---|
[9be9c4d] | 86 | snprintf(found, PATH_MAX, "%s/%s", search_dir[i], cmd);
|
---|
[216d6fc] | 87 | if (-1 != try_access(found)) {
|
---|
| 88 | return (char *) found;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /* We didn't find it, just give it back as-is. */
|
---|
| 93 | return (char *) cmd;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[6ea9a1d] | 96 | unsigned int try_exec(char *cmd, char **argv, iostate_t *io)
|
---|
[216d6fc] | 97 | {
|
---|
| 98 | task_id_t tid;
|
---|
[1c635d6] | 99 | task_wait_t twait;
|
---|
[adb49f58] | 100 | task_exit_t texit;
|
---|
[216d6fc] | 101 | char *tmp;
|
---|
[ae45201] | 102 | int rc, retval, i;
|
---|
[7171760] | 103 | int file_handles[3];
|
---|
| 104 | int *file_handles_p[4];
|
---|
[6ea9a1d] | 105 | FILE *files[3];
|
---|
[216d6fc] | 106 |
|
---|
[095003a8] | 107 | tmp = str_dup(find_command(cmd));
|
---|
[216d6fc] | 108 | free(found);
|
---|
[ae45201] | 109 |
|
---|
[6ea9a1d] | 110 | files[0] = io->stdin;
|
---|
| 111 | files[1] = io->stdout;
|
---|
| 112 | files[2] = io->stderr;
|
---|
| 113 |
|
---|
[ae45201] | 114 | for (i = 0; i < 3 && files[i] != NULL; i++) {
|
---|
[6afc9d7] | 115 | if (vfs_fhandle(files[i], &file_handles[i]) == EOK) {
|
---|
[7171760] | 116 | file_handles_p[i] = &file_handles[i];
|
---|
[ae45201] | 117 | }
|
---|
| 118 | else {
|
---|
[7171760] | 119 | file_handles_p[i] = NULL;
|
---|
[ae45201] | 120 | }
|
---|
| 121 | }
|
---|
[7171760] | 122 | file_handles_p[i] = NULL;
|
---|
[216d6fc] | 123 |
|
---|
[1c635d6] | 124 | rc = task_spawnvf(&tid, &twait, tmp, (const char **) argv, file_handles_p);
|
---|
[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 |
|
---|
[1c635d6] | 133 | rc = task_wait(&twait, &texit, &retval);
|
---|
[0485135] | 134 | if (rc != EOK) {
|
---|
[7e752b2] | 135 | printf("%s: Failed waiting for command (%s)\n", progname,
|
---|
| 136 | str_error(rc));
|
---|
[c4a8e4a] | 137 | return 1;
|
---|
[0485135] | 138 | } else if (texit != TASK_EXIT_NORMAL) {
|
---|
[a397f1d] | 139 | printf("%s: Command failed (unexpectedly terminated)\n", progname);
|
---|
[c4a8e4a] | 140 | return 1;
|
---|
[adb49f58] | 141 | } else if (retval != 0) {
|
---|
[0485135] | 142 | printf("%s: Command failed (exit code %d)\n",
|
---|
| 143 | progname, retval);
|
---|
[c4a8e4a] | 144 | return 1;
|
---|
[adb49f58] | 145 | }
|
---|
[7114d83] | 146 |
|
---|
[73878c1] | 147 | return 0;
|
---|
[216d6fc] | 148 | }
|
---|