tty.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_constants.h"
  10. #include "kern_util.h"
  11. #include "os.h"
  12. #include "user.h"
  13. struct grantpt_info {
  14. int fd;
  15. int res;
  16. int err;
  17. };
  18. static void grantpt_cb(void *arg)
  19. {
  20. struct grantpt_info *info = arg;
  21. info->res = grantpt(info->fd);
  22. info->err = errno;
  23. }
  24. int get_pty(void)
  25. {
  26. struct grantpt_info info;
  27. int fd, err;
  28. fd = open("/dev/ptmx", O_RDWR);
  29. if (fd < 0) {
  30. err = -errno;
  31. printk(UM_KERN_ERR "get_pty : Couldn't open /dev/ptmx - "
  32. "err = %d\n", errno);
  33. return err;
  34. }
  35. info.fd = fd;
  36. initial_thread_cb(grantpt_cb, &info);
  37. if (info.res < 0) {
  38. err = -info.err;
  39. printk(UM_KERN_ERR "get_pty : Couldn't grant pty - "
  40. "errno = %d\n", -info.err);
  41. goto out;
  42. }
  43. if (unlockpt(fd) < 0) {
  44. err = -errno;
  45. printk(UM_KERN_ERR "get_pty : Couldn't unlock pty - "
  46. "errno = %d\n", errno);
  47. goto out;
  48. }
  49. return fd;
  50. out:
  51. close(fd);
  52. return err;
  53. }