return err;
 }
 
+static void unregister_winch(struct tty_struct *tty);
+
 void line_close(struct tty_struct *tty, struct file * filp)
 {
        struct line *line = tty->driver_data;
 
-       /* XXX: I assume this should be called in process context, not with interrupt
-        * disabled!*/
+       /* XXX: I assume this should be called in process context, not with
+         *  interrupts disabled!
+         */
        spin_lock_irq(&line->lock);
 
        /* We ignore the error anyway! */
                line_disable(tty, -1);
                tty->driver_data = NULL;
        }
+
+        if((line->count == 0) && line->sigio){
+                unregister_winch(tty);
+                line->sigio = 0;
+        }
+
        spin_unlock_irq(&line->lock);
 }
 
        up(&winch_handler_sem);
 }
 
+static void unregister_winch(struct tty_struct *tty)
+{
+       struct list_head *ele;
+       struct winch *winch, *found = NULL;
+
+       down(&winch_handler_sem);
+       list_for_each(ele, &winch_handlers){
+               winch = list_entry(ele, struct winch, list);
+                if(winch->tty == tty){
+                        found = winch;
+                        break;
+                }
+        }
+
+        if(found == NULL)
+                goto out;
+
+        if(winch->pid != -1)
+                os_kill_process(winch->pid, 1);
+
+        free_irq_by_irq_and_dev(WINCH_IRQ, winch);
+        free_irq(WINCH_IRQ, winch);
+        list_del(&winch->list);
+        kfree(winch);
+ out:
+       up(&winch_handler_sem);
+}
+
 static void winch_cleanup(void)
 {
        struct list_head *ele;
 
 extern void os_kill_ptraced_process(int pid, int reap_child);
 extern void os_usr1_process(int pid);
 extern int os_getpid(void);
+extern int os_getpgrp(void);
 
 extern int os_map_memory(void *virt, int fd, unsigned long long off,
                         unsigned long len, int r, int w, int x);
 
                irq_desc[i].handler = &SIGIO_irq_type;
                enable_irq(i);
        }
-       init_irq_signals(0);
 }
 
 /*
 
                    SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
        set_handler(SIGBUS, (__sighandler_t) sig_handler, flags, 
                    SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
-       set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags, 
-                   SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
        set_handler(SIGUSR2, (__sighandler_t) sig_handler, 
                    flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
        signal(SIGHUP, SIG_IGN);
 
        int i, n, respond_fd;
        char c;
 
+        signal(SIGWINCH, SIG_IGN);
        fds = ¤t_poll;
        while(1){
                n = poll(fds->poll, fds->used, -1);
 
 #include "chan_user.h"
 #include "signal_user.h"
 #include "registers.h"
+#include "process.h"
 
 int is_skas_winch(int pid, int fd, void *data)
 {
-       if(pid != os_getpid())
+        if(pid != os_getpgrp())
                return(0);
 
        register_winch_irq(-1, fd, -1, data);
        sigjmp_buf **switch_buf = switch_buf_ptr;
        int n;
 
+       set_handler(SIGWINCH, (__sighandler_t) sig_handler,
+                   SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
+                   SIGVTALRM, -1);
+
        *fork_buf_ptr = &initial_jmpbuf;
        n = sigsetjmp(initial_jmpbuf, 1);
        if(n == 0)
 
 #include "kern_util.h"
 #include "chan_user.h"
 #include "ptrace_user.h"
+#include "irq_user.h"
 #include "mode.h"
 #include "tt.h"
 
 
 int is_tracer_winch(int pid, int fd, void *data)
 {
-       if(pid != tracing_pid)
+       if(pid != os_getpgrp())
                return(0);
 
        register_winch_irq(tracer_winch[0], fd, -1, data);
        signal(SIGSEGV, (__sighandler_t) sig_handler);
        set_cmdline("(idle thread)");
        set_init_pid(os_getpid());
+       init_irq_signals(0);
        proc = arg;
        return((*proc)(NULL));
 }
 
        return(getpid());
 }
 
+int os_getpgrp(void)
+{
+       return getpgrp();
+}
+
 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
                  int r, int w, int x)
 {