]> Pileus Git - ~andy/linux/blob - arch/um/drivers/line.c
fb6e4ea099217071164a59146a790e766d0191b0
[~andy/linux] / arch / um / drivers / line.c
1 /*
2  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/irqreturn.h"
7 #include "linux/kd.h"
8 #include "linux/sched.h"
9 #include "linux/slab.h"
10 #include "chan.h"
11 #include "irq_kern.h"
12 #include "irq_user.h"
13 #include "kern_util.h"
14 #include "os.h"
15
16 #define LINE_BUFSIZE 4096
17
18 static irqreturn_t line_interrupt(int irq, void *data)
19 {
20         struct chan *chan = data;
21         struct line *line = chan->line;
22         struct tty_struct *tty = tty_port_tty_get(&line->port);
23
24         if (line)
25                 chan_interrupt(line, tty, irq);
26         tty_kref_put(tty);
27         return IRQ_HANDLED;
28 }
29
30 /*
31  * Returns the free space inside the ring buffer of this line.
32  *
33  * Should be called while holding line->lock (this does not modify data).
34  */
35 static int write_room(struct line *line)
36 {
37         int n;
38
39         if (line->buffer == NULL)
40                 return LINE_BUFSIZE - 1;
41
42         /* This is for the case where the buffer is wrapped! */
43         n = line->head - line->tail;
44
45         if (n <= 0)
46                 n += LINE_BUFSIZE; /* The other case */
47         return n - 1;
48 }
49
50 int line_write_room(struct tty_struct *tty)
51 {
52         struct line *line = tty->driver_data;
53         unsigned long flags;
54         int room;
55
56         spin_lock_irqsave(&line->lock, flags);
57         room = write_room(line);
58         spin_unlock_irqrestore(&line->lock, flags);
59
60         return room;
61 }
62
63 int line_chars_in_buffer(struct tty_struct *tty)
64 {
65         struct line *line = tty->driver_data;
66         unsigned long flags;
67         int ret;
68
69         spin_lock_irqsave(&line->lock, flags);
70         /* write_room subtracts 1 for the needed NULL, so we readd it.*/
71         ret = LINE_BUFSIZE - (write_room(line) + 1);
72         spin_unlock_irqrestore(&line->lock, flags);
73
74         return ret;
75 }
76
77 /*
78  * This copies the content of buf into the circular buffer associated with
79  * this line.
80  * The return value is the number of characters actually copied, i.e. the ones
81  * for which there was space: this function is not supposed to ever flush out
82  * the circular buffer.
83  *
84  * Must be called while holding line->lock!
85  */
86 static int buffer_data(struct line *line, const char *buf, int len)
87 {
88         int end, room;
89
90         if (line->buffer == NULL) {
91                 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
92                 if (line->buffer == NULL) {
93                         printk(KERN_ERR "buffer_data - atomic allocation "
94                                "failed\n");
95                         return 0;
96                 }
97                 line->head = line->buffer;
98                 line->tail = line->buffer;
99         }
100
101         room = write_room(line);
102         len = (len > room) ? room : len;
103
104         end = line->buffer + LINE_BUFSIZE - line->tail;
105
106         if (len < end) {
107                 memcpy(line->tail, buf, len);
108                 line->tail += len;
109         }
110         else {
111                 /* The circular buffer is wrapping */
112                 memcpy(line->tail, buf, end);
113                 buf += end;
114                 memcpy(line->buffer, buf, len - end);
115                 line->tail = line->buffer + len - end;
116         }
117
118         return len;
119 }
120
121 /*
122  * Flushes the ring buffer to the output channels. That is, write_chan is
123  * called, passing it line->head as buffer, and an appropriate count.
124  *
125  * On exit, returns 1 when the buffer is empty,
126  * 0 when the buffer is not empty on exit,
127  * and -errno when an error occurred.
128  *
129  * Must be called while holding line->lock!*/
130 static int flush_buffer(struct line *line)
131 {
132         int n, count;
133
134         if ((line->buffer == NULL) || (line->head == line->tail))
135                 return 1;
136
137         if (line->tail < line->head) {
138                 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
139                 count = line->buffer + LINE_BUFSIZE - line->head;
140
141                 n = write_chan(line->chan_out, line->head, count,
142                                line->driver->write_irq);
143                 if (n < 0)
144                         return n;
145                 if (n == count) {
146                         /*
147                          * We have flushed from ->head to buffer end, now we
148                          * must flush only from the beginning to ->tail.
149                          */
150                         line->head = line->buffer;
151                 } else {
152                         line->head += n;
153                         return 0;
154                 }
155         }
156
157         count = line->tail - line->head;
158         n = write_chan(line->chan_out, line->head, count,
159                        line->driver->write_irq);
160
161         if (n < 0)
162                 return n;
163
164         line->head += n;
165         return line->head == line->tail;
166 }
167
168 void line_flush_buffer(struct tty_struct *tty)
169 {
170         struct line *line = tty->driver_data;
171         unsigned long flags;
172
173         spin_lock_irqsave(&line->lock, flags);
174         flush_buffer(line);
175         spin_unlock_irqrestore(&line->lock, flags);
176 }
177
178 /*
179  * We map both ->flush_chars and ->put_char (which go in pair) onto
180  * ->flush_buffer and ->write. Hope it's not that bad.
181  */
182 void line_flush_chars(struct tty_struct *tty)
183 {
184         line_flush_buffer(tty);
185 }
186
187 int line_put_char(struct tty_struct *tty, unsigned char ch)
188 {
189         return line_write(tty, &ch, sizeof(ch));
190 }
191
192 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
193 {
194         struct line *line = tty->driver_data;
195         unsigned long flags;
196         int n, ret = 0;
197
198         spin_lock_irqsave(&line->lock, flags);
199         if (line->head != line->tail)
200                 ret = buffer_data(line, buf, len);
201         else {
202                 n = write_chan(line->chan_out, buf, len,
203                                line->driver->write_irq);
204                 if (n < 0) {
205                         ret = n;
206                         goto out_up;
207                 }
208
209                 len -= n;
210                 ret += n;
211                 if (len > 0)
212                         ret += buffer_data(line, buf + n, len);
213         }
214 out_up:
215         spin_unlock_irqrestore(&line->lock, flags);
216         return ret;
217 }
218
219 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
220 {
221         /* nothing */
222 }
223
224 static const struct {
225         int  cmd;
226         char *level;
227         char *name;
228 } tty_ioctls[] = {
229         /* don't print these, they flood the log ... */
230         { TCGETS,      NULL,       "TCGETS"      },
231         { TCSETS,      NULL,       "TCSETS"      },
232         { TCSETSW,     NULL,       "TCSETSW"     },
233         { TCFLSH,      NULL,       "TCFLSH"      },
234         { TCSBRK,      NULL,       "TCSBRK"      },
235
236         /* general tty stuff */
237         { TCSETSF,     KERN_DEBUG, "TCSETSF"     },
238         { TCGETA,      KERN_DEBUG, "TCGETA"      },
239         { TIOCMGET,    KERN_DEBUG, "TIOCMGET"    },
240         { TCSBRKP,     KERN_DEBUG, "TCSBRKP"     },
241         { TIOCMSET,    KERN_DEBUG, "TIOCMSET"    },
242
243         /* linux-specific ones */
244         { TIOCLINUX,   KERN_INFO,  "TIOCLINUX"   },
245         { KDGKBMODE,   KERN_INFO,  "KDGKBMODE"   },
246         { KDGKBTYPE,   KERN_INFO,  "KDGKBTYPE"   },
247         { KDSIGACCEPT, KERN_INFO,  "KDSIGACCEPT" },
248 };
249
250 int line_ioctl(struct tty_struct *tty, unsigned int cmd,
251                                 unsigned long arg)
252 {
253         int ret;
254         int i;
255
256         ret = 0;
257         switch(cmd) {
258 #ifdef TIOCGETP
259         case TIOCGETP:
260         case TIOCSETP:
261         case TIOCSETN:
262 #endif
263 #ifdef TIOCGETC
264         case TIOCGETC:
265         case TIOCSETC:
266 #endif
267 #ifdef TIOCGLTC
268         case TIOCGLTC:
269         case TIOCSLTC:
270 #endif
271         /* Note: these are out of date as we now have TCGETS2 etc but this
272            whole lot should probably go away */
273         case TCGETS:
274         case TCSETSF:
275         case TCSETSW:
276         case TCSETS:
277         case TCGETA:
278         case TCSETAF:
279         case TCSETAW:
280         case TCSETA:
281         case TCXONC:
282         case TCFLSH:
283         case TIOCOUTQ:
284         case TIOCINQ:
285         case TIOCGLCKTRMIOS:
286         case TIOCSLCKTRMIOS:
287         case TIOCPKT:
288         case TIOCGSOFTCAR:
289         case TIOCSSOFTCAR:
290                 return -ENOIOCTLCMD;
291 #if 0
292         case TCwhatever:
293                 /* do something */
294                 break;
295 #endif
296         default:
297                 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
298                         if (cmd == tty_ioctls[i].cmd)
299                                 break;
300                 if (i == ARRAY_SIZE(tty_ioctls)) {
301                         printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
302                                __func__, tty->name, cmd);
303                 }
304                 ret = -ENOIOCTLCMD;
305                 break;
306         }
307         return ret;
308 }
309
310 void line_throttle(struct tty_struct *tty)
311 {
312         struct line *line = tty->driver_data;
313
314         deactivate_chan(line->chan_in, line->driver->read_irq);
315         line->throttled = 1;
316 }
317
318 void line_unthrottle(struct tty_struct *tty)
319 {
320         struct line *line = tty->driver_data;
321
322         line->throttled = 0;
323         chan_interrupt(line, tty, line->driver->read_irq);
324
325         /*
326          * Maybe there is enough stuff pending that calling the interrupt
327          * throttles us again.  In this case, line->throttled will be 1
328          * again and we shouldn't turn the interrupt back on.
329          */
330         if (!line->throttled)
331                 reactivate_chan(line->chan_in, line->driver->read_irq);
332 }
333
334 static irqreturn_t line_write_interrupt(int irq, void *data)
335 {
336         struct chan *chan = data;
337         struct line *line = chan->line;
338         struct tty_struct *tty;
339         int err;
340
341         /*
342          * Interrupts are disabled here because genirq keep irqs disabled when
343          * calling the action handler.
344          */
345
346         spin_lock(&line->lock);
347         err = flush_buffer(line);
348         if (err == 0) {
349                 spin_unlock(&line->lock);
350                 return IRQ_NONE;
351         } else if (err < 0) {
352                 line->head = line->buffer;
353                 line->tail = line->buffer;
354         }
355         spin_unlock(&line->lock);
356
357         tty = tty_port_tty_get(&line->port);
358         if (tty == NULL)
359                 return IRQ_NONE;
360
361         tty_wakeup(tty);
362         tty_kref_put(tty);
363
364         return IRQ_HANDLED;
365 }
366
367 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
368 {
369         const struct line_driver *driver = line->driver;
370         int err = 0, flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
371
372         if (input)
373                 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
374                                        line_interrupt, flags,
375                                        driver->read_irq_name, data);
376         if (err)
377                 return err;
378         if (output)
379                 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
380                                         line_write_interrupt, flags,
381                                         driver->write_irq_name, data);
382         return err;
383 }
384
385 /*
386  * Normally, a driver like this can rely mostly on the tty layer
387  * locking, particularly when it comes to the driver structure.
388  * However, in this case, mconsole requests can come in "from the
389  * side", and race with opens and closes.
390  *
391  * mconsole config requests will want to be sure the device isn't in
392  * use, and get_config, open, and close will want a stable
393  * configuration.  The checking and modification of the configuration
394  * is done under a spinlock.  Checking whether the device is in use is
395  * line->tty->count > 1, also under the spinlock.
396  *
397  * line->count serves to decide whether the device should be enabled or
398  * disabled on the host.  If it's equal to 0, then we are doing the
399  * first open or last close.  Otherwise, open and close just return.
400  */
401
402 int line_open(struct line *lines, struct tty_struct *tty)
403 {
404         struct line *line = &lines[tty->index];
405         int err = -ENODEV;
406
407         mutex_lock(&line->count_lock);
408         if (!line->valid)
409                 goto out_unlock;
410
411         err = 0;
412         if (line->port.count++)
413                 goto out_unlock;
414
415         BUG_ON(tty->driver_data);
416         tty->driver_data = line;
417         tty_port_tty_set(&line->port, tty);
418
419         err = enable_chan(line);
420         if (err) /* line_close() will be called by our caller */
421                 goto out_unlock;
422
423         if (!line->sigio) {
424                 chan_enable_winch(line->chan_out, tty);
425                 line->sigio = 1;
426         }
427
428         chan_window_size(line, &tty->winsize.ws_row,
429                          &tty->winsize.ws_col);
430 out_unlock:
431         mutex_unlock(&line->count_lock);
432         return err;
433 }
434
435 static void unregister_winch(struct tty_struct *tty);
436
437 void line_close(struct tty_struct *tty, struct file * filp)
438 {
439         struct line *line = tty->driver_data;
440
441         /*
442          * If line_open fails (and tty->driver_data is never set),
443          * tty_open will call line_close.  So just return in this case.
444          */
445         if (line == NULL)
446                 return;
447
448         /* We ignore the error anyway! */
449         flush_buffer(line);
450
451         mutex_lock(&line->count_lock);
452         BUG_ON(!line->valid);
453
454         if (--line->port.count)
455                 goto out_unlock;
456
457         tty_port_tty_set(&line->port, NULL);
458         tty->driver_data = NULL;
459
460         if (line->sigio) {
461                 unregister_winch(tty);
462                 line->sigio = 0;
463         }
464
465 out_unlock:
466         mutex_unlock(&line->count_lock);
467 }
468
469 void close_lines(struct line *lines, int nlines)
470 {
471         int i;
472
473         for(i = 0; i < nlines; i++)
474                 close_chan(&lines[i]);
475 }
476
477 int setup_one_line(struct line *lines, int n, char *init,
478                    const struct chan_opts *opts, char **error_out)
479 {
480         struct line *line = &lines[n];
481         struct tty_driver *driver = line->driver->driver;
482         int err = -EINVAL;
483
484         mutex_lock(&line->count_lock);
485
486         if (line->port.count) {
487                 *error_out = "Device is already open";
488                 goto out;
489         }
490
491         if (!strcmp(init, "none")) {
492                 if (line->valid) {
493                         line->valid = 0;
494                         kfree(line->init_str);
495                         tty_unregister_device(driver, n);
496                         parse_chan_pair(NULL, line, n, opts, error_out);
497                         err = 0;
498                 }
499         } else {
500                 char *new = kstrdup(init, GFP_KERNEL);
501                 if (!new) {
502                         *error_out = "Failed to allocate memory";
503                         return -ENOMEM;
504                 }
505                 if (line->valid) {
506                         tty_unregister_device(driver, n);
507                         kfree(line->init_str);
508                 }
509                 line->init_str = new;
510                 line->valid = 1;
511                 err = parse_chan_pair(new, line, n, opts, error_out);
512                 if (!err) {
513                         struct device *d = tty_register_device(driver, n, NULL);
514                         if (IS_ERR(d)) {
515                                 *error_out = "Failed to register device";
516                                 err = PTR_ERR(d);
517                                 parse_chan_pair(NULL, line, n, opts, error_out);
518                         }
519                 }
520                 if (err) {
521                         line->init_str = NULL;
522                         line->valid = 0;
523                         kfree(new);
524                 }
525         }
526 out:
527         mutex_unlock(&line->count_lock);
528         return err;
529 }
530
531 /*
532  * Common setup code for both startup command line and mconsole initialization.
533  * @lines contains the array (of size @num) to modify;
534  * @init is the setup string;
535  * @error_out is an error string in the case of failure;
536  */
537
538 int line_setup(char **conf, unsigned int num, char **def,
539                char *init, char *name)
540 {
541         char *error;
542
543         if (*init == '=') {
544                 /*
545                  * We said con=/ssl= instead of con#=, so we are configuring all
546                  * consoles at once.
547                  */
548                 *def = init + 1;
549         } else {
550                 char *end;
551                 unsigned n = simple_strtoul(init, &end, 0);
552
553                 if (*end != '=') {
554                         error = "Couldn't parse device number";
555                         goto out;
556                 }
557                 if (n >= num) {
558                         error = "Device number out of range";
559                         goto out;
560                 }
561                 conf[n] = end + 1;
562         }
563         return 0;
564
565 out:
566         printk(KERN_ERR "Failed to set up %s with "
567                "configuration string \"%s\" : %s\n", name, init, error);
568         return -EINVAL;
569 }
570
571 int line_config(struct line *lines, unsigned int num, char *str,
572                 const struct chan_opts *opts, char **error_out)
573 {
574         char *end;
575         int n;
576
577         if (*str == '=') {
578                 *error_out = "Can't configure all devices from mconsole";
579                 return -EINVAL;
580         }
581
582         n = simple_strtoul(str, &end, 0);
583         if (*end++ != '=') {
584                 *error_out = "Couldn't parse device number";
585                 return -EINVAL;
586         }
587         if (n >= num) {
588                 *error_out = "Device number out of range";
589                 return -EINVAL;
590         }
591
592         return setup_one_line(lines, n, end, opts, error_out);
593 }
594
595 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
596                     int size, char **error_out)
597 {
598         struct line *line;
599         char *end;
600         int dev, n = 0;
601
602         dev = simple_strtoul(name, &end, 0);
603         if ((*end != '\0') || (end == name)) {
604                 *error_out = "line_get_config failed to parse device number";
605                 return 0;
606         }
607
608         if ((dev < 0) || (dev >= num)) {
609                 *error_out = "device number out of range";
610                 return 0;
611         }
612
613         line = &lines[dev];
614
615         mutex_lock(&line->count_lock);
616         if (!line->valid)
617                 CONFIG_CHUNK(str, size, n, "none", 1);
618         else {
619                 struct tty_struct *tty = tty_port_tty_get(&line->port);
620                 if (tty == NULL) {
621                         CONFIG_CHUNK(str, size, n, line->init_str, 1);
622                 } else {
623                         n = chan_config_string(line, str, size, error_out);
624                         tty_kref_put(tty);
625                 }
626         }
627         mutex_unlock(&line->count_lock);
628
629         return n;
630 }
631
632 int line_id(char **str, int *start_out, int *end_out)
633 {
634         char *end;
635         int n;
636
637         n = simple_strtoul(*str, &end, 0);
638         if ((*end != '\0') || (end == *str))
639                 return -1;
640
641         *str = end;
642         *start_out = n;
643         *end_out = n;
644         return n;
645 }
646
647 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
648 {
649         if (n >= num) {
650                 *error_out = "Device number out of range";
651                 return -EINVAL;
652         }
653         return setup_one_line(lines, n, "none", NULL, error_out);
654 }
655
656 int register_lines(struct line_driver *line_driver,
657                    const struct tty_operations *ops,
658                    struct line *lines, int nlines)
659 {
660         struct tty_driver *driver = alloc_tty_driver(nlines);
661         int err;
662         int i;
663
664         if (!driver)
665                 return -ENOMEM;
666
667         driver->driver_name = line_driver->name;
668         driver->name = line_driver->device_name;
669         driver->major = line_driver->major;
670         driver->minor_start = line_driver->minor_start;
671         driver->type = line_driver->type;
672         driver->subtype = line_driver->subtype;
673         driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
674         driver->init_termios = tty_std_termios;
675         
676         for (i = 0; i < nlines; i++) {
677                 tty_port_init(&lines[i].port);
678                 spin_lock_init(&lines[i].lock);
679                 mutex_init(&lines[i].count_lock);
680                 lines[i].driver = line_driver;
681                 INIT_LIST_HEAD(&lines[i].chan_list);
682         }
683         tty_set_operations(driver, ops);
684
685         err = tty_register_driver(driver);
686         if (err) {
687                 printk(KERN_ERR "register_lines : can't register %s driver\n",
688                        line_driver->name);
689                 put_tty_driver(driver);
690                 return err;
691         }
692
693         line_driver->driver = driver;
694         mconsole_register_dev(&line_driver->mc);
695         return 0;
696 }
697
698 static DEFINE_SPINLOCK(winch_handler_lock);
699 static LIST_HEAD(winch_handlers);
700
701 struct winch {
702         struct list_head list;
703         int fd;
704         int tty_fd;
705         int pid;
706         struct tty_struct *tty;
707         unsigned long stack;
708         struct work_struct work;
709 };
710
711 static void __free_winch(struct work_struct *work)
712 {
713         struct winch *winch = container_of(work, struct winch, work);
714         um_free_irq(WINCH_IRQ, winch);
715
716         if (winch->pid != -1)
717                 os_kill_process(winch->pid, 1);
718         if (winch->stack != 0)
719                 free_stack(winch->stack, 0);
720         kfree(winch);
721 }
722
723 static void free_winch(struct winch *winch)
724 {
725         int fd = winch->fd;
726         winch->fd = -1;
727         if (fd != -1)
728                 os_close_file(fd);
729         list_del(&winch->list);
730         __free_winch(&winch->work);
731 }
732
733 static irqreturn_t winch_interrupt(int irq, void *data)
734 {
735         struct winch *winch = data;
736         struct tty_struct *tty;
737         struct line *line;
738         int fd = winch->fd;
739         int err;
740         char c;
741
742         if (fd != -1) {
743                 err = generic_read(fd, &c, NULL);
744                 if (err < 0) {
745                         if (err != -EAGAIN) {
746                                 winch->fd = -1;
747                                 list_del(&winch->list);
748                                 os_close_file(fd);
749                                 printk(KERN_ERR "winch_interrupt : "
750                                        "read failed, errno = %d\n", -err);
751                                 printk(KERN_ERR "fd %d is losing SIGWINCH "
752                                        "support\n", winch->tty_fd);
753                                 INIT_WORK(&winch->work, __free_winch);
754                                 schedule_work(&winch->work);
755                                 return IRQ_HANDLED;
756                         }
757                         goto out;
758                 }
759         }
760         tty = winch->tty;
761         if (tty != NULL) {
762                 line = tty->driver_data;
763                 if (line != NULL) {
764                         chan_window_size(line, &tty->winsize.ws_row,
765                                          &tty->winsize.ws_col);
766                         kill_pgrp(tty->pgrp, SIGWINCH, 1);
767                 }
768         }
769  out:
770         if (winch->fd != -1)
771                 reactivate_fd(winch->fd, WINCH_IRQ);
772         return IRQ_HANDLED;
773 }
774
775 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
776                         unsigned long stack)
777 {
778         struct winch *winch;
779
780         winch = kmalloc(sizeof(*winch), GFP_KERNEL);
781         if (winch == NULL) {
782                 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
783                 goto cleanup;
784         }
785
786         *winch = ((struct winch) { .list        = LIST_HEAD_INIT(winch->list),
787                                    .fd          = fd,
788                                    .tty_fd      = tty_fd,
789                                    .pid         = pid,
790                                    .tty         = tty,
791                                    .stack       = stack });
792
793         if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
794                            IRQF_SHARED | IRQF_SAMPLE_RANDOM,
795                            "winch", winch) < 0) {
796                 printk(KERN_ERR "register_winch_irq - failed to register "
797                        "IRQ\n");
798                 goto out_free;
799         }
800
801         spin_lock(&winch_handler_lock);
802         list_add(&winch->list, &winch_handlers);
803         spin_unlock(&winch_handler_lock);
804
805         return;
806
807  out_free:
808         kfree(winch);
809  cleanup:
810         os_kill_process(pid, 1);
811         os_close_file(fd);
812         if (stack != 0)
813                 free_stack(stack, 0);
814 }
815
816 static void unregister_winch(struct tty_struct *tty)
817 {
818         struct list_head *ele, *next;
819         struct winch *winch;
820
821         spin_lock(&winch_handler_lock);
822
823         list_for_each_safe(ele, next, &winch_handlers) {
824                 winch = list_entry(ele, struct winch, list);
825                 if (winch->tty == tty) {
826                         free_winch(winch);
827                         break;
828                 }
829         }
830         spin_unlock(&winch_handler_lock);
831 }
832
833 static void winch_cleanup(void)
834 {
835         struct list_head *ele, *next;
836         struct winch *winch;
837
838         spin_lock(&winch_handler_lock);
839
840         list_for_each_safe(ele, next, &winch_handlers) {
841                 winch = list_entry(ele, struct winch, list);
842                 free_winch(winch);
843         }
844
845         spin_unlock(&winch_handler_lock);
846 }
847 __uml_exitcall(winch_cleanup);
848
849 char *add_xterm_umid(char *base)
850 {
851         char *umid, *title;
852         int len;
853
854         umid = get_umid();
855         if (*umid == '\0')
856                 return base;
857
858         len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
859         title = kmalloc(len, GFP_KERNEL);
860         if (title == NULL) {
861                 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
862                 return base;
863         }
864
865         snprintf(title, len, "%s (%s)", base, umid);
866         return title;
867 }