exec.c

00001 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
00002  * All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *
00007  * Redistributions of source code must retain the above copyright notice, this
00008  * list of conditions and the following disclaimer.
00009  *
00010  * Redistributions in binary form must reproduce the above copyright notice,
00011  * this list of conditions and the following disclaimer in the documentation
00012  * and/or other materials provided with the distribution.
00013  *
00014  * Neither the name of the original program's authors nor the names of its
00015  * contributors may be used to endorse or promote products derived from this
00016  * software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 /* The VERY basics of execute in place support. These are buggy, leaky
00032  * and not nearly done. Only here for beta testing!! You were warned!!
00033  * TODO:
00034  * Hash command lookups to save time
00035  * Create a running pointer to **path and advance/rewind it as we go */
00036 
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <unistd.h>
00040 #include <str.h>
00041 #include <fcntl.h>
00042 #include <str_error.h>
00043 #include <errno.h>
00044 
00045 #include "config.h"
00046 #include "util.h"
00047 #include "exec.h"
00048 #include "errors.h"
00049 
00050 /* FIXME: Just have find_command() return an allocated string */
00051 static char *found;
00052 
00053 static char *find_command(char *);
00054 static int try_access(const char *);
00055 
00056 /* work-around for access() */
00057 static int try_access(const char *f)
00058 {
00059         int fd;
00060 
00061         fd = open(f, O_RDONLY);
00062         if (fd > -1) {
00063                 close(fd);
00064                 return 0;
00065         } else
00066                 return -1;
00067 }
00068 
00069 /* Returns the full path of "cmd" if cmd is found, else just hand back
00070  * cmd as it was presented */
00071 static char *find_command(char *cmd)
00072 {
00073         char *path_tok;
00074         char *path[PATH_MAX];
00075         int n = 0, i = 0;
00076         size_t x = str_size(cmd) + 2;
00077 
00078         found = (char *)malloc(PATH_MAX);
00079 
00080         /* The user has specified a full or relative path, just give it back. */
00081         if (-1 != try_access(cmd)) {
00082                 return (char *) cmd;
00083         }
00084 
00085         path_tok = str_dup(PATH);
00086 
00087         /* Extract the PATH env to a path[] array */
00088         path[n] = strtok(path_tok, PATH_DELIM);
00089         while (NULL != path[n]) {
00090                 if ((str_size(path[n]) + x ) > PATH_MAX) {
00091                         cli_error(CL_ENOTSUP,
00092                                 "Segment %d of path is too large, search ends at segment %d",
00093                                 n, n-1);
00094                         break;
00095                 }
00096                 path[++n] = strtok(NULL, PATH_DELIM);
00097         }
00098 
00099         /* We now have n places to look for the command */
00100         for (i=0; path[i]; i++) {
00101                 memset(found, 0, sizeof(found));
00102                 snprintf(found, PATH_MAX, "%s/%s", path[i], cmd);
00103                 if (-1 != try_access(found)) {
00104                         free(path_tok);
00105                         return (char *) found;
00106                 }
00107         }
00108 
00109         /* We didn't find it, just give it back as-is. */
00110         free(path_tok);
00111         return (char *) cmd;
00112 }
00113 
00114 unsigned int try_exec(char *cmd, char **argv)
00115 {
00116         task_id_t tid;
00117         task_exit_t texit;
00118         char *tmp;
00119         int rc, retval;
00120 
00121         tmp = str_dup(find_command(cmd));
00122         free(found);
00123 
00124         rc = task_spawnv(&tid, tmp, (const char **) argv);
00125         free(tmp);
00126 
00127         if (rc != 0) {
00128                 cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
00129                     str_error(rc));
00130                 return 1;
00131         }
00132         
00133         rc = task_wait(tid, &texit, &retval);
00134         if (rc != EOK) {
00135                 printf("%s: Failed waiting for command (%s)\n", progname,
00136                     str_error(rc));
00137         } else if (texit != TASK_EXIT_NORMAL) {
00138                 printf("%s: Command failed (unexpectedly terminated)\n", progname);
00139         } else if (retval != 0) {
00140                 printf("%s: Command failed (exit code %d)\n",
00141                     progname, retval);
00142         }
00143 
00144         return 0;
00145 }

Generated on Thu Jun 2 07:45:41 2011 for HelenOS/USB by  doxygen 1.4.7