comparison scp.c @ 666:0ad95abf8d3c

check for fork() and not __uClinux__
author Mike Frysinger <vapier@gentoo.org>
date Sun, 08 Apr 2012 01:50:52 -0400
parents cf5a167776c3
children fc7ae88e63b3
comparison
equal deleted inserted replaced
665:4d9511f98462 666:0ad95abf8d3c
128 fprintf(stderr, "Executing:"); 128 fprintf(stderr, "Executing:");
129 for (i = 0; i < a->num; i++) 129 for (i = 0; i < a->num; i++)
130 fprintf(stderr, " %s", a->list[i]); 130 fprintf(stderr, " %s", a->list[i]);
131 fprintf(stderr, "\n"); 131 fprintf(stderr, "\n");
132 } 132 }
133 #ifdef __uClinux__ 133 #ifndef HAVE_FORK
134 pid = vfork(); 134 pid = vfork();
135 #else 135 #else
136 pid = fork(); 136 pid = fork();
137 #endif /* __uClinux__ */ 137 #endif
138 if (pid == -1) 138 if (pid == -1)
139 fatal("do_local_cmd: fork: %s", strerror(errno)); 139 fatal("do_local_cmd: fork: %s", strerror(errno));
140 140
141 if (pid == 0) { 141 if (pid == 0) {
142 execvp(a->list[0], a->list); 142 execvp(a->list[0], a->list);
143 perror(a->list[0]); 143 perror(a->list[0]);
144 #ifdef __uClinux__ 144 #ifndef HAVE_FORK
145 _exit(1); 145 _exit(1);
146 #else 146 #else
147 exit(1); 147 exit(1);
148 #endif /* __uClinux__ */ 148 #endif
149 } 149 }
150 150
151 do_cmd_pid = pid; 151 do_cmd_pid = pid;
152 signal(SIGTERM, killchild); 152 signal(SIGTERM, killchild);
153 signal(SIGINT, killchild); 153 signal(SIGINT, killchild);
169 * This function executes the given command as the specified user on the 169 * This function executes the given command as the specified user on the
170 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This 170 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
171 * assigns the input and output file descriptors on success. 171 * assigns the input and output file descriptors on success.
172 */ 172 */
173 173
174 static void
175 arg_setup(char *host, char *remuser, char *cmd)
176 {
177 replacearg(&args, 0, "%s", ssh_program);
178 if (remuser != NULL)
179 addargs(&args, "-l%s", remuser);
180 addargs(&args, "%s", host);
181 addargs(&args, "%s", cmd);
182 }
183
174 int 184 int
175 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) 185 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
176 { 186 {
177 int pin[2], pout[2], reserved[2]; 187 int pin[2], pout[2], reserved[2];
178 188
196 206
197 /* Free the reserved descriptors. */ 207 /* Free the reserved descriptors. */
198 close(reserved[0]); 208 close(reserved[0]);
199 close(reserved[1]); 209 close(reserved[1]);
200 210
201 /* uClinux needs to build the args here before vforking, 211 /* uClinux needs to build the args here before vforking,
202 otherwise we do it later on. */ 212 otherwise we do it later on. */
203 #ifdef __uClinux__ 213 #ifndef HAVE_FORK
204 replacearg(&args, 0, "%s", ssh_program); 214 arg_setup(host, remuser, cmd);
205 if (remuser != NULL) 215 #endif
206 addargs(&args, "-l%s", remuser);
207 addargs(&args, "%s", host);
208 addargs(&args, "%s", cmd);
209 #endif /* __uClinux__ */
210 216
211 /* Fork a child to execute the command on the remote host using ssh. */ 217 /* Fork a child to execute the command on the remote host using ssh. */
212 #ifdef __uClinux__ 218 #ifndef HAVE_FORK
213 do_cmd_pid = vfork(); 219 do_cmd_pid = vfork();
214 #else 220 #else
215 do_cmd_pid = fork(); 221 do_cmd_pid = fork();
216 #endif /* __uClinux__ */ 222 #endif
217 223
218 if (do_cmd_pid == 0) { 224 if (do_cmd_pid == 0) {
219 /* Child. */ 225 /* Child. */
220 close(pin[1]); 226 close(pin[1]);
221 close(pout[0]); 227 close(pout[0]);
222 dup2(pin[0], 0); 228 dup2(pin[0], 0);
223 dup2(pout[1], 1); 229 dup2(pout[1], 1);
224 close(pin[0]); 230 close(pin[0]);
225 close(pout[1]); 231 close(pout[1]);
226 232
227 #ifndef __uClinux__ 233 #ifndef HAVE_FORK
228 replacearg(&args, 0, "%s", ssh_program); 234 arg_setup(host, remuser, cmd);
229 if (remuser != NULL) 235 #endif
230 addargs(&args, "-l%s", remuser);
231 addargs(&args, "%s", host);
232 addargs(&args, "%s", cmd);
233 #endif /* __uClinux__ */
234 236
235 execvp(ssh_program, args.list); 237 execvp(ssh_program, args.list);
236 perror(ssh_program); 238 perror(ssh_program);
237 #ifndef __uClinux__ 239 #ifndef HAVE_FORK
240 _exit(1);
241 #else
238 exit(1); 242 exit(1);
239 #else 243 #endif
240 _exit(1);
241 #endif /* __uClinux__ */
242 } else if (do_cmd_pid == -1) { 244 } else if (do_cmd_pid == -1) {
243 fatal("fork: %s", strerror(errno)); 245 fatal("fork: %s", strerror(errno));
244 } 246 }
245 247
246 248 #ifndef HAVE_FORK
247 #ifdef __uClinux__
248 /* clean up command */ 249 /* clean up command */
249 /* pop cmd */ 250 /* pop cmd */
250 xfree(args.list[args.num-1]); 251 xfree(args.list[args.num-1]);
251 args.list[args.num-1]=NULL; 252 args.list[args.num-1]=NULL;
252 args.num--; 253 args.num--;
258 if (remuser != NULL) { 259 if (remuser != NULL) {
259 xfree(args.list[args.num-1]); 260 xfree(args.list[args.num-1]);
260 args.list[args.num-1]=NULL; 261 args.list[args.num-1]=NULL;
261 args.num--; 262 args.num--;
262 } 263 }
263 #endif /* __uClinux__ */ 264 #endif
264 265
265 /* Parent. Close the other side, and return the local side. */ 266 /* Parent. Close the other side, and return the local side. */
266 close(pin[0]); 267 close(pin[0]);
267 *fdout = pin[1]; 268 *fdout = pin[1];
268 close(pout[1]); 269 close(pout[1]);