1: #include "global.h"
2: #include "clog.h"
3:
4:
5: int Verbose;
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"
16: "\t-v\t\tVerbose, more -vv more verbosity\n"
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"
19: "\t-m <perm>\tUse permissions with init for set file.log\n"
20: "\t-f\t\tRead and follow log file.log\n"
21: "\n", compiled, compiledby, compilehost);
22: }
23:
24: static int
25: initlog(const char *csLog, size_t size, int mode)
26: {
27: int f, ret = -1, fill = size;
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:
34: f = open(csLog, O_WRONLY | O_CREAT | O_TRUNC, mode);
35: if (f == -1) {
36: printf("Error:: in open log %s #%d - %s\n", csLog, errno, strerror(errno));
37: return -1;
38: } else
39: VERB(1) printf("Verbose:: Create log %s\n", csLog);
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:
63: ret = 0;
64: VERB(1) printf("Verbose:: Init done.\n");
65: end:
66: close(f);
67: return ret;
68: }
69:
70: static int
71: readlog(const char *csLog, char m)
72: {
73: int f, cx;
74: size_t end;
75: struct clogFooter *cf;
76: char *buffer = NULL;
77: uint32_t next, start = 0;
78: struct iovec iov[2];
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;
85: } else
86: VERB(1) printf("Verbose:: Opened log %s\n", csLog);
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));
90: close(f);
91: return -1;
92: } else
93: lseek(f, 0, SEEK_SET);
94: VERB(2) printf("Verbose(2):: Get file size %lu\n", (u_long) end);
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);
102:
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);
105: }
106:
107: if (cf->cf_wrap == 1)
108: start = cf->cf_next + 1;
109: VERB(3) printf("Verbose(3):: wrapped log? %d\n", cf->cf_wrap);
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);
125: if (!(m & 2))
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);
135: VERB(1) printf("Verbose:: Read done.\n");
136: return 0;
137: }
138:
139:
140: int
141: main(int argc, char **argv)
142: {
143: char ch, m = 0, szLog[MAXPATHLEN];
144: size_t siz = 0;
145: int mode = 0644;
146:
147: while ((ch = getopt(argc, argv, "hm:vfis:")) != -1)
148: switch (ch) {
149: case 'm':
150: mode = strtol(optarg, NULL, 0);
151: break;
152: case 'v':
153: Verbose++;
154: break;
155: case 'i':
156: m |= 1;
157: break;
158: case 'f':
159: m |= 2;
160: break;
161: case 's':
162: siz = strtol(optarg, NULL, 0);
163: if (siz < 1) {
164: printf("Error:: size is invalid %lu!\n", (u_long) siz);
165: Usage();
166: return 1;
167: }
168: break;
169: case 'h':
170: default:
171: Usage();
172: return 1;
173: }
174: argc -= optind;
175: argv += optind;
176: if (m & 1 && !siz) {
177: printf("Error:: not specified size when use with init log!\n");
178: Usage();
179: return 1;
180: }
181: if (m == 3) {
182: printf("Error:: can`t in same time init and force!\n");
183: Usage();
184: return 1;
185: }
186: if (!argc) {
187: printf("Error:: not specified log file!\n");
188: Usage();
189: return 1;
190: } else
191: strlcpy(szLog, *argv, MAXPATHLEN);
192:
193: if (m & 1 && initlog(szLog, siz, mode) == -1)
194: return 2;
195: if (readlog(szLog, m) == -1)
196: return 3;
197:
198: return 0;
199: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>