mixer: 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 #include "text_object.h"
32 #include "logging.h"
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #define MAX_HEADTAIL_LINES 30
38 #define DEFAULT_MAX_HEADTAIL_USES 2
39
40 struct headtail {
41         int wantedlines;
42         char *logfile;
43         char *buffer;
44         int current_use;
45         int max_uses;
46         int reported;
47 };
48
49 static void tailstring(char *string, int endofstring, int wantedlines) {
50         int i, linescounted = 0;
51
52         string[endofstring] = 0;
53         if(endofstring > 0) {
54                 if(string[endofstring-1] == '\n') {     //work with or without \n at end of file
55                         string[endofstring-1] = 0;
56                 }
57                 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
58                         if(string[i] == '\n') {
59                                 linescounted++;
60                         }
61                 }
62                 if(i > 0) {
63                         strfold(string, i+2);
64                 }
65         }
66 }
67
68 void free_tailhead(struct text_object *obj)
69 {
70         struct headtail *ht = obj->data.opaque;
71         if (!ht)
72                 return;
73         if (ht->logfile)
74                 free(ht->logfile);
75         if (ht->buffer)
76                 free(ht->buffer);
77         free(obj->data.opaque);
78         obj->data.opaque = NULL;
79 }
80
81 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
82         unsigned int args;
83         struct headtail *ht;
84
85         ht = malloc(sizeof(struct headtail));
86         memset(ht, 0, sizeof(struct headtail));
87
88         ht->logfile = malloc(DEFAULT_TEXT_BUFFER_SIZE);
89         memset(ht->logfile, 0, DEFAULT_TEXT_BUFFER_SIZE);
90
91         ht->max_uses = DEFAULT_MAX_HEADTAIL_USES;
92
93         args = sscanf(arg, "%s %d %d", ht->logfile, &ht->wantedlines, &ht->max_uses);
94         if (args < 2 || args > 3) {
95                 free_tailhead(obj);
96                 CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
97         }
98         if (ht->max_uses < 1) {
99                 free_tailhead(obj);
100                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
101         }
102         if (ht->wantedlines > 0 && ht->wantedlines <= MAX_HEADTAIL_LINES) {
103                 to_real_path(ht->logfile, ht->logfile);
104                 ht->buffer = NULL;
105                 ht->current_use = 0;
106         } else {
107                 free_tailhead(obj);
108                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
109         }
110         obj->data.opaque = ht;
111 }
112
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;
115         FILE *fp;
116         struct stat st;
117         struct headtail *ht = obj->data.opaque;
118
119         if (!ht)
120                 return;
121
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) {
124                 free(ht->buffer);
125                 ht->buffer = NULL;
126                 ht->current_use = 0;
127         }
128         //use the buffer if possible
129         if(ht->buffer) {
130                 strcpy(p, ht->buffer);
131                 ht->current_use++;
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);
136                                 if(fd != -1) {
137                                         if(strcmp(type, "head") == 0) {
138                                                 for(i = 0; linescounted < ht->wantedlines; i++) {
139                                                         read(fd, p + i, 1);
140                                                         if(p[i] == '\n') {
141                                                                 linescounted++;
142                                                         }
143                                                 }
144                                                 p[i] = 0;
145                                         } else if(strcmp(type, "tail") == 0) {
146                                                 i = read(fd, p, p_max_size - 1);
147                                                 tailstring(p, i, ht->wantedlines);
148                                         } else {
149                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
150                                         }
151                                 }
152                                 close(fd);
153                         } else {
154                                 fp = open_file(ht->logfile, &ht->reported);
155                                 if(fp != NULL) {
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);
160                                                 }
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);
165                                         } else {
166                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
167                                         }
168                                         fclose(fp);
169                                 }
170                         }
171                         ht->buffer = strdup(p);
172                 } else {
173                         CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, ht->logfile);
174                 }
175         }
176         return;
177 }