2 * collectd - src/utils_tail.c
3 * Copyright (C) 2007-2008 C-Ware, Inc.
4 * Copyright (C) 2008 Florian Forster
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 * Luke Heberling <lukeh at c-ware.com>
26 * Florian Forster <octo at collectd.org>
29 * Encapsulates useful code for plugins which must watch for appends to
35 #include "utils_tail.h"
44 static int cu_tail_reopen (cu_tail_t *obj)
51 memset (&stat_buf, 0, sizeof (stat_buf));
52 status = stat (obj->file, &stat_buf);
56 ERROR ("utils_tail: stat (%s) failed: %s", obj->file,
57 sstrerror (errno, errbuf, sizeof (errbuf)));
61 /* The file is already open.. */
62 if ((obj->fh != NULL) && (stat_buf.st_ino == obj->stat.st_ino))
64 /* Seek to the beginning if file was truncated */
65 if (stat_buf.st_size < obj->stat.st_size)
67 INFO ("utils_tail: File `%s' was truncated.", obj->file);
68 status = fseek (obj->fh, 0, SEEK_SET);
72 ERROR ("utils_tail: fseek (%s) failed: %s", obj->file,
73 sstrerror (errno, errbuf, sizeof (errbuf)));
79 memcpy (&obj->stat, &stat_buf, sizeof (struct stat));
83 /* Seek to the end if we re-open the same file again or the file opened
84 * is the first at all or the first after an error */
85 if ((obj->stat.st_ino == 0) || (obj->stat.st_ino == stat_buf.st_ino))
88 fh = fopen (obj->file, "r");
92 ERROR ("utils_tail: fopen (%s) failed: %s", obj->file,
93 sstrerror (errno, errbuf, sizeof (errbuf)));
99 status = fseek (fh, 0, SEEK_END);
103 ERROR ("utils_tail: fseek (%s) failed: %s", obj->file,
104 sstrerror (errno, errbuf, sizeof (errbuf)));
113 memcpy (&obj->stat, &stat_buf, sizeof (struct stat));
116 } /* int cu_tail_reopen */
118 cu_tail_t *cu_tail_create (const char *file)
122 obj = calloc (1, sizeof (*obj));
126 obj->file = strdup (file);
127 if (obj->file == NULL)
136 } /* cu_tail_t *cu_tail_create */
138 int cu_tail_destroy (cu_tail_t *obj)
146 } /* int cu_tail_destroy */
148 int cu_tail_readline (cu_tail_t *obj, char *buf, int buflen)
154 ERROR ("utils_tail: cu_tail_readline: buflen too small: %i bytes.",
161 status = cu_tail_reopen (obj);
165 assert (obj->fh != NULL);
167 /* Try to read from the filehandle. If that succeeds, everything appears to
168 * be fine and we can return. */
170 if (fgets (buf, buflen, obj->fh) != NULL)
176 /* Check if we encountered an error */
177 if (ferror (obj->fh) != 0)
179 /* Jupp, error. Force `cu_tail_reopen' to reopen the file.. */
183 /* else: eof -> check if the file was moved away and reopen the new file if
186 status = cu_tail_reopen (obj);
187 /* error -> return with error */
190 /* file end reached and file not reopened -> nothing more to read */
197 /* If we get here: file was re-opened and there may be more to read.. Let's
199 if (fgets (buf, buflen, obj->fh) != NULL)
205 if (ferror (obj->fh) != 0)
208 WARNING ("utils_tail: fgets (%s) returned an error: %s", obj->file,
209 sstrerror (errno, errbuf, sizeof (errbuf)));
215 /* EOf, well, apparently the new file is empty.. */
218 } /* int cu_tail_readline */
220 int cu_tail_read (cu_tail_t *obj, char *buf, int buflen, tailfunc_t *callback,
229 status = cu_tail_readline (obj, buf, buflen);
232 ERROR ("utils_tail: cu_tail_read: cu_tail_readline "
243 if (buf[len - 1] != '\n')
249 status = callback (data, buf, buflen);
252 ERROR ("utils_tail: cu_tail_read: callback returned "
253 "status %i.", status);
259 } /* int cu_tail_read */