forkpty-aix.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
  3. * Copyright (c) 2012 Ross Palmer Mohn <rpmohn@waxandwane.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/ioctl.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <stropts.h>
  22. #include <unistd.h>
  23. #include <paths.h>
  24. pid_t forkpty(int *master, char *name, struct termios *tio, struct winsize *ws)
  25. {
  26. int slave, fd;
  27. char *path;
  28. pid_t pid;
  29. struct termios tio2;
  30. if ((*master = open("/dev/ptc", O_RDWR|O_NOCTTY)) == -1)
  31. return -1;
  32. if ((path = ttyname(*master)) == NULL)
  33. goto out;
  34. if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1)
  35. goto out;
  36. switch (pid = fork()) {
  37. case -1:
  38. goto out;
  39. case 0:
  40. close(*master);
  41. fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
  42. if (fd >= 0) {
  43. ioctl(fd, TIOCNOTTY, NULL);
  44. close(fd);
  45. }
  46. setsid();
  47. fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
  48. if (fd >= 0)
  49. return -1;
  50. fd = open(path, O_RDWR);
  51. if (fd < 0)
  52. return -1;
  53. close(fd);
  54. fd = open("/dev/tty", O_WRONLY);
  55. if (fd < 0)
  56. return -1;
  57. close(fd);
  58. if (tcgetattr(slave, &tio2) != 0)
  59. return -1;
  60. if (tio != NULL)
  61. memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
  62. tio2.c_cc[VERASE] = '\177';
  63. if (tcsetattr(slave, TCSAFLUSH, &tio2) == -1)
  64. return -1;
  65. if (ioctl(slave, TIOCSWINSZ, ws) == -1)
  66. return -1;
  67. dup2(slave, 0);
  68. dup2(slave, 1);
  69. dup2(slave, 2);
  70. if (slave > 2)
  71. close(slave);
  72. return 0;
  73. }
  74. close(slave);
  75. return pid;
  76. out:
  77. if (*master != -1)
  78. close(*master);
  79. if (slave != -1)
  80. close(slave);
  81. return -1;
  82. }