2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
3 * Licensed under the GPL
12 #include <sys/ioctl.h>
13 #include "chan_user.h"
15 #include "um_malloc.h"
18 void generic_close(int fd
, void *unused
)
23 int generic_read(int fd
, char *c_out
, void *unused
)
27 n
= read(fd
, c_out
, sizeof(*c_out
));
30 else if (errno
== EAGAIN
)
37 /* XXX Trivial wrapper around write */
39 int generic_write(int fd
, const char *buf
, int n
, void *unused
)
43 err
= write(fd
, buf
, n
);
46 else if (errno
== EAGAIN
)
53 int generic_window_size(int fd
, void *unused
, unsigned short *rows_out
,
54 unsigned short *cols_out
)
59 if (ioctl(fd
, TIOCGWINSZ
, &size
) < 0)
62 ret
= ((*rows_out
!= size
.ws_row
) || (*cols_out
!= size
.ws_col
));
64 *rows_out
= size
.ws_row
;
65 *cols_out
= size
.ws_col
;
70 void generic_free(void *data
)
75 int generic_console_write(int fd
, const char *buf
, int n
)
77 struct termios save
, new;
81 CATCH_EINTR(err
= tcgetattr(fd
, &save
));
86 * The terminal becomes a bit less raw, to handle \n also as
87 * "Carriage Return", not only as "New Line". Otherwise, the new
88 * line won't start at the first column.
91 CATCH_EINTR(err
= tcsetattr(fd
, TCSAFLUSH
, &new));
95 err
= generic_write(fd
, buf
, n
, NULL
);
97 * Restore raw mode, in any case; we *must* ignore any error apart
98 * EINTR, except for debug.
101 CATCH_EINTR(tcsetattr(fd
, TCSAFLUSH
, &save
));
108 * UML SIGWINCH handling
110 * The point of this is to handle SIGWINCH on consoles which have host
111 * ttys and relay them inside UML to whatever might be running on the
112 * console and cares about the window size (since SIGWINCH notifies
113 * about terminal size changes).
115 * So, we have a separate thread for each host tty attached to a UML
116 * device (side-issue - I'm annoyed that one thread can't have
117 * multiple controlling ttys for the purpose of handling SIGWINCH, but
118 * I imagine there are other reasons that doesn't make any sense).
120 * SIGWINCH can't be received synchronously, so you have to set up to
121 * receive it as a signal. That being the case, if you are going to
122 * wait for it, it is convenient to sit in sigsuspend() and wait for
123 * the signal to bounce you out of it (see below for how we make sure
124 * to exit only on SIGWINCH).
127 static void winch_handler(int sig
)
136 static int winch_thread(void *arg
)
138 struct winch_data
*data
= arg
;
144 pty_fd
= data
->pty_fd
;
145 pipe_fd
= data
->pipe_fd
;
146 count
= write(pipe_fd
, &c
, sizeof(c
));
147 if (count
!= sizeof(c
))
148 printk(UM_KERN_ERR
"winch_thread : failed to write "
149 "synchronization byte, err = %d\n", -count
);
152 * We are not using SIG_IGN on purpose, so don't fix it as I thought to
153 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
157 signal(SIGWINCH
, winch_handler
);
159 /* Block all signals possible. */
160 if (sigprocmask(SIG_SETMASK
, &sigs
, NULL
) < 0) {
161 printk(UM_KERN_ERR
"winch_thread : sigprocmask failed, "
162 "errno = %d\n", errno
);
165 /* In sigsuspend(), block anything else than SIGWINCH. */
166 sigdelset(&sigs
, SIGWINCH
);
169 printk(UM_KERN_ERR
"winch_thread : setsid failed, errno = %d\n",
174 if (ioctl(pty_fd
, TIOCSCTTY
, 0) < 0) {
175 printk(UM_KERN_ERR
"winch_thread : TIOCSCTTY failed on "
176 "fd %d err = %d\n", pty_fd
, errno
);
180 if (tcsetpgrp(pty_fd
, os_getpid()) < 0) {
181 printk(UM_KERN_ERR
"winch_thread : tcsetpgrp failed on "
182 "fd %d err = %d\n", pty_fd
, errno
);
187 * These are synchronization calls between various UML threads on the
188 * host - since they are not different kernel threads, we cannot use
189 * kernel semaphores. We don't use SysV semaphores because they are
192 count
= read(pipe_fd
, &c
, sizeof(c
));
193 if (count
!= sizeof(c
))
194 printk(UM_KERN_ERR
"winch_thread : failed to read "
195 "synchronization byte, err = %d\n", errno
);
199 * This will be interrupted by SIGWINCH only, since
200 * other signals are blocked.
204 count
= write(pipe_fd
, &c
, sizeof(c
));
205 if (count
!= sizeof(c
))
206 printk(UM_KERN_ERR
"winch_thread : write failed, "
207 "err = %d\n", errno
);
211 static int winch_tramp(int fd
, struct tty_struct
*tty
, int *fd_out
,
212 unsigned long *stack_out
)
214 struct winch_data data
;
218 err
= os_pipe(fds
, 1, 1);
220 printk(UM_KERN_ERR
"winch_tramp : os_pipe failed, err = %d\n",
225 data
= ((struct winch_data
) { .pty_fd
= fd
,
226 .pipe_fd
= fds
[1] } );
228 * CLONE_FILES so this thread doesn't hold open files which are open
229 * now, but later closed in a different thread. This is a
230 * problem with /dev/net/tun, which if held open by this
231 * thread, prevents the TUN/TAP device from being reused.
233 err
= run_helper_thread(winch_thread
, &data
, CLONE_FILES
, stack_out
);
235 printk(UM_KERN_ERR
"fork of winch_thread failed - errno = %d\n",
241 n
= read(fds
[0], &c
, sizeof(c
));
242 if (n
!= sizeof(c
)) {
243 printk(UM_KERN_ERR
"winch_tramp : failed to read "
244 "synchronization byte\n");
245 printk(UM_KERN_ERR
"read failed, err = %d\n", errno
);
246 printk(UM_KERN_ERR
"fd %d will not support SIGWINCH\n", fd
);
251 if (os_set_fd_block(*fd_out
, 0)) {
252 printk(UM_KERN_ERR
"winch_tramp: failed to set thread_fd "
266 void register_winch(int fd
, struct tty_struct
*tty
)
269 int pid
, thread
, count
, thread_fd
= -1;
276 if (!is_skas_winch(pid
, fd
, tty
) && (pid
== -1)) {
277 thread
= winch_tramp(fd
, tty
, &thread_fd
, &stack
);
281 register_winch_irq(thread_fd
, fd
, thread
, tty
, stack
);
283 count
= write(thread_fd
, &c
, sizeof(c
));
284 if (count
!= sizeof(c
))
285 printk(UM_KERN_ERR
"register_winch : failed to write "
286 "synchronization byte, err = %d\n", errno
);