vserver 1.9.5.x5
[linux-2.6.git] / arch / um / drivers / chan_user.c
index 8a5180a..583b8e1 100644 (file)
@@ -27,16 +27,46 @@ int generic_console_write(int fd, const char *buf, int n, void *unused)
        int err;
 
        if(isatty(fd)){
-               tcgetattr(fd, &save);
+               CATCH_EINTR(err = tcgetattr(fd, &save));
+               if (err)
+                       goto error;
                new = save;
+               /* The terminal becomes a bit less raw, to handle \n also as
+                * "Carriage Return", not only as "New Line". Otherwise, the new
+                * line won't start at the first column.*/
                new.c_oflag |= OPOST;
-               tcsetattr(fd, TCSAFLUSH, &new);
+               CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
+               if (err)
+                       goto error;
        }
        err = generic_write(fd, buf, n, NULL);
-       if(isatty(fd)) tcsetattr(fd, TCSAFLUSH, &save);
+       /* Restore raw mode, in any case; we *must* ignore any error apart
+        * EINTR, except for debug.*/
+       if(isatty(fd))
+               CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
        return(err);
+error:
+       return(-errno);
 }
 
+/*
+ * UML SIGWINCH handling
+ *
+ * The point of this is to handle SIGWINCH on consoles which have host ttys and
+ * relay them inside UML to whatever might be running on the console and cares
+ * about the window size (since SIGWINCH notifies about terminal size changes).
+ *
+ * So, we have a separate thread for each host tty attached to a UML device
+ * (side-issue - I'm annoyed that one thread can't have multiple controlling
+ * ttys for purposed of handling SIGWINCH, but I imagine there are other reasons
+ * that doesn't make any sense).
+ *
+ * SIGWINCH can't be received synchronously, so you have to set up to receive it
+ * as a signal.  That being the case, if you are going to wait for it, it is
+ * convenient to sit in a pause() and wait for the signal to bounce you out of
+ * it (see below for how we make sure to exit only on SIGWINCH).
+ */
+
 static void winch_handler(int sig)
 {
 }
@@ -63,9 +93,14 @@ static int winch_thread(void *arg)
                printk("winch_thread : failed to write synchronization "
                       "byte, err = %d\n", -count);
 
+       /* We are not using SIG_IGN on purpose, so don't fix it as I thought to
+        * do! If using SIG_IGN, the pause() call below would not stop on
+        * SIGWINCH. */
+
        signal(SIGWINCH, winch_handler);
        sigfillset(&sigs);
        sigdelset(&sigs, SIGWINCH);
+       /* Block anything else than SIGWINCH. */
        if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
                printk("winch_thread : sigprocmask failed, errno = %d\n", 
                       errno);
@@ -83,12 +118,18 @@ static int winch_thread(void *arg)
                exit(1);
        }
 
+       /* These are synchronization calls between various UML threads on the
+        * host - since they are not different kernel threads, we cannot use
+        * kernel semaphores. We don't use SysV semaphores because they are
+        * persistant. */
        count = os_read_file(pipe_fd, &c, sizeof(c));
        if(count != sizeof(c))
                printk("winch_thread : failed to read synchronization byte, "
                       "err = %d\n", -count);
 
        while(1){
+               /* This will be interrupted by SIGWINCH only, since other signals
+                * are blocked.*/
                pause();
 
                count = os_write_file(pipe_fd, &c, sizeof(c));
@@ -98,7 +139,7 @@ static int winch_thread(void *arg)
        }
 }
 
-static int winch_tramp(int fd, void *device_data, int *fd_out)
+static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out)
 {
        struct winch_data data;
        unsigned long stack;
@@ -132,7 +173,7 @@ static int winch_tramp(int fd, void *device_data, int *fd_out)
        return(pid);
 }
 
-void register_winch(int fd, void *device_data)
+void register_winch(int fd, struct tty_struct *tty)
 {
        int pid, thread, thread_fd;
        int count;
@@ -143,10 +184,10 @@ void register_winch(int fd, void *device_data)
 
        pid = tcgetpgrp(fd);
        if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd,
-                            device_data) && (pid == -1)){
-               thread = winch_tramp(fd, device_data, &thread_fd);
+                            tty) && (pid == -1)){
+               thread = winch_tramp(fd, tty, &thread_fd);
                if(fd != -1){
-                       register_winch_irq(thread_fd, fd, thread, device_data);
+                       register_winch_irq(thread_fd, fd, thread, tty);
 
                        count = os_write_file(thread_fd, &c, sizeof(c));
                        if(count != sizeof(c))