This source file includes following definitions.
- generic_close
- generic_read
- generic_write
- generic_window_size
- generic_free
- generic_console_write
- winch_handler
- winch_thread
- winch_tramp
- register_winch
1
2
3
4
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sched.h>
10 #include <signal.h>
11 #include <termios.h>
12 #include <sys/ioctl.h>
13 #include "chan_user.h"
14 #include <os.h>
15 #include <um_malloc.h>
16
17 void generic_close(int fd, void *unused)
18 {
19 close(fd);
20 }
21
22 int generic_read(int fd, char *c_out, void *unused)
23 {
24 int n;
25
26 n = read(fd, c_out, sizeof(*c_out));
27 if (n > 0)
28 return n;
29 else if (errno == EAGAIN)
30 return 0;
31 else if (n == 0)
32 return -EIO;
33 return -errno;
34 }
35
36
37
38 int generic_write(int fd, const char *buf, int n, void *unused)
39 {
40 int err;
41
42 err = write(fd, buf, n);
43 if (err > 0)
44 return err;
45 else if (errno == EAGAIN)
46 return 0;
47 else if (err == 0)
48 return -EIO;
49 return -errno;
50 }
51
52 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
53 unsigned short *cols_out)
54 {
55 struct winsize size;
56 int ret;
57
58 if (ioctl(fd, TIOCGWINSZ, &size) < 0)
59 return -errno;
60
61 ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
62
63 *rows_out = size.ws_row;
64 *cols_out = size.ws_col;
65
66 return ret;
67 }
68
69 void generic_free(void *data)
70 {
71 kfree(data);
72 }
73
74 int generic_console_write(int fd, const char *buf, int n)
75 {
76 sigset_t old, no_sigio;
77 struct termios save, new;
78 int err;
79
80 if (isatty(fd)) {
81 sigemptyset(&no_sigio);
82 sigaddset(&no_sigio, SIGIO);
83 if (sigprocmask(SIG_BLOCK, &no_sigio, &old))
84 goto error;
85
86 CATCH_EINTR(err = tcgetattr(fd, &save));
87 if (err)
88 goto error;
89 new = save;
90
91
92
93
94
95 new.c_oflag |= OPOST;
96 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
97 if (err)
98 goto error;
99 }
100 err = generic_write(fd, buf, n, NULL);
101
102
103
104
105 if (isatty(fd)) {
106 CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
107 sigprocmask(SIG_SETMASK, &old, NULL);
108 }
109
110 return err;
111 error:
112 return -errno;
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 static void winch_handler(int sig)
136 {
137 }
138
139 struct winch_data {
140 int pty_fd;
141 int pipe_fd;
142 };
143
144 static int winch_thread(void *arg)
145 {
146 struct winch_data *data = arg;
147 sigset_t sigs;
148 int pty_fd, pipe_fd;
149 int count;
150 char c = 1;
151
152 pty_fd = data->pty_fd;
153 pipe_fd = data->pipe_fd;
154 count = write(pipe_fd, &c, sizeof(c));
155 if (count != sizeof(c))
156 printk(UM_KERN_ERR "winch_thread : failed to write "
157 "synchronization byte, err = %d\n", -count);
158
159
160
161
162
163
164
165 signal(SIGWINCH, winch_handler);
166 sigfillset(&sigs);
167
168 if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
169 printk(UM_KERN_ERR "winch_thread : sigprocmask failed, "
170 "errno = %d\n", errno);
171 exit(1);
172 }
173
174 sigdelset(&sigs, SIGWINCH);
175
176 if (setsid() < 0) {
177 printk(UM_KERN_ERR "winch_thread : setsid failed, errno = %d\n",
178 errno);
179 exit(1);
180 }
181
182 if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
183 printk(UM_KERN_ERR "winch_thread : TIOCSCTTY failed on "
184 "fd %d err = %d\n", pty_fd, errno);
185 exit(1);
186 }
187
188 if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
189 printk(UM_KERN_ERR "winch_thread : tcsetpgrp failed on "
190 "fd %d err = %d\n", pty_fd, errno);
191 exit(1);
192 }
193
194
195
196
197
198
199
200 count = read(pipe_fd, &c, sizeof(c));
201 if (count != sizeof(c))
202 printk(UM_KERN_ERR "winch_thread : failed to read "
203 "synchronization byte, err = %d\n", errno);
204
205 while(1) {
206
207
208
209
210 sigsuspend(&sigs);
211
212 count = write(pipe_fd, &c, sizeof(c));
213 if (count != sizeof(c))
214 printk(UM_KERN_ERR "winch_thread : write failed, "
215 "err = %d\n", errno);
216 }
217 }
218
219 static int winch_tramp(int fd, struct tty_port *port, int *fd_out,
220 unsigned long *stack_out)
221 {
222 struct winch_data data;
223 int fds[2], n, err;
224 char c;
225
226 err = os_pipe(fds, 1, 1);
227 if (err < 0) {
228 printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
229 -err);
230 goto out;
231 }
232
233 data = ((struct winch_data) { .pty_fd = fd,
234 .pipe_fd = fds[1] } );
235
236
237
238
239
240
241 err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
242 if (err < 0) {
243 printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
244 -err);
245 goto out_close;
246 }
247
248 *fd_out = fds[0];
249 n = read(fds[0], &c, sizeof(c));
250 if (n != sizeof(c)) {
251 printk(UM_KERN_ERR "winch_tramp : failed to read "
252 "synchronization byte\n");
253 printk(UM_KERN_ERR "read failed, err = %d\n", errno);
254 printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
255 err = -EINVAL;
256 goto out_close;
257 }
258
259 if (os_set_fd_block(*fd_out, 0)) {
260 printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
261 "non-blocking.\n");
262 goto out_close;
263 }
264
265 return err;
266
267 out_close:
268 close(fds[1]);
269 close(fds[0]);
270 out:
271 return err;
272 }
273
274 void register_winch(int fd, struct tty_port *port)
275 {
276 unsigned long stack;
277 int pid, thread, count, thread_fd = -1;
278 char c = 1;
279
280 if (!isatty(fd))
281 return;
282
283 pid = tcgetpgrp(fd);
284 if (is_skas_winch(pid, fd, port)) {
285 register_winch_irq(-1, fd, -1, port, 0);
286 return;
287 }
288
289 if (pid == -1) {
290 thread = winch_tramp(fd, port, &thread_fd, &stack);
291 if (thread < 0)
292 return;
293
294 register_winch_irq(thread_fd, fd, thread, port, stack);
295
296 count = write(thread_fd, &c, sizeof(c));
297 if (count != sizeof(c))
298 printk(UM_KERN_ERR "register_winch : failed to write "
299 "synchronization byte, err = %d\n", errno);
300 }
301 }