forkpty-hpux.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2008 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 "tmux.h"
  23. pid_t
  24. forkpty(int *master, char *name, struct termios *tio, struct winsize *ws)
  25. {
  26. int slave = -1;
  27. char *path;
  28. pid_t pid;
  29. if ((*master = open("/dev/ptmx", O_RDWR|O_NOCTTY)) == -1)
  30. return (-1);
  31. if (grantpt(*master) != 0)
  32. goto out;
  33. if (unlockpt(*master) != 0)
  34. goto out;
  35. if ((path = ptsname(*master)) == NULL)
  36. goto out;
  37. if (name != NULL)
  38. strlcpy(name, path, TTY_NAME_MAX);
  39. if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1)
  40. goto out;
  41. switch (pid = fork()) {
  42. case -1:
  43. goto out;
  44. case 0:
  45. close(*master);
  46. setsid();
  47. #ifdef TIOCSCTTY
  48. if (ioctl(slave, TIOCSCTTY, NULL) == -1)
  49. fatal("ioctl failed");
  50. #endif
  51. if (ioctl(slave, I_PUSH, "ptem") == -1)
  52. fatal("ioctl failed");
  53. if (ioctl(slave, I_PUSH, "ldterm") == -1)
  54. fatal("ioctl failed");
  55. if (tio != NULL && tcsetattr(slave, TCSAFLUSH, tio) == -1)
  56. fatal("tcsetattr failed");
  57. if (ioctl(slave, TIOCSWINSZ, ws) == -1)
  58. fatal("ioctl failed");
  59. dup2(slave, 0);
  60. dup2(slave, 1);
  61. dup2(slave, 2);
  62. if (slave > 2)
  63. close(slave);
  64. return (0);
  65. }
  66. close(slave);
  67. return (pid);
  68. out:
  69. if (*master != -1)
  70. close(*master);
  71. if (slave != -1)
  72. close(slave);
  73. return (-1);
  74. }