-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposix.c
188 lines (143 loc) · 3.5 KB
/
posix.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <errno.h>
#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <glib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "types.h"
#include "platform.h"
#include "console.h"
#include "protocol.h"
#include "proxy.h"
static int console_readline = 0;
static int opipe_read, opipe_write;
static gpointer console_thread(gpointer data);
void socket_init(void)
{
return;
}
/* readline input and log output interlacing */
void console_init(void)
{
if (isatty(0) && isatty(1))
{
int pipefd[2];
if (pipe(pipefd) != 0)
goto no_terminal;
opipe_read = pipefd[0];
opipe_write = pipefd[1];
rl_readline_name = "mcmap";
console_readline = 1;
console_outfd = opipe_write;
atexit(console_cleanup);
g_thread_create(console_thread, NULL, false, 0);
}
no_terminal:
return;
}
static void console_line_ready(char *line)
{
if (line)
{
add_history(line);
inject_to_server(packet_new(PACKET_CHAT_MESSAGE, line));
}
else /* ^D */
exit(0);
free(line);
rl_callback_handler_install("> ", console_line_ready);
}
static gpointer console_thread(gpointer data)
{
struct pollfd pfds[2] = {
{ .fd = opipe_read, .events = POLLIN },
{ .fd = 0, .events = POLLIN }
};
GIOChannel *och = g_io_channel_unix_new(opipe_read);
rl_callback_handler_install("> ", console_line_ready);
while (1)
{
/* wait for input (stdin) or output (pipe) */
int i = poll(pfds, 2, -1);
if (i < 0 && errno == EINTR)
continue;
if (pfds[0].revents & POLLIN)
{
/* clean up the prompt and its contents */
int old_point = rl_point, old_mark = rl_mark;
char *old_text = strdup(rl_line_buffer);
rl_replace_line("", 0);
rl_redisplay();
fputs("\r\x1b[K", stdout);
/* display pending output lines (TODO: more than one) */
char *line = 0;
size_t line_eol = 0;
g_io_channel_read_line(och, &line, 0, &line_eol, 0);
if (!line || !*line)
{
rl_callback_handler_remove();
console_cleanup();
return NULL;
}
fputs(line, stdout);
g_free(line);
/* restore the readline prompt and contents */
rl_insert_text(old_text);
rl_point = old_point;
rl_mark = old_mark;
free(old_text);
rl_forced_update_display();
}
if (pfds[1].revents & POLLIN)
{
/* let readline eat from stdin */
rl_callback_read_char();
}
}
exit(0);
}
void console_cleanup(void)
{
if (console_readline)
{
rl_deprep_terminal();
putchar('\n');
close(opipe_write);
}
console_readline = 0;
console_outfd = 1;
}
/* mmap/mremap-based solution for mmapping */
mmap_handle_t make_mmap(int fd, size_t len, void **addr)
{
void *m = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (m == MAP_FAILED)
*addr = 0;
else
*addr = m;
return *addr;
}
mmap_handle_t resize_mmap(mmap_handle_t old, void *old_addr, int fd, size_t old_len, size_t new_len, void **addr)
{
#ifdef __linux
/* Linux at least has mremap... */
void *new_addr = mremap(old_addr, old_len, new_len, MREMAP_MAYMOVE);
if (new_addr == MAP_FAILED)
*addr = 0;
else
*addr = new_addr;
return *addr;
#else
munmap(old_addr, old_len);
return make_mmap(fd, new_len, addr);
#endif
}
void sync_mmap(void *addr, size_t len)
{
msync(addr, len, MS_ASYNC);
}