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

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

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