2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
16 #include "kern_util.h"
17 #include "chan_user.h"
20 #include "choose-mode.h"
22 #include "um_malloc.h"
24 void generic_close(int fd
, void *unused
)
29 int generic_read(int fd
, char *c_out
, void *unused
)
33 n
= os_read_file(fd
, c_out
, sizeof(*c_out
));
42 /* XXX Trivial wrapper around os_write_file */
44 int generic_write(int fd
, const char *buf
, int n
, void *unused
)
46 return os_write_file(fd
, buf
, n
);
49 int generic_window_size(int fd
, void *unused
, unsigned short *rows_out
,
50 unsigned short *cols_out
)
55 ret
= os_window_size(fd
, &rows
, &cols
);
59 ret
= ((*rows_out
!= rows
) || (*cols_out
!= cols
));
67 void generic_free(void *data
)
72 int generic_console_write(int fd
, const char *buf
, int n
)
74 struct termios save
, new;
78 CATCH_EINTR(err
= tcgetattr(fd
, &save
));
82 /* The terminal becomes a bit less raw, to handle \n also as
83 * "Carriage Return", not only as "New Line". Otherwise, the new
84 * line won't start at the first column.*/
86 CATCH_EINTR(err
= tcsetattr(fd
, TCSAFLUSH
, &new));
90 err
= generic_write(fd
, buf
, n
, NULL
);
91 /* Restore raw mode, in any case; we *must* ignore any error apart
92 * EINTR, except for debug.*/
94 CATCH_EINTR(tcsetattr(fd
, TCSAFLUSH
, &save
));
101 * UML SIGWINCH handling
103 * The point of this is to handle SIGWINCH on consoles which have host
104 * ttys and relay them inside UML to whatever might be running on the
105 * console and cares about the window size (since SIGWINCH notifies
106 * about terminal size changes).
108 * So, we have a separate thread for each host tty attached to a UML
109 * device (side-issue - I'm annoyed that one thread can't have
110 * multiple controlling ttys for the purpose of handling SIGWINCH, but
111 * I imagine there are other reasons that doesn't make any sense).
113 * SIGWINCH can't be received synchronously, so you have to set up to
114 * receive it as a signal. That being the case, if you are going to
115 * wait for it, it is convenient to sit in sigsuspend() and wait for
116 * the signal to bounce you out of it (see below for how we make sure
117 * to exit only on SIGWINCH).
120 static void winch_handler(int sig
)
129 static int winch_thread(void *arg
)
131 struct winch_data
*data
= arg
;
137 pty_fd
= data
->pty_fd
;
138 pipe_fd
= data
->pipe_fd
;
139 count
= os_write_file(pipe_fd
, &c
, sizeof(c
));
140 if(count
!= sizeof(c
))
141 printk("winch_thread : failed to write synchronization "
142 "byte, err = %d\n", -count
);
144 /* We are not using SIG_IGN on purpose, so don't fix it as I thought to
145 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
148 signal(SIGWINCH
, winch_handler
);
150 /* Block all signals possible. */
151 if(sigprocmask(SIG_SETMASK
, &sigs
, NULL
) < 0){
152 printk("winch_thread : sigprocmask failed, errno = %d\n",
156 /* In sigsuspend(), block anything else than SIGWINCH. */
157 sigdelset(&sigs
, SIGWINCH
);
160 printk("winch_thread : setsid failed, errno = %d\n", errno
);
164 err
= os_new_tty_pgrp(pty_fd
, os_getpid());
166 printk("winch_thread : new_tty_pgrp failed on fd %d, "
167 "err = %d\n", pty_fd
, -err
);
171 /* These are synchronization calls between various UML threads on the
172 * host - since they are not different kernel threads, we cannot use
173 * kernel semaphores. We don't use SysV semaphores because they are
175 count
= os_read_file(pipe_fd
, &c
, sizeof(c
));
176 if(count
!= sizeof(c
))
177 printk("winch_thread : failed to read synchronization byte, "
178 "err = %d\n", -count
);
181 /* This will be interrupted by SIGWINCH only, since
182 * other signals are blocked.
186 count
= os_write_file(pipe_fd
, &c
, sizeof(c
));
187 if(count
!= sizeof(c
))
188 printk("winch_thread : write failed, err = %d\n",
193 static int winch_tramp(int fd
, struct tty_struct
*tty
, int *fd_out
,
194 unsigned long *stack_out
)
196 struct winch_data data
;
200 err
= os_pipe(fds
, 1, 1);
202 printk("winch_tramp : os_pipe failed, err = %d\n", -err
);
206 data
= ((struct winch_data
) { .pty_fd
= fd
,
207 .pipe_fd
= fds
[1] } );
208 /* CLONE_FILES so this thread doesn't hold open files which are open
209 * now, but later closed in a different thread. This is a
210 * problem with /dev/net/tun, which if held open by this
211 * thread, prevents the TUN/TAP device from being reused.
213 err
= run_helper_thread(winch_thread
, &data
, CLONE_FILES
, stack_out
);
215 printk("fork of winch_thread failed - errno = %d\n", -err
);
220 n
= os_read_file(fds
[0], &c
, sizeof(c
));
222 printk("winch_tramp : failed to read synchronization byte\n");
223 printk("read failed, err = %d\n", -n
);
224 printk("fd %d will not support SIGWINCH\n", fd
);
229 if (os_set_fd_block(*fd_out
, 0)) {
230 printk("winch_tramp: failed to set thread_fd non-blocking.\n");
237 os_close_file(fds
[1]);
238 os_close_file(fds
[0]);
243 void register_winch(int fd
, struct tty_struct
*tty
)
246 int pid
, thread
, count
, thread_fd
= -1;
253 if (!CHOOSE_MODE_PROC(is_tracer_winch
, is_skas_winch
, pid
, fd
, tty
) &&
255 thread
= winch_tramp(fd
, tty
, &thread_fd
, &stack
);
259 register_winch_irq(thread_fd
, fd
, thread
, tty
, stack
);
261 count
= os_write_file(thread_fd
, &c
, sizeof(c
));
262 if(count
!= sizeof(c
))
263 printk("register_winch : failed to write "
264 "synchronization byte, err = %d\n", -count
);