Rev 3265 | Rev 3298 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3265 | post | 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 <string.h> |
||
41 | #include <fcntl.h> |
||
42 | |||
43 | #ifndef HELENOS |
||
44 | #include <linux/limits.h> |
||
45 | #include <sys/errno.h> |
||
46 | #include <sys/wait.h> |
||
47 | #endif |
||
48 | |||
49 | #include "config.h" |
||
50 | #include "util.h" |
||
51 | #include "exec.h" |
||
52 | #include "errors.h" |
||
53 | |||
54 | /* Easiest way to handle a shared + allocated string, a structure with |
||
55 | * one member is sort of silly :) */ |
||
56 | char *found; |
||
57 | |||
58 | /* work-around for access() */ |
||
59 | unsigned int try_access(const char *f) |
||
60 | { |
||
61 | int fd; |
||
62 | |||
63 | fd = open(f, O_RDONLY); |
||
3287 | post | 64 | if (fd > -1) { |
3265 | post | 65 | close(fd); |
3287 | post | 66 | return 0; |
3265 | post | 67 | } else |
3287 | post | 68 | return -1; |
3265 | post | 69 | } |
70 | |||
71 | /* Returns the full path of "cmd" if cmd is found, else just hand back |
||
72 | * cmd as it was presented */ |
||
73 | char *find_command(char *cmd) |
||
74 | { |
||
75 | char *path_orig, *path_tok; |
||
76 | char *path[PATH_MAX]; |
||
3287 | post | 77 | int n = 0, i = 0; |
3265 | post | 78 | size_t x = strlen(cmd) + 2; |
79 | |||
80 | found = (char *)malloc(PATH_MAX); |
||
81 | |||
3287 | post | 82 | /* The user has specified a full or relative path, just give it back. */ |
83 | #ifdef HELENOS |
||
84 | if (-1 != try_access(cmd)) |
||
85 | #else |
||
86 | if (-1 != access(cmd, F_OK)) |
||
87 | #endif |
||
88 | { |
||
89 | return (char *) cmd; |
||
90 | } |
||
3265 | post | 91 | |
92 | #ifdef HELENOS |
||
93 | path_orig = PATH; |
||
94 | #else |
||
95 | path_orig = getenv("PATH"); |
||
96 | if (NULL == path_orig) |
||
97 | path_orig = PATH; |
||
98 | #endif |
||
99 | |||
100 | path_tok = cli_strdup(path_orig); |
||
101 | |||
102 | /* Extract the PATH env to a path[] array */ |
||
103 | path[n] = cli_strtok(path_tok, PATH_DELIM); |
||
104 | while (NULL != path[n]) { |
||
105 | if ((strlen(path[n]) + x ) > PATH_MAX) { |
||
106 | cli_error(CL_ENOTSUP, |
||
107 | "Segment %d of path is too large, search ends at segment %d", |
||
108 | n, n-1); |
||
109 | break; |
||
110 | } |
||
111 | path[++n] = cli_strtok(NULL, PATH_DELIM); |
||
112 | } |
||
113 | |||
114 | /* We now have n places to look for the command */ |
||
115 | for (i=0; path[i]; i++) { |
||
116 | memset(found, 0, sizeof(found)); |
||
117 | snprintf(found, PATH_MAX, "%s/%s", path[i], cmd); |
||
118 | #ifdef HELENOS |
||
3287 | post | 119 | if (-1 != try_access(found)) |
3265 | post | 120 | #else |
3287 | post | 121 | if (-1 != access(found, F_OK)) |
3265 | post | 122 | #endif |
3287 | post | 123 | { |
3265 | post | 124 | free(path_tok); |
125 | return (char *) found; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | /* We didn't find it, just give it back as-is. Could be an alias |
||
130 | * set in the parent shell */ |
||
131 | free(path_tok); |
||
132 | return (char *) cmd; |
||
133 | } |
||
134 | |||
135 | #ifdef HELENOS |
||
136 | task_id_t try_exec(char *cmd, char **argv) |
||
137 | { |
||
138 | task_id_t tid; |
||
139 | char *tmp; |
||
140 | |||
141 | tmp = cli_strdup(find_command(cmd)); |
||
142 | free(found); |
||
143 | |||
144 | tid = task_spawn((const char *)tmp, (const char **)argv); |
||
145 | free(tmp); |
||
146 | |||
147 | if (tid == 0) |
||
148 | /* HelenOS does not set errno, so all we can do is report |
||
149 | * a task ID of 0 as a generic failure. */ |
||
150 | cli_error(CL_EEXEC, "Can not spawn %s", cmd); |
||
151 | |||
152 | return tid; |
||
153 | } |
||
154 | |||
155 | #else |
||
156 | int try_exec(char *cmd, char **argv) |
||
157 | { |
||
158 | char *tmp; |
||
159 | pid_t pid; |
||
160 | int status; |
||
161 | |||
162 | /* Copy the result of allocated 'found' */ |
||
163 | tmp = cli_strdup(find_command(cmd)); |
||
164 | /* free the pointer, no longer needed */ |
||
165 | free(found); |
||
166 | |||
167 | if (-1 == (access(tmp, F_OK))) { |
||
168 | cli_error(CL_EFAIL, |
||
169 | "%s : No such external, modular or builtin command", tmp); |
||
170 | free(tmp); |
||
171 | return CL_EFAIL; |
||
172 | } |
||
173 | |||
174 | /* Create a child to run the program */ |
||
175 | pid = fork(); |
||
176 | if (pid == 0) { |
||
177 | execv(tmp, argv); |
||
178 | exit(errno); |
||
179 | } else if (pid > 0) { |
||
180 | /* Block until we get a PID (and result) */ |
||
181 | wait(&status); |
||
182 | status = status / 256; |
||
183 | } else { |
||
184 | /* Could not fork, ulimit or out of memory */ |
||
185 | cli_error(CL_ENOMEM, "Could not fork"); |
||
186 | free(tmp); |
||
187 | return CL_ENOMEM; |
||
188 | } |
||
189 | |||
190 | free(tmp); |
||
191 | |||
192 | /* Decode any errors from execv() (these explain themselves) */ |
||
193 | switch (status) { |
||
194 | case ENOTDIR: |
||
195 | cli_error(CL_EFAIL, "%s : No such file or directory", tmp); |
||
196 | status = CL_EFAIL; |
||
197 | break; |
||
198 | case EISDIR: |
||
199 | cli_error(CL_EFAIL, "%s : Command is a directory", tmp); |
||
200 | status = CL_EFAIL; |
||
201 | break; |
||
202 | case EACCES: |
||
203 | cli_error(CL_EPERM, "%s :", tmp); |
||
204 | status = CL_EFAIL; |
||
205 | break; |
||
206 | case ENOMEM: |
||
207 | cli_error(CL_ENOMEM, "%s :", tmp); |
||
208 | status = CL_EFAIL; |
||
209 | break; |
||
210 | case ENAMETOOLONG: |
||
211 | cli_error(CL_EFAIL, "Argument list too long"); |
||
212 | status = CL_EFAIL; |
||
213 | break; |
||
214 | case EFAULT: |
||
215 | cli_error(CL_EFAIL, "Invalid argument pointer\n(please report " |
||
216 | "this to %s)", PACKAGE_BUGREPORT); |
||
217 | status = CL_EFAIL; |
||
218 | break; |
||
219 | } |
||
220 | |||
221 | return status; |
||
222 | } |
||
223 | |||
224 | #endif /* HELENOS */ |