1 /* 2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com) 3 * Licensed under the GPL 4 */ 5 6 #include <unistd.h> 7 #include <stdlib.h> 8 #include <errno.h> 9 #include <termios.h> 10 #include <string.h> 11 #include <signal.h> 12 #include <sys/stat.h> 13 #include <sys/ioctl.h> 14 #include <sys/socket.h> 15 #include "kern_util.h" 16 #include "user_util.h" 17 #include "chan_user.h" 18 #include "user.h" 19 #include "os.h" 20 #include "choose-mode.h" 21 #include "mode.h" 22 23 int generic_console_write(int fd, const char *buf, int n) 24 { 25 struct termios save, new; 26 int err; 27 28 if(isatty(fd)){ 29 CATCH_EINTR(err = tcgetattr(fd, &save)); 30 if (err) 31 goto error; 32 new = save; 33 /* The terminal becomes a bit less raw, to handle \n also as 34 * "Carriage Return", not only as "New Line". Otherwise, the new 35 * line won't start at the first column.*/ 36 new.c_oflag |= OPOST; 37 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new)); 38 if (err) 39 goto error; 40 } 41 err = generic_write(fd, buf, n, NULL); 42 /* Restore raw mode, in any case; we *must* ignore any error apart 43 * EINTR, except for debug.*/ 44 if(isatty(fd)) 45 CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save)); 46 return(err); 47 error: 48 return(-errno); 49 } 50 51 /* 52 * UML SIGWINCH handling 53 * 54 * The point of this is to handle SIGWINCH on consoles which have host ttys and 55 * relay them inside UML to whatever might be running on the console and cares 56 * about the window size (since SIGWINCH notifies about terminal size changes). 57 * 58 * So, we have a separate thread for each host tty attached to a UML device 59 * (side-issue - I'm annoyed that one thread can't have multiple controlling 60 * ttys for purposed of handling SIGWINCH, but I imagine there are other reasons 61 * that doesn't make any sense). 62 * 63 * SIGWINCH can't be received synchronously, so you have to set up to receive it 64 * as a signal. That being the case, if you are going to wait for it, it is 65 * convenient to sit in sigsuspend() and wait for the signal to bounce you out of 66 * it (see below for how we make sure to exit only on SIGWINCH). 67 */ 68 69 static void winch_handler(int sig) 70 { 71 } 72 73 struct winch_data { 74 int pty_fd; 75 int pipe_fd; 76 int close_me; 77 }; 78 79 static int winch_thread(void *arg) 80 { 81 struct winch_data *data = arg; 82 sigset_t sigs; 83 int pty_fd, pipe_fd; 84 int count, err; 85 char c = 1; 86 87 os_close_file(data->close_me); 88 pty_fd = data->pty_fd; 89 pipe_fd = data->pipe_fd; 90 count = os_write_file(pipe_fd, &c, sizeof(c)); 91 if(count != sizeof(c)) 92 printk("winch_thread : failed to write synchronization " 93 "byte, err = %d\n", -count); 94 95 /* We are not using SIG_IGN on purpose, so don't fix it as I thought to 96 * do! If using SIG_IGN, the sigsuspend() call below would not stop on 97 * SIGWINCH. */ 98 99 signal(SIGWINCH, winch_handler); 100 sigfillset(&sigs); 101 /* Block all signals possible. */ 102 if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){ 103 printk("winch_thread : sigprocmask failed, errno = %d\n", 104 errno); 105 exit(1); 106 } 107 /* In sigsuspend(), block anything else than SIGWINCH. */ 108 sigdelset(&sigs, SIGWINCH); 109 110 if(setsid() < 0){ 111 printk("winch_thread : setsid failed, errno = %d\n", errno); 112 exit(1); 113 } 114 115 err = os_new_tty_pgrp(pty_fd, os_getpid()); 116 if(err < 0){ 117 printk("winch_thread : new_tty_pgrp failed, err = %d\n", -err); 118 exit(1); 119 } 120 121 /* These are synchronization calls between various UML threads on the 122 * host - since they are not different kernel threads, we cannot use 123 * kernel semaphores. We don't use SysV semaphores because they are 124 * persistant. */ 125 count = os_read_file(pipe_fd, &c, sizeof(c)); 126 if(count != sizeof(c)) 127 printk("winch_thread : failed to read synchronization byte, " 128 "err = %d\n", -count); 129 130 while(1){ 131 /* This will be interrupted by SIGWINCH only, since other signals 132 * are blocked.*/ 133 sigsuspend(&sigs); 134 135 count = os_write_file(pipe_fd, &c, sizeof(c)); 136 if(count != sizeof(c)) 137 printk("winch_thread : write failed, err = %d\n", 138 -count); 139 } 140 } 141 142 static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out) 143 { 144 struct winch_data data; 145 unsigned long stack; 146 int fds[2], n, err; 147 char c; 148 149 err = os_pipe(fds, 1, 1); 150 if(err < 0){ 151 printk("winch_tramp : os_pipe failed, err = %d\n", -err); 152 goto out; 153 } 154 155 data = ((struct winch_data) { .pty_fd = fd, 156 .pipe_fd = fds[1], 157 .close_me = fds[0] } ); 158 err = run_helper_thread(winch_thread, &data, 0, &stack, 0); 159 if(err < 0){ 160 printk("fork of winch_thread failed - errno = %d\n", errno); 161 goto out_close; 162 } 163 164 os_close_file(fds[1]); 165 *fd_out = fds[0]; 166 n = os_read_file(fds[0], &c, sizeof(c)); 167 if(n != sizeof(c)){ 168 printk("winch_tramp : failed to read synchronization byte\n"); 169 printk("read failed, err = %d\n", -n); 170 printk("fd %d will not support SIGWINCH\n", fd); 171 err = -EINVAL; 172 goto out_close1; 173 } 174 return err ; 175 176 out_close: 177 os_close_file(fds[1]); 178 out_close1: 179 os_close_file(fds[0]); 180 out: 181 return err; 182 } 183 184 void register_winch(int fd, struct tty_struct *tty) 185 { 186 int pid, thread, thread_fd = -1; 187 int count; 188 char c = 1; 189 190 if(!isatty(fd)) 191 return; 192 193 pid = tcgetpgrp(fd); 194 if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd, 195 tty) && (pid == -1)){ 196 thread = winch_tramp(fd, tty, &thread_fd); 197 if(thread > 0){ 198 register_winch_irq(thread_fd, fd, thread, tty); 199 200 count = os_write_file(thread_fd, &c, sizeof(c)); 201 if(count != sizeof(c)) 202 printk("register_winch : failed to write " 203 "synchronization byte, err = %d\n", 204 -count); 205 } 206 } 207 } 208 209 /* 210 * Overrides for Emacs so that we follow Linus's tabbing style. 211 * Emacs will notice this stuff at the end of the file and automatically 212 * adjust the settings for this buffer only. This must remain at the end 213 * of the file. 214 * --------------------------------------------------------------------------- 215 * Local variables: 216 * c-file-style: "linux" 217 * End: 218 */ 219