Skip to content

Commit bf970ee

Browse files
Alan Coxtorvalds
authored andcommitted
tty: extract the pty init time special cases
The majority of the remaining init_dev code is pty special cases. We refactor this code into the driver->install method. Signed-off-by: Alan Cox <alan@redhat.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 73ec06f commit bf970ee

3 files changed

Lines changed: 187 additions & 142 deletions

File tree

‎drivers/char/pty.c‎

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,58 @@ static void pty_set_termios(struct tty_struct *tty, struct ktermios *old_termios
227227
tty->termios->c_cflag |= (CS8 | CREAD);
228228
}
229229

230+
static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
231+
{
232+
struct tty_struct *o_tty;
233+
int idx = tty->index;
234+
int retval;
235+
236+
o_tty = alloc_tty_struct();
237+
if (!o_tty)
238+
return -ENOMEM;
239+
if (!try_module_get(driver->other->owner)) {
240+
/* This cannot in fact currently happen */
241+
free_tty_struct(o_tty);
242+
return -ENOMEM;
243+
}
244+
initialize_tty_struct(o_tty, driver->other, idx);
245+
246+
/* We always use new tty termios data so we can do this
247+
the easy way .. */
248+
retval = tty_init_termios(tty);
249+
if (retval)
250+
goto free_mem_out;
251+
252+
retval = tty_init_termios(o_tty);
253+
if (retval) {
254+
tty_free_termios(tty);
255+
goto free_mem_out;
256+
}
257+
258+
/*
259+
* Everything allocated ... set up the o_tty structure.
260+
*/
261+
driver->other->ttys[idx] = o_tty;
262+
tty_driver_kref_get(driver->other);
263+
if (driver->subtype == PTY_TYPE_MASTER)
264+
o_tty->count++;
265+
/* Establish the links in both directions */
266+
tty->link = o_tty;
267+
o_tty->link = tty;
268+
269+
tty_driver_kref_get(driver);
270+
tty->count++;
271+
driver->ttys[idx] = tty;
272+
return 0;
273+
free_mem_out:
274+
module_put(o_tty->driver->owner);
275+
free_tty_struct(o_tty);
276+
return -ENOMEM;
277+
}
278+
279+
230280
static const struct tty_operations pty_ops = {
281+
.install = pty_install,
231282
.open = pty_open,
232283
.close = pty_close,
233284
.write = pty_write,
@@ -332,6 +383,7 @@ static inline void legacy_pty_init(void) { }
332383
int pty_limit = NR_UNIX98_PTY_DEFAULT;
333384
static int pty_limit_min = 0;
334385
static int pty_limit_max = NR_UNIX98_PTY_MAX;
386+
static int pty_count = 0;
335387

336388
static struct cdev ptmx_cdev;
337389

@@ -351,6 +403,7 @@ static struct ctl_table pty_table[] = {
351403
.procname = "nr",
352404
.maxlen = sizeof(int),
353405
.mode = 0444,
406+
.data = &pty_count,
354407
.proc_handler = &proc_dointvec,
355408
}, {
356409
.ctl_name = 0
@@ -426,7 +479,7 @@ static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, int idx)
426479
return tty;
427480
}
428481

429-
static void pty_shutdown(struct tty_struct *tty)
482+
static void pty_unix98_shutdown(struct tty_struct *tty)
430483
{
431484
/* We have our own method as we don't use the tty index */
432485
kfree(tty->termios);
@@ -436,19 +489,71 @@ static void pty_shutdown(struct tty_struct *tty)
436489
/* We have no need to install and remove our tty objects as devpts does all
437490
the work for us */
438491

439-
static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
492+
static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
440493
{
494+
struct tty_struct *o_tty;
495+
int idx = tty->index;
496+
497+
o_tty = alloc_tty_struct();
498+
if (!o_tty)
499+
return -ENOMEM;
500+
if (!try_module_get(driver->other->owner)) {
501+
/* This cannot in fact currently happen */
502+
free_tty_struct(o_tty);
503+
return -ENOMEM;
504+
}
505+
initialize_tty_struct(o_tty, driver->other, idx);
506+
507+
tty->termios = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
508+
if (tty->termios == NULL)
509+
goto free_mem_out;
510+
*tty->termios = driver->init_termios;
511+
tty->termios_locked = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
512+
if (tty->termios_locked == NULL)
513+
goto free_mem_out;
514+
o_tty->termios = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
515+
if (o_tty->termios == NULL)
516+
goto free_mem_out;
517+
*o_tty->termios = driver->other->init_termios;
518+
o_tty->termios_locked = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
519+
if (o_tty->termios_locked == NULL)
520+
goto free_mem_out;
521+
522+
tty_driver_kref_get(driver->other);
523+
if (driver->subtype == PTY_TYPE_MASTER)
524+
o_tty->count++;
525+
/* Establish the links in both directions */
526+
tty->link = o_tty;
527+
o_tty->link = tty;
528+
/*
529+
* All structures have been allocated, so now we install them.
530+
* Failures after this point use release_tty to clean up, so
531+
* there's no need to null out the local pointers.
532+
*/
533+
tty_driver_kref_get(driver);
534+
tty->count++;
535+
pty_count++;
441536
return 0;
537+
free_mem_out:
538+
kfree(o_tty->termios);
539+
module_put(o_tty->driver->owner);
540+
free_tty_struct(o_tty);
541+
kfree(tty->termios_locked);
542+
kfree(tty->termios);
543+
free_tty_struct(tty);
544+
module_put(driver->owner);
545+
return -ENOMEM;
442546
}
443547

444-
static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
548+
static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
445549
{
550+
pty_count--;
446551
}
447552

448553
static const struct tty_operations ptm_unix98_ops = {
449554
.lookup = ptm_unix98_lookup,
450-
.install = pty_install,
451-
.remove = pty_remove,
555+
.install = pty_unix98_install,
556+
.remove = pty_unix98_remove,
452557
.open = pty_open,
453558
.close = pty_close,
454559
.write = pty_write,
@@ -458,13 +563,13 @@ static const struct tty_operations ptm_unix98_ops = {
458563
.unthrottle = pty_unthrottle,
459564
.set_termios = pty_set_termios,
460565
.ioctl = pty_unix98_ioctl,
461-
.shutdown = pty_shutdown
566+
.shutdown = pty_unix98_shutdown
462567
};
463568

464569
static const struct tty_operations pty_unix98_ops = {
465570
.lookup = pts_unix98_lookup,
466-
.install = pty_install,
467-
.remove = pty_remove,
571+
.install = pty_unix98_install,
572+
.remove = pty_unix98_remove,
468573
.open = pty_open,
469574
.close = pty_close,
470575
.write = pty_write,
@@ -473,6 +578,7 @@ static const struct tty_operations pty_unix98_ops = {
473578
.chars_in_buffer = pty_chars_in_buffer,
474579
.unthrottle = pty_unthrottle,
475580
.set_termios = pty_set_termios,
581+
.shutdown = pty_unix98_shutdown
476582
};
477583

478584
/**
@@ -589,10 +695,6 @@ static void __init unix98_pty_init(void)
589695
if (tty_register_driver(pts_driver))
590696
panic("Couldn't register Unix98 pts driver");
591697

592-
/* FIXME: WTF */
593-
#if 0
594-
pty_table[1].data = &ptm_driver->refcount;
595-
#endif
596698
register_sysctl_table(pty_root_table);
597699

598700
/* Now create the /dev/ptmx special device */

0 commit comments

Comments
 (0)