Rev 3304 | Rev 3346 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3304 | Rev 3340 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
2 | * All rights reserved. |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: |
5 | * modification, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * Redistributions of source code must retain the above copyright notice, this |
7 | * Redistributions of source code must retain the above copyright notice, this |
8 | * list of conditions and the following disclaimer. |
8 | * list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
11 | * this list of conditions and the following disclaimer in the documentation |
11 | * this list of conditions and the following disclaimer in the documentation |
12 | * and/or other materials provided with the distribution. |
12 | * and/or other materials provided with the distribution. |
13 | * |
13 | * |
14 | * Neither the name of the original program's authors nor the names of its |
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 |
15 | * contributors may be used to endorse or promote products derived from this |
16 | * software without specific prior written permission. |
16 | * software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
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 |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
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 |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
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 |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | /* The VERY basics of execute in place support. These are buggy, leaky |
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!! |
32 | * and not nearly done. Only here for beta testing!! You were warned!! |
33 | * TODO: |
33 | * TODO: |
34 | * Hash command lookups to save time |
34 | * Hash command lookups to save time |
35 | * Create a running pointer to **path and advance/rewind it as we go */ |
35 | * Create a running pointer to **path and advance/rewind it as we go */ |
36 | 36 | ||
37 | #include <stdio.h> |
37 | #include <stdio.h> |
38 | #include <stdlib.h> |
38 | #include <stdlib.h> |
39 | #include <unistd.h> |
39 | #include <unistd.h> |
40 | #include <string.h> |
40 | #include <string.h> |
41 | #include <fcntl.h> |
41 | #include <fcntl.h> |
42 | 42 | ||
43 | #include "config.h" |
43 | #include "config.h" |
44 | #include "util.h" |
44 | #include "util.h" |
45 | #include "exec.h" |
45 | #include "exec.h" |
46 | #include "errors.h" |
46 | #include "errors.h" |
47 | 47 | ||
48 | /* TODO: create exec, exev, execve wrappers for task_spawn() */ |
- | |
49 | - | ||
50 | /* Easiest way to handle a shared + allocated string */ |
48 | /* FIXME: Just have find_command() return an allocated string */ |
51 | char *found; |
49 | char *found; |
52 | 50 | ||
53 | /* work-around for access() */ |
51 | /* work-around for access() */ |
54 | unsigned int try_access(const char *f) |
52 | unsigned int try_access(const char *f) |
55 | { |
53 | { |
56 | int fd; |
54 | int fd; |
57 | 55 | ||
58 | fd = open(f, O_RDONLY); |
56 | fd = open(f, O_RDONLY); |
59 | if (fd > -1) { |
57 | if (fd > -1) { |
60 | close(fd); |
58 | close(fd); |
61 | return 0; |
59 | return 0; |
62 | } else |
60 | } else |
63 | return -1; |
61 | return -1; |
64 | } |
62 | } |
65 | 63 | ||
66 | /* Returns the full path of "cmd" if cmd is found, else just hand back |
64 | /* Returns the full path of "cmd" if cmd is found, else just hand back |
67 | * cmd as it was presented */ |
65 | * cmd as it was presented */ |
68 | char *find_command(char *cmd) |
66 | char *find_command(char *cmd) |
69 | { |
67 | { |
70 | char *path_orig, *path_tok; |
68 | char *path_orig, *path_tok; |
71 | char *path[PATH_MAX]; |
69 | char *path[PATH_MAX]; |
72 | int n = 0, i = 0; |
70 | int n = 0, i = 0; |
73 | size_t x = strlen(cmd) + 2; |
71 | size_t x = strlen(cmd) + 2; |
74 | 72 | ||
75 | found = (char *)malloc(PATH_MAX); |
73 | found = (char *)malloc(PATH_MAX); |
76 | 74 | ||
77 | /* The user has specified a full or relative path, just give it back. */ |
75 | /* The user has specified a full or relative path, just give it back. */ |
78 | if (-1 != try_access(cmd)) { |
76 | if (-1 != try_access(cmd)) { |
79 | return (char *) cmd; |
77 | return (char *) cmd; |
80 | } |
78 | } |
81 | path_orig = PATH; |
79 | path_orig = PATH; |
82 | path_tok = cli_strdup(path_orig); |
80 | path_tok = cli_strdup(path_orig); |
83 | 81 | ||
84 | /* Extract the PATH env to a path[] array */ |
82 | /* Extract the PATH env to a path[] array */ |
85 | path[n] = cli_strtok(path_tok, PATH_DELIM); |
83 | path[n] = cli_strtok(path_tok, PATH_DELIM); |
86 | while (NULL != path[n]) { |
84 | while (NULL != path[n]) { |
87 | if ((strlen(path[n]) + x ) > PATH_MAX) { |
85 | if ((strlen(path[n]) + x ) > PATH_MAX) { |
88 | cli_error(CL_ENOTSUP, |
86 | cli_error(CL_ENOTSUP, |
89 | "Segment %d of path is too large, search ends at segment %d", |
87 | "Segment %d of path is too large, search ends at segment %d", |
90 | n, n-1); |
88 | n, n-1); |
91 | break; |
89 | break; |
92 | } |
90 | } |
93 | path[++n] = cli_strtok(NULL, PATH_DELIM); |
91 | path[++n] = cli_strtok(NULL, PATH_DELIM); |
94 | } |
92 | } |
95 | 93 | ||
96 | /* We now have n places to look for the command */ |
94 | /* We now have n places to look for the command */ |
97 | for (i=0; path[i]; i++) { |
95 | for (i=0; path[i]; i++) { |
98 | memset(found, 0, sizeof(found)); |
96 | memset(found, 0, sizeof(found)); |
99 | snprintf(found, PATH_MAX, "%s/%s", path[i], cmd); |
97 | snprintf(found, PATH_MAX, "%s/%s", path[i], cmd); |
100 | if (-1 != try_access(found)) { |
98 | if (-1 != try_access(found)) { |
101 | free(path_tok); |
99 | free(path_tok); |
102 | return (char *) found; |
100 | return (char *) found; |
103 | } |
101 | } |
104 | } |
102 | } |
105 | 103 | ||
106 | /* We didn't find it, just give it back as-is. Could be an alias |
104 | /* We didn't find it, just give it back as-is. */ |
107 | * set in the parent shell */ |
- | |
108 | free(path_tok); |
105 | free(path_tok); |
109 | return (char *) cmd; |
106 | return (char *) cmd; |
110 | } |
107 | } |
111 | 108 | ||
112 | task_id_t try_exec(char *cmd, char **argv) |
109 | unsigned int try_exec(char *cmd, char **argv) |
113 | { |
110 | { |
114 | task_id_t tid; |
111 | task_id_t tid; |
115 | char *tmp; |
112 | char *tmp; |
116 | 113 | ||
117 | tmp = cli_strdup(find_command(cmd)); |
114 | tmp = cli_strdup(find_command(cmd)); |
118 | free(found); |
115 | free(found); |
119 | 116 | ||
120 | /* TODO: put the cwd in front of tmp if tmp[0] != root |
- | |
121 | * which fixes relative paths fed to task_spawn */ |
- | |
122 | - | ||
123 | tid = task_spawn((const char *)tmp, (const char **)argv); |
117 | tid = task_spawn((const char *)tmp, (const char **)argv); |
124 | free(tmp); |
118 | free(tmp); |
125 | 119 | ||
126 | if (tid == 0) |
120 | if (tid == 0) { |
127 | /* HelenOS does not set errno, so all we can do is report |
- | |
128 | * a task ID of 0 as a generic failure. */ |
- | |
129 | cli_error(CL_EEXEC, "Can not spawn %s", cmd); |
121 | cli_error(CL_EEXEC, "Can not spawn %s", cmd); |
- | 122 | return 1; |
|
130 | 123 | } else { |
|
131 | return tid; |
124 | return 0; |
- | 125 | } |
|
132 | } |
126 | } |
133 | 127 |