0001
0002 #include <linux/types.h>
0003 #include <linux/errno.h>
0004 #include <linux/kmod.h>
0005 #include <linux/sched.h>
0006 #include <linux/interrupt.h>
0007 #include <linux/tty.h>
0008 #include <linux/tty_driver.h>
0009 #include <linux/file.h>
0010 #include <linux/mm.h>
0011 #include <linux/string.h>
0012 #include <linux/slab.h>
0013 #include <linux/poll.h>
0014 #include <linux/proc_fs.h>
0015 #include <linux/module.h>
0016 #include <linux/device.h>
0017 #include <linux/wait.h>
0018 #include <linux/bitops.h>
0019 #include <linux/seq_file.h>
0020 #include <linux/uaccess.h>
0021 #include <linux/ratelimit.h>
0022 #include "tty.h"
0023
0024 #undef LDISC_DEBUG_HANGUP
0025
0026 #ifdef LDISC_DEBUG_HANGUP
0027 #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args)
0028 #else
0029 #define tty_ldisc_debug(tty, f, args...)
0030 #endif
0031
0032
0033 enum {
0034 LDISC_SEM_NORMAL,
0035 LDISC_SEM_OTHER,
0036 };
0037
0038
0039
0040
0041
0042
0043
0044
0045 static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
0046
0047 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058 int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc)
0059 {
0060 unsigned long flags;
0061 int ret = 0;
0062
0063 if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS)
0064 return -EINVAL;
0065
0066 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
0067 tty_ldiscs[new_ldisc->num] = new_ldisc;
0068 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
0069
0070 return ret;
0071 }
0072 EXPORT_SYMBOL(tty_register_ldisc);
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc)
0085 {
0086 unsigned long flags;
0087
0088 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
0089 tty_ldiscs[ldisc->num] = NULL;
0090 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
0091 }
0092 EXPORT_SYMBOL(tty_unregister_ldisc);
0093
0094 static struct tty_ldisc_ops *get_ldops(int disc)
0095 {
0096 unsigned long flags;
0097 struct tty_ldisc_ops *ldops, *ret;
0098
0099 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
0100 ret = ERR_PTR(-EINVAL);
0101 ldops = tty_ldiscs[disc];
0102 if (ldops) {
0103 ret = ERR_PTR(-EAGAIN);
0104 if (try_module_get(ldops->owner))
0105 ret = ldops;
0106 }
0107 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
0108 return ret;
0109 }
0110
0111 static void put_ldops(struct tty_ldisc_ops *ldops)
0112 {
0113 unsigned long flags;
0114
0115 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
0116 module_put(ldops->owner);
0117 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
0118 }
0119
0120 static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
0141 {
0142 struct tty_ldisc *ld;
0143 struct tty_ldisc_ops *ldops;
0144
0145 if (disc < N_TTY || disc >= NR_LDISCS)
0146 return ERR_PTR(-EINVAL);
0147
0148
0149
0150
0151
0152 ldops = get_ldops(disc);
0153 if (IS_ERR(ldops)) {
0154 if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
0155 return ERR_PTR(-EPERM);
0156 request_module("tty-ldisc-%d", disc);
0157 ldops = get_ldops(disc);
0158 if (IS_ERR(ldops))
0159 return ERR_CAST(ldops);
0160 }
0161
0162
0163
0164
0165
0166 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
0167 ld->ops = ldops;
0168 ld->tty = tty;
0169
0170 return ld;
0171 }
0172
0173
0174
0175
0176
0177
0178
0179 static void tty_ldisc_put(struct tty_ldisc *ld)
0180 {
0181 if (WARN_ON_ONCE(!ld))
0182 return;
0183
0184 put_ldops(ld->ops);
0185 kfree(ld);
0186 }
0187
0188 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
0189 {
0190 return (*pos < NR_LDISCS) ? pos : NULL;
0191 }
0192
0193 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
0194 {
0195 (*pos)++;
0196 return (*pos < NR_LDISCS) ? pos : NULL;
0197 }
0198
0199 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
0200 {
0201 }
0202
0203 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
0204 {
0205 int i = *(loff_t *)v;
0206 struct tty_ldisc_ops *ldops;
0207
0208 ldops = get_ldops(i);
0209 if (IS_ERR(ldops))
0210 return 0;
0211 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
0212 put_ldops(ldops);
0213 return 0;
0214 }
0215
0216 const struct seq_operations tty_ldiscs_seq_ops = {
0217 .start = tty_ldiscs_seq_start,
0218 .next = tty_ldiscs_seq_next,
0219 .stop = tty_ldiscs_seq_stop,
0220 .show = tty_ldiscs_seq_show,
0221 };
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
0241 {
0242 struct tty_ldisc *ld;
0243
0244 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
0245 ld = tty->ldisc;
0246 if (!ld)
0247 ldsem_up_read(&tty->ldisc_sem);
0248 return ld;
0249 }
0250 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
0261 {
0262 struct tty_ldisc *ld = NULL;
0263
0264 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
0265 ld = tty->ldisc;
0266 if (!ld)
0267 ldsem_up_read(&tty->ldisc_sem);
0268 }
0269 return ld;
0270 }
0271 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
0272
0273
0274
0275
0276
0277
0278
0279
0280 void tty_ldisc_deref(struct tty_ldisc *ld)
0281 {
0282 ldsem_up_read(&ld->tty->ldisc_sem);
0283 }
0284 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
0285
0286
0287 static inline int
0288 __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
0289 {
0290 return ldsem_down_write(&tty->ldisc_sem, timeout);
0291 }
0292
0293 static inline int
0294 __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
0295 {
0296 return ldsem_down_write_nested(&tty->ldisc_sem,
0297 LDISC_SEM_OTHER, timeout);
0298 }
0299
0300 static inline void __tty_ldisc_unlock(struct tty_struct *tty)
0301 {
0302 ldsem_up_write(&tty->ldisc_sem);
0303 }
0304
0305 int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
0306 {
0307 int ret;
0308
0309
0310 set_bit(TTY_LDISC_CHANGING, &tty->flags);
0311 wake_up_interruptible_all(&tty->read_wait);
0312 wake_up_interruptible_all(&tty->write_wait);
0313
0314 ret = __tty_ldisc_lock(tty, timeout);
0315 if (!ret)
0316 return -EBUSY;
0317 set_bit(TTY_LDISC_HALTED, &tty->flags);
0318 return 0;
0319 }
0320
0321 void tty_ldisc_unlock(struct tty_struct *tty)
0322 {
0323 clear_bit(TTY_LDISC_HALTED, &tty->flags);
0324
0325 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
0326 __tty_ldisc_unlock(tty);
0327 }
0328
0329 static int
0330 tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
0331 unsigned long timeout)
0332 {
0333 int ret;
0334
0335 if (tty < tty2) {
0336 ret = __tty_ldisc_lock(tty, timeout);
0337 if (ret) {
0338 ret = __tty_ldisc_lock_nested(tty2, timeout);
0339 if (!ret)
0340 __tty_ldisc_unlock(tty);
0341 }
0342 } else {
0343
0344 WARN_ON_ONCE(tty == tty2);
0345 if (tty2 && tty != tty2) {
0346 ret = __tty_ldisc_lock(tty2, timeout);
0347 if (ret) {
0348 ret = __tty_ldisc_lock_nested(tty, timeout);
0349 if (!ret)
0350 __tty_ldisc_unlock(tty2);
0351 }
0352 } else
0353 ret = __tty_ldisc_lock(tty, timeout);
0354 }
0355
0356 if (!ret)
0357 return -EBUSY;
0358
0359 set_bit(TTY_LDISC_HALTED, &tty->flags);
0360 if (tty2)
0361 set_bit(TTY_LDISC_HALTED, &tty2->flags);
0362 return 0;
0363 }
0364
0365 static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
0366 {
0367 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
0368 }
0369
0370 static void tty_ldisc_unlock_pair(struct tty_struct *tty,
0371 struct tty_struct *tty2)
0372 {
0373 __tty_ldisc_unlock(tty);
0374 if (tty2)
0375 __tty_ldisc_unlock(tty2);
0376 }
0377
0378
0379
0380
0381
0382
0383
0384
0385 void tty_ldisc_flush(struct tty_struct *tty)
0386 {
0387 struct tty_ldisc *ld = tty_ldisc_ref(tty);
0388
0389 tty_buffer_flush(tty, ld);
0390 if (ld)
0391 tty_ldisc_deref(ld);
0392 }
0393 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408 static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
0409 {
0410 down_write(&tty->termios_rwsem);
0411 tty->termios.c_line = disc;
0412 up_write(&tty->termios_rwsem);
0413
0414 tty->disc_data = NULL;
0415 tty->receive_room = 0;
0416 }
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
0428 {
0429 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
0430 if (ld->ops->open) {
0431 int ret;
0432
0433 ret = ld->ops->open(tty);
0434 if (ret)
0435 clear_bit(TTY_LDISC_OPEN, &tty->flags);
0436
0437 tty_ldisc_debug(tty, "%p: opened\n", ld);
0438 return ret;
0439 }
0440 return 0;
0441 }
0442
0443
0444
0445
0446
0447
0448
0449
0450 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
0451 {
0452 lockdep_assert_held_write(&tty->ldisc_sem);
0453 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
0454 clear_bit(TTY_LDISC_OPEN, &tty->flags);
0455 if (ld->ops->close)
0456 ld->ops->close(tty);
0457 tty_ldisc_debug(tty, "%p: closed\n", ld);
0458 }
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468 static int tty_ldisc_failto(struct tty_struct *tty, int ld)
0469 {
0470 struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
0471 int r;
0472
0473 lockdep_assert_held_write(&tty->ldisc_sem);
0474 if (IS_ERR(disc))
0475 return PTR_ERR(disc);
0476 tty->ldisc = disc;
0477 tty_set_termios_ldisc(tty, ld);
0478 r = tty_ldisc_open(tty, disc);
0479 if (r < 0)
0480 tty_ldisc_put(disc);
0481 return r;
0482 }
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
0493 {
0494
0495 if (tty_ldisc_failto(tty, old->ops->num) < 0) {
0496 const char *name = tty_name(tty);
0497
0498 pr_warn("Falling back ldisc for %s.\n", name);
0499
0500
0501
0502
0503
0504 if (tty_ldisc_failto(tty, N_TTY) < 0 &&
0505 tty_ldisc_failto(tty, N_NULL) < 0)
0506 panic("Couldn't open N_NULL ldisc for %s.", name);
0507 }
0508 }
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520 int tty_set_ldisc(struct tty_struct *tty, int disc)
0521 {
0522 int retval;
0523 struct tty_ldisc *old_ldisc, *new_ldisc;
0524
0525 new_ldisc = tty_ldisc_get(tty, disc);
0526 if (IS_ERR(new_ldisc))
0527 return PTR_ERR(new_ldisc);
0528
0529 tty_lock(tty);
0530 retval = tty_ldisc_lock(tty, 5 * HZ);
0531 if (retval)
0532 goto err;
0533
0534 if (!tty->ldisc) {
0535 retval = -EIO;
0536 goto out;
0537 }
0538
0539
0540 if (tty->ldisc->ops->num == disc)
0541 goto out;
0542
0543 if (test_bit(TTY_HUPPED, &tty->flags)) {
0544
0545 retval = -EIO;
0546 goto out;
0547 }
0548
0549 old_ldisc = tty->ldisc;
0550
0551
0552 tty_ldisc_close(tty, old_ldisc);
0553
0554
0555 tty->ldisc = new_ldisc;
0556 tty_set_termios_ldisc(tty, disc);
0557
0558 retval = tty_ldisc_open(tty, new_ldisc);
0559 if (retval < 0) {
0560
0561 tty_ldisc_put(new_ldisc);
0562 tty_ldisc_restore(tty, old_ldisc);
0563 }
0564
0565 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
0566 down_read(&tty->termios_rwsem);
0567 tty->ops->set_ldisc(tty);
0568 up_read(&tty->termios_rwsem);
0569 }
0570
0571
0572
0573
0574
0575
0576
0577
0578 new_ldisc = old_ldisc;
0579 out:
0580 tty_ldisc_unlock(tty);
0581
0582
0583
0584
0585
0586 tty_buffer_restart_work(tty->port);
0587 err:
0588 tty_ldisc_put(new_ldisc);
0589 tty_unlock(tty);
0590 return retval;
0591 }
0592 EXPORT_SYMBOL_GPL(tty_set_ldisc);
0593
0594
0595
0596
0597
0598
0599
0600 static void tty_ldisc_kill(struct tty_struct *tty)
0601 {
0602 lockdep_assert_held_write(&tty->ldisc_sem);
0603 if (!tty->ldisc)
0604 return;
0605
0606
0607
0608 tty_ldisc_close(tty, tty->ldisc);
0609 tty_ldisc_put(tty->ldisc);
0610
0611 tty->ldisc = NULL;
0612 }
0613
0614
0615
0616
0617
0618
0619
0620 static void tty_reset_termios(struct tty_struct *tty)
0621 {
0622 down_write(&tty->termios_rwsem);
0623 tty->termios = tty->driver->init_termios;
0624 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
0625 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
0626 up_write(&tty->termios_rwsem);
0627 }
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642 int tty_ldisc_reinit(struct tty_struct *tty, int disc)
0643 {
0644 struct tty_ldisc *ld;
0645 int retval;
0646
0647 lockdep_assert_held_write(&tty->ldisc_sem);
0648 ld = tty_ldisc_get(tty, disc);
0649 if (IS_ERR(ld)) {
0650 BUG_ON(disc == N_TTY);
0651 return PTR_ERR(ld);
0652 }
0653
0654 if (tty->ldisc) {
0655 tty_ldisc_close(tty, tty->ldisc);
0656 tty_ldisc_put(tty->ldisc);
0657 }
0658
0659
0660 tty->ldisc = ld;
0661 tty_set_termios_ldisc(tty, disc);
0662 retval = tty_ldisc_open(tty, tty->ldisc);
0663 if (retval) {
0664 tty_ldisc_put(tty->ldisc);
0665 tty->ldisc = NULL;
0666 }
0667 return retval;
0668 }
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685 void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
0686 {
0687 struct tty_ldisc *ld;
0688
0689 tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
0690
0691 ld = tty_ldisc_ref(tty);
0692 if (ld != NULL) {
0693 if (ld->ops->flush_buffer)
0694 ld->ops->flush_buffer(tty);
0695 tty_driver_flush_buffer(tty);
0696 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
0697 ld->ops->write_wakeup)
0698 ld->ops->write_wakeup(tty);
0699 if (ld->ops->hangup)
0700 ld->ops->hangup(tty);
0701 tty_ldisc_deref(ld);
0702 }
0703
0704 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
0705 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
0706
0707
0708
0709
0710
0711
0712
0713 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
0714
0715 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
0716 tty_reset_termios(tty);
0717
0718 if (tty->ldisc) {
0719 if (reinit) {
0720 if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
0721 tty_ldisc_reinit(tty, N_TTY) < 0)
0722 WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
0723 } else
0724 tty_ldisc_kill(tty);
0725 }
0726 tty_ldisc_unlock(tty);
0727 }
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
0739 {
0740 int retval = tty_ldisc_open(tty, tty->ldisc);
0741
0742 if (retval)
0743 return retval;
0744
0745 if (o_tty) {
0746
0747
0748
0749
0750 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
0751 if (retval) {
0752 tty_ldisc_close(tty, tty->ldisc);
0753 return retval;
0754 }
0755 }
0756 return 0;
0757 }
0758
0759
0760
0761
0762
0763
0764
0765
0766 void tty_ldisc_release(struct tty_struct *tty)
0767 {
0768 struct tty_struct *o_tty = tty->link;
0769
0770
0771
0772
0773
0774
0775 tty_ldisc_lock_pair(tty, o_tty);
0776 tty_ldisc_kill(tty);
0777 if (o_tty)
0778 tty_ldisc_kill(o_tty);
0779 tty_ldisc_unlock_pair(tty, o_tty);
0780
0781
0782
0783
0784
0785
0786 tty_ldisc_debug(tty, "released\n");
0787 }
0788
0789
0790
0791
0792
0793
0794
0795
0796 int tty_ldisc_init(struct tty_struct *tty)
0797 {
0798 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
0799
0800 if (IS_ERR(ld))
0801 return PTR_ERR(ld);
0802 tty->ldisc = ld;
0803 return 0;
0804 }
0805
0806
0807
0808
0809
0810
0811
0812
0813 void tty_ldisc_deinit(struct tty_struct *tty)
0814 {
0815
0816 if (tty->ldisc)
0817 tty_ldisc_put(tty->ldisc);
0818 tty->ldisc = NULL;
0819 }
0820
0821 static struct ctl_table tty_table[] = {
0822 {
0823 .procname = "ldisc_autoload",
0824 .data = &tty_ldisc_autoload,
0825 .maxlen = sizeof(tty_ldisc_autoload),
0826 .mode = 0644,
0827 .proc_handler = proc_dointvec,
0828 .extra1 = SYSCTL_ZERO,
0829 .extra2 = SYSCTL_ONE,
0830 },
0831 { }
0832 };
0833
0834 static struct ctl_table tty_dir_table[] = {
0835 {
0836 .procname = "tty",
0837 .mode = 0555,
0838 .child = tty_table,
0839 },
0840 { }
0841 };
0842
0843 static struct ctl_table tty_root_table[] = {
0844 {
0845 .procname = "dev",
0846 .mode = 0555,
0847 .child = tty_dir_table,
0848 },
0849 { }
0850 };
0851
0852 void tty_sysctl_init(void)
0853 {
0854 register_sysctl_table(tty_root_table);
0855 }