| 1 |
/** |
| 2 |
* collectd - src/utils_tail.c |
| 3 |
* Copyright (C) 2007-2008 C-Ware, Inc. |
| 4 |
* Copyright (C) 2008 Florian Forster |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 7 |
* copy of this software and associated documentation files (the "Software"), |
| 8 |
* to deal in the Software without restriction, including without limitation |
| 9 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 11 |
* Software is furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
* |
| 24 |
* Author: |
| 25 |
* Luke Heberling <lukeh at c-ware.com> |
| 26 |
* Florian Forster <octo at collectd.org> |
| 27 |
* |
| 28 |
* Description: |
| 29 |
* Encapsulates useful code for plugins which must watch for appends to |
| 30 |
* the end of a file. |
| 31 |
**/ |
| 32 |
|
| 33 |
#include "collectd.h" |
| 34 |
|
| 35 |
#include "utils/common/common.h" |
| 36 |
#include "utils/tail/tail.h" |
| 37 |
|
| 38 |
struct cu_tail_s { |
| 39 |
char *file; |
| 40 |
FILE *fh; |
| 41 |
struct stat stat; |
| 42 |
}; |
| 43 |
|
| 44 |
static int cu_tail_reopen(cu_tail_t *obj, bool force_rewind) { |
| 45 |
int seek_end = 0; |
| 46 |
struct stat stat_buf = {0}; |
| 47 |
|
| 48 |
int status = stat(obj->file, &stat_buf); |
| 49 |
if (status != 0) { |
| 50 |
P_ERROR("utils_tail: stat (%s) failed: %s", obj->file, STRERRNO); |
| 51 |
return -1; |
| 52 |
} |
| 53 |
|
| 54 |
/* The file is already open.. */ |
| 55 |
if ((obj->fh != NULL) && (stat_buf.st_ino == obj->stat.st_ino)) { |
| 56 |
/* Seek to the beginning if file was truncated */ |
| 57 |
if (stat_buf.st_size < obj->stat.st_size) { |
| 58 |
P_INFO("utils_tail: File `%s' was truncated.", obj->file); |
| 59 |
status = fseek(obj->fh, 0, SEEK_SET); |
| 60 |
if (status != 0) { |
| 61 |
P_ERROR("utils_tail: fseek (%s) failed: %s", obj->file, STRERRNO); |
| 62 |
fclose(obj->fh); |
| 63 |
obj->fh = NULL; |
| 64 |
return -1; |
| 65 |
} |
| 66 |
} |
| 67 |
memcpy(&obj->stat, &stat_buf, sizeof(struct stat)); |
| 68 |
return 1; |
| 69 |
} |
| 70 |
|
| 71 |
/* Unless flag for rewinding to the start is set, seek to the end |
| 72 |
* if we re-open the same file again or the file opened is the first at all |
| 73 |
* or the first after an error */ |
| 74 |
if ((obj->stat.st_ino == 0) || (obj->stat.st_ino == stat_buf.st_ino)) |
| 75 |
seek_end = !force_rewind; |
| 76 |
|
| 77 |
FILE *fh = fopen(obj->file, "r"); |
| 78 |
if (fh == NULL) { |
| 79 |
P_ERROR("utils_tail: fopen (%s) failed: %s", obj->file, STRERRNO); |
| 80 |
return -1; |
| 81 |
} |
| 82 |
|
| 83 |
if (seek_end != 0) { |
| 84 |
status = fseek(fh, 0, SEEK_END); |
| 85 |
if (status != 0) { |
| 86 |
P_ERROR("utils_tail: fseek (%s) failed: %s", obj->file, STRERRNO); |
| 87 |
fclose(fh); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (obj->fh != NULL) |
| 93 |
fclose(obj->fh); |
| 94 |
obj->fh = fh; |
| 95 |
memcpy(&obj->stat, &stat_buf, sizeof(struct stat)); |
| 96 |
|
| 97 |
return 0; |
| 98 |
} /* int cu_tail_reopen */ |
| 99 |
|
| 100 |
cu_tail_t *cu_tail_create(const char *file) { |
| 101 |
cu_tail_t *obj; |
| 102 |
|
| 103 |
obj = calloc(1, sizeof(*obj)); |
| 104 |
if (obj == NULL) |
| 105 |
return NULL; |
| 106 |
|
| 107 |
obj->file = strdup(file); |
| 108 |
if (obj->file == NULL) { |
| 109 |
free(obj); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
obj->fh = NULL; |
| 114 |
|
| 115 |
return obj; |
| 116 |
} /* cu_tail_t *cu_tail_create */ |
| 117 |
|
| 118 |
int cu_tail_destroy(cu_tail_t *obj) { |
| 119 |
if (obj->fh != NULL) |
| 120 |
fclose(obj->fh); |
| 121 |
free(obj->file); |
| 122 |
free(obj); |
| 123 |
|
| 124 |
return 0; |
| 125 |
} /* int cu_tail_destroy */ |
| 126 |
|
| 127 |
int cu_tail_readline(cu_tail_t *obj, char *buf, int buflen, bool force_rewind) { |
| 128 |
int status; |
| 129 |
|
| 130 |
if (buflen < 1) { |
| 131 |
ERROR("utils_tail: cu_tail_readline: buflen too small: %i bytes.", buflen); |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
if (obj->fh == NULL) { |
| 136 |
status = cu_tail_reopen(obj, force_rewind); |
| 137 |
if (status < 0) |
| 138 |
return status; |
| 139 |
} |
| 140 |
assert(obj->fh != NULL); |
| 141 |
|
| 142 |
/* Try to read from the filehandle. If that succeeds, everything appears to |
| 143 |
* be fine and we can return. */ |
| 144 |
clearerr(obj->fh); |
| 145 |
if (fgets(buf, buflen, obj->fh) != NULL) { |
| 146 |
buf[buflen - 1] = '\0'; |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
/* Check if we encountered an error */ |
| 151 |
if (ferror(obj->fh) != 0) { |
| 152 |
/* Jupp, error. Force `cu_tail_reopen' to reopen the file.. */ |
| 153 |
fclose(obj->fh); |
| 154 |
obj->fh = NULL; |
| 155 |
} |
| 156 |
/* else: eof -> check if the file was moved away and reopen the new file if |
| 157 |
* so.. */ |
| 158 |
|
| 159 |
status = cu_tail_reopen(obj, force_rewind); |
| 160 |
/* error -> return with error */ |
| 161 |
if (status < 0) |
| 162 |
return status; |
| 163 |
/* file end reached and file not reopened -> nothing more to read */ |
| 164 |
else if (status > 0) { |
| 165 |
buf[0] = 0; |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
/* If we get here: file was re-opened and there may be more to read.. Let's |
| 170 |
* try again. */ |
| 171 |
if (fgets(buf, buflen, obj->fh) != NULL) { |
| 172 |
buf[buflen - 1] = '\0'; |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
if (ferror(obj->fh) != 0) { |
| 177 |
WARNING("utils_tail: fgets (%s) returned an error: %s", obj->file, |
| 178 |
STRERRNO); |
| 179 |
fclose(obj->fh); |
| 180 |
obj->fh = NULL; |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
/* EOf, well, apparently the new file is empty.. */ |
| 185 |
buf[0] = 0; |
| 186 |
return 0; |
| 187 |
} /* int cu_tail_readline */ |
| 188 |
|
| 189 |
int cu_tail_read(cu_tail_t *obj, char *buf, int buflen, tailfunc_t *callback, |
| 190 |
void *data, bool force_rewind) { |
| 191 |
int status; |
| 192 |
|
| 193 |
while (42) { |
| 194 |
size_t len; |
| 195 |
|
| 196 |
status = cu_tail_readline(obj, buf, buflen, force_rewind); |
| 197 |
if (status != 0) { |
| 198 |
ERROR("utils_tail: cu_tail_read: cu_tail_readline " |
| 199 |
"failed."); |
| 200 |
break; |
| 201 |
} |
| 202 |
|
| 203 |
/* check for EOF */ |
| 204 |
if (buf[0] == 0) |
| 205 |
break; |
| 206 |
|
| 207 |
len = strlen(buf); |
| 208 |
while (len > 0) { |
| 209 |
if (buf[len - 1] != '\n') |
| 210 |
break; |
| 211 |
buf[len - 1] = '\0'; |
| 212 |
len--; |
| 213 |
} |
| 214 |
|
| 215 |
status = callback(data, buf, buflen); |
| 216 |
if (status != 0) { |
| 217 |
ERROR("utils_tail: cu_tail_read: callback returned " |
| 218 |
"status %i.", |
| 219 |
status); |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return status; |
| 225 |
} /* int cu_tail_read */ |