home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnunet10.zip / source / libinetutils / ttymsg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-27  |  5.1 KB  |  170 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. #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. #ifndef __EMX__
  45. # include <paths.h>
  46. #endif /* __EMX__ */
  47. #include <unistd.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51.  
  52. /*
  53.  * Display the contents of a uio structure on a terminal.  Used by wall(1),
  54.  * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
  55.  * waiting up to tmout seconds.  Returns pointer to error string on unexpected
  56.  * error; string is not newline-terminated.  Various "normal" errors are
  57.  * ignored (exclusive-use, lack of permission, etc.).
  58.  */
  59. char *
  60. ttymsg(iov, iovcnt, line, tmout)
  61.     struct iovec *iov;
  62.     int iovcnt;
  63.     char *line;
  64.     int tmout;
  65. {
  66. #ifdef __EMX__
  67.         return "ttymsg() from libinetutils not supported in this port.\n";
  68. #else
  69.     static char device[MAXNAMLEN] = _PATH_DEV;
  70.     static char errbuf[1024];
  71.     register int cnt, fd, left, wret;
  72.     struct iovec localiov[6];
  73.     int forked = 0;
  74.  
  75.     if (iovcnt > sizeof(localiov) / sizeof(localiov[0]))
  76.         return ("too many iov's (change code in wall/ttymsg.c)");
  77.  
  78.     (void) strcpy(device + sizeof(_PATH_DEV) - 1, line);
  79.     if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
  80.         /* A slash is an attempt to break security... */
  81.         (void) snprintf(errbuf, sizeof(errbuf), "'/' in \"%s\"",
  82.             device);
  83.         return (errbuf);
  84.     }
  85.  
  86.     /*
  87.      * open will fail on slip lines or exclusive-use lines
  88.      * if not running as root; not an error.
  89.      */
  90.     if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
  91.         if (errno == EBUSY || errno == EACCES)
  92.             return (NULL);
  93.         (void) snprintf(errbuf, sizeof(errbuf),
  94.             "%s: %s", device, strerror(errno));
  95.         return (errbuf);
  96.     }
  97.  
  98.     for (cnt = left = 0; cnt < iovcnt; ++cnt)
  99.         left += iov[cnt].iov_len;
  100.  
  101.     for (;;) {
  102.         wret = writev(fd, iov, iovcnt);
  103.         if (wret >= left)
  104.             break;
  105.         if (wret >= 0) {
  106.             left -= wret;
  107.             if (iov != localiov) {
  108.                 bcopy(iov, localiov,
  109.                     iovcnt * sizeof(struct iovec));
  110.                 iov = localiov;
  111.             }
  112.             for (cnt = 0; wret >= iov->iov_len; ++cnt) {
  113.                 wret -= iov->iov_len;
  114.                 ++iov;
  115.                 --iovcnt;
  116.             }
  117.             if (wret) {
  118.                 iov->iov_base += wret;
  119.                 iov->iov_len -= wret;
  120.             }
  121.             continue;
  122.         }
  123.         if (errno == EWOULDBLOCK) {
  124.             int cpid, off = 0;
  125.  
  126.             if (forked) {
  127.                 (void) close(fd);
  128.                 _exit(1);
  129.             }
  130.             cpid = fork();
  131.             if (cpid < 0) {
  132.                 (void) snprintf(errbuf, sizeof(errbuf),
  133.                     "fork: %s", strerror(errno));
  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 tmout seconds */
  143.             (void) signal(SIGALRM, SIG_DFL);
  144.             (void) signal(SIGTERM, SIG_DFL); /* XXX */
  145.             (void) sigsetmask(0);
  146.             (void) alarm((u_int)tmout);
  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.         (void) snprintf(errbuf, sizeof(errbuf),
  160.             "%s: %s", device, strerror(errno));
  161.         return (errbuf);
  162.     }
  163.  
  164.     (void) close(fd);
  165.     if (forked)
  166.         _exit(0);
  167.     return (NULL);
  168. #endif /* __EMX__ */
  169. }
  170.