cons.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* $OpenBSD: cons.c,v 1.25 2015/03/14 03:38:46 jsg Exp $ */
  2. /* $NetBSD: cons.c,v 1.30 1996/04/08 19:57:30 jonathan Exp $ */
  3. /*
  4. * Copyright (c) 1988 University of Utah.
  5. * Copyright (c) 1990, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This code is derived from software contributed to Berkeley by
  9. * the Systems Programming Group of the University of Utah Computer
  10. * Science Department.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * from: Utah $Hdr: cons.c 1.7 92/01/21$
  37. *
  38. * @(#)cons.c 8.2 (Berkeley) 1/12/94
  39. */
  40. #include <sys/param.h>
  41. #include <sys/systm.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/tty.h>
  44. #include <sys/file.h>
  45. #include <sys/conf.h>
  46. #include <sys/vnode.h>
  47. #include <sys/poll.h>
  48. #include <dev/cons.h>
  49. struct tty *constty = NULL; /* virtual console output device */
  50. struct vnode *cn_devvp; /* vnode for underlying device. */
  51. extern struct consdev *cn_tab; /* physical console device info */
  52. int
  53. cnopen(dev_t dev, int flag, int mode, struct proc *p)
  54. {
  55. dev_t cndev;
  56. if (cn_tab == NULL)
  57. return (0);
  58. /*
  59. * always open the 'real' console device, so we don't get nailed
  60. * later. This follows normal device semantics; they always get
  61. * open() calls.
  62. */
  63. cndev = cn_tab->cn_dev;
  64. if (cndev == NODEV)
  65. return (ENXIO);
  66. #ifdef DIAGNOSTIC
  67. if (cndev == dev)
  68. panic("cnopen: recursive");
  69. #endif
  70. if (cn_devvp == NULLVP) {
  71. /* try to get a reference on its vnode, but fail silently */
  72. cdevvp(cndev, &cn_devvp);
  73. }
  74. return ((*cdevsw[major(cndev)].d_open)(cndev, flag, mode, p));
  75. }
  76. int
  77. cnclose(dev_t dev, int flag, int mode, struct proc *p)
  78. {
  79. struct vnode *vp;
  80. if (cn_tab == NULL)
  81. return (0);
  82. /*
  83. * If the real console isn't otherwise open, close it.
  84. * If it's otherwise open, don't close it, because that'll
  85. * screw up others who have it open.
  86. */
  87. dev = cn_tab->cn_dev;
  88. if (cn_devvp != NULLVP) {
  89. /* release our reference to real dev's vnode */
  90. vrele(cn_devvp);
  91. cn_devvp = NULLVP;
  92. }
  93. if (vfinddev(dev, VCHR, &vp) && vcount(vp))
  94. return (0);
  95. return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
  96. }
  97. int
  98. cnread(dev_t dev, struct uio *uio, int flag)
  99. {
  100. /*
  101. * If we would redirect input, punt. This will keep strange
  102. * things from happening to people who are using the real
  103. * console. Nothing should be using /dev/console for
  104. * input (except a shell in single-user mode, but then,
  105. * one wouldn't TIOCCONS then).
  106. */
  107. if (constty != NULL)
  108. return 0;
  109. else if (cn_tab == NULL)
  110. return ENXIO;
  111. dev = cn_tab->cn_dev;
  112. return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
  113. }
  114. int
  115. cnwrite(dev_t dev, struct uio *uio, int flag)
  116. {
  117. /*
  118. * Redirect output, if that's appropriate.
  119. * If there's no real console, return ENXIO.
  120. */
  121. if (constty != NULL)
  122. dev = constty->t_dev;
  123. else if (cn_tab == NULL)
  124. return ENXIO;
  125. else
  126. dev = cn_tab->cn_dev;
  127. return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
  128. }
  129. int
  130. cnstop(struct tty *tp, int flag)
  131. {
  132. return (0);
  133. }
  134. int
  135. cnioctl(dev_t dev, u_long cmd, caddr_t data, int flag,
  136. struct proc *p)
  137. {
  138. int error;
  139. /*
  140. * Superuser can always use this to wrest control of console
  141. * output from the "virtual" console.
  142. */
  143. if (cmd == TIOCCONS && constty != NULL) {
  144. error = suser(p, SUSER_NOACCT);
  145. if (error)
  146. return (error);
  147. constty = NULL;
  148. return (0);
  149. }
  150. /*
  151. * Redirect the ioctl, if that's appropriate.
  152. * Note that strange things can happen, if a program does
  153. * ioctls on /dev/console, then the console is redirected
  154. * out from under it.
  155. */
  156. if (constty != NULL)
  157. dev = constty->t_dev;
  158. else if (cn_tab == NULL)
  159. return ENXIO;
  160. else
  161. dev = cn_tab->cn_dev;
  162. return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
  163. }
  164. /*ARGSUSED*/
  165. int
  166. cnpoll(dev_t dev, int rw, struct proc *p)
  167. {
  168. /*
  169. * Redirect the poll, if that's appropriate.
  170. * I don't want to think of the possible side effects
  171. * of console redirection here.
  172. */
  173. if (constty != NULL)
  174. dev = constty->t_dev;
  175. else if (cn_tab == NULL)
  176. return POLLERR;
  177. else
  178. dev = cn_tab->cn_dev;
  179. return (ttpoll(dev, rw, p));
  180. }
  181. int
  182. cnkqfilter(dev_t dev, struct knote *kn)
  183. {
  184. /*
  185. * Redirect output, if that's appropriate.
  186. * If there's no real console, return 1.
  187. */
  188. if (constty != NULL)
  189. dev = constty->t_dev;
  190. else if (cn_tab == NULL)
  191. return (ENXIO);
  192. else
  193. dev = cn_tab->cn_dev;
  194. if (cdevsw[major(dev)].d_kqfilter)
  195. return ((*cdevsw[major(dev)].d_kqfilter)(dev, kn));
  196. return (ENXIO);
  197. }
  198. int
  199. cngetc(void)
  200. {
  201. if (cn_tab == NULL)
  202. return (0);
  203. return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
  204. }
  205. void
  206. cnputc(int c)
  207. {
  208. if (cn_tab == NULL)
  209. return;
  210. if (c) {
  211. (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
  212. if (c == '\n')
  213. (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
  214. }
  215. }
  216. void
  217. cnpollc(int on)
  218. {
  219. static int refcount = 0;
  220. if (cn_tab == NULL)
  221. return;
  222. if (!on)
  223. --refcount;
  224. if (refcount == 0)
  225. (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
  226. if (on)
  227. ++refcount;
  228. }
  229. void
  230. nullcnpollc(dev_t dev, int on)
  231. {
  232. }
  233. void
  234. cnbell(u_int pitch, u_int period, u_int volume)
  235. {
  236. if (cn_tab == NULL || cn_tab->cn_bell == NULL)
  237. return;
  238. (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
  239. }