tty.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <kern_util.h>
  10. #include <os.h>
  11. struct grantpt_info {
  12. int fd;
  13. int res;
  14. int err;
  15. };
  16. static void grantpt_cb(void *arg)
  17. {
  18. struct grantpt_info *info = arg;
  19. info->res = grantpt(info->fd);
  20. info->err = errno;
  21. }
  22. int get_pty(void)
  23. {
  24. struct grantpt_info info;
  25. int fd, err;
  26. fd = open("/dev/ptmx", O_RDWR);
  27. if (fd < 0) {
  28. err = -errno;
  29. printk(UM_KERN_ERR "get_pty : Couldn't open /dev/ptmx - "
  30. "err = %d\n", errno);
  31. return err;
  32. }
  33. info.fd = fd;
  34. initial_thread_cb(grantpt_cb, &info);
  35. if (info.res < 0) {
  36. err = -info.err;
  37. printk(UM_KERN_ERR "get_pty : Couldn't grant pty - "
  38. "errno = %d\n", -info.err);
  39. goto out;
  40. }
  41. if (unlockpt(fd) < 0) {
  42. err = -errno;
  43. printk(UM_KERN_ERR "get_pty : Couldn't unlock pty - "
  44. "errno = %d\n", errno);
  45. goto out;
  46. }
  47. return fd;
  48. out:
  49. close(fd);
  50. return err;
  51. }