* Operations that are exposed through the character device in /dev. */
| 271 | * Operations that are exposed through the character device in /dev. |
| 272 | */ |
| 273 | static int |
| 274 | ttydev_open(struct cdev *dev, int oflags, int devtype __unused, |
| 275 | struct thread *td) |
| 276 | { |
| 277 | struct tty *tp; |
| 278 | int error; |
| 279 | |
| 280 | tp = dev->si_drv1; |
| 281 | error = 0; |
| 282 | tty_lock(tp); |
| 283 | if (tty_gone(tp)) { |
| 284 | /* Device is already gone. */ |
| 285 | tty_unlock(tp); |
| 286 | return (ENXIO); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Block when other processes are currently opening or closing |
| 291 | * the TTY. |
| 292 | */ |
| 293 | while (tp->t_flags & TF_OPENCLOSE) { |
| 294 | error = tty_wait(tp, &tp->t_dcdwait); |
| 295 | if (error != 0) { |
| 296 | tty_unlock(tp); |
| 297 | return (error); |
| 298 | } |
| 299 | } |
| 300 | tp->t_flags |= TF_OPENCLOSE; |
| 301 | |
| 302 | /* |
| 303 | * Make sure the "tty" and "cua" device cannot be opened at the |
| 304 | * same time. The console is a "tty" device. |
| 305 | */ |
| 306 | if (TTY_CALLOUT(tp, dev)) { |
| 307 | if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) { |
| 308 | error = EBUSY; |
| 309 | goto done; |
| 310 | } |
| 311 | } else { |
| 312 | if (tp->t_flags & TF_OPENED_OUT) { |
| 313 | error = EBUSY; |
| 314 | goto done; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) { |
| 319 | error = EBUSY; |
| 320 | goto done; |
| 321 | } |
| 322 | |
| 323 | if (!tty_opened(tp)) { |
| 324 | /* Set proper termios flags. */ |
| 325 | if (TTY_CALLOUT(tp, dev)) |
| 326 | tp->t_termios = tp->t_termios_init_out; |
| 327 | else |
| 328 | tp->t_termios = tp->t_termios_init_in; |
| 329 | ttydevsw_param(tp, &tp->t_termios); |
| 330 | /* Prevent modem control on callout devices and /dev/console. */ |
no test coverage detected