home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / wall / ttymsg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-30  |  4.6 KB  |  155 lines

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