source: mainline/uspace/app/bdsh/exec.c@ aa8053f1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aa8053f1 was aa8053f1, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Fix search paths. (thx Petr Koupy)

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[36ab7c7]1/*
2 * Copyright (c) 2008 Tim Post
[216d6fc]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
[36ab7c7]6 * modification, are permitted provided that the following conditions
7 * are met:
[216d6fc]8 *
[36ab7c7]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.
[216d6fc]16 *
[36ab7c7]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.
[216d6fc]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 <unistd.h>
[19f857a]38#include <str.h>
[216d6fc]39#include <fcntl.h>
[2721a75]40#include <str_error.h>
[0485135]41#include <errno.h>
[216d6fc]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
[aa8053f1]54const char *search_dir[] = { "/app", "/srv", NULL };
[9be9c4d]55
[216d6fc]56/* work-around for access() */
[af4eda50]57static int try_access(const char *f)
[216d6fc]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 */
[88944695]71static char *find_command(char *cmd)
[216d6fc]72{
[9be9c4d]73 size_t i;
[216d6fc]74
75 found = (char *)malloc(PATH_MAX);
76
77 /* The user has specified a full or relative path, just give it back. */
78 if (-1 != try_access(cmd)) {
79 return (char *) cmd;
80 }
[e2ea8d7e]81
[216d6fc]82 /* We now have n places to look for the command */
[9be9c4d]83 for (i = 0; search_dir[i] != NULL; i++) {
[216d6fc]84 memset(found, 0, sizeof(found));
[9be9c4d]85 snprintf(found, PATH_MAX, "%s/%s", search_dir[i], cmd);
[216d6fc]86 if (-1 != try_access(found)) {
87 return (char *) found;
88 }
89 }
90
91 /* We didn't find it, just give it back as-is. */
92 return (char *) cmd;
93}
94
[6ea9a1d]95unsigned int try_exec(char *cmd, char **argv, iostate_t *io)
[216d6fc]96{
97 task_id_t tid;
[adb49f58]98 task_exit_t texit;
[216d6fc]99 char *tmp;
[ae45201]100 int rc, retval, i;
101 fdi_node_t file_nodes[3];
102 fdi_node_t *file_nodes_p[4];
[6ea9a1d]103 FILE *files[3];
[216d6fc]104
[095003a8]105 tmp = str_dup(find_command(cmd));
[216d6fc]106 free(found);
[ae45201]107
[6ea9a1d]108 files[0] = io->stdin;
109 files[1] = io->stdout;
110 files[2] = io->stderr;
111
[ae45201]112 for (i = 0; i < 3 && files[i] != NULL; i++) {
113 if (fnode(files[i], &file_nodes[i]) == EOK) {
114 file_nodes_p[i] = &file_nodes[i];
115 }
116 else {
117 file_nodes_p[i] = NULL;
118 }
119 }
120 file_nodes_p[i] = NULL;
[216d6fc]121
[ae45201]122 rc = task_spawnvf(&tid, tmp, (const char **) argv, file_nodes_p);
[216d6fc]123 free(tmp);
124
[0485135]125 if (rc != 0) {
[d9fae235]126 cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
[0485135]127 str_error(rc));
[216d6fc]128 return 1;
129 }
[73878c1]130
[0485135]131 rc = task_wait(tid, &texit, &retval);
132 if (rc != EOK) {
[7e752b2]133 printf("%s: Failed waiting for command (%s)\n", progname,
134 str_error(rc));
[0485135]135 } else if (texit != TASK_EXIT_NORMAL) {
[a397f1d]136 printf("%s: Command failed (unexpectedly terminated)\n", progname);
[adb49f58]137 } else if (retval != 0) {
[0485135]138 printf("%s: Command failed (exit code %d)\n",
139 progname, retval);
[adb49f58]140 }
[7114d83]141
[73878c1]142 return 0;
[216d6fc]143}
Note: See TracBrowser for help on using the repository browser.