unix_grantpt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Copyright (C) 1998 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <grp.h>
  19. #include <limits.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/resource.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <unistd.h>
  27. #include "pty-private.h"
  28. extern int ptsname_r (int fd, char *buf, size_t buflen);
  29. /* Return the result of ptsname_r in the buffer pointed to by PTS,
  30. which should be of length BUF_LEN. If it is too long to fit in
  31. this buffer, a sufficiently long buffer is allocated using malloc,
  32. and returned in PTS. 0 is returned upon success, -1 otherwise. */
  33. static int
  34. pts_name (int fd, char **pts, size_t buf_len)
  35. {
  36. int rv;
  37. char *buf = *pts;
  38. for (;;)
  39. {
  40. char *new_buf;
  41. if (buf_len)
  42. {
  43. rv = ptsname_r (fd, buf, buf_len);
  44. if (rv != 0 || memchr (buf, '\0', buf_len))
  45. /* We either got an error, or we succeeded and the
  46. returned name fit in the buffer. */
  47. break;
  48. /* Try again with a longer buffer. */
  49. buf_len += buf_len; /* Double it */
  50. }
  51. else
  52. /* No initial buffer; start out by mallocing one. */
  53. buf_len = 128; /* First time guess. */
  54. if (buf != *pts)
  55. /* We've already malloced another buffer at least once. */
  56. new_buf = realloc (buf, buf_len);
  57. else
  58. new_buf = malloc (buf_len);
  59. if (! new_buf)
  60. {
  61. rv = -1;
  62. errno = ENOMEM;
  63. break;
  64. }
  65. buf = new_buf;
  66. }
  67. if (rv == 0)
  68. *pts = buf; /* Return buffer to the user. */
  69. else if (buf != *pts)
  70. free (buf); /* Free what we malloced when returning an error. */
  71. return rv;
  72. }
  73. /* Change the ownership and access permission of the slave pseudo
  74. terminal associated with the master pseudo terminal specified
  75. by FD. */
  76. int
  77. grantpt (int fd)
  78. {
  79. int retval = -1;
  80. #ifdef PATH_MAX
  81. char _buf[PATH_MAX];
  82. #else
  83. char _buf[512];
  84. #endif
  85. char *buf = _buf;
  86. struct stat st;
  87. uid_t uid;
  88. gid_t gid;
  89. pid_t pid;
  90. if (pts_name (fd, &buf, sizeof (_buf)))
  91. return -1;
  92. if (__xstat (_STAT_VER, buf, &st) < 0)
  93. goto cleanup;
  94. /* Make sure that we own the device. */
  95. uid = getuid ();
  96. if (st.st_uid != uid)
  97. {
  98. if (chown (buf, uid, st.st_gid) < 0)
  99. goto helper;
  100. }
  101. gid = getgid ();
  102. /* Make sure the group of the device is that special group. */
  103. if (st.st_gid != gid)
  104. {
  105. if (chown (buf, uid, gid) < 0)
  106. goto helper;
  107. }
  108. /* Make sure the permission mode is set to readable and writable by
  109. the owner, and writable by the group. */
  110. if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP))
  111. {
  112. if (chmod (buf, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
  113. goto helper;
  114. }
  115. retval = 0;
  116. goto cleanup;
  117. /* We have to use the helper program. */
  118. helper:
  119. #ifdef __UCLIBC_HAS_MMU__
  120. pid = fork ();
  121. #else
  122. pid = vfork ();
  123. #endif
  124. if (pid == -1)
  125. goto cleanup;
  126. else if (pid == 0)
  127. {
  128. /* Disable core dumps. */
  129. struct rlimit rl = { 0, 0 };
  130. setrlimit (RLIMIT_CORE, &rl);
  131. /* We pase the master pseudo terminal as file descriptor PTY_FILENO. */
  132. if (fd != PTY_FILENO)
  133. if (dup2 (fd, PTY_FILENO) < 0)
  134. exit (FAIL_EBADF);
  135. execle (_PATH_PT_CHOWN, _PATH_PT_CHOWN, NULL, NULL);
  136. exit (FAIL_EXEC);
  137. }
  138. else
  139. {
  140. int w;
  141. if (waitpid (pid, &w, 0) == -1)
  142. goto cleanup;
  143. if (!WIFEXITED (w))
  144. errno = ENOEXEC;
  145. else
  146. switch (WEXITSTATUS(w))
  147. {
  148. case 0:
  149. retval = 0;
  150. break;
  151. case FAIL_EBADF:
  152. errno = EBADF;
  153. break;
  154. case FAIL_EINVAL:
  155. errno = EINVAL;
  156. break;
  157. case FAIL_EACCES:
  158. errno = EACCES;
  159. break;
  160. case FAIL_EXEC:
  161. errno = ENOEXEC;
  162. break;
  163. default:
  164. assert(! "getpt: internal error: invalid exit code from pt_chown");
  165. }
  166. }
  167. cleanup:
  168. if (buf != _buf)
  169. free (buf);
  170. return retval;
  171. }