Annotation of researchv9/jerq/src/sysmon/generate.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <errno.h>
        !             3: #include <sys/param.h>
        !             4: #include <sys/types.h>
        !             5: #include <sys/stat.h>
        !             6: #include <dir.h>
        !             7: #include "defs.h"
        !             8: #include "load.h"
        !             9: 
        !            10: /* globals for mail scanning */
        !            11: FILE *mlfp = NULL;
        !            12: struct stat s;
        !            13: struct stat os;
        !            14: int doscan = FALSE;
        !            15: char *user[NOFILE];
        !            16: 
        !            17: /* imported */
        !            18: extern int errno;
        !            19: 
        !            20: /* initialize */
        !            21: init()
        !            22: {
        !            23:        initload();
        !            24: }
        !            25: 
        !            26: /* return my name */
        !            27: char *
        !            28: myname()
        !            29: {
        !            30:        return ("mon");
        !            31: }
        !            32: 
        !            33: /* send the current information to a client */
        !            34: sendinfo (fd)
        !            35: int fd;
        !            36: {
        !            37:        char *p, *strchr();
        !            38:        char buf[128];
        !            39:        char line[128];
        !            40:        char token[128];
        !            41:        int len, n;
        !            42: 
        !            43:        /* send the load */
        !            44:        sprintf(buf, "load %f time %d vec %d %d %d %d %d\n",
        !            45:                load.l_runq, load.l_time, load.l_cp[0], load.l_cp[1],
        !            46:                load.l_cp[2], load.l_cp[3], load.l_cp[4]);
        !            47:        len = strlen(buf);
        !            48:        if (write (fd, buf, len) != len)
        !            49:                return (-1);
        !            50: 
        !            51:        /* check for mail if its time */
        !            52:        strcpy(token, "delivered ");
        !            53:        strcat(token, user);
        !            54:        strcat(token, " From ");
        !            55:        n = strlen(token);
        !            56:        if (doscan) {
        !            57:                if (fseek(mlfp, os.st_size, 0) < 0) {
        !            58:                        os.st_ino = 0;
        !            59:                        return 0;
        !            60:                }
        !            61:                while (fgets(line, sizeof(line), mlfp)!=NULL) {
        !            62:                        if (strncmp(token, line, n)!=0)
        !            63:                                continue;
        !            64:                        p = strchr(&line[n], ' ');
        !            65:                        if (p==NULL)
        !            66:                                continue;
        !            67:                        *p = '\0';
        !            68:                        sprintf(buf, "from %s\n", &line[n]);
        !            69:                        len = strlen(buf);
        !            70:                        if (write (fd, buf, len) != len)
        !            71:                                return (-1);
        !            72:                }
        !            73:        }
        !            74:        return (0);
        !            75: }
        !            76: 
        !            77: /* generate the current information */
        !            78: generate()
        !            79: {
        !            80:        long now;
        !            81:        static long lastscan;
        !            82: #define MAILNAME "/usr/spool/mail/mail.log"
        !            83: 
        !            84:        /* get load info */
        !            85:        genload ();
        !            86: 
        !            87:        /* find out if we should scanmn the mail */
        !            88:        now = time (NULL);
        !            89:        doscan = (now - lastscan) > 60;
        !            90:        if (doscan) {
        !            91:                os = s;
        !            92:                lastscan = now;
        !            93:                if (stat(MAILNAME, &s)<0) {
        !            94:                        doscan = FALSE;
        !            95:                } else if (os.st_dev != s.st_dev || os.st_ino != s.st_ino) {
        !            96:                        if (mlfp)
        !            97:                                fclose(mlfp);
        !            98:                        mlfp = fopen (MAILNAME, "r");
        !            99:                        os.st_size = 0;
        !           100:                } else if (os.st_size == s.st_size) {
        !           101:                        doscan = FALSE;
        !           102:                }
        !           103:        }
        !           104: }
        !           105: 
        !           106: /* add a client */
        !           107: void
        !           108: add (fd, who)
        !           109:        int fd;
        !           110:        char *who;
        !           111: {
        !           112:        char *strdup();
        !           113: 
        !           114:        if (user[fd])
        !           115:                free(user[fd]);
        !           116:        user[fd] = strdup(who);
        !           117: }
        !           118: 
        !           119: /* drop a client */
        !           120: void
        !           121: drop (fd)
        !           122: {
        !           123: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.