root / prex-0.9.0 / usr / server / exec / exec_script.c @ 03e9c04a
History | View | Annotate | Download (2.98 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2009, 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 |
/*
|
31 |
* exec_script.c - Script file loader
|
32 |
*/
|
33 |
|
34 |
#include <sys/prex.h> |
35 |
#include <ipc/fs.h> |
36 |
#include <ipc/proc.h> |
37 |
#include <sys/param.h> |
38 |
|
39 |
#include <string.h> |
40 |
#include <limits.h> |
41 |
#include <stdio.h> |
42 |
#include <stdlib.h> |
43 |
#include <errno.h> |
44 |
|
45 |
#include "exec.h" |
46 |
|
47 |
static char interp[PATH_MAX]; /* interpreter name */ |
48 |
static char intarg[LINE_MAX]; /* argument to interpreter */ |
49 |
static char script[LINE_MAX]; /* script name */ |
50 |
|
51 |
/*
|
52 |
* Load script file
|
53 |
*/
|
54 |
int
|
55 |
script_load(struct exec *exec)
|
56 |
{ |
57 |
|
58 |
return 0; |
59 |
} |
60 |
|
61 |
/*
|
62 |
* Probe script file
|
63 |
*/
|
64 |
int
|
65 |
script_probe(struct exec *exec)
|
66 |
{ |
67 |
char *hdrstr = exec->header;
|
68 |
char *p, *name;
|
69 |
|
70 |
/* Check magic header */
|
71 |
if ((hdrstr[0] != '#') || (hdrstr[1] != '!')) |
72 |
return PROBE_ERROR;
|
73 |
|
74 |
/* Strip spaces before the interpriter name */
|
75 |
for (p = hdrstr + 2; *p == ' ' || *p == '\t'; p++) |
76 |
; |
77 |
if (*p == '\0') |
78 |
return PROBE_ERROR;
|
79 |
|
80 |
DPRINTF(("script_probe: found\n"));
|
81 |
|
82 |
/* Pick up interpreter name */
|
83 |
name = p; |
84 |
for (; *p != '\0' && *p != ' ' && *p != '\t'; p++) |
85 |
; |
86 |
*p++ = '\0';
|
87 |
|
88 |
if (!strncmp(name, "/bin/sh", PATH_MAX)) { |
89 |
strlcpy(interp, "/boot/cmdbox", sizeof(interp)); |
90 |
strlcpy(intarg, "sh", sizeof(intarg)); |
91 |
exec->xarg1 = intarg; |
92 |
exec->xarg2 = script; |
93 |
} else {
|
94 |
strlcpy(interp, name, sizeof(interp));
|
95 |
exec->xarg1 = intarg; |
96 |
exec->xarg2 = NULL;
|
97 |
} |
98 |
strlcpy(script, exec->path, sizeof(script));
|
99 |
exec->path = interp; |
100 |
|
101 |
DPRINTF(("script_probe: interpreter=%s arg=%s script=%s\n",
|
102 |
interp, intarg, script)); |
103 |
|
104 |
return PROBE_INDIRECT;
|
105 |
} |
106 |
|
107 |
void
|
108 |
script_init(void)
|
109 |
{ |
110 |
} |