summaryrefslogtreecommitdiff
path: root/init/init.c
blob: 090dfe2709ad65ea19be5398b8df112ac770d0a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <poll.h>

int run_start(char* _prog)
{
	pid_t pid = fork();

	if( pid < 0 )
		return 1;
	else if (pid == 0)
		return 0;

	execl(_prog, _prog, NULL);
}

int run_stdio(char* _prog)
{
	
	int fork_pipe_out[2];
	int fork_pipe_in [2];
	pid_t pid = 0;

	if(pipe(fork_pipe_out) == -1)
		return 1;

	if(pipe(fork_pipe_in) == -1)
		return 1;

	if((pid = fork()) == -1)
		return 2;

	if (pid == 0)
	{
		dup2(fork_pipe_out[1], STDOUT_FILENO);
		dup2(fork_pipe_in [0], STDIN_FILENO );
		close (fork_pipe_out[0]);
		close (fork_pipe_in [1]);
		
		execl(_prog, _prog, NULL);

		return 3;
	}
	else
	{
		printf("asdf\n");
		close(fork_pipe_out[1]);
		close(fork_pipe_in [0]);
		
		int ret;
		struct pollfd fds[2];
		fds[0].fd = STDIN_FILENO;
		fds[0].events = POLLIN;

		fds[1].fd = fork_pipe_out[0];
		fds[1].events = POLLIN;

		while(1)
		{
			int timeout = 1; //Timeout msecs
			ret = poll(fds, 2, timeout);

			if(ret < 0)
				break;
			else if (ret == 0)
				continue;
			

			int nbytes;
			char buffer[256];

			if(fds[0].revents & POLLIN) //STDIN_FILENO
			{
				printf("a\n");
				nbytes = read(STDIN_FILENO, buffer, sizeof(buffer));
				write(fork_pipe_in[1], buffer, nbytes);
				write(fork_pipe_in[1], "\n", 1);
				
			}
			if(fds[1].revents & POLLIN) //PIPE
			{
				printf("b\”");
				nbytes = read(fork_pipe_out[0], buffer, sizeof(buffer));
				write(STDOUT_FILENO, buffer, nbytes);
				write(STDOUT_FILENO, "\n", 1);
			}
		}
		wait(0);	
	}
}

int main(int argc, char* argv[])
{
	printf("MegumInit explosion!\n");
	run_stdio("yes");

	return 0;
}