version 1.7.3.0
[socat.git] / xio-progcall.c
blob9b2c7fa3306d1398541444c364f3a4849a61339a
1 /* source: xio-progcall.c */
2 /* Copyright Gerhard Rieger */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* this file contains common code dealing with program calls (exec, system) */
7 #include "xiosysincludes.h"
8 #include "xioopen.h"
10 #include "xio-process.h"
11 #include "xio-progcall.h"
13 #include "xio-socket.h"
16 /* these options are used by address pty too */
17 #if HAVE_OPENPTY
18 const struct optdesc opt_openpty = { "openpty", NULL, OPT_OPENPTY, GROUP_PTY, PH_BIGEN, TYPE_BOOL, OFUNC_SPEC };
19 #endif /* HAVE_OPENPTY */
20 #if HAVE_DEV_PTMX || HAVE_DEV_PTC
21 const struct optdesc opt_ptmx = { "ptmx", NULL, OPT_PTMX, GROUP_PTY, PH_BIGEN, TYPE_BOOL, OFUNC_SPEC };
22 #endif
24 #if WITH_EXEC || WITH_SYSTEM
26 #define MAXPTYNAMELEN 64
28 const struct optdesc opt_fdin = { "fdin", NULL, OPT_FDIN, GROUP_FORK, PH_PASTBIGEN, TYPE_USHORT, OFUNC_SPEC };
29 const struct optdesc opt_fdout = { "fdout", NULL, OPT_FDOUT, GROUP_FORK, PH_PASTBIGEN, TYPE_USHORT, OFUNC_SPEC };
30 const struct optdesc opt_path = { "path", NULL, OPT_PATH, GROUP_EXEC, PH_PREEXEC, TYPE_STRING, OFUNC_SPEC };
31 const struct optdesc opt_pipes = { "pipes", NULL, OPT_PIPES, GROUP_FORK, PH_BIGEN, TYPE_BOOL, OFUNC_SPEC };
32 #if HAVE_PTY
33 const struct optdesc opt_pty = { "pty", NULL, OPT_PTY, GROUP_FORK, PH_BIGEN, TYPE_BOOL, OFUNC_SPEC };
34 #endif
35 const struct optdesc opt_stderr = { "stderr", NULL, OPT_STDERR, GROUP_FORK, PH_PASTFORK, TYPE_BOOL, OFUNC_SPEC };
36 const struct optdesc opt_nofork = { "nofork", NULL, OPT_NOFORK, GROUP_FORK, PH_BIGEN, TYPE_BOOL, OFUNC_SPEC };
37 const struct optdesc opt_sighup = { "sighup", NULL, OPT_SIGHUP, GROUP_PARENT, PH_LATE, TYPE_CONST, OFUNC_SIGNAL, SIGHUP };
38 const struct optdesc opt_sigint = { "sigint", NULL, OPT_SIGINT, GROUP_PARENT, PH_LATE, TYPE_CONST, OFUNC_SIGNAL, SIGINT };
39 const struct optdesc opt_sigquit = { "sigquit", NULL, OPT_SIGQUIT, GROUP_PARENT, PH_LATE, TYPE_CONST, OFUNC_SIGNAL, SIGQUIT };
42 /* fork for exec/system, but return before exec'ing.
43 return=0: is child process
44 return>0: is parent process
45 return<0: error occurred, assume parent process and no child exists !!!
47 int _xioopen_foxec(int xioflags, /* XIO_RDONLY etc. */
48 struct single *fd,
49 unsigned groups,
50 struct opt **copts, /* in: opts; out: opts for child */
51 int *duptostderr /* out: redirect stderr to output fd */
52 ) {
53 struct opt *popts; /* parent process options */
54 int numleft;
55 int d, sv[2], rdpip[2], wrpip[2];
56 int rw = (xioflags & XIO_ACCMODE);
57 bool usepipes = false;
58 #if HAVE_PTY
59 int ptyfd = -1, ttyfd = -1;
60 bool usebestpty = false; /* use the best available way to open pty */
61 #if defined(HAVE_DEV_PTMX) || defined(HAVE_DEV_PTC)
62 bool useptmx = false; /* use /dev/ptmx or equivalent */
63 #endif
64 #if HAVE_OPENPTY
65 bool useopenpty = false; /* try only openpty */
66 #endif /* HAVE_OPENPTY */
67 bool usepty = false; /* any of the pty options is selected */
68 char ptyname[MAXPTYNAMELEN];
69 #endif /* HAVE_PTY */
70 pid_t pid = 0; /* mostly int */
71 short fdi = 0, fdo = 1;
72 short result;
73 bool withstderr = false;
74 bool nofork = false;
75 bool withfork;
77 popts = moveopts(*copts, GROUP_ALL);
78 if (applyopts_single(fd, popts, PH_INIT) < 0) return -1;
79 applyopts2(-1, popts, PH_INIT, PH_EARLY);
81 retropt_bool(popts, OPT_NOFORK, &nofork);
82 withfork = !nofork;
84 retropt_bool(popts, OPT_PIPES, &usepipes);
85 #if HAVE_PTY
86 retropt_bool(popts, OPT_PTY, &usebestpty);
87 #if HAVE_OPENPTY
88 retropt_bool(popts, OPT_OPENPTY, &useopenpty);
89 #endif
90 #if defined(HAVE_DEV_PTMX) || defined(HAVE_DEV_PTC)
91 retropt_bool(popts, OPT_PTMX, &useptmx);
92 #endif
93 usepty = (usebestpty
94 #if HAVE_OPENPTY
95 || useopenpty
96 #endif
97 #if defined(HAVE_DEV_PTMX) || defined(HAVE_DEV_PTC)
98 || useptmx
99 #endif
101 if (usepipes && usepty) {
102 Warn("_xioopen_foxec(): options \"pipes\" and \"pty\" must not be specified together; ignoring \"pipes\"");
103 usepipes = false;
105 #endif /* HAVE_PTY */
107 if (retropt_ushort(popts, OPT_FDIN, (unsigned short *)&fdi) >= 0) {
108 if ((xioflags&XIO_ACCMODE) == XIO_RDONLY) {
109 Error("_xioopen_foxec(): option fdin is useless in read-only mode");
112 if (retropt_ushort(popts, OPT_FDOUT, (unsigned short *)&fdo) >= 0) {
113 if ((xioflags&XIO_ACCMODE) == XIO_WRONLY) {
114 Error("_xioopen_foxec(): option fdout is useless in write-only mode");
118 if (withfork) {
119 if (!(xioflags&XIO_MAYCHILD)) {
120 Error("cannot fork off child process here");
121 /*!! free something */
122 return -1;
124 fd->flags |= XIO_DOESCHILD;
126 #if HAVE_PTY
127 Notice2("forking off child, using %s for %s",
128 &("socket\0\0pipes\0\0\0pty\0\0\0\0\0"[(usepipes<<3)|(usepty<<4)]),
129 ddirection[rw]);
130 #else
131 Notice2("forking off child, using %s for %s",
132 &("socket\0\0pipes\0\0\0"[(usepipes<<3)]),
133 ddirection[rw]);
134 #endif /* HAVE_PTY */
136 applyopts(-1, popts, PH_PREBIGEN);
138 if (!withfork) {
139 /*0 struct single *stream1, *stream2;*/
141 if (!(xioflags & XIO_MAYEXEC /* means exec+nofork */)) {
142 Error("option nofork is not allowed here");
143 /*!! free something */
144 return -1;
146 fd->flags |= XIO_DOESEXEC;
148 free(*copts);
149 *copts = moveopts(popts, GROUP_ALL);
151 #if 0 /*!! */
152 if (sock1->tag == XIO_TAG_DUAL) {
153 stream1 = &sock1->dual.stream[0]->stream;
154 stream2 = &sock1->dual.stream[1]->stream;
155 } else {
156 stream1 = &sock1->stream;
157 stream2 = &sock1->stream;
159 if (stream1->dtype == DATA_READLINE || stream2->dtype == DATA_READLINE ||
160 stream1->dtype == DATA_OPENSSL || stream2->dtype == DATA_OPENSSL
162 Error("with option nofork, openssl and readline in address1 do not work");
164 if (stream1->lineterm != LINETERM_RAW ||
165 stream2->lineterm != LINETERM_RAW ||
166 stream1->ignoreeof || stream2->ignoreeof) {
167 Warn("due to option nofork, address1 options for lineterm and igoreeof do not apply");
169 #endif
171 /*! problem: when fdi==WRFD(sock[0]) or fdo==RDFD(sock[0]) */
172 if (rw != XIO_WRONLY) {
173 if (XIO_GETRDFD(sock[0]/*!!*/) == fdi) {
174 if (Fcntl_l(fdi, F_SETFD, 0) < 0) {
175 Warn2("fcntl(%d, F_SETFD, 0): %s", fdi, strerror(errno));
177 if (Dup2(XIO_GETRDFD(sock[0]), fdi) < 0) {
178 Error3("dup2(%d, %d): %s",
179 XIO_GETRDFD(sock[0]), fdi, strerror(errno));
181 /*0 Info2("dup2(%d, %d)", XIO_GETRDFD(sock[0]), fdi);*/
182 } else {
183 if (Dup2(XIO_GETRDFD(sock[0]), fdi) < 0) {
184 Error3("dup2(%d, %d): %s",
185 XIO_GETRDFD(sock[0]), fdi, strerror(errno));
187 /*0 Info2("dup2(%d, %d)", XIO_GETRDFD(sock[0]), fdi);*/
190 if (rw != XIO_RDONLY) {
191 if (XIO_GETWRFD(sock[0]) == fdo) {
192 if (Fcntl_l(fdo, F_SETFD, 0) < 0) {
193 Warn2("fcntl(%d, F_SETFD, 0): %s", fdo, strerror(errno));
195 if (Dup2(XIO_GETWRFD(sock[0]), fdo) < 0) {
196 Error3("dup2(%d, %d): %s)",
197 XIO_GETWRFD(sock[0]), fdo, strerror(errno));
199 /*0 Info2("dup2(%d, %d)", XIO_GETWRFD(sock[0]), fdo);*/
200 } else {
201 if (Dup2(XIO_GETWRFD(sock[0]), fdo) < 0) {
202 Error3("dup2(%d, %d): %s)",
203 XIO_GETWRFD(sock[0]), fdo, strerror(errno));
205 /*0 Info2("dup2(%d, %d)", XIO_GETWRFD(sock[0]), fdo);*/
208 } else
209 #if HAVE_PTY
210 if (usepty) {
212 #if defined(HAVE_DEV_PTMX)
213 # define PTMX "/dev/ptmx" /* Linux */
214 #elif HAVE_DEV_PTC
215 # define PTMX "/dev/ptc" /* AIX 4.3.3 */
216 #endif
217 fd->dtype = XIODATA_PTY;
218 #if HAVE_DEV_PTMX || HAVE_DEV_PTC
219 if (usebestpty || useptmx) {
220 if ((ptyfd = Open(PTMX, O_RDWR|O_NOCTTY, 0620)) < 0) {
221 Warn1("open(\""PTMX"\", O_RDWR|O_NOCTTY, 0620): %s",
222 strerror(errno));
223 /*!*/
224 } else {
225 /*0 Info2("open(\"%s\", O_RDWR|O_NOCTTY, 0620) -> %d", PTMX, ptyfd);*/
227 if (ptyfd >= 0 && ttyfd < 0) {
228 char *tn = NULL;
229 /* we used PTMX before forking */
230 extern char *ptsname(int);
231 #if HAVE_GRANTPT /* AIX, not Linux */
232 if (Grantpt(ptyfd)/*!*/ < 0) {
233 Warn2("grantpt(%d): %s", ptyfd, strerror(errno));
235 #endif /* HAVE_GRANTPT */
236 #if HAVE_UNLOCKPT
237 if (Unlockpt(ptyfd)/*!*/ < 0) {
238 Warn2("unlockpt(%d): %s", ptyfd, strerror(errno));
240 #endif /* HAVE_UNLOCKPT */
241 #if HAVE_PROTOTYPE_LIB_ptsname /* AIX, not Linux */
242 if ((tn = Ptsname(ptyfd)) == NULL) {
243 Warn2("ptsname(%d): %s", ptyfd, strerror(errno));
245 #endif /* HAVE_PROTOTYPE_LIB_ptsname */
246 if (tn == NULL) {
247 if ((tn = Ttyname(ptyfd)) == NULL) {
248 Error2("ttyname(%d): %s", ptyfd, strerror(errno));
251 ptyname[0] = '\0'; strncat(ptyname, tn, MAXPTYNAMELEN-1);
252 if ((ttyfd = Open(tn, O_RDWR|O_NOCTTY, 0620)) < 0) {
253 Warn2("open(\"%s\", O_RDWR|O_NOCTTY, 0620): %s", tn, strerror(errno));
254 } else {
255 /*0 Info2("open(\"%s\", O_RDWR|O_NOCTTY, 0620) -> %d", tn, ttyfd);*/
258 #ifdef I_PUSH
259 /* Linux: I_PUSH def'd; pty: ioctl(, I_FIND, ...) -> -1 EINVAL */
260 /* AIX: I_PUSH def'd; pty: ioctl(, I_FIND, ...) -> 1 */
261 /* SunOS: I_PUSH def'd; pty: ioctl(, I_FIND, ...) -> 0 */
262 /* HP-UX: I_PUSH def'd; pty: ioctl(, I_FIND, ...) -> 0 */
263 if (Ioctl(ttyfd, I_FIND, "ldterm") == 0) {
264 Ioctl(ttyfd, I_PUSH, "ptem"); /* 0 */
265 Ioctl(ttyfd, I_PUSH, "ldterm"); /* 0 */
266 Ioctl(ttyfd, I_PUSH, "ttcompat"); /* HP-UX: -1 */
268 #endif
270 #if 0 /* the following block need not work */
272 if (ttyfd >= 0 && ((tn = Ttyname(ttyfd)) == NULL)) {
273 Warn2("ttyname(%d): %s", ttyfd, strerror(errno));
275 if (tn == NULL) {
276 Error("could not open pty");
277 return -1;
279 #endif
280 Info1("opened pseudo terminal %s", tn);
283 #endif /* HAVE_DEV_PTMX || HAVE_DEV_PTC */
284 #if HAVE_OPENPTY
285 if (ptyfd < 0) {
286 int result;
287 if ((result = Openpty(&ptyfd, &ttyfd, ptyname, NULL, NULL)) < 0) {
288 Error4("openpty(%p, %p, %p, NULL, NULL): %s",
289 &ptyfd, &ttyfd, ptyname, strerror(errno));
290 return -1;
293 #endif /* HAVE_OPENPTY */
294 free(*copts);
295 if ((*copts = moveopts(popts, GROUP_TERMIOS|GROUP_FORK|GROUP_EXEC|GROUP_PROCESS)) == NULL) {
296 return -1;
298 applyopts_cloexec(ptyfd, popts);/*!*/
299 /* exec:...,pty did not kill child process under some circumstances */
300 if (fd->howtoend == END_UNSPEC) {
301 fd->howtoend = END_CLOSE_KILL;
304 /* this for parent, was after fork */
305 applyopts(ptyfd, popts, PH_FD);
306 applyopts(ptyfd, popts, PH_LATE);
307 if (applyopts_single(fd, popts, PH_LATE) < 0) return -1;
309 fd->fd = ptyfd;
311 /* this for child, was after fork */
312 applyopts(ttyfd, *copts, PH_FD);
313 } else
314 #endif /* HAVE_PTY */
315 if (usepipes) {
316 struct opt *popts2, *copts2;
318 if (rw == XIO_RDWR)
319 fd->dtype = XIODATA_2PIPE;
320 if (rw != XIO_WRONLY) {
321 if (Pipe(rdpip) < 0) {
322 Error2("pipe(%p): %s", rdpip, strerror(errno));
323 return -1;
326 /*0 Info2("pipe({%d,%d})", rdpip[0], rdpip[1]);*/
327 /* rdpip[0]: read by socat; rdpip[1]: write by child */
328 free(*copts);
329 if ((*copts = moveopts(popts, GROUP_FORK|GROUP_EXEC|GROUP_PROCESS))
330 == NULL) {
331 return -1;
334 popts2 = copyopts(popts, GROUP_ALL);
335 copts2 = copyopts(*copts, GROUP_ALL);
337 if (rw != XIO_WRONLY) {
338 applyopts_cloexec(rdpip[0], popts);
339 applyopts(rdpip[0], popts, PH_FD);
340 applyopts(rdpip[1], *copts, PH_FD);
343 if (rw != XIO_RDONLY) {
344 if (Pipe(wrpip) < 0) {
345 Error2("pipe(%p): %s", wrpip, strerror(errno));
346 return -1;
349 /*0 Info2("pipe({%d,%d})", wrpip[0], wrpip[1]);*/
351 /* wrpip[1]: write by socat; wrpip[0]: read by child */
352 if (rw != XIO_RDONLY) {
353 applyopts_cloexec(wrpip[1], popts2);
354 applyopts(wrpip[1], popts2, PH_FD);
355 applyopts(wrpip[0], copts2, PH_FD);
357 if (fd->howtoend == END_UNSPEC) {
358 fd->howtoend = END_CLOSE_KILL;
361 /* this for parent, was after fork */
362 switch (rw) {
363 case XIO_RDONLY: fd->fd = rdpip[0]; break;
364 case XIO_WRONLY: fd->fd = wrpip[1]; break;
365 case XIO_RDWR: fd->fd = rdpip[0];
366 fd->para.exec.fdout = wrpip[1];
367 break;
369 applyopts(fd->fd, popts, PH_FD);
370 applyopts(fd->fd, popts, PH_LATE);
371 if (applyopts_single(fd, popts, PH_LATE) < 0) return -1;
372 } else {
373 d = AF_UNIX;
374 retropt_int(popts, OPT_PROTOCOL_FAMILY, &d);
375 result = xiosocketpair(popts, d, SOCK_STREAM, 0, sv);
376 if (result < 0) {
377 return -1;
379 /*0 Info5("socketpair(%d, %d, %d, {%d,%d})",
380 d, type, protocol, sv[0], sv[1]);*/
381 free(*copts);
382 if ((*copts = moveopts(popts, GROUP_FORK|GROUP_EXEC|GROUP_PROCESS)) == NULL) {
383 return -1;
385 applyopts(sv[0], *copts, PH_PASTSOCKET);
386 applyopts(sv[1], popts, PH_PASTSOCKET);
388 applyopts_cloexec(sv[0], *copts);
389 applyopts(sv[0], *copts, PH_FD);
390 applyopts(sv[1], popts, PH_FD);
392 applyopts(sv[0], *copts, PH_PREBIND);
393 applyopts(sv[0], *copts, PH_BIND);
394 applyopts(sv[0], *copts, PH_PASTBIND);
395 applyopts(sv[1], popts, PH_PREBIND);
396 applyopts(sv[1], popts, PH_BIND);
397 applyopts(sv[1], popts, PH_PASTBIND);
399 if (fd->howtoend == END_UNSPEC) {
400 fd->howtoend = END_SHUTDOWN_KILL;
403 /* this for parent, was after fork */
404 fd->fd = sv[0];
405 applyopts(fd->fd, popts, PH_FD);
406 applyopts(fd->fd, popts, PH_LATE);
407 if (applyopts_single(fd, popts, PH_LATE) < 0) return -1;
409 /*0 if ((optpr = copyopts(*copts, GROUP_PROCESS)) == NULL)
410 return -1;*/
411 retropt_bool(*copts, OPT_STDERR, &withstderr);
413 xiosetchilddied(); /* set SIGCHLD handler */
415 if (withfork) {
416 pid = xio_fork(true, E_ERROR);
417 if (pid < 0) {
418 return -1;
421 if (!withfork || pid == 0) { /* child */
422 uid_t user;
423 gid_t group;
425 if (withfork) {
426 /* The child should have default handling for SIGCHLD. */
427 /* In particular, it's not defined whether ignoring SIGCHLD is inheritable. */
428 if (Signal(SIGCHLD, SIG_DFL) == SIG_ERR) {
429 Warn1("signal(SIGCHLD, SIG_DFL): %s", strerror(errno));
432 #if HAVE_PTY
433 if (usepty) {
434 Close(ptyfd);
435 if (rw != XIO_RDONLY && fdi != ttyfd) {
436 if (Dup2(ttyfd, fdi) < 0) {
437 Error3("dup2(%d, %d): %s", ttyfd, fdi, strerror(errno));
438 return -1; }
439 /*0 Info2("dup2(%d, %d)", ttyfd, fdi);*/
441 if (rw != XIO_WRONLY && fdo != ttyfd) {
442 if (Dup2(ttyfd, fdo) < 0) {
443 Error3("dup2(%d, %d): %s", ttyfd, fdo, strerror(errno));
444 return -1; }
445 /*0 Info2("dup2(%d, %d)", ttyfd, fdo);*/
447 if ((rw == XIO_RDONLY || fdi != ttyfd) &&
448 (rw == XIO_WRONLY || fdo != ttyfd)) {
449 applyopts_cloexec(ttyfd, *copts);
452 applyopts(ttyfd, *copts, PH_LATE);
454 applyopts(ttyfd, *copts, PH_LATE2);
455 } else
456 #endif /* HAVE_PTY */
457 if (usepipes) {
458 /* we might have a temporary conflict between what FDs are
459 currently allocated, and which are to be used. We try to find
460 a graceful solution via temporary descriptors */
461 int tmpi, tmpo;
463 if (rw != XIO_WRONLY) Close(rdpip[0]);
464 if (rw != XIO_RDONLY) Close(wrpip[1]);
465 if (fdi == rdpip[1]) { /* a conflict here */
466 if ((tmpi = Dup(wrpip[0])) < 0) {
467 Error2("dup(%d): %s", wrpip[0], strerror(errno));
468 return -1;
470 /*0 Info2("dup(%d) -> %d", wrpip[0], tmpi);*/
471 rdpip[1] = tmpi;
473 if (fdo == wrpip[0]) { /* a conflict here */
474 if ((tmpo = Dup(rdpip[1])) < 0) {
475 Error2("dup(%d): %s", rdpip[1], strerror(errno));
476 return -1;
478 /*0 Info2("dup(%d) -> %d", rdpip[1], tmpo);*/
479 wrpip[0] = tmpo;
482 if (rw != XIO_WRONLY && rdpip[1] != fdo) {
483 if (Dup2(rdpip[1], fdo) < 0) {
484 Error3("dup2(%d, %d): %s", rdpip[1], fdo, strerror(errno));
485 return -1;
487 Close(rdpip[1]);
488 /*0 Info2("dup2(%d, %d)", rdpip[1], fdo);*/
489 /*0 applyopts_cloexec(fdo, *copts);*/
491 if (rw != XIO_RDONLY && wrpip[0] != fdi) {
492 if (Dup2(wrpip[0], fdi) < 0) {
493 Error3("dup2(%d, %d): %s", wrpip[0], fdi, strerror(errno));
494 return -1;
496 Close(wrpip[0]);
497 /*0 Info2("dup2(%d, %d)", wrpip[0], fdi);*/
498 /*0 applyopts_cloexec(wrpip[0], *copts);*/ /* option is already consumed! */
499 /* applyopts_cloexec(fdi, *copts);*/ /* option is already consumed! */
502 applyopts(fdi, *copts, PH_LATE);
503 applyopts(fdo, *copts, PH_LATE);
504 applyopts(fdi, *copts, PH_LATE2);
505 applyopts(fdo, *copts, PH_LATE2);
507 } else { /* socketpair */
508 Close(sv[0]);
509 if (rw != XIO_RDONLY && fdi != sv[1]) {
510 if (Dup2(sv[1], fdi) < 0) {
511 Error3("dup2(%d, %d): %s", sv[1], fdi, strerror(errno));
512 return -1; }
513 /*0 Info2("dup2(%d, %d)", sv[1], fdi);*/
515 if (rw != XIO_WRONLY && fdo != sv[1]) {
516 if (Dup2(sv[1], fdo) < 0) {
517 Error3("dup2(%d, %d): %s", sv[1], fdo, strerror(errno));
518 return -1; }
519 /*0 Info2("dup2(%d, %d)", sv[1], fdo);*/
521 if (fdi != sv[1] && fdo != sv[1]) {
522 applyopts_cloexec(sv[1], *copts);
523 Close(sv[1]);
526 applyopts(fdi, *copts, PH_LATE);
527 applyopts(fdi, *copts, PH_LATE2);
529 } /* withfork */
530 else {
531 applyopts(-1, *copts, PH_LATE);
532 applyopts(-1, *copts, PH_LATE2);
534 _xioopen_setdelayeduser();
535 /* set group before user - maybe you are not permitted afterwards */
536 if (retropt_gidt(*copts, OPT_SETGID, &group) >= 0) {
537 Setgid(group);
539 if (retropt_uidt(*copts, OPT_SETUID, &user) >= 0) {
540 Setuid(user);
542 if (withstderr) {
543 *duptostderr = fdo;
544 } else {
545 *duptostderr = -1;
548 return 0; /* indicate child process */
551 /* for parent (this is our socat process) */
552 Notice1("forked off child process "F_pid, pid);
554 #if 0
555 if ((popts = copyopts(*copts,
556 GROUP_FD|GROUP_TERMIOS|GROUP_FORK|GROUP_SOCKET|GROUP_SOCK_UNIX|GROUP_FIFO)) == NULL)
557 return STAT_RETRYLATER;
558 #endif
560 #if HAVE_PTY
561 if (usepty) {
562 if (Close(ttyfd) < 0) {
563 Info2("close(%d): %s", ttyfd, strerror(errno));
565 } else
566 #endif /* HAVE_PTY */
567 if (usepipes) {
568 if (rw == XIO_RDONLY) Close(rdpip[1]);
569 if (rw == XIO_WRONLY) Close(wrpip[0]);
570 } else {
571 Close(sv[1]);
573 fd->para.exec.pid = pid;
575 if (applyopts_single(fd, popts, PH_LATE) < 0) return -1;
576 applyopts_signal(fd, popts);
577 if ((numleft = leftopts(popts)) > 0) {
578 Error1("%d option(s) could not be used", numleft);
579 showleft(popts);
580 return STAT_NORETRY;
583 return pid; /* indicate parent (main) process */
585 #endif /* WITH_EXEC || WITH_SYSTEM */
588 int setopt_path(struct opt *opts, char **path) {
589 if (retropt_string(opts, OPT_PATH, path) >= 0) {
590 if (setenv("PATH", *path, 1) < 0) {
591 Error1("setenv(\"PATH\", \"%s\", 1): insufficient space", *path);
592 return -1;
595 return 0;