tty_tty.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* $OpenBSD: tty_tty.c,v 1.15 2015/05/01 01:30:58 millert Exp $ */
  2. /* $NetBSD: tty_tty.c,v 1.13 1996/03/30 22:24:46 christos Exp $ */
  3. /*-
  4. * Copyright (c) 1982, 1986, 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93
  32. */
  33. /*
  34. * Indirect driver for controlling tty.
  35. */
  36. #include <sys/param.h>
  37. #include <sys/systm.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/proc.h>
  40. #include <sys/tty.h>
  41. #include <sys/vnode.h>
  42. #include <sys/lock.h>
  43. #include <sys/file.h>
  44. #define cttyvp(p) \
  45. ((p)->p_p->ps_flags & PS_CONTROLT ? \
  46. (p)->p_p->ps_session->s_ttyvp : NULL)
  47. /*ARGSUSED*/
  48. int
  49. cttyopen(dev_t dev, int flag, int mode, struct proc *p)
  50. {
  51. struct vnode *ttyvp = cttyvp(p);
  52. int error;
  53. if (ttyvp == NULL)
  54. return (ENXIO);
  55. vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
  56. #ifdef PARANOID
  57. /*
  58. * Since group is tty and mode is 620 on most terminal lines
  59. * and since sessions protect terminals from processes outside
  60. * your session, this check is probably no longer necessary.
  61. * Since it inhibits setuid root programs that later switch
  62. * to another user from accessing /dev/tty, we have decided
  63. * to delete this test. (mckusick 5/93)
  64. */
  65. error = VOP_ACCESS(ttyvp,
  66. (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
  67. if (!error)
  68. #endif /* PARANOID */
  69. error = VOP_OPEN(ttyvp, flag, NOCRED, p);
  70. VOP_UNLOCK(ttyvp, 0, p);
  71. return (error);
  72. }
  73. /*ARGSUSED*/
  74. int
  75. cttyread(dev_t dev, struct uio *uio, int flag)
  76. {
  77. struct proc *p = uio->uio_procp;
  78. struct vnode *ttyvp = cttyvp(uio->uio_procp);
  79. int error;
  80. if (ttyvp == NULL)
  81. return (EIO);
  82. vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
  83. error = VOP_READ(ttyvp, uio, flag, NOCRED);
  84. VOP_UNLOCK(ttyvp, 0, p);
  85. return (error);
  86. }
  87. /*ARGSUSED*/
  88. int
  89. cttywrite(dev_t dev, struct uio *uio, int flag)
  90. {
  91. struct proc *p = uio->uio_procp;
  92. struct vnode *ttyvp = cttyvp(uio->uio_procp);
  93. int error;
  94. if (ttyvp == NULL)
  95. return (EIO);
  96. vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
  97. error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
  98. VOP_UNLOCK(ttyvp, 0, p);
  99. return (error);
  100. }
  101. /*ARGSUSED*/
  102. int
  103. cttyioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
  104. {
  105. struct vnode *ttyvp = cttyvp(p);
  106. if (ttyvp == NULL)
  107. return (EIO);
  108. if (cmd == TIOCSCTTY) /* XXX */
  109. return (EINVAL);
  110. if (cmd == TIOCNOTTY) {
  111. if (!SESS_LEADER(p->p_p)) {
  112. atomic_clearbits_int(&p->p_p->ps_flags, PS_CONTROLT);
  113. return (0);
  114. } else
  115. return (EINVAL);
  116. }
  117. return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
  118. }
  119. /*ARGSUSED*/
  120. int
  121. cttypoll(dev_t dev, int events, struct proc *p)
  122. {
  123. struct vnode *ttyvp = cttyvp(p);
  124. if (ttyvp == NULL) /* try operation to get EOF/failure */
  125. return (seltrue(dev, events, p));
  126. return (VOP_POLL(ttyvp, FREAD|FWRITE, events, p));
  127. }
  128. /*ARGSUSED*/
  129. int
  130. cttykqfilter(dev_t dev, struct knote *kn)
  131. {
  132. struct vnode *ttyvp = cttyvp(curproc);
  133. if (ttyvp == NULL)
  134. return (ENXIO);
  135. return (VOP_KQFILTER(ttyvp, kn));
  136. }