source: mainline/uspace/app/bdsh/exec.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was adb49f58, checked in by Jiri Svoboda <jirik.svoboda@…>, 16 years ago

Allow to determine whether a task returned value before terminatign.

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[216d6fc]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#include "config.h"
44#include "util.h"
45#include "exec.h"
46#include "errors.h"
47
48/* FIXME: Just have find_command() return an allocated string */
[af4eda50]49static char *found;
[216d6fc]50
[88944695]51static char *find_command(char *);
[af4eda50]52static int try_access(const char *);
[88944695]53
[216d6fc]54/* work-around for access() */
[af4eda50]55static int try_access(const char *f)
[216d6fc]56{
57 int fd;
58
59 fd = open(f, O_RDONLY);
60 if (fd > -1) {
61 close(fd);
62 return 0;
63 } else
64 return -1;
65}
66
67/* Returns the full path of "cmd" if cmd is found, else just hand back
68 * cmd as it was presented */
[88944695]69static char *find_command(char *cmd)
[216d6fc]70{
[e2ea8d7e]71 char *path_tok;
[216d6fc]72 char *path[PATH_MAX];
73 int n = 0, i = 0;
[92fd52d7]74 size_t x = str_size(cmd) + 2;
[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
[095003a8]83 path_tok = str_dup(PATH);
[216d6fc]84
85 /* Extract the PATH env to a path[] array */
[4cc0c9ee]86 path[n] = strtok(path_tok, PATH_DELIM);
[216d6fc]87 while (NULL != path[n]) {
[92fd52d7]88 if ((str_size(path[n]) + x ) > PATH_MAX) {
[216d6fc]89 cli_error(CL_ENOTSUP,
90 "Segment %d of path is too large, search ends at segment %d",
91 n, n-1);
92 break;
93 }
[4cc0c9ee]94 path[++n] = strtok(NULL, PATH_DELIM);
[216d6fc]95 }
96
97 /* We now have n places to look for the command */
98 for (i=0; path[i]; i++) {
99 memset(found, 0, sizeof(found));
100 snprintf(found, PATH_MAX, "%s/%s", path[i], cmd);
101 if (-1 != try_access(found)) {
102 free(path_tok);
103 return (char *) found;
104 }
105 }
106
107 /* We didn't find it, just give it back as-is. */
108 free(path_tok);
109 return (char *) cmd;
110}
111
112unsigned int try_exec(char *cmd, char **argv)
113{
114 task_id_t tid;
[adb49f58]115 task_exit_t texit;
[216d6fc]116 char *tmp;
[7114d83]117 int retval;
[216d6fc]118
[095003a8]119 tmp = str_dup(find_command(cmd));
[216d6fc]120 free(found);
121
[06d6505]122 tid = task_spawn((const char *)tmp, argv);
[216d6fc]123 free(tmp);
124
125 if (tid == 0) {
[29295cd]126 cli_error(CL_EEXEC, "Cannot spawn `%s'.", cmd);
[216d6fc]127 return 1;
128 }
[73878c1]129
[adb49f58]130 task_wait(tid, &texit, &retval);
131 if (texit != TASK_EXIT_NORMAL) {
132 printf("Command failed (unexpectedly terminated).\n");
133 } else if (retval != 0) {
[7114d83]134 printf("Command failed (return value %d).\n", retval);
[adb49f58]135 }
[7114d83]136
[73878c1]137 return 0;
[216d6fc]138}
Note: See TracBrowser for help on using the repository browser.