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