Code sync
[external/libijs.git] / ijs_exec_unix.c
1 /**
2  * Copyright (c) 2001-2002 artofcode LLC.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23 **/
24
25 #include "unistd_.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include "ijs.h"
30 #include "ijs_client.h"
31
32 int 
33 ijs_exec_server(const char *server_cmd, int *pfd_to, int *pfd_from, 
34     int *pchild_pid)
35 {
36   int fds_to[2], fds_from[2];
37   int child_pid;
38
39   if (pipe (fds_to) < 0)
40     return -1;
41   if (pipe (fds_from) < 0)
42     {
43       close (fds_to[0]);
44       close (fds_to[1]);
45       return -1;
46     }
47
48   child_pid = fork ();
49   if (child_pid < 0)
50     {
51       close (fds_to[0]);
52       close (fds_to[1]);
53       close (fds_from[0]);
54       close (fds_from[1]);
55       return -1;
56     }
57   if (child_pid == 0)
58     {
59       int status;
60       char *argv[8];
61       int i = 0;
62
63       close (fds_to[1]);
64       close (fds_from[0]);
65
66       dup2 (fds_to[0], STDIN_FILENO);
67       dup2 (fds_from[1], STDOUT_FILENO);
68 #define noGDB
69 #ifdef GDB
70       argv[i++] = "gdb";
71 #endif
72
73       argv[i++] = "sh";
74       argv[i++] = "-c";
75
76       argv[i++] = (char *)server_cmd;
77       argv[i++] = NULL;
78       status = execvp (argv[0], argv);
79       if (status < 0)
80         exit (1);
81     }
82
83   /* Ignore SIGPIPE signals. This is the behaviour you'll want most of
84      the time, as the attempt to read or write to the pipe will fail
85      and the client will find out then. If the client needs to preserve
86      the SIGPIPE signal, it can either install its own signal handler
87      after this call, or hack the code.
88   */
89   signal (SIGPIPE, SIG_IGN);
90
91 #ifdef VERBOSE
92   fprintf (stderr, "child_pid = %d; %d %d\n",
93            child_pid, fds_to[0], fds_from[1]);
94 #endif
95   close (fds_to[0]);
96   close (fds_from[1]);
97
98   *pfd_to = fds_to[1];
99   *pfd_from = fds_from[0];
100   *pchild_pid = child_pid;
101
102   return 0;
103 }
104