0,0 → 1,225 |
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* Redistributions of source code must retain the above copyright notice, this |
* list of conditions and the following disclaimer. |
* |
* Redistributions in binary form must reproduce the above copyright notice, |
* this list of conditions and the following disclaimer in the documentation |
* and/or other materials provided with the distribution. |
* |
* Neither the name of the original program's authors nor the names of its |
* contributors may be used to endorse or promote products derived from this |
* software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
* POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
/* The VERY basics of execute in place support. These are buggy, leaky |
* and not nearly done. Only here for beta testing!! You were warned!! |
* TODO: |
* Hash command lookups to save time |
* Create a running pointer to **path and advance/rewind it as we go */ |
|
#include <stdio.h> |
#include <stdlib.h> |
#include <unistd.h> |
#include <string.h> |
#include <fcntl.h> |
|
#ifndef HELENOS |
#include <linux/limits.h> |
#include <sys/errno.h> |
#include <sys/wait.h> |
#endif |
|
#include "config.h" |
#include "util.h" |
#include "exec.h" |
#include "errors.h" |
|
/* Easiest way to handle a shared + allocated string, a structure with |
* one member is sort of silly :) */ |
char *found; |
|
/* work-around for access() */ |
unsigned int try_access(const char *f) |
{ |
int fd; |
|
fd = open(f, O_RDONLY); |
if (fd > 0) { |
close(fd); |
return 1; |
} else |
return 0; |
} |
|
/* Returns the full path of "cmd" if cmd is found, else just hand back |
* cmd as it was presented */ |
char *find_command(char *cmd) |
{ |
char *path_orig, *path_tok; |
char *path[PATH_MAX]; |
int rc, n = 0, i = 0; |
size_t x = strlen(cmd) + 2; |
|
found = (char *)malloc(PATH_MAX); |
|
/* The user has specified the full path, just give it back. Don't test it, |
* they seem to know what they're doing. */ |
if (cmd[0] == '/' || cmd [0] == '.') |
return (char *)cmd; |
|
#ifdef HELENOS |
path_orig = PATH; |
#else |
path_orig = getenv("PATH"); |
if (NULL == path_orig) |
path_orig = PATH; |
#endif |
|
path_tok = cli_strdup(path_orig); |
|
/* Extract the PATH env to a path[] array */ |
path[n] = cli_strtok(path_tok, PATH_DELIM); |
while (NULL != path[n]) { |
if ((strlen(path[n]) + x ) > PATH_MAX) { |
cli_error(CL_ENOTSUP, |
"Segment %d of path is too large, search ends at segment %d", |
n, n-1); |
break; |
} |
path[++n] = cli_strtok(NULL, PATH_DELIM); |
} |
|
/* We now have n places to look for the command */ |
for (i=0; path[i]; i++) { |
memset(found, 0, sizeof(found)); |
snprintf(found, PATH_MAX, "%s/%s", path[i], cmd); |
#ifdef HELENOS |
rc = try_access(found); |
#else |
rc = access(found, F_OK); |
#endif |
if (rc >= 0) { |
/* We found it */ |
free(path_tok); |
return (char *) found; |
} |
} |
|
/* We didn't find it, just give it back as-is. Could be an alias |
* set in the parent shell */ |
free(path_tok); |
return (char *) cmd; |
} |
|
#ifdef HELENOS |
task_id_t try_exec(char *cmd, char **argv) |
{ |
task_id_t tid; |
char *tmp; |
|
tmp = cli_strdup(find_command(cmd)); |
free(found); |
|
tid = task_spawn((const char *)tmp, (const char **)argv); |
free(tmp); |
|
if (tid == 0) |
/* HelenOS does not set errno, so all we can do is report |
* a task ID of 0 as a generic failure. */ |
cli_error(CL_EEXEC, "Can not spawn %s", cmd); |
|
return tid; |
} |
|
#else |
int try_exec(char *cmd, char **argv) |
{ |
char *tmp; |
pid_t pid; |
int status; |
|
/* Copy the result of allocated 'found' */ |
tmp = cli_strdup(find_command(cmd)); |
/* free the pointer, no longer needed */ |
free(found); |
|
if (-1 == (access(tmp, F_OK))) { |
cli_error(CL_EFAIL, |
"%s : No such external, modular or builtin command", tmp); |
free(tmp); |
return CL_EFAIL; |
} |
|
/* Create a child to run the program */ |
pid = fork(); |
if (pid == 0) { |
execv(tmp, argv); |
exit(errno); |
} else if (pid > 0) { |
/* Block until we get a PID (and result) */ |
wait(&status); |
status = status / 256; |
} else { |
/* Could not fork, ulimit or out of memory */ |
cli_error(CL_ENOMEM, "Could not fork"); |
free(tmp); |
return CL_ENOMEM; |
} |
|
free(tmp); |
|
/* No need to go any further if status == 0 */ |
/* |
if (status == 0) |
return status; |
*/ |
/* Decode any errors from execv() (these explain themselves) */ |
switch (status) { |
case ENOTDIR: |
cli_error(CL_EFAIL, "%s : No such file or directory", tmp); |
status = CL_EFAIL; |
break; |
case EISDIR: |
cli_error(CL_EFAIL, "%s : Command is a directory", tmp); |
status = CL_EFAIL; |
break; |
case EACCES: |
cli_error(CL_EPERM, "%s :", tmp); |
status = CL_EFAIL; |
break; |
case ENOMEM: |
cli_error(CL_ENOMEM, "%s :", tmp); |
status = CL_EFAIL; |
break; |
case ENAMETOOLONG: |
cli_error(CL_EFAIL, "Argument list too long"); |
status = CL_EFAIL; |
break; |
case EFAULT: |
cli_error(CL_EFAIL, "Invalid argument pointer\n(please report " |
"this to %s)", PACKAGE_BUGREPORT); |
status = CL_EFAIL; |
break; |
} |
|
return status; |
} |
|
#endif /* HELENOS */ |