tty_mutex.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/tty.h>
  3. #include <linux/module.h>
  4. #include <linux/kallsyms.h>
  5. #include <linux/semaphore.h>
  6. #include <linux/sched.h>
  7. /* Legacy tty mutex glue */
  8. /*
  9. * Getting the big tty mutex.
  10. */
  11. void tty_lock(struct tty_struct *tty)
  12. {
  13. if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
  14. return;
  15. tty_kref_get(tty);
  16. mutex_lock(&tty->legacy_mutex);
  17. }
  18. EXPORT_SYMBOL(tty_lock);
  19. int tty_lock_interruptible(struct tty_struct *tty)
  20. {
  21. int ret;
  22. if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
  23. return -EIO;
  24. tty_kref_get(tty);
  25. ret = mutex_lock_interruptible(&tty->legacy_mutex);
  26. if (ret)
  27. tty_kref_put(tty);
  28. return ret;
  29. }
  30. void tty_unlock(struct tty_struct *tty)
  31. {
  32. if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
  33. return;
  34. mutex_unlock(&tty->legacy_mutex);
  35. tty_kref_put(tty);
  36. }
  37. EXPORT_SYMBOL(tty_unlock);
  38. void tty_lock_slave(struct tty_struct *tty)
  39. {
  40. if (tty && tty != tty->link)
  41. tty_lock(tty);
  42. }
  43. void tty_unlock_slave(struct tty_struct *tty)
  44. {
  45. if (tty && tty != tty->link)
  46. tty_unlock(tty);
  47. }
  48. void tty_set_lock_subclass(struct tty_struct *tty)
  49. {
  50. lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
  51. }