2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2009-01-05 12:11:13 +00:00
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2021-02-27 15:14:19 +00:00
|
|
|
* Copyright (c) 2005-2021 Brenden Matthews, Philip Kovacs, et. al.
|
2009-01-05 12:11:13 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2009-01-05 00:27:07 +00:00
|
|
|
|
2009-07-22 20:08:31 +00:00
|
|
|
#include <fcntl.h>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <sys/stat.h>
|
2009-07-22 20:08:31 +00:00
|
|
|
#include <unistd.h>
|
2018-05-12 23:26:31 +00:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstring>
|
2010-03-08 17:13:55 +00:00
|
|
|
#include <memory>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "conky.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "text_object.h"
|
2009-01-05 00:27:07 +00:00
|
|
|
|
2009-07-21 21:41:47 +00:00
|
|
|
#define MAX_HEADTAIL_LINES 30
|
|
|
|
#define DEFAULT_MAX_HEADTAIL_USES 2
|
2009-01-05 00:27:07 +00:00
|
|
|
|
2009-10-04 12:41:51 +00:00
|
|
|
struct headtail {
|
2018-05-12 23:26:31 +00:00
|
|
|
int wantedlines{0};
|
2018-05-12 16:03:00 +00:00
|
|
|
std::string logfile;
|
2018-05-12 23:26:31 +00:00
|
|
|
char *buffer{nullptr};
|
|
|
|
int current_use{0};
|
|
|
|
int max_uses{0};
|
|
|
|
int reported{0};
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
headtail() = default;
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
~headtail() { free(buffer); }
|
2009-10-04 12:41:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void tailstring(char *string, int endofstring, int wantedlines) {
|
2018-05-12 16:03:00 +00:00
|
|
|
int i, linescounted = 0;
|
|
|
|
|
|
|
|
string[endofstring] = 0;
|
|
|
|
if (endofstring > 0) {
|
|
|
|
if (string[endofstring - 1] ==
|
|
|
|
'\n') { // work with or without \n at end of file
|
|
|
|
string[endofstring - 1] = 0;
|
|
|
|
}
|
|
|
|
for (i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
|
2019-02-23 19:40:34 +00:00
|
|
|
if (string[i] == '\n') { linescounted++; }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2019-02-23 19:40:34 +00:00
|
|
|
if (i > 0) { strfold(string, i + 2); }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2009-07-22 20:08:31 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void free_tailhead(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ht = static_cast<struct headtail *>(obj->data.opaque);
|
|
|
|
obj->data.opaque = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
delete ht;
|
2009-10-04 12:41:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void init_tailhead(const char *type, const char *arg, struct text_object *obj,
|
|
|
|
void *free_at_crash) {
|
|
|
|
unsigned int args;
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ht = new headtail;
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
std::unique_ptr<char[]> tmp(new char[DEFAULT_TEXT_BUFFER_SIZE]);
|
|
|
|
memset(tmp.get(), 0, DEFAULT_TEXT_BUFFER_SIZE);
|
|
|
|
|
|
|
|
ht->max_uses = DEFAULT_MAX_HEADTAIL_USES;
|
|
|
|
|
|
|
|
// XXX: Buffer overflow ?
|
|
|
|
args = sscanf(arg, "%s %d %d", tmp.get(), &ht->wantedlines, &ht->max_uses);
|
|
|
|
if (args < 2 || args > 3) {
|
|
|
|
free_tailhead(obj);
|
|
|
|
CRIT_ERR(obj, free_at_crash,
|
|
|
|
"%s needs a file as 1st and a number of lines as 2nd argument",
|
|
|
|
type);
|
|
|
|
}
|
|
|
|
if (ht->max_uses < 1) {
|
|
|
|
free_tailhead(obj);
|
|
|
|
CRIT_ERR(obj, free_at_crash,
|
|
|
|
"invalid arg for %s, next_check must be larger than 0", type);
|
|
|
|
}
|
|
|
|
if (ht->wantedlines > 0 && ht->wantedlines <= MAX_HEADTAIL_LINES) {
|
|
|
|
ht->logfile = to_real_path(tmp.get());
|
2018-05-12 23:26:31 +00:00
|
|
|
ht->buffer = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
ht->current_use = 0;
|
|
|
|
} else {
|
|
|
|
free_tailhead(obj);
|
|
|
|
CRIT_ERR(obj, free_at_crash,
|
|
|
|
"invalid arg for %s, number of lines must be between 1 and %d",
|
|
|
|
type, MAX_HEADTAIL_LINES);
|
|
|
|
}
|
|
|
|
obj->data.opaque = ht;
|
2009-01-05 00:27:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static void print_tailhead(const char *type, struct text_object *obj, char *p,
|
2018-08-07 23:22:23 +00:00
|
|
|
unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
int fd, i, endofstring = 0, linescounted = 0;
|
|
|
|
FILE *fp;
|
2018-05-12 23:26:31 +00:00
|
|
|
struct stat st {};
|
|
|
|
auto *ht = static_cast<struct headtail *>(obj->data.opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2019-02-23 19:40:34 +00:00
|
|
|
if (ht == nullptr) { return; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
// empty the buffer and reset the counter if we used it the max number of
|
|
|
|
// times
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((ht->buffer != nullptr) && ht->current_use >= ht->max_uses - 1) {
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(ht->buffer);
|
|
|
|
ht->current_use = 0;
|
|
|
|
}
|
|
|
|
// use the buffer if possible
|
2018-05-12 23:26:31 +00:00
|
|
|
if (ht->buffer != nullptr) {
|
2020-10-02 23:37:48 +00:00
|
|
|
strncpy(p, ht->buffer, p_max_size);
|
2018-05-12 16:03:00 +00:00
|
|
|
ht->current_use++;
|
|
|
|
} else { // otherwise find the needed data
|
|
|
|
if (stat(ht->logfile.c_str(), &st) == 0) {
|
|
|
|
if (S_ISFIFO(st.st_mode)) {
|
|
|
|
fd = open_fifo(ht->logfile.c_str(), &ht->reported);
|
|
|
|
if (fd != -1) {
|
|
|
|
if (strcmp(type, "head") == 0) {
|
|
|
|
for (i = 0; linescounted < ht->wantedlines; i++) {
|
2019-02-23 19:40:34 +00:00
|
|
|
if (read(fd, p + i, 1) <= 0) { break; }
|
|
|
|
if (p[i] == '\n') { linescounted++; }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
p[i] = 0;
|
|
|
|
} else if (strcmp(type, "tail") == 0) {
|
|
|
|
i = read(fd, p, p_max_size - 1);
|
|
|
|
tailstring(p, i, ht->wantedlines);
|
|
|
|
} else {
|
2018-05-12 23:26:31 +00:00
|
|
|
CRIT_ERR(nullptr, nullptr,
|
2018-05-12 16:03:00 +00:00
|
|
|
"If you are seeing this then there is a bug in the code, "
|
|
|
|
"report it !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
fp = open_file(ht->logfile.c_str(), &ht->reported);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (fp != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
if (strcmp(type, "head") == 0) {
|
|
|
|
for (i = 0; i < ht->wantedlines; i++) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (fgets(p + endofstring, p_max_size - endofstring, fp) ==
|
|
|
|
nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
endofstring = strlen(p);
|
|
|
|
}
|
|
|
|
} else if (strcmp(type, "tail") == 0) {
|
2019-02-23 19:43:44 +00:00
|
|
|
fseek(fp, -static_cast<long>(p_max_size), SEEK_END);
|
2018-05-12 16:03:00 +00:00
|
|
|
i = fread(p, 1, p_max_size - 1, fp);
|
|
|
|
tailstring(p, i, ht->wantedlines);
|
|
|
|
} else {
|
2018-05-12 23:26:31 +00:00
|
|
|
CRIT_ERR(nullptr, nullptr,
|
2018-05-12 16:03:00 +00:00
|
|
|
"If you are seeing this then there is a bug in the code, "
|
|
|
|
"report it !");
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ht->buffer = strdup(p);
|
|
|
|
} else {
|
2018-05-12 23:26:31 +00:00
|
|
|
CRIT_ERR(nullptr, nullptr, "$%s can't find information about %s", type,
|
2018-05-12 16:03:00 +00:00
|
|
|
ht->logfile.c_str());
|
|
|
|
}
|
|
|
|
}
|
2009-01-05 12:11:13 +00:00
|
|
|
}
|
2009-11-19 22:59:50 +00:00
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_head(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
print_tailhead("head", obj, p, p_max_size);
|
2009-11-08 16:02:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_tail(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
print_tailhead("tail", obj, p, p_max_size);
|
2009-11-08 16:02:19 +00:00
|
|
|
}
|
|
|
|
|
2009-11-19 22:59:50 +00:00
|
|
|
/* FIXME: use something more general (see also tail.c, head.c */
|
|
|
|
#define BUFSZ 0x1000
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_lines(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
static int rep = 0;
|
|
|
|
FILE *fp = open_file(obj->data.s, &rep);
|
|
|
|
char buf[BUFSZ];
|
|
|
|
int j, lines;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (fp == nullptr) {
|
2018-08-02 15:15:16 +00:00
|
|
|
snprintf(p, p_max_size, "%s", "File Unreadable");
|
2018-05-12 16:03:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-11-19 22:59:50 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
lines = 0;
|
2018-05-12 23:26:31 +00:00
|
|
|
while (fgets(buf, BUFSZ, fp) != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
for (j = 0; buf[j] != 0; j++) {
|
2019-02-23 19:40:34 +00:00
|
|
|
if (buf[j] == '\n') { lines++; }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
snprintf(p, p_max_size, "%d", lines);
|
|
|
|
fclose(fp);
|
2009-11-19 22:59:50 +00:00
|
|
|
}
|
2010-01-04 17:06:14 +00:00
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_words(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
static int rep = 0;
|
|
|
|
FILE *fp = open_file(obj->data.s, &rep);
|
|
|
|
char buf[BUFSZ];
|
|
|
|
int j, words;
|
|
|
|
char inword = 0;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (fp == nullptr) {
|
2018-08-02 15:15:16 +00:00
|
|
|
snprintf(p, p_max_size, "%s", "File Unreadable");
|
2018-05-12 16:03:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
words = 0;
|
2018-05-12 23:26:31 +00:00
|
|
|
while (fgets(buf, BUFSZ, fp) != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
for (j = 0; buf[j] != 0; j++) {
|
2019-02-23 19:43:44 +00:00
|
|
|
if (isspace(static_cast<unsigned char>(buf[j])) == 0) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (inword == 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
words++;
|
|
|
|
inword = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inword = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snprintf(p, p_max_size, "%d", words);
|
|
|
|
fclose(fp);
|
|
|
|
}
|