Version 1.8.0.0
[socat.git] / xio-system.c
blobebe1bfbd6ce1b393ca554de86b964a168394db36
1 /* source: xio-system.c */
2 /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* this file contains the source for opening addresses of system type */
7 #include "xiosysincludes.h"
8 #include "xioopen.h"
10 #include "xio-progcall.h"
11 #include "xio-system.h"
14 #if WITH_SYSTEM
16 static int xioopen_system(int arg, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, const struct addrdesc *addrdesc);
18 const struct addrdesc xioaddr_system = { "SYSTEM", 3, xioopen_system, GROUP_FD|GROUP_FORK|GROUP_EXEC|GROUP_SOCKET|GROUP_SOCK_UNIX|GROUP_TERMIOS|GROUP_FIFO|GROUP_PTY|GROUP_PARENT, 1, 0, 0 HELP(":<shell-command>") };
21 static int xioopen_system(
22 int argc,
23 const char *argv[],
24 struct opt *opts,
25 int xioflags, /* XIO_RDONLY etc. */
26 xiofile_t *xfd,
27 const struct addrdesc *addrdesc)
29 struct single *sfd = &xfd->stream;
30 int status;
31 char *path = NULL;
32 int duptostderr;
33 int result;
34 const char *string = argv[1];
36 if (argc != 2) {
37 xio_syntax(argv[0], 1, argc-1, addrdesc->syntax);
38 return STAT_NORETRY;
41 status =
42 _xioopen_foxec(xioflags, sfd, addrdesc->groups, &opts, &duptostderr);
43 if (status < 0)
44 return status;
45 if (status == 0) { /* child */
46 int numleft;
48 /* do not shutdown connections that belong our parent */
49 sock[0] = NULL;
50 sock[1] = NULL;
52 if (setopt_path(opts, &path) < 0) {
53 /* this could be dangerous, so let us abort this child... */
54 Exit(1);
57 if ((numleft = leftopts(opts)) > 0) {
58 showleft(opts);
59 Error1("INTERNAL: %d option(s) remained unused", numleft);
60 return STAT_NORETRY;
63 /* only now redirect stderr */
64 if (duptostderr >= 0) {
65 diag_dup();
66 Dup2(duptostderr, 2);
68 Info1("executing shell command \"%s\"", string);
69 errno=0;
70 result = System(string);
71 if (result != 0) {
72 Warn2("system(\"%s\") returned with status %d", string, result);
73 if (errno != 0)
74 Warn1("system(): %s", strerror(errno));
76 Exit(result>>8); /* this child process */
79 /* parent */
80 _xio_openlate(sfd, opts);
81 return 0;
84 #endif /* WITH_SYSTEM */