/usr.bin/wall/ttymsg.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 167 lines · 109 code · 13 blank · 45 comment · 29 complexity · 49aeb156f582aa71540a456bf9b41562 MD5 · raw file

  1. /*
  2. * Copyright (c) 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. #ifndef lint
  32. static const char sccsid[] = "@(#)ttymsg.c 8.2 (Berkeley) 11/16/93";
  33. #endif
  34. #include <sys/types.h>
  35. #include <sys/uio.h>
  36. #include <dirent.h>
  37. #include <errno.h>
  38. #include <fcntl.h>
  39. #include <paths.h>
  40. #include <signal.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <unistd.h>
  45. #include "ttymsg.h"
  46. /*
  47. * Display the contents of a uio structure on a terminal. Used by wall(1),
  48. * syslogd(8), and talkd(8). Forks and finishes in child if write would block,
  49. * waiting up to tmout seconds. Returns pointer to error string on unexpected
  50. * error; string is not newline-terminated. Various "normal" errors are
  51. * ignored (exclusive-use, lack of permission, etc.).
  52. */
  53. const char *
  54. ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
  55. {
  56. struct iovec localiov[7];
  57. ssize_t left, wret;
  58. int cnt, fd;
  59. static char device[MAXNAMLEN] = _PATH_DEV;
  60. static char errbuf[1024];
  61. char *p;
  62. int forked;
  63. forked = 0;
  64. if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
  65. return ("too many iov's (change code in wall/ttymsg.c)");
  66. p = device + sizeof(_PATH_DEV) - 1;
  67. strlcpy(p, line, sizeof(device));
  68. if (strncmp(p, "pts/", 4) == 0)
  69. p += 4;
  70. if (strchr(p, '/') != NULL) {
  71. /* A slash is an attempt to break security... */
  72. (void) snprintf(errbuf, sizeof(errbuf),
  73. "Too many '/' in \"%s\"", device);
  74. return (errbuf);
  75. }
  76. /*
  77. * open will fail on slip lines or exclusive-use lines
  78. * if not running as root; not an error.
  79. */
  80. if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
  81. if (errno == EBUSY || errno == EACCES)
  82. return (NULL);
  83. (void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
  84. strerror(errno));
  85. return (errbuf);
  86. }
  87. for (cnt = 0, left = 0; cnt < iovcnt; ++cnt)
  88. left += iov[cnt].iov_len;
  89. for (;;) {
  90. wret = writev(fd, iov, iovcnt);
  91. if (wret >= left)
  92. break;
  93. if (wret >= 0) {
  94. left -= wret;
  95. if (iov != localiov) {
  96. bcopy(iov, localiov,
  97. iovcnt * sizeof(struct iovec));
  98. iov = localiov;
  99. }
  100. for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) {
  101. wret -= iov->iov_len;
  102. ++iov;
  103. --iovcnt;
  104. }
  105. if (wret) {
  106. iov->iov_base = (char *)iov->iov_base + wret;
  107. iov->iov_len -= wret;
  108. }
  109. continue;
  110. }
  111. if (errno == EWOULDBLOCK) {
  112. int cpid;
  113. if (forked) {
  114. (void) close(fd);
  115. _exit(1);
  116. }
  117. cpid = fork();
  118. if (cpid < 0) {
  119. (void) snprintf(errbuf, sizeof(errbuf),
  120. "fork: %s", strerror(errno));
  121. (void) close(fd);
  122. return (errbuf);
  123. }
  124. if (cpid) { /* parent */
  125. (void) close(fd);
  126. return (NULL);
  127. }
  128. forked++;
  129. /* wait at most tmout seconds */
  130. (void) signal(SIGALRM, SIG_DFL);
  131. (void) signal(SIGTERM, SIG_DFL); /* XXX */
  132. (void) sigsetmask(0);
  133. (void) alarm((u_int)tmout);
  134. (void) fcntl(fd, F_SETFL, 0); /* clear O_NONBLOCK */
  135. continue;
  136. }
  137. /*
  138. * We get ENODEV on a slip line if we're running as root,
  139. * and EIO if the line just went away.
  140. */
  141. if (errno == ENODEV || errno == EIO)
  142. break;
  143. (void) close(fd);
  144. if (forked)
  145. _exit(1);
  146. (void) snprintf(errbuf, sizeof(errbuf),
  147. "%s: %s", device, strerror(errno));
  148. return (errbuf);
  149. }
  150. (void) close(fd);
  151. if (forked)
  152. _exit(0);
  153. return (NULL);
  154. }