1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "text_object.h"
36 #include <sys/types.h>
40 static void *memrchr(const void *buffer, char c, size_t n)
42 const unsigned char *p = buffer;
44 for (p += n; n; n--) {
53 int init_tailhead_object(enum tailhead_type type,
54 struct text_object *obj, const char *arg)
64 /* FIXME: use #define for that */
65 me = (type == TAIL) ? "tail" : "head";
68 ERR("%s needs arguments", me);
72 numargs = sscanf(arg, "%63s %i %i", buf, &n1, &n2);
74 if (numargs < 2 || numargs > 3) {
75 ERR("incorrect number of arguments given to %s object", me);
79 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
80 ERR("invalid arg for %s, number of lines must be "
81 "between 1 and %i", me, MAX_TAIL_LINES);
85 obj->data.tail.fd = -1;
90 if (stat(buf, &st) == 0) {
91 if (S_ISFIFO(st.st_mode)) {
92 fd = open(buf, O_RDONLY | O_NONBLOCK);
95 ERR("%s logfile does not exist, or you do "
96 "not have correct permissions", me);
100 obj->data.tail.fd = fd;
103 fp = fopen(buf, "r");
107 if (fp || obj->data.tail.fd != -1) {
108 obj->data.tail.logfile = malloc(text_buffer_size);
109 strcpy(obj->data.tail.logfile, buf);
110 obj->data.tail.wantedlines = n1;
111 obj->data.tail.interval = update_interval * 2;
113 if (obj->data.tail.fd == -1) {
118 ERR("%s logfile does not exist, or you do not have "
119 "correct permissions", me);
122 /* XXX: the following implies update_interval >= 1 ?! */
123 if (numargs == 3 && (n2 < 1 || n2 < update_interval)) {
124 ERR("%s interval must be greater than "
125 "0 and "PACKAGE_NAME"'s interval, ignoring", me);
126 } else if (numargs == 3) {
127 obj->data.tail.interval = n2;
129 /* asumming all else worked */
130 obj->data.tail.buffer = malloc(text_buffer_size * 20);
134 /* Allows reading from a FIFO (i.e., /dev/xconsole).
135 * The file descriptor is set to non-blocking which makes this possible.
137 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
138 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
140 #define TAIL_PIPE_BUFSIZE 4096
144 int fd = obj->data.tail.fd;
147 char buf[TAIL_PIPE_BUFSIZE];
148 ssize_t len = read(fd, buf, sizeof(buf));
152 if (errno != EAGAIN) {
153 strcpy(obj->data.tail.buffer, "Logfile Read Error");
154 snprintf(dst, dst_size, "Logfile Read Error");
158 } else if (len == 0) {
159 strcpy(obj->data.tail.buffer, "Logfile Empty");
160 snprintf(dst, dst_size, "Logfile Empty");
164 for (line_len = 0, i = 0; i < len; i++) {
168 if (buf[i] == '\n') {
171 if (obj->data.tail.readlines > 0) {
176 for (n = 0; obj->data.tail.buffer[n]; n++) {
177 if (obj->data.tail.buffer[n] == '\n') {
182 if (++olines < obj->data.tail.wantedlines) {
188 p = obj->data.tail.buffer + first_line;
189 pos = n - first_line;
190 memmove(obj->data.tail.buffer,
191 obj->data.tail.buffer + first_line, strlen(p));
192 obj->data.tail.buffer[pos] = 0;
200 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
201 obj->data.tail.buffer[pos + line_len] = 0;
204 obj->data.tail.readlines = lines;
212 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
215 static long rev_fcharfind(FILE *fp, char val, unsigned int step)
219 unsigned int count = 0;
220 static char buf[BUFSZ];
221 long orig_pos = ftell(fp);
223 long file_pos = orig_pos;
224 long buf_size = BUFSZ;
227 while (count < step) {
229 if (file_pos > BUFSZ) {
230 fseek(fp, file_pos - BUFSZ, SEEK_SET);
233 fseek(fp, 0, SEEK_SET);
235 file_pos = ftell(fp);
236 buf_pos = fread(buf, 1, buf_size, fp);
238 cur_found = memrchr(buf, val, (size_t) buf_pos);
239 if (cur_found != NULL) {
240 buf_pos = cur_found - buf;
249 fseek(fp, orig_pos, SEEK_SET);
251 ret = file_pos + buf_pos;
256 int print_tail_object(struct text_object *obj, char *p, size_t p_max_size)
262 if (current_update_time - obj->data.tail.last_update < obj->data.tail.interval) {
263 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
267 obj->data.tail.last_update = current_update_time;
269 if (obj->data.tail.fd != -1) {
270 tail_pipe(obj, p, p_max_size);
274 fp = fopen(obj->data.tail.logfile, "rt");
276 /* Send one message, but do not consistently spam
277 * on missing logfiles. */
278 if (obj->data.tail.readlines != 0) {
279 ERR("tail logfile failed to open");
280 strcpy(obj->data.tail.buffer, "Logfile Missing");
282 obj->data.tail.readlines = 0;
283 snprintf(p, p_max_size, "Logfile Missing");
285 obj->data.tail.readlines = 0;
286 /* -1 instead of 0 to avoid counting a trailing
288 fseek(fp, -1, SEEK_END);
289 bsize = ftell(fp) + 1;
290 for (iter = obj->data.tail.wantedlines; iter > 0;
292 nl = rev_fcharfind(fp, '\n', iter);
297 obj->data.tail.readlines = iter;
298 if (obj->data.tail.readlines
299 < obj->data.tail.wantedlines) {
300 fseek(fp, 0, SEEK_SET);
302 fseek(fp, nl + 1, SEEK_SET);
305 /* Make sure bsize is at least 1 byte smaller than the
306 * buffer max size. */
307 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
308 fseek(fp, bsize - text_buffer_size * 20 - 1,
310 bsize = text_buffer_size * 20 - 1;
312 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
315 /* Clean up trailing newline, make sure the
316 * buffer is null terminated. */
317 if (obj->data.tail.buffer[bsize - 1] == '\n') {
318 obj->data.tail.buffer[bsize - 1] = '\0';
320 obj->data.tail.buffer[bsize] = '\0';
322 snprintf(p, p_max_size, "%s",
323 obj->data.tail.buffer);
325 strcpy(obj->data.tail.buffer, "Logfile Empty");
326 snprintf(p, p_max_size, "Logfile Empty");
332 long fwd_fcharfind(FILE *fp, char val, unsigned int step)
336 unsigned int count = 0;
337 static char buf[BUFSZ];
338 long orig_pos = ftell(fp);
340 long buf_size = BUFSZ;
341 char *cur_found = NULL;
343 while (count < step) {
344 if (cur_found == NULL) {
345 buf_size = fread(buf, 1, buf_size, fp);
348 cur_found = memchr(buf + buf_pos, val, buf_size - buf_pos);
349 if (cur_found != NULL) {
350 buf_pos = cur_found - buf + 1;
359 ret = ftell(fp) - buf_size + buf_pos - 1;
361 fseek(fp, orig_pos, SEEK_SET);
365 int print_head_object(struct text_object *obj, char *p, size_t p_max_size)
371 if (current_update_time - obj->data.tail.last_update < obj->data.tail.interval) {
372 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
376 obj->data.tail.last_update = current_update_time;
378 fp = fopen(obj->data.tail.logfile, "rt");
380 /* Send one message, but do not consistently spam
381 * on missing logfiles. */
382 if (obj->data.tail.readlines != 0) {
383 ERR("head logfile failed to open");
384 strcpy(obj->data.tail.buffer, "Logfile Missing");
386 obj->data.tail.readlines = 0;
387 snprintf(p, p_max_size, "Logfile Missing");
389 obj->data.tail.readlines = 0;
390 for (iter = obj->data.tail.wantedlines; iter > 0;
392 nl = fwd_fcharfind(fp, '\n', iter);
397 obj->data.tail.readlines = iter;
398 /* Make sure nl is at least 1 byte smaller than the
399 * buffer max size. */
400 if (nl > (long) ((text_buffer_size * 20) - 1)) {
401 nl = text_buffer_size * 20 - 1;
403 nl = fread(obj->data.tail.buffer, 1, nl, fp);
406 /* Clean up trailing newline, make sure the buffer
407 * is null terminated. */
408 if (obj->data.tail.buffer[nl - 1] == '\n') {
409 obj->data.tail.buffer[nl - 1] = '\0';
411 obj->data.tail.buffer[nl] = '\0';
413 snprintf(p, p_max_size, "%s",
414 obj->data.tail.buffer);
416 strcpy(obj->data.tail.buffer, "Logfile Empty");
417 snprintf(p, p_max_size, "Logfile Empty");
419 } /* if fp == null */