1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*-----------------------------------------------------------------
* haserl functions specific to a bash/ash/dash shell
* Copyright (c) 2003-2007 Nathan Angelacos (nangel@users.sourceforge.net)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
------------------------------------------------------------------------- */
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#include "common.h"
#include "h_error.h"
#include "h_bash.h"
#include "h_script.h"
#include "haserl.h"
#ifdef DEBUG
#include <syslog.h>
#endif
/* Local subshell variables */
static int subshell_pipe[2];
static int subshell_pid;
void
bash_open (char *shell)
{
int retcode = 0;
int count;
argv_t *argv;
char *av[20];
if (shell == NULL)
return;
retcode = pipe (&subshell_pipe[PARENT_IN]);
if (retcode == 0)
{
subshell_pid = fork ();
if (subshell_pid == -1)
{
die_with_message (NULL, NULL, g_err_msg[E_SUBSHELL_FAIL]);
}
if (subshell_pid == 0)
{
/* I'm the child, connect stdin to the parent */
dup2 (subshell_pipe[PARENT_IN], STDIN_FILENO);
close (subshell_pipe[PARENT_IN]);
count = argc_argv (shell, &argv, "");
if (count > 19)
{
/* over 20 command line args, silently truncate */
av[19] = "\0";
count = 18;
}
while (count >= 0)
{
av[count] = argv[count].string;
count--;
}
execv (argv[0].string, av);
free (argv);
/* if we get here, we had a failure */
die_with_message (NULL, NULL, g_err_msg[E_SUBSHELL_FAIL]);
}
else
{
/* I'm parent, move along please */
}
}
/* control should get to this point only in the parent.
*/
}
void
bash_destroy (void)
{
int status;
waitpid (subshell_pid, &status, 0);
}
void
bash_exec (buffer_t * buf, char *str)
{
buffer_add (buf, str, strlen (str));
return;
}
/* Run the echo command in a subshell */
void
bash_echo (buffer_t * buf, char *str, size_t len)
{
/* limits.h would tell us the ARG_MAX characters we COULD send to the echo command, but
* we will take the (ancient) POSIX1 standard of 4K, subtract 1K from it and use that
* as the maxmimum. The Linux limit appears to be 128K, so 3K will fit. */
static char echo_start[] = "echo -n '";
static char echo_quote[] = "'\\''";
static char echo_end[] = "'\n";
const size_t maxlen = 3096;
size_t pos;
if (len == 0)
return;
pos = 0;
buffer_add (buf, echo_start, strlen (echo_start));
while (pos < len)
{
if (str[pos] == '\'')
buffer_add (buf, echo_quote, strlen (echo_quote));
else
buffer_add (buf, str + pos, 1);
pos++;
if ((pos % maxlen) == 0)
{
buffer_add (buf, echo_end, strlen (echo_end));
buffer_add (buf, echo_start, strlen (echo_start));
}
}
buffer_add (buf, echo_end, strlen (echo_end));
}
/* do an evaluation in a subshell */
void
bash_eval (buffer_t * buf, char *str, size_t len)
{
static char echo_start[] = "echo -n ";
static char echo_end[] = "\n";
if (len == 0)
return;
buffer_add (buf, echo_start, strlen (echo_start));
buffer_add (buf, str, len);
buffer_add (buf, echo_end, strlen (echo_end));
}
void
bash_setup (char *shell, list_t * env)
{
list_t *next;
/* populate the environment */
while (env)
{
next = env->next;
putenv (env->buf);
env = next;
}
/* start the subshell */
bash_open (shell);
}
void
bash_doscript (buffer_t * script, char *name)
{
static char postfix[] = "\nexit\n";
/* dump the script to the subshell */
write (subshell_pipe[PARENT_OUT], script->data, script->ptr - script->data);
/* write the postfix */
write (subshell_pipe[PARENT_OUT], postfix, strlen (postfix));
return;
}
|