1 |
slords |
1.1 |
#include <sys/types.h> |
2 |
|
|
#include <sys/socket.h> |
3 |
|
|
#include <stdio.h> |
4 |
|
|
#include <errno.h> |
5 |
|
|
#include <stdlib.h> |
6 |
|
|
#include <fcntl.h> |
7 |
|
|
#include <strings.h> |
8 |
|
|
|
9 |
|
|
int main(int argc, char *argv[]) |
10 |
|
|
{ |
11 |
|
|
int s[2]; |
12 |
|
|
char **args1, **args2; |
13 |
|
|
int firstarg, lastarg; |
14 |
|
|
int i; |
15 |
|
|
|
16 |
|
|
if (socketpair(PF_UNIX, SOCK_STREAM, 0, s) < 0) |
17 |
|
|
{ |
18 |
|
|
fprintf(stderr,"socketpair failed: %s\n",strerror(errno)); |
19 |
|
|
exit(1); |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
/* Make this configurable later, but for now it's fine. */ |
23 |
|
|
|
24 |
|
|
/* Find the first program to execute. */ |
25 |
|
|
firstarg = 1; |
26 |
|
|
for(i=1;i<argc;i++) |
27 |
|
|
{ |
28 |
|
|
if (strcmp(argv[i],"-makesock_connect_to")==0) |
29 |
|
|
{ |
30 |
|
|
break; |
31 |
|
|
} |
32 |
|
|
} |
33 |
|
|
lastarg = i; |
34 |
|
|
if ( (args1=malloc((lastarg-firstarg+1) * sizeof(char *))) == NULL) |
35 |
|
|
{ |
36 |
|
|
fprintf(stderr,"malloc error: %s\n",strerror(errno)); |
37 |
|
|
exit(1); |
38 |
|
|
} |
39 |
|
|
for (i=0;i<(lastarg-firstarg);i++) |
40 |
|
|
{ |
41 |
|
|
args1[i]=argv[firstarg+i]; |
42 |
|
|
} |
43 |
|
|
args1[i] = NULL; |
44 |
|
|
|
45 |
|
|
lastarg++; |
46 |
|
|
|
47 |
|
|
/* Now find the last program to execute. */ |
48 |
|
|
if ( (args2=malloc((argc-lastarg+1) * sizeof(char *))) == NULL) |
49 |
|
|
{ |
50 |
|
|
fprintf(stderr,"malloc error: %s\n",strerror(errno)); |
51 |
|
|
exit(1); |
52 |
|
|
} |
53 |
|
|
for(i=0;i<(argc-lastarg);i++) |
54 |
|
|
{ |
55 |
|
|
args2[i] = argv[lastarg+i]; |
56 |
|
|
} |
57 |
|
|
args2[i] = NULL; |
58 |
|
|
|
59 |
|
|
switch(fork()) |
60 |
|
|
{ |
61 |
|
|
case -1: |
62 |
|
|
/* Error. */ |
63 |
|
|
exit(1); |
64 |
|
|
case 0: |
65 |
|
|
/* Child */ |
66 |
|
|
close(s[0]); |
67 |
|
|
close(0); |
68 |
|
|
close(1); |
69 |
|
|
if (dup2(s[1],0) < 0) |
70 |
|
|
{ |
71 |
|
|
fprintf(stderr,"dup2 error: %s\n",strerror(errno)); |
72 |
|
|
exit(1); |
73 |
|
|
} |
74 |
|
|
if (dup2(s[1],1) < 0) |
75 |
|
|
{ |
76 |
|
|
fprintf(stderr,"dup2 error: %s\n",strerror(errno)); |
77 |
|
|
exit(1); |
78 |
|
|
} |
79 |
|
|
if (execvp(args2[0],args2) < 0) |
80 |
|
|
{ |
81 |
|
|
fprintf(stderr,"execvp error: %s\n",strerror(errno)); |
82 |
|
|
exit(1); |
83 |
|
|
} |
84 |
|
|
break; |
85 |
|
|
default: |
86 |
|
|
/* Parent */ |
87 |
|
|
if (s[0] != 3) |
88 |
|
|
{ |
89 |
|
|
close(3); |
90 |
|
|
if (dup2(s[0],3) < 0) |
91 |
|
|
{ |
92 |
|
|
fprintf(stderr,"dup2 error: %s\n",strerror(errno)); |
93 |
|
|
exit(1); |
94 |
|
|
} |
95 |
|
|
if (fcntl(3,F_SETFD,0) == -1) /* Turn off close-on-exec */ |
96 |
|
|
{ |
97 |
|
|
fprintf(stderr,"fcntl error: %s\n",strerror(errno)); |
98 |
|
|
exit(1); |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
if (s[1] != 3) |
102 |
|
|
close(s[1]); |
103 |
|
|
if (execvp(args1[0],args1) < 0) |
104 |
|
|
{ |
105 |
|
|
fprintf(stderr,"execvp error: %s\n",strerror(errno)); |
106 |
|
|
exit(1); |
107 |
|
|
} |
108 |
|
|
break; |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|