Annotation of prex/usr/bin/sh/exec.c, Revision 1.1.1.1
1.1 nbrk 1: /*
2: * Copyright (c) 2005-2006, Kohsuke Ohtani
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. Neither the name of the author nor the names of any co-contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: #include <prex/prex.h>
31: #include <sys/wait.h>
32:
33: #include <stdlib.h>
34: #include <string.h>
35: #include <dirent.h>
36: #include <limits.h>
37: #include <errno.h>
38: #include <unistd.h>
39: #include <stdio.h>
40:
41: #include "sh.h"
42:
43: /*
44: * Fork and execute command.
45: */
46: int
47: exec_cmd(int argc, char *argv[])
48: {
49: int pid, cpid;
50: int status;
51: static char **arg;
52: char *file;
53: char path[PATH_MAX];
54: char *env[] = { "PATH=path", "Foo", 0 };
55:
56: arg = argc > 1 ? &argv[1] : NULL;
57: file = argv[0];
58: if (file[0] == '/')
59: strlcpy(path, file, sizeof(path));
60: else {
61: getcwd(path, PATH_MAX);
62: if (!(path[0] == '/' && path[1] == '\0'))
63: strlcat(path, "/", sizeof(path));
64: strlcat(path, file, sizeof(path));
65: }
66: path[PATH_MAX - 1] = '\0';
67:
68: pid = vfork();
69: if (pid == -1) {
70: fprintf(stderr, "cmdbox: Cannot fork\n");
71: return -1;
72: }
73: if (pid == 0) {
74: /* Child only */
75: execve(path, arg, env);
76: if (errno == ENOENT || errno == ENOTDIR)
77: fprintf(stderr, "cmdbox: %s: command not found\n",
78: argv[0]);
79: else
80: fprintf(stderr, "cmdbox: %s cannot execute\n",
81: argv[0]);
82: exit(1);
83: }
84: /* Parent */
85: while (1) {
86: cpid = wait(&status);
87: if (cpid == -1 && errno != EINTR)
88: break;
89: if (cpid == pid)
90: break;
91: }
92: return 0;
93: }
94:
95: #ifdef CMDBOX
96: static void
97: show_signal(int s)
98: {
99: int signo = WTERMSIG(s);
100:
101: signo &= 0x7f;
102: if (signo < NSIG && sys_siglist[signo])
103: fputs(sys_siglist[signo], stderr);
104: else
105: fprintf(stderr, "Signal %d", signo);
106:
107: fputs("\n", stderr);
108: }
109:
110: int
111: exec_builtin(cmd_func_t cmd, int argc, char *argv[])
112: {
113: int status;
114: pid_t cpid;
115:
116: cpid = vfork();
117: if (cpid == -1) {
118: fprintf(stderr, "cmdbox: Cannot fork\n");
119: return -1;
120: }
121: if (cpid == 0) {
122: /* Child only */
123: task_name(task_self(), argv[0]);
124: errno = 0;
125: if (cmd(argc, argv) != 0)
126: printf("%s: %s\n", argv[0], strerror(errno));
127: exit(1);
128: }
129: /* Parent */
130: while (wait(&status) != cpid) ;
131:
132: if (status) {
133: if (WIFSIGNALED(status))
134: show_signal(status);
135: else if (WIFEXITED(status))
136: return WEXITSTATUS(status);
137: }
138: return 0;
139: }
140: #endif
CVSweb