home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.10 / util-lin / util-linux-1.10 / ttymsg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-08  |  4.9 KB  |  174 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.  * Modified for Linux (which doesn\'t have snprintf()) by faith@cs.unc.edu
  34.  * on Sun Mar  7 16:14:18 1993.
  35.  *
  36.  */
  37.  
  38. #ifndef lint
  39. static char sccsid[] = "@(#)ttymsg.c    5.8 (Berkeley) 7/1/91";
  40. #endif /* not lint */
  41.  
  42. #include <sys/types.h>
  43. #include <sys/uio.h>
  44. #include <signal.h>
  45. #include <fcntl.h>
  46. #include <dirent.h>
  47. #include <errno.h>
  48. #include <paths.h>
  49. #include <unistd.h>
  50. #include <stdio.h>
  51. #include <string.h>
  52. #include <stdlib.h>
  53.  
  54. /*
  55.  * Display the contents of a uio structure on a terminal.  Used by wall(1)
  56.  * and syslogd(8).  Forks and finishes in child if write would block, waiting
  57.  * at most five minutes.  Returns pointer to error string on unexpected error;
  58.  * string is not newline-terminated.  Various "normal" errors are ignored
  59.  * (exclusive-use, lack of permission, etc.).
  60.  */
  61. char *
  62. ttymsg(iov, iovcnt, line)
  63.     struct iovec *iov;
  64.     int iovcnt;
  65.     char *line;
  66. {
  67.     static char device[MAXNAMLEN] = _PATH_DEV;
  68.     static char errbuf[1024];
  69.     register int cnt, fd, left, wret;
  70.     struct iovec localiov[6];
  71.     int forked = 0;
  72.  
  73.     if (iovcnt > 6)
  74.         return ("too many iov's (change code in wall/ttymsg.c)");
  75.     /*
  76.      * open will fail on slip lines or exclusive-use lines
  77.      * if not running as root; not an error.
  78.      */
  79.     (void) strcpy(device + sizeof(_PATH_DEV) - 1, line);
  80.     if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
  81.         if (errno == EBUSY || errno == EACCES)
  82.             return (NULL);
  83. #ifdef __linux__
  84.         (void) sprintf(errbuf,
  85.             "%s: %s", device, strerror(errno));
  86. #else
  87.         (void) snprintf(errbuf, sizeof(errbuf),
  88.             "%s: %s", device, strerror(errno));
  89. #endif
  90.         return (errbuf);
  91.     }
  92.  
  93.     for (cnt = left = 0; cnt < iovcnt; ++cnt)
  94.         left += iov[cnt].iov_len;
  95.  
  96.     for (;;) {
  97.         wret = writev(fd, iov, iovcnt);
  98.         if (wret >= left)
  99.             break;
  100.         if (wret >= 0) {
  101.             left -= wret;
  102.             if (iov != localiov) {
  103.                 bcopy(iov, localiov, 
  104.                     iovcnt * sizeof(struct iovec));
  105.                 iov = localiov;
  106.             }
  107.             for (cnt = 0; wret >= iov->iov_len; ++cnt) {
  108.                 wret -= iov->iov_len;
  109.                 ++iov;
  110.                 --iovcnt;
  111.             }
  112.             if (wret) {
  113.                 iov->iov_base += wret;
  114.                 iov->iov_len -= wret;
  115.             }
  116.             continue;
  117.         }
  118.         if (errno == EWOULDBLOCK) {
  119.             int cpid, off = 0;
  120.  
  121.             if (forked) {
  122.                 (void) close(fd);
  123.                 _exit(1);
  124.             }
  125.             cpid = fork();
  126.             if (cpid < 0) {
  127. #ifdef __linux__
  128.                 (void) sprintf(errbuf,
  129.                     "fork: %s", strerror(errno));
  130. #else
  131.                 (void) snprintf(errbuf, sizeof(errbuf),
  132.                     "fork: %s", strerror(errno));
  133. #endif
  134.                 (void) close(fd);
  135.                 return (errbuf);
  136.             }
  137.             if (cpid) {    /* parent */
  138.                 (void) close(fd);
  139.                 return (NULL);
  140.             }
  141.             forked++;
  142.             /* wait at most 5 minutes */
  143.             (void) signal(SIGALRM, SIG_DFL);
  144.             (void) signal(SIGTERM, SIG_DFL); /* XXX */
  145.             (void) sigsetmask(0);
  146.             (void) alarm((u_int)(60 * 5));
  147.             (void) fcntl(fd, O_NONBLOCK, &off);
  148.             continue;
  149.         } 
  150.         /*
  151.          * We get ENODEV on a slip line if we're running as root,
  152.          * and EIO if the line just went away.
  153.          */
  154.         if (errno == ENODEV || errno == EIO)
  155.             break;
  156.         (void) close(fd);
  157.         if (forked)
  158.             _exit(1);
  159. #ifdef __linux__
  160.         (void) sprintf(errbuf,
  161.             "%s: %s", device, strerror(errno));
  162. #else
  163.         (void) snprintf(errbuf, sizeof(errbuf),
  164.             "%s: %s", device, strerror(errno));
  165. #endif
  166.         return (errbuf);
  167.     }
  168.  
  169.     (void) close(fd);
  170.     if (forked)
  171.         _exit(0);
  172.     return (NULL);
  173. }
  174.