1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "text_object.h"
37 #define MAX_HEADTAIL_LINES 30
38 #define DEFAULT_MAX_HEADTAIL_USES 2
49 static void tailstring(char *string, int endofstring, int wantedlines) {
50 int i, linescounted = 0;
52 string[endofstring] = 0;
54 if(string[endofstring-1] == '\n') { //work with or without \n at end of file
55 string[endofstring-1] = 0;
57 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
58 if(string[i] == '\n') {
68 void free_tailhead(struct text_object *obj)
70 struct headtail *ht = obj->data.opaque;
77 free(obj->data.opaque);
78 obj->data.opaque = NULL;
81 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
85 ht = malloc(sizeof(struct headtail));
86 memset(ht, 0, sizeof(struct headtail));
88 ht->logfile = malloc(DEFAULT_TEXT_BUFFER_SIZE);
89 memset(ht->logfile, 0, DEFAULT_TEXT_BUFFER_SIZE);
91 ht->max_uses = DEFAULT_MAX_HEADTAIL_USES;
93 args = sscanf(arg, "%s %d %d", ht->logfile, &ht->wantedlines, &ht->max_uses);
94 if (args < 2 || args > 3) {
96 CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
98 if (ht->max_uses < 1) {
100 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
102 if (ht->wantedlines > 0 && ht->wantedlines <= MAX_HEADTAIL_LINES) {
103 to_real_path(ht->logfile, ht->logfile);
108 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
110 obj->data.opaque = ht;
113 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
114 int fd, i, endofstring = 0, linescounted = 0;
117 struct headtail *ht = obj->data.opaque;
122 //empty the buffer and reset the counter if we used it the max number of times
123 if(ht->buffer && ht->current_use >= ht->max_uses - 1) {
128 //use the buffer if possible
130 strcpy(p, ht->buffer);
132 }else{ //otherwise find the needed data
133 if(stat(ht->logfile, &st) == 0) {
134 if (S_ISFIFO(st.st_mode)) {
135 fd = open_fifo(ht->logfile, &ht->reported);
137 if(strcmp(type, "head") == 0) {
138 for(i = 0; linescounted < ht->wantedlines; i++) {
145 } else if(strcmp(type, "tail") == 0) {
146 i = read(fd, p, p_max_size - 1);
147 tailstring(p, i, ht->wantedlines);
149 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
154 fp = open_file(ht->logfile, &ht->reported);
156 if(strcmp(type, "head") == 0) {
157 for(i = 0; i < ht->wantedlines; i++) {
158 fgets(p + endofstring, p_max_size - endofstring, fp);
159 endofstring = strlen(p);
161 } else if(strcmp(type, "tail") == 0) {
162 fseek(fp, - p_max_size, SEEK_END);
163 i = fread(p, 1, p_max_size - 1, fp);
164 tailstring(p, i, ht->wantedlines);
166 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
171 ht->buffer = strdup(p);
173 CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, ht->logfile);