note other reasons for pclose to return -1; wait(2) -> wait4(2)
[unix-history] / usr / src / libexec / comsat / comsat.c
index 2ea59a2..c71fc77 100644 (file)
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)comsat.c    4.5 82/12/23";
-#endif
+static char sccsid[] = "@(#)comsat.c   5.13 (Berkeley) %G%";
+#endif /* not lint */
 
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/wait.h>
 
 #include <netinet/in.h>
 
 #include <stdio.h>
 #include <sgtty.h>
 #include <utmp.h>
 
 #include <netinet/in.h>
 
 #include <stdio.h>
 #include <sgtty.h>
 #include <utmp.h>
-#include <stat.h>
-#include <wait.h>
 #include <signal.h>
 #include <errno.h>
 #include <netdb.h>
 #include <signal.h>
 #include <errno.h>
 #include <netdb.h>
+#include <syslog.h>
+#include <strings.h>
 
 /*
  * comsat
  */
 
 /*
  * comsat
  */
-#define        dprintf if (0) printf
+int    debug = 0;
+#define        dsyslog if (debug) syslog
 
 
-#define MAXUTMP 100            /* down from init */
+#define MAXIDLE        120
 
 
-struct sockaddr_in sin = { AF_INET };
-extern errno;
-
-struct utmp utmp[100];
-int    nutmp;
-int    uf;
-unsigned utmpmtime;                    /* last modification time for utmp */
-int    onalrm();
-struct servent *sp;
-
-#define NAMLEN (sizeof (uts[0].ut_name) + 1)
+char   hostname[MAXHOSTNAMELEN];
+struct utmp *utmp = NULL;
+time_t lastmsgtime, time();
+int    nutmp, uf;
 
 main(argc, argv)
 
 main(argc, argv)
-char **argv;
+       int argc;
+       char **argv;
 {
 {
-       register cc;
-       char buf[BUFSIZ];
-       int s;
-
-       sp = getservbyname("biff", "udp");
-       if (sp == 0) {
-               fprintf(stderr, "comsat: biff/udp: unknown service\n");
+       extern int errno;
+       register int cc;
+       char msgbuf[100];
+       struct sockaddr_in from;
+       int fromlen, reapchildren(), onalrm();
+
+       /* verify proper invocation */
+       fromlen = sizeof (from);
+       if (getsockname(0, &from, &fromlen) < 0) {
+               fprintf(stderr, "%s: ", argv[0]);
+               perror("getsockname");
                exit(1);
        }
                exit(1);
        }
-#ifndef DEBUG
-       if (fork())
-               exit();
-#endif
-       chdir("/usr/spool/mail");
-       if((uf = open("/etc/utmp",0)) < 0)
-               perror("/etc/utmp"), exit(1);
-#ifndef DEBUG
-       while (fork())
-               wait(0);
-#endif
-       sleep(10);
-       onalrm();
-       sigset(SIGALRM, onalrm);
-       sigignore(SIGTTOU);
-       s = socket(AF_INET, SOCK_DGRAM, 0, 0);
-       if (s < 0) {
-               perror("socket");
+       openlog("comsat", LOG_PID, LOG_DAEMON);
+       if (chdir("/usr/spool/mail")) {
+               syslog(LOG_ERR, "chdir: /usr/spool/mail");
                exit(1);
        }
                exit(1);
        }
-       sin.sin_port = sp->s_port;
-       if (bind(s, &sin, sizeof (sin), 0) < 0) {
-               perror("bind");
+       if ((uf = open("/etc/utmp", O_RDONLY, 0)) < 0) {
+               syslog(LOG_ERR, ".main: /etc/utmp: %m");
+               (void) recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
                exit(1);
        }
                exit(1);
        }
+       (void)time(&lastmsgtime);
+       (void)gethostname(hostname, sizeof (hostname));
+       onalrm();
+       (void)signal(SIGALRM, onalrm);
+       (void)signal(SIGTTOU, SIG_IGN);
+       (void)signal(SIGCHLD, reapchildren);
        for (;;) {
        for (;;) {
-               char msgbuf[100];
-               int cc;
-
-               cc = recv(s, msgbuf, sizeof (msgbuf) - 1, 0);
+               cc = recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
                if (cc <= 0) {
                        if (errno != EINTR)
                                sleep(1);
                        errno = 0;
                        continue;
                }
                if (cc <= 0) {
                        if (errno != EINTR)
                                sleep(1);
                        errno = 0;
                        continue;
                }
+               if (!nutmp)             /* no one has logged in yet */
+                       continue;
+               sigblock(sigmask(SIGALRM));
                msgbuf[cc] = 0;
                msgbuf[cc] = 0;
+               (void)time(&lastmsgtime);
                mailfor(msgbuf);
                mailfor(msgbuf);
+               sigsetmask(0L);
        }
 }
 
        }
 }
 
+reapchildren()
+{
+       while (wait3((union wait *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
+}
+
 onalrm()
 {
 onalrm()
 {
+       static u_int utmpsize;          /* last malloced size for utmp */
+       static u_int utmpmtime;         /* last modification time for utmp */
        struct stat statbf;
        struct stat statbf;
-       struct utmp *utp;
+       off_t lseek();
+       char *malloc(), *realloc();
 
 
-       dprintf("alarm\n");
-       alarm(15);
-       fstat(uf,&statbf);
+       if (time((time_t *)NULL) - lastmsgtime >= MAXIDLE)
+               exit(0);
+       (void)alarm((u_int)15);
+       (void)fstat(uf, &statbf);
        if (statbf.st_mtime > utmpmtime) {
        if (statbf.st_mtime > utmpmtime) {
-               dprintf(" changed\n");
                utmpmtime = statbf.st_mtime;
                utmpmtime = statbf.st_mtime;
-               lseek(uf, 0, 0);
-               nutmp = read(uf,utmp,sizeof(utmp))/sizeof(struct utmp);
-       } else
-               dprintf(" ok\n");
+               if (statbf.st_size > utmpsize) {
+                       utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
+                       if (utmp)
+                               utmp = (struct utmp *)realloc((char *)utmp, utmpsize);
+                       else
+                               utmp = (struct utmp *)malloc(utmpsize);
+                       if (!utmp) {
+                               syslog(LOG_ERR, "malloc failed");
+                               exit(1);
+                       }
+               }
+               (void)lseek(uf, 0L, L_SET);
+               nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
+       }
 }
 
 mailfor(name)
 }
 
 mailfor(name)
@@ -110,103 +146,96 @@ mailfor(name)
 {
        register struct utmp *utp = &utmp[nutmp];
        register char *cp;
 {
        register struct utmp *utp = &utmp[nutmp];
        register char *cp;
-       char *rindex();
-       int offset;
-
-       dprintf("mailfor %s\n", name);
-       cp = name;
-       while (*cp && *cp != '@')
-               cp++;
-       if (*cp == 0) {
-               dprintf("bad format\n");
+       off_t offset, atol();
+
+       if (!(cp = index(name, '@')))
                return;
                return;
-       }
-       *cp = 0;
-       offset = atoi(cp+1);
+       *cp = '\0';
+       offset = atoi(cp + 1);
        while (--utp >= utmp)
                if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
        while (--utp >= utmp)
                if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
-                       if (fork() == 0) {
-                               signal(SIGALRM, SIG_DFL);
-                               alarm(30);
-                               notify(utp, offset), exit(0);
-                       } else
-                               while (wait3(0, WNOHANG, 0) > 0)
-                                       continue;
+                       notify(utp, offset);
 }
 
 }
 
-char *cr;
+static char    *cr;
 
 notify(utp, offset)
        register struct utmp *utp;
 
 notify(utp, offset)
        register struct utmp *utp;
+       off_t offset;
 {
 {
-       FILE *tp;
+       static char tty[20] = "/dev/";
        struct sgttyb gttybuf;
        struct sgttyb gttybuf;
-       char tty[20];
+       FILE *tp;
        char name[sizeof (utmp[0].ut_name) + 1];
        struct stat stb;
 
        char name[sizeof (utmp[0].ut_name) + 1];
        struct stat stb;
 
-       strcpy(tty, "/dev/");
-       strncat(tty, utp->ut_line, sizeof(utp->ut_line));
-       dprintf("notify %s on %s\n", utp->ut_name, tty);
-       if (stat(tty, &stb) == 0 && (stb.st_mode & 0100) == 0) {
-               dprintf("wrong mode\n");
+       (void)strncpy(tty + 5, utp->ut_line, sizeof(utp->ut_line));
+       if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
+               dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
                return;
        }
                return;
        }
-       if ((tp = fopen(tty,"w")) == 0) {
-               dprintf("fopen failed\n");
+       dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
+       if (fork())
                return;
                return;
+       (void)signal(SIGALRM, SIG_DFL);
+       (void)alarm((u_int)30);
+       if ((tp = fopen(tty, "w")) == NULL) {
+               dsyslog(LOG_ERR, "fopen of tty %s failed", tty);
+               _exit(-1);
        }
        }
-       gtty(fileno(tp),&gttybuf);
-       cr = (gttybuf.sg_flags & CRMOD) ? "" : "\r";
-       strncpy(name, utp->ut_name, sizeof (utp->ut_name));
-       name[sizeof (name) - 1] = 0;
-       fprintf(tp,"%s\n\007New mail for %s\007 has arrived:%s\n",
-           cr, name, cr);
-       fprintf(tp,"----%s\n", cr);
+       (void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
+       cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? "" : "\r";
+       (void)strncpy(name, utp->ut_name, sizeof (utp->ut_name));
+       name[sizeof (name) - 1] = '\0';
+       fprintf(tp, "%s\n\007New mail for %s@%.*s\007 has arrived:%s\n----%s\n",
+           cr, name, sizeof (hostname), hostname, cr, cr);
        jkfprintf(tp, name, offset);
        jkfprintf(tp, name, offset);
-        fclose(tp);
+       fclose(tp);
+       _exit(0);
 }
 
 jkfprintf(tp, name, offset)
        register FILE *tp;
 }
 
 jkfprintf(tp, name, offset)
        register FILE *tp;
+       char name[];
+       off_t offset;
 {
 {
+       register char *cp;
        register FILE *fi;
        register FILE *fi;
-       register int linecnt, charcnt;
+       register int linecnt, charcnt, inheader;
+       char line[BUFSIZ];
+       off_t fseek();
 
 
-       dprintf("HERE %s's mail starting at %d\n",
-           name, offset);
-       if ((fi = fopen(name,"r")) == NULL) {
-               dprintf("Cant read the mail\n");
+       if ((fi = fopen(name, "r")) == NULL)
                return;
                return;
-       }
-       fseek(fi, offset, 0);
-       linecnt = 7;
-       charcnt = 560;
+       (void)fseek(fi, offset, L_SET);
        /* 
        /* 
-        * print the first 7 lines or 560 characters of the new mail
-        * (whichever comes first)
+        * Print the first 7 lines or 560 characters of the new mail
+        * (whichever comes first).  Skip header crap other than
+        * From, Subject, To, and Date.
         */
         */
-       for (;;) {
-               register ch;
-
-               if ((ch = getc(fi)) == EOF) {  
-                       fprintf(tp,"----%s\n", cr);
-                       break;
-               }
-               if (ch == '\n') {  
-                       fprintf(tp,"%s\n", cr);
-                       if (linecnt-- < 0) {  
-                               fprintf(tp,"...more...%s\n", cr);
-                               break;
+       linecnt = 7;
+       charcnt = 560;
+       inheader = 1;
+       while (fgets(line, sizeof (line), fi) != NULL) {
+               if (inheader) {
+                       if (line[0] == '\n') {
+                               inheader = 0;
+                               continue;
                        }
                        }
-               } else if(linecnt <= 0) {  
-                       fprintf(tp,"...more...%s\n", cr);
-                       break;
-               } else
-                       putc(ch, tp);
-               if (charcnt-- == 0) {   
-                       fprintf(tp, "%s\n", cr);
-                       break;
+                       if (line[0] == ' ' || line[0] == '\t' ||
+                           strncmp(line, "From:", 5) &&
+                           strncmp(line, "Subject:", 8))
+                               continue;
+               }
+               if (linecnt <= 0 || charcnt <= 0) {
+                       fprintf(tp, "...more...%s\n", cr);
+                       return;
                }
                }
+               if (cp = index(line, '\n'))
+                       *cp = '\0';
+               fprintf(tp, "%s%s\n", line, cr);
+               charcnt -= strlen(line);
+               linecnt--;
        }
        }
+       fprintf(tp, "----%s\n", cr);
 }
 }