Annotation of embedtools/src/clog.c, revision 1.1.2.7

1.1.2.1   misho       1: #include "global.h"
                      2: #include "clog.h"
                      3: 
                      4: 
1.1.2.7 ! misho       5: int Verbose;
1.1.2.1   misho       6: extern char compiled[], compiledby[], compilehost[];
                      7: 
                      8: 
                      9: static void
                     10: Usage()
                     11: {
                     12: 
                     13:        printf( "cLOG is tool for managment syslogd operation with circular logs\n"
                     14:                "=== %s === %s@%s ===\n\n"
                     15:                "  Syntax: clog [options] <file.log>\n\n"
1.1.2.7 ! misho      16:                "\t-v\t\tVerbose, more -vv more verbosity\n"
1.1.2.1   misho      17:                "\t-i\t\tInit file.log, create and prepare for use\n"
                     18:                "\t-s <size>\tUse with init for set needed file.log size\n"
1.1.2.5   misho      19:                "\t-f\t\tStay in foreground for read from file.log\n"
1.1.2.1   misho      20:                "\n", compiled, compiledby, compilehost);
                     21: }
                     22: 
1.1.2.3   misho      23: static int
                     24: initlog(const char *csLog, size_t size)
                     25: {
1.1.2.4   misho      26:        int f, ret = -1, fill = size;
1.1.2.3   misho      27:        struct clogFooter cf = { 0 };
                     28:        char buffer[BUFLEN] = { 0 };
                     29: 
                     30:        memcpy(&cf.cf_magic, MAGIC, sizeof cf.cf_magic);
                     31:        cf.cf_max = size - sizeof cf;
                     32: 
                     33:        f = open(csLog, O_WRONLY | O_CREAT | O_TRUNC, 0666);
                     34:        if (f == -1) {
                     35:                printf("Error:: in open log %s #%d - %s\n", csLog, errno, strerror(errno));
                     36:                return -1;
1.1.2.7 ! misho      37:        } else
        !            38:                VERB(1) printf("Verbose:: Create log %s\n", csLog);
1.1.2.3   misho      39:        while (fill > BUFLEN)
                     40:                if (write(f, buffer, BUFLEN) == -1) {
                     41:                        printf("Error:: in fill log %s #%d - %s\n", csLog, errno, strerror(errno));
                     42:                        goto end;
                     43:                } else
                     44:                        fill -= BUFLEN;
                     45:        if (fill > BUFLEN) {
                     46:                printf("Error:: in fill log %s uninspected result!!!\n", csLog);
                     47:                goto end;
                     48:        } else if (fill && write(f, buffer, fill) == -1) {
                     49:                printf("Error:: in last fill log %s #%d - %s\n", csLog, errno, strerror(errno));
                     50:                goto end;
                     51:        }
                     52:        // return to write cfooter
                     53:        if (lseek(f, -(off_t)(sizeof cf), SEEK_END) == -1) {
                     54:                printf("Error:: can`t set position for write footer #%d - %s\n", errno, strerror(errno));
                     55:                goto end;
                     56:        }
                     57:        if (write(f, &cf, sizeof cf) == -1) {
                     58:                printf("Error:: in footer log %s #%d - %s\n", csLog, errno, strerror(errno));
                     59:                goto end;
                     60:        }
                     61: 
1.1.2.4   misho      62:        ret = 0;
1.1.2.7 ! misho      63:        VERB(1) printf("Verbose:: Init done.\n");
1.1.2.3   misho      64: end:
                     65:        close(f);
1.1.2.4   misho      66:        return ret;
                     67: }
                     68: 
                     69: static int
                     70: readlog(const char *csLog, char m)
                     71: {
1.1.2.5   misho      72:        int f, cx;
1.1.2.4   misho      73:        size_t end;
1.1.2.5   misho      74:        struct clogFooter *cf;
1.1.2.7 ! misho      75:        char *buffer = NULL;
1.1.2.5   misho      76:        uint32_t next, start = 0;
                     77:        struct iovec iov[2];
1.1.2.4   misho      78:        struct pollfd pfd = { 0 };
                     79: 
                     80:        f = open(csLog, O_RDONLY);
                     81:        if (f == -1) {
                     82:                printf("Error:: in open read %s #%d - %s\n", csLog, errno, strerror(errno));
                     83:                return -1;
1.1.2.7 ! misho      84:        } else
        !            85:                VERB(1) printf("Verbose:: Opened log %s\n", csLog);
1.1.2.4   misho      86:        end = lseek(f, 0, SEEK_END);
                     87:        if (end == -1) {
                     88:                printf("Error:: in get size %s #%d - %s\n", csLog, errno, strerror(errno));
1.1.2.5   misho      89:                close(f);
                     90:                return -1;
1.1.2.4   misho      91:        } else
                     92:                lseek(f, 0, SEEK_SET);
1.1.2.7 ! misho      93:        VERB(2) printf("Verbose(2):: Get file size %u\n", end);
1.1.2.5   misho      94:        buffer = mmap(NULL, end, PROT_READ, MAP_SHARED, f, 0);
                     95:        if (!buffer) {
                     96:                printf("Error:: in map %s #%d - %s\n", csLog, errno, strerror(errno));
                     97:                close(f);
                     98:                return -1;
                     99:        } else {
                    100:                close(f);
1.1.2.4   misho     101: 
1.1.2.7 ! misho     102:                cf = (struct clogFooter*) (buffer + end - sizeof(struct clogFooter));
        !           103:                VERB(3) printf("Verbose(3):: Map file to address %p and footer %p\n", buffer, cf);
1.1.2.5   misho     104:        }
                    105: 
                    106:        if (cf->cf_wrap == 1)
                    107:                start = cf->cf_next + 1;
1.1.2.7 ! misho     108:        VERB(3) printf("Verbose(3):: wrapped log? %d\n", cf->cf_wrap);
1.1.2.5   misho     109:        do {
                    110:                cx = 0;
                    111:                while (cf->cf_lock == 1)
                    112:                        sched_yield();
                    113:                next = cf->cf_next;
                    114: 
                    115:                if (start > next) {
                    116:                        iov[cx].iov_base = buffer + start;
                    117:                        iov[cx++].iov_len = cf->cf_max - start;
                    118:                        start = 0;
                    119:                }
                    120:                iov[cx].iov_base = buffer + start;
                    121:                iov[cx++].iov_len = next - start;
                    122:                start = next;
                    123:                writev(1, iov, cx);
1.1.2.6   misho     124:                if (!(m & 2))
1.1.2.5   misho     125:                        break;
                    126:                if (poll(&pfd, 1, 50) == -1) {
                    127:                        printf("Error:: in poll %s #%d - %s\n", csLog, errno, strerror(errno));
                    128:                        munmap(buffer, end);
                    129:                        return -1;
                    130:                }
                    131:        } while (42);
                    132: 
                    133:        munmap(buffer, end);
1.1.2.7 ! misho     134:        VERB(1) printf("Verbose:: Read done.\n");
1.1.2.5   misho     135:        return 0;
1.1.2.3   misho     136: }
                    137: 
1.1.2.1   misho     138: 
                    139: int
                    140: main(int argc, char **argv)
                    141: {
1.1.2.2   misho     142:        char ch, m = 0, szLog[MAXPATHLEN];
1.1.2.3   misho     143:        size_t siz = 0;
1.1.2.1   misho     144: 
1.1.2.7 ! misho     145:        while ((ch = getopt(argc, argv, "hvfis:")) != -1)
1.1.2.1   misho     146:                switch (ch) {
1.1.2.7 ! misho     147:                        case 'v':
        !           148:                                Verbose++;
        !           149:                                break;
1.1.2.2   misho     150:                        case 'i':
                    151:                                m |= 1;
                    152:                                break;
                    153:                        case 'f':
                    154:                                m |= 2;
1.1.2.6   misho     155:                                break;
1.1.2.2   misho     156:                        case 's':
1.1.2.3   misho     157:                                siz = strtol(optarg, NULL, 0);
1.1.2.2   misho     158:                                if (siz < 1) {
1.1.2.3   misho     159:                                        printf("Error:: size is invalid %u!\n", siz);
1.1.2.2   misho     160:                                        Usage();
                    161:                                        return 1;
                    162:                                }
                    163:                                break;
1.1.2.1   misho     164:                        case 'h':
                    165:                        default:
                    166:                                Usage();
                    167:                                return 1;
                    168:                }
                    169:        argc -= optind;
                    170:        argv += optind;
1.1.2.2   misho     171:        if (m & 1 && !siz) {
                    172:                printf("Error:: not specified size when use with init log!\n");
                    173:                Usage();
                    174:                return 1;
                    175:        }
1.1.2.3   misho     176:        if (m == 3) {
                    177:                printf("Error:: can`t in same time init and force!\n");
                    178:                Usage();
                    179:                return 1;
                    180:        }
1.1.2.2   misho     181:        if (!argc) {
                    182:                printf("Error:: not specified log file!\n");
                    183:                Usage();
                    184:                return 1;
                    185:        } else
                    186:                strlcpy(szLog, *argv, MAXPATHLEN);
1.1.2.1   misho     187: 
1.1.2.3   misho     188:        if (m & 1 && initlog(szLog, siz) == -1)
                    189:                return 2;
1.1.2.4   misho     190:        if (readlog(szLog, m) == -1)
                    191:                return 3;
1.1.2.3   misho     192: 
1.1.2.1   misho     193:        return 0;
                    194: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>