forkpty-sunos.c 2.1 KB

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