forkpty-aix.c 2.5 KB

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