home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / inetutils-1.2-src.tgz / tar.out / fsf / inetutils / libinetutils / ttymsg.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  5KB  |  167 lines

  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.  * 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    8.2 (Berkeley) 11/16/93";
  36. #endif /* not lint */
  37.  
  38. #ifdef HAVE_CONFIG_H
  39. #include <config.h>
  40. #endif
  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 <unistd.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include <stdlib.h>
  52.  
  53. /*
  54.  * Display the contents of a uio structure on a terminal.  Used by wall(1),
  55.  * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
  56.  * waiting up to tmout seconds.  Returns pointer to error string on unexpected
  57.  * error; string is not newline-terminated.  Various "normal" errors are
  58.  * ignored (exclusive-use, lack of permission, etc.).
  59.  */
  60. char *
  61. ttymsg(iov, iovcnt, line, tmout)
  62.     struct iovec *iov;
  63.     int iovcnt;
  64.     char *line;
  65.     int tmout;
  66. {
  67.     static char device[MAXNAMLEN] = PATH_TTY_PFX;
  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 > sizeof(localiov) / sizeof(localiov[0]))
  74.         return ("too many iov's (change code in wall/ttymsg.c)");
  75.  
  76.     (void) strcpy(device + sizeof(PATH_TTY_PFX) - 1, line);
  77.     if (strchr(device + sizeof(PATH_TTY_PFX) - 1, '/')) {
  78.         /* A slash is an attempt to break security... */
  79.         (void) snprintf(errbuf, sizeof(errbuf), "'/' in \"%s\"",
  80.             device);
  81.         return (errbuf);
  82.     }
  83.  
  84.     /*
  85.      * open will fail on slip lines or exclusive-use lines
  86.      * if not running as root; not an error.
  87.      */
  88.     if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
  89.         if (errno == EBUSY || errno == EACCES)
  90.             return (NULL);
  91.         (void) snprintf(errbuf, sizeof(errbuf),
  92.             "%s: %s", device, strerror(errno));
  93.         return (errbuf);
  94.     }
  95.  
  96.     for (cnt = left = 0; cnt < iovcnt; ++cnt)
  97.         left += iov[cnt].iov_len;
  98.  
  99.     for (;;) {
  100.         wret = writev(fd, iov, iovcnt);
  101.         if (wret >= left)
  102.             break;
  103.         if (wret >= 0) {
  104.             left -= wret;
  105.             if (iov != localiov) {
  106.                 bcopy(iov, localiov,
  107.                     iovcnt * sizeof(struct iovec));
  108.                 iov = localiov;
  109.             }
  110.             for (cnt = 0; wret >= iov->iov_len; ++cnt) {
  111.                 wret -= iov->iov_len;
  112.                 ++iov;
  113.                 --iovcnt;
  114.             }
  115.             if (wret) {
  116.                 iov->iov_base += wret;
  117.                 iov->iov_len -= wret;
  118.             }
  119.             continue;
  120.         }
  121.         if (errno == EWOULDBLOCK) {
  122.             int cpid, off = 0;
  123.  
  124.             if (forked) {
  125.                 (void) close(fd);
  126.                 _exit(1);
  127.             }
  128.             cpid = fork();
  129.             if (cpid < 0) {
  130.                 (void) snprintf(errbuf, sizeof(errbuf),
  131.                     "fork: %s", strerror(errno));
  132.                 (void) close(fd);
  133.                 return (errbuf);
  134.             }
  135.             if (cpid) {    /* parent */
  136.                 (void) close(fd);
  137.                 return (NULL);
  138.             }
  139.             forked++;
  140.             /* wait at most tmout seconds */
  141.             (void) signal(SIGALRM, SIG_DFL);
  142.             (void) signal(SIGTERM, SIG_DFL); /* XXX */
  143.             (void) sigsetmask(0);
  144.             (void) alarm((u_int)tmout);
  145.             (void) fcntl(fd, O_NONBLOCK, &off);
  146.             continue;
  147.         }
  148.         /*
  149.          * We get ENODEV on a slip line if we're running as root,
  150.          * and EIO if the line just went away.
  151.          */
  152.         if (errno == ENODEV || errno == EIO)
  153.             break;
  154.         (void) close(fd);
  155.         if (forked)
  156.             _exit(1);
  157.         (void) snprintf(errbuf, sizeof(errbuf),
  158.             "%s: %s", device, strerror(errno));
  159.         return (errbuf);
  160.     }
  161.  
  162.     (void) close(fd);
  163.     if (forked)
  164.         _exit(0);
  165.     return (NULL);
  166. }
  167.