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>
|
---|
40 | #include <str.h>
|
---|
41 | #include <fcntl.h>
|
---|
42 | #include <str_error.h>
|
---|
43 | #include <errno.h>
|
---|
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 */
|
---|
51 | static char *found;
|
---|
52 |
|
---|
53 | static char *find_command(char *);
|
---|
54 | static int try_access(const char *);
|
---|
55 |
|
---|
56 | /* work-around for access() */
|
---|
57 | static int try_access(const char *f)
|
---|
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 */
|
---|
71 | static char *find_command(char *cmd)
|
---|
72 | {
|
---|
73 | char *path_tok;
|
---|
74 | char *path[PATH_MAX];
|
---|
75 | int n = 0, i = 0;
|
---|
76 | size_t x = str_size(cmd) + 2;
|
---|
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 | }
|
---|
84 |
|
---|
85 | path_tok = str_dup(PATH);
|
---|
86 |
|
---|
87 | /* Extract the PATH env to a path[] array */
|
---|
88 | path[n] = strtok(path_tok, PATH_DELIM);
|
---|
89 | while (NULL != path[n]) {
|
---|
90 | if ((str_size(path[n]) + x ) > PATH_MAX) {
|
---|
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 | }
|
---|
96 | path[++n] = strtok(NULL, PATH_DELIM);
|
---|
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;
|
---|
117 | task_exit_t texit;
|
---|
118 | char *tmp;
|
---|
119 | int rc, retval;
|
---|
120 |
|
---|
121 | tmp = str_dup(find_command(cmd));
|
---|
122 | free(found);
|
---|
123 |
|
---|
124 | rc = task_spawnv(&tid, tmp, (const char **) argv);
|
---|
125 | free(tmp);
|
---|
126 |
|
---|
127 | if (rc != 0) {
|
---|
128 | cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
|
---|
129 | str_error(rc));
|
---|
130 | return 1;
|
---|
131 | }
|
---|
132 |
|
---|
133 | rc = task_wait(tid, &texit, &retval);
|
---|
134 | if (rc != EOK) {
|
---|
135 | printf("%s: Failed waiting for command (%s)\n", progname,
|
---|
136 | str_error(rc));
|
---|
137 | } else if (texit != TASK_EXIT_NORMAL) {
|
---|
138 | printf("%s: Command failed (unexpectedly terminated)\n", progname);
|
---|
139 | } else if (retval != 0) {
|
---|
140 | printf("%s: Command failed (exit code %d)\n",
|
---|
141 | progname, retval);
|
---|
142 | }
|
---|
143 |
|
---|
144 | return 0;
|
---|
145 | }
|
---|