| 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 <str.h>
|
|---|
| 38 | #include <str_error.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <vfs/vfs.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "config.h"
|
|---|
| 43 | #include "util.h"
|
|---|
| 44 | #include "exec.h"
|
|---|
| 45 | #include "errors.h"
|
|---|
| 46 |
|
|---|
| 47 | /* FIXME: Just have find_command() return an allocated string */
|
|---|
| 48 | static char *found;
|
|---|
| 49 |
|
|---|
| 50 | static char *find_command(char *);
|
|---|
| 51 | static int try_access(const char *);
|
|---|
| 52 |
|
|---|
| 53 | const char *search_dir[] = { "/app", "/srv", NULL };
|
|---|
| 54 |
|
|---|
| 55 | /* work-around for access() */
|
|---|
| 56 | static int try_access(const char *f)
|
|---|
| 57 | {
|
|---|
| 58 | int fd;
|
|---|
| 59 |
|
|---|
| 60 | fd = vfs_lookup_open(f, WALK_REGULAR, MODE_READ);
|
|---|
| 61 | if (fd >= 0) {
|
|---|
| 62 | vfs_put(fd);
|
|---|
| 63 | return 0;
|
|---|
| 64 | } else
|
|---|
| 65 | return -1;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /* Returns the full path of "cmd" if cmd is found, else just hand back
|
|---|
| 69 | * cmd as it was presented */
|
|---|
| 70 | static char *find_command(char *cmd)
|
|---|
| 71 | {
|
|---|
| 72 | size_t i;
|
|---|
| 73 |
|
|---|
| 74 | found = (char *)malloc(PATH_MAX);
|
|---|
| 75 |
|
|---|
| 76 | /* The user has specified a full or relative path, just give it back. */
|
|---|
| 77 | if (-1 != try_access(cmd)) {
|
|---|
| 78 | return (char *) cmd;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /* We now have n places to look for the command */
|
|---|
| 82 | for (i = 0; search_dir[i] != NULL; i++) {
|
|---|
| 83 | memset(found, 0, PATH_MAX);
|
|---|
| 84 | snprintf(found, PATH_MAX, "%s/%s", search_dir[i], cmd);
|
|---|
| 85 | if (-1 != try_access(found)) {
|
|---|
| 86 | return (char *) found;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /* We didn't find it, just give it back as-is. */
|
|---|
| 91 | return (char *) cmd;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | unsigned int try_exec(char *cmd, char **argv, iostate_t *io)
|
|---|
| 95 | {
|
|---|
| 96 | task_id_t tid;
|
|---|
| 97 | task_wait_t twait;
|
|---|
| 98 | task_exit_t texit;
|
|---|
| 99 | char *tmp;
|
|---|
| 100 | int rc, retval, i;
|
|---|
| 101 | int file_handles[3] = { -1, -1, -1 };
|
|---|
| 102 | FILE *files[3];
|
|---|
| 103 |
|
|---|
| 104 | tmp = str_dup(find_command(cmd));
|
|---|
| 105 | free(found);
|
|---|
| 106 |
|
|---|
| 107 | files[0] = io->stdin;
|
|---|
| 108 | files[1] = io->stdout;
|
|---|
| 109 | files[2] = io->stderr;
|
|---|
| 110 |
|
|---|
| 111 | for (i = 0; i < 3 && files[i] != NULL; i++) {
|
|---|
| 112 | vfs_fhandle(files[i], &file_handles[i]);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | rc = task_spawnvf(&tid, &twait, tmp, (const char **) argv,
|
|---|
| 116 | file_handles[0], file_handles[1], file_handles[2]);
|
|---|
| 117 | free(tmp);
|
|---|
| 118 |
|
|---|
| 119 | if (rc != 0) {
|
|---|
| 120 | cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
|
|---|
| 121 | str_error(rc));
|
|---|
| 122 | return 1;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | rc = task_wait(&twait, &texit, &retval);
|
|---|
| 126 | if (rc != EOK) {
|
|---|
| 127 | printf("%s: Failed waiting for command (%s)\n", progname,
|
|---|
| 128 | str_error(rc));
|
|---|
| 129 | return 1;
|
|---|
| 130 | } else if (texit != TASK_EXIT_NORMAL) {
|
|---|
| 131 | printf("%s: Command failed (unexpectedly terminated)\n", progname);
|
|---|
| 132 | return 1;
|
|---|
| 133 | } else if (retval != 0) {
|
|---|
| 134 | printf("%s: Command failed (exit code %d)\n",
|
|---|
| 135 | progname, retval);
|
|---|
| 136 | return 1;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | return 0;
|
|---|
| 140 | }
|
|---|