author | viric@llimona |
Mon, 24 Sep 2007 13:59:40 +0200 | |
changeset 38 | f1e581c862d5 |
parent 29 | 91286c3ecebc |
child 41 | 954941c6e40a |
permissions | -rw-r--r-- |
23 | 1 |
#include <sys/types.h> |
2 |
#include <unistd.h> |
|
3 |
#include <errno.h> |
|
25 | 4 |
#include <fcntl.h> |
23 | 5 |
#include <string.h> |
6 |
#include <sys/select.h> |
|
25 | 7 |
#include <sys/ioctl.h> |
8 |
#include <termios.h> |
|
9 |
#include <pty.h> |
|
23 | 10 |
|
11 |
#include "main.h" |
|
12 |
||
25 | 13 |
enum |
14 |
{ |
|
15 |
STDIN, |
|
16 |
STDOUT, |
|
17 |
STDERR |
|
18 |
}; |
|
19 |
||
20 |
static void set_close_on_exec(int fd) |
|
21 |
{ |
|
22 |
int res; |
|
23 |
res = fcntl(fd, F_SETFD, FD_CLOEXEC); |
|
24 |
if (res == -1) |
|
25 |
error("Set close-on-exec failed"); |
|
26 |
} |
|
27 |
||
28 |
static void give_terminal(int parent[], int child[]) |
|
29 |
{ |
|
30 |
struct termios tios; |
|
31 |
struct winsize wsize; |
|
32 |
int res; |
|
33 |
int master, slave; |
|
34 |
||
28
b73712b8370d
use the termios calls to stdout instead of stdin.
viric@llimona
parents:
26
diff
changeset
|
35 |
res = tcgetattr(1, &tios); |
25 | 36 |
if (res == -1) |
37 |
error("tcgetatttr"); |
|
38 |
||
28
b73712b8370d
use the termios calls to stdout instead of stdin.
viric@llimona
parents:
26
diff
changeset
|
39 |
res = ioctl(1, TIOCGWINSZ, &wsize); |
25 | 40 |
if (res == -1) |
41 |
error("ioctl TIOCGWINSZ"); |
|
42 |
||
43 |
res = openpty(&master, &slave, 0, &tios, &wsize); |
|
44 |
if (res == -1) |
|
45 |
error("openpty"); |
|
46 |
||
47 |
parent[STDIN] = master; |
|
48 |
parent[STDOUT] = master; |
|
49 |
parent[STDERR] = master; |
|
50 |
child[STDIN] = slave; |
|
51 |
child[STDOUT] = slave; |
|
52 |
child[STDERR] = slave; |
|
53 |
set_close_on_exec(master); |
|
54 |
set_close_on_exec(slave); |
|
55 |
} |
|
56 |
||
57 |
static void give_pipes(int parent[], int child[]) |
|
58 |
{ |
|
59 |
int p_stdin[2]; /* from us to mpg321 */ |
|
60 |
int p_stdout[2]; /* from mpg321 to us */ |
|
61 |
int p_stderr[2]; /* from mpg321 to us, its stderr */ |
|
62 |
||
63 |
pipe(p_stdin); |
|
64 |
pipe(p_stdout); |
|
65 |
pipe(p_stderr); |
|
66 |
||
67 |
parent[STDIN] = p_stdin[1]; |
|
68 |
parent[STDOUT] = p_stdout[0]; |
|
69 |
parent[STDERR] = p_stderr[0]; |
|
70 |
child[STDIN] = p_stdin[0]; |
|
71 |
child[STDOUT] = p_stdout[1]; |
|
72 |
child[STDERR] = p_stderr[1]; |
|
73 |
set_close_on_exec(parent[STDIN]); |
|
74 |
set_close_on_exec(parent[STDOUT]); |
|
75 |
set_close_on_exec(parent[STDERR]); |
|
76 |
set_close_on_exec(child[STDIN]); |
|
77 |
set_close_on_exec(child[STDOUT]); |
|
78 |
set_close_on_exec(child[STDERR]); |
|
79 |
} |
|
80 |
||
23 | 81 |
int fork_app(char * const command[]) |
82 |
{ |
|
25 | 83 |
int p_parent[3]; |
84 |
int p_child[3]; |
|
23 | 85 |
int pid; |
86 |
int res; |
|
87 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
28
diff
changeset
|
88 |
if (command_line.s_param.run_in_subterminal) |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
89 |
give_terminal(p_parent, p_child); |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
90 |
else |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
91 |
give_pipes(p_parent, p_child); |
25 | 92 |
|
93 |
/* globals */ |
|
94 |
app_stdin = p_parent[STDIN]; /* For us to write */ |
|
95 |
app_stdout = p_parent[STDOUT]; /* For us to read */ |
|
96 |
app_stderr = p_parent[STDERR]; /* For us to read */ |
|
23 | 97 |
|
98 |
pid = fork(); |
|
99 |
||
100 |
switch(pid) |
|
101 |
{ |
|
102 |
case 0: /* child */ |
|
25 | 103 |
res = dup2(p_child[STDIN], 0); |
104 |
if (res == -1) error("Dup2 stdin"); |
|
23 | 105 |
|
25 | 106 |
res = dup2(p_child[STDOUT], 1); |
107 |
if (res == -1) error("Dup2 stdout"); |
|
108 |
||
109 |
res = dup2(p_child[STDERR], 2); |
|
110 |
if (res == -1) error("Dup2 stderr"); |
|
23 | 111 |
|
112 |
execvp(command[0], command); |
|
113 |
||
25 | 114 |
error("Cannot execvp %s", command[0]); |
23 | 115 |
case -1: |
116 |
error("Failed fork"); |
|
117 |
default: /* parent */ |
|
25 | 118 |
close(p_child[STDIN]); |
119 |
close(p_child[STDOUT]); |
|
120 |
close(p_child[STDERR]); |
|
23 | 121 |
} |
122 |
||
123 |
return pid; |
|
124 |
} |
|
125 |