home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / daemons / syslog_f.z / syslog_f / syslogd / RCS / ttymsg.c,v < prev   
Encoding:
Text File  |  1993-03-17  |  5.3 KB  |  218 lines

  1. head    1.2;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @ * @;
  6.  
  7.  
  8. 1.2
  9. date    93.03.17.19.53.32;    author negaard;    state Exp;
  10. branches;
  11. next    1.1;
  12.  
  13. 1.1
  14. date    93.03.17.19.51.01;    author negaard;    state Exp;
  15. branches;
  16. next    ;
  17.  
  18.  
  19. desc
  20. @Send a message to a logged on user.
  21. @
  22.  
  23.  
  24. 1.2
  25. log
  26. @Changes for linux.
  27. @
  28. text
  29. @/*
  30.  * Copyright (c) 1989 The Regents of the University of California.
  31.  * All rights reserved.
  32.  *
  33.  * Redistribution and use in source and binary forms, with or without
  34.  * modification, are permitted provided that the following conditions
  35.  * are met:
  36.  * 1. Redistributions of source code must retain the above copyright
  37.  *    notice, this list of conditions and the following disclaimer.
  38.  * 2. Redistributions in binary form must reproduce the above copyright
  39.  *    notice, this list of conditions and the following disclaimer in the
  40.  *    documentation and/or other materials provided with the distribution.
  41.  * 3. All advertising materials mentioning features or use of this software
  42.  *    must display the following acknowledgement:
  43.  *    This product includes software developed by the University of
  44.  *    California, Berkeley and its contributors.
  45.  * 4. Neither the name of the University nor the names of its contributors
  46.  *    may be used to endorse or promote products derived from this software
  47.  *    without specific prior written permission.
  48.  *
  49.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  50.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  53.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59.  * SUCH DAMAGE.
  60.  */
  61.  
  62. #ifndef lint
  63. static char sccsid[] = "@@(#)ttymsg.c    5.8 (Berkeley) 7/1/91";
  64. #endif /* not lint */
  65.  
  66. #include <sys/types.h>
  67. #include <sys/uio.h>
  68. #include <signal.h>
  69. #include <fcntl.h>
  70. #include <dirent.h>
  71. #include <errno.h>
  72. #include <paths.h>
  73. #include <unistd.h>
  74. #include <stdio.h>
  75. #include <string.h>
  76. #include <stdlib.h>
  77.  
  78. #ifndef MAXNAMLEN
  79. # ifdef NAME_MAX
  80. #  define MAXNAMLEN NAME_MAX
  81. # endif
  82. #endif
  83.  
  84. /*
  85.  * Display the contents of a uio structure on a terminal.  Used by wall(1)
  86.  * and syslogd(8).  Forks and finishes in child if write would block, waiting
  87.  * at most five minutes.  Returns pointer to error string on unexpected error;
  88.  * string is not newline-terminated.  Various "normal" errors are ignored
  89.  * (exclusive-use, lack of permission, etc.).
  90.  */
  91. char *
  92. ttymsg(iov, iovcnt, line)
  93.     struct iovec *iov;
  94.     int iovcnt;
  95.     char *line;
  96. {
  97.     static char device[MAXNAMLEN] = _PATH_DEV;
  98.     static char errbuf[1024];
  99.     register int cnt, fd, left, wret;
  100.     struct iovec localiov[6];
  101.     int forked = 0;
  102.  
  103.     if (iovcnt > 6)
  104.         return ("too many iov's (change code in wall/ttymsg.c)");
  105.     /*
  106.      * open will fail on slip lines or exclusive-use lines
  107.      * if not running as root; not an error.
  108.      */
  109.     (void) strcpy(device + sizeof(_PATH_DEV) - 1, line);
  110.     if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
  111.         if (errno == EBUSY || errno == EACCES)
  112.             return (NULL);
  113. #ifdef linux
  114.         (void) sprintf(errbuf, "%s: %s", device, strerror(errno));
  115. #else
  116.         (void) snprintf(errbuf, sizeof(errbuf),
  117.             "%s: %s", device, strerror(errno));
  118. #endif
  119.         return (errbuf);
  120.     }
  121.  
  122.     for (cnt = left = 0; cnt < iovcnt; ++cnt)
  123.         left += iov[cnt].iov_len;
  124.  
  125.     for (;;) {
  126.         wret = writev(fd, iov, iovcnt);
  127.         if (wret >= left)
  128.             break;
  129.         if (wret >= 0) {
  130.             left -= wret;
  131.             if (iov != localiov) {
  132.                 bcopy(iov, localiov, 
  133.                     iovcnt * sizeof(struct iovec));
  134.                 iov = localiov;
  135.             }
  136.             for (cnt = 0; wret >= iov->iov_len; ++cnt) {
  137.                 wret -= iov->iov_len;
  138.                 ++iov;
  139.                 --iovcnt;
  140.             }
  141.             if (wret) {
  142.                 iov->iov_base += wret;
  143.                 iov->iov_len -= wret;
  144.             }
  145.             continue;
  146.         }
  147.         if (errno == EWOULDBLOCK) {
  148.             int cpid, off = 0;
  149.  
  150.             if (forked) {
  151.                 (void) close(fd);
  152.                 _exit(1);
  153.             }
  154.             cpid = fork();
  155.             if (cpid < 0) {
  156. #ifdef linux
  157.                 (void) sprintf(errbuf,
  158.                     "fork: %s", strerror(errno));
  159. #else
  160.                 (void) snprintf(errbuf, sizeof(errbuf),
  161.                     "fork: %s", strerror(errno));
  162. #endif
  163.                 (void) close(fd);
  164.                 return (errbuf);
  165.             }
  166.             if (cpid) {    /* parent */
  167.                 (void) close(fd);
  168.                 return (NULL);
  169.             }
  170.             forked++;
  171.             /* wait at most 5 minutes */
  172.             (void) signal(SIGALRM, SIG_DFL);
  173.             (void) signal(SIGTERM, SIG_DFL); /* XXX */
  174.             (void) sigsetmask(0);
  175.             (void) alarm((u_int)(60 * 5));
  176.             (void) fcntl(fd, O_NONBLOCK, &off);
  177.             continue;
  178.         } 
  179.         /*
  180.          * We get ENODEV on a slip line if we're running as root,
  181.          * and EIO if the line just went away.
  182.          */
  183.         if (errno == ENODEV || errno == EIO)
  184.             break;
  185.         (void) close(fd);
  186.         if (forked)
  187.             _exit(1);
  188. #ifdef linux
  189.         (void) sprintf(errbuf, "%s: %s", device, strerror(errno));
  190. #else
  191.         (void) snprintf(errbuf, sizeof(errbuf),
  192.             "%s: %s", device, strerror(errno));
  193. #endif
  194.         return (errbuf);
  195.     }
  196.  
  197.     (void) close(fd);
  198.     if (forked)
  199.         _exit(0);
  200.     return (NULL);
  201. }
  202. @
  203.  
  204.  
  205. 1.1
  206. log
  207. @Initial revision
  208. @
  209. text
  210. @d50 6
  211. d85 3
  212. d90 1
  213. d128 4
  214. d134 1
  215. d160 3
  216. d165 1
  217. @
  218.