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

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

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