| 284 | | static void process_exec(PROCESS_REC *rec, const char *cmd) |
| 285 | | { |
| 286 | | const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
|
| 287 | | char **args; |
| 288 | | int in[2], out[2]; |
| 289 | | int n; |
| 290 | | |
| 291 | | if (pipe(in) == -1) |
| 292 | | return; |
| 293 | | if (pipe(out) == -1) |
| 294 | | return; |
| 295 | | |
| 296 | | shell_args[2] = cmd; |
| 297 | | rec->pid = fork(); |
| 298 | | if (rec->pid == -1) { |
| 299 | | |
| 300 | | close(in[0]); close(in[1]); |
| 301 | | close(out[0]); close(out[1]); |
| 302 | | return; |
| 303 | | } |
| 304 | | |
| 305 | | if (rec->pid != 0) { |
| 306 | | |
| 307 | | GIOChannel *outio = g_io_channel_unix_new(in[1]); |
| 308 | | |
| 309 | | rec->in = g_io_channel_unix_new(out[0]); |
| 310 | | rec->out = net_sendbuffer_create(outio, 0); |
| 311 | | |
| 312 | | close(out[1]); |
| 313 | | close(in[0]); |
| 314 | | pidwait_add(rec->pid); |
| 315 | | return; |
| 316 | | } |
| 317 | | |
| 318 | | |
| 319 | | setsid(); |
| 320 | | setuid(getuid()); |
| 321 | | setgid(getgid()); |
| 322 | | signal(SIGINT, SIG_IGN);
|
| 323 | | signal(SIGQUIT, SIG_DFL);
|
| 324 | | |
| 325 | | putenv("TERM=tty"); |
| 326 | | |
| 327 | | |
| 328 | | dup2(in[0], STDIN_FILENO);
|
| 329 | | dup2(out[1], STDOUT_FILENO);
|
| 330 | | dup2(out[1], STDERR_FILENO);
|
| 331 | | |
| 332 | | |
| 333 | | for (n = 3; n < 256; n++) |
| 334 | | close(n); |
| 335 | | |
| 336 | | if (rec->shell) { |
| 337 | | execvp(shell_args[0], (char **) shell_args); |
| 338 | | |
| 339 | | fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno)); |
| 340 | | } else { |
| 341 | | args = g_strsplit(cmd, " ", -1); |
| 342 | | execvp(args[0], args); |
| 343 | | |
| 344 | | fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno)); |
| 345 | | } |
| 346 | | |
| 347 | | _exit(-1); |
| 348 | | } |