local_mails: convert to generic object payload
[monky] / src / tailhead.c
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
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
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.
21  *
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/>.
28  *
29  */
30
31 #define _GNU_SOURCE
32 #include "common.h"
33 #include "text_object.h"
34 #include "logging.h"
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #define MAX_HEADTAIL_LINES 30
41 #define DEFAULT_MAX_HEADTAIL_USES 2
42
43 struct headtail {
44         int wantedlines;
45         char *logfile;
46         char *buffer;
47         int current_use;
48         int max_uses;
49         int reported;
50 };
51
52 static void tailstring(char *string, int endofstring, int wantedlines) {
53         int i, linescounted = 0;
54
55         string[endofstring] = 0;
56         if(endofstring > 0) {
57                 if(string[endofstring-1] == '\n') {     //work with or without \n at end of file
58                         string[endofstring-1] = 0;
59                 }
60                 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
61                         if(string[i] == '\n') {
62                                 linescounted++;
63                         }
64                 }
65                 if(i > 0) {
66                         strfold(string, i+2);
67                 }
68         }
69 }
70
71 void free_tailhead(struct text_object *obj)
72 {
73         struct headtail *ht = obj->data.opaque;
74         if (!ht)
75                 return;
76         if (ht->logfile)
77                 free(ht->logfile);
78         if (ht->buffer)
79                 free(ht->buffer);
80         free(obj->data.opaque);
81         obj->data.opaque = NULL;
82 }
83
84 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
85         unsigned int args;
86         struct headtail *ht;
87
88         ht = malloc(sizeof(struct headtail));
89         memset(ht, 0, sizeof(struct headtail));
90
91         ht->logfile = malloc(DEFAULT_TEXT_BUFFER_SIZE);
92         memset(ht->logfile, 0, DEFAULT_TEXT_BUFFER_SIZE);
93
94         ht->max_uses = DEFAULT_MAX_HEADTAIL_USES;
95
96         args = sscanf(arg, "%s %d %d", ht->logfile, &ht->wantedlines, &ht->max_uses);
97         if (args < 2 || args > 3) {
98                 free_tailhead(obj);
99                 CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
100         }
101         if (ht->max_uses < 1) {
102                 free_tailhead(obj);
103                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
104         }
105         if (ht->wantedlines > 0 && ht->wantedlines <= MAX_HEADTAIL_LINES) {
106                 to_real_path(ht->logfile, ht->logfile);
107                 ht->buffer = NULL;
108                 ht->current_use = 0;
109         } else {
110                 free_tailhead(obj);
111                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
112         }
113         obj->data.opaque = ht;
114 }
115
116 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
117         int fd, i, endofstring = 0, linescounted = 0;
118         FILE *fp;
119         struct stat st;
120         struct headtail *ht = obj->data.opaque;
121
122         if (!ht)
123                 return;
124
125         //empty the buffer and reset the counter if we used it the max number of times
126         if(ht->buffer && ht->current_use >= ht->max_uses - 1) {
127                 free(ht->buffer);
128                 ht->buffer = NULL;
129                 ht->current_use = 0;
130         }
131         //use the buffer if possible
132         if(ht->buffer) {
133                 strcpy(p, ht->buffer);
134                 ht->current_use++;
135         }else{  //otherwise find the needed data
136                 if(stat(ht->logfile, &st) == 0) {
137                         if (S_ISFIFO(st.st_mode)) {
138                                 fd = open_fifo(ht->logfile, &ht->reported);
139                                 if(fd != -1) {
140                                         if(strcmp(type, "head") == 0) {
141                                                 for(i = 0; linescounted < ht->wantedlines; i++) {
142                                                         read(fd, p + i, 1);
143                                                         if(p[i] == '\n') {
144                                                                 linescounted++;
145                                                         }
146                                                 }
147                                                 p[i] = 0;
148                                         } else if(strcmp(type, "tail") == 0) {
149                                                 i = read(fd, p, p_max_size - 1);
150                                                 tailstring(p, i, ht->wantedlines);
151                                         } else {
152                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
153                                         }
154                                 }
155                                 close(fd);
156                         } else {
157                                 fp = open_file(ht->logfile, &ht->reported);
158                                 if(fp != NULL) {
159                                         if(strcmp(type, "head") == 0) {
160                                                 for(i = 0; i < ht->wantedlines; i++) {
161                                                         fgets(p + endofstring, p_max_size - endofstring, fp);
162                                                         endofstring = strlen(p);
163                                                 }
164                                         } else if(strcmp(type, "tail") == 0) {
165                                                 fseek(fp, - p_max_size, SEEK_END);
166                                                 i = fread(p, 1, p_max_size - 1, fp);
167                                                 tailstring(p, i, ht->wantedlines);
168                                         } else {
169                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
170                                         }
171                                         fclose(fp);
172                                 }
173                         }
174                         ht->buffer = strdup(p);
175                 } else {
176                         CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, ht->logfile);
177                 }
178         }
179         return;
180 }