2 * Routines for packet capture windows
4 * $Id: ringbuffer.c,v 1.11 2004/06/29 20:51:26 ulfl Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * <laurent.deniel@free.fr>
28 * Almost completely rewritten in order to:
30 * - be able to use a unlimited number of ringbuffer files
31 * - close the current file and open (truncating) the next file at switch
32 * - set the final file name once open (or reopen)
33 * - avoid the deletion of files that could not be truncated (can't arise now)
34 * and do not erase empty files
36 * The idea behind that is to remove the limitation of the maximum # of
37 * ringbuffer files being less than the maximum # of open fd per process
38 * and to be able to reduce the amount of virtual memory usage (having only
39 * one file open at most) or the amount of file system usage (by truncating
40 * the files at switch and not the capture stop, and by closing them which
41 * makes possible their move or deletion after a switch).
59 #ifdef HAVE_SYS_STAT_H
72 #ifdef NEED_SNPRINTF_H
76 #include "wiretap/wtap.h"
77 #include "ringbuffer.h"
79 /* Ringbuffer file structure */
80 typedef struct _rb_file {
84 /* Ringbuffer data structure */
85 typedef struct _ringbuf_data {
87 guint num_files; /* Number of ringbuffer files */
88 guint curr_file_num; /* Number of the current file */
89 gchar *fprefix; /* Filename prefix */
90 gchar *fsuffix; /* Filename suffix */
91 gboolean unlimited; /* TRUE if unlimited number of files */
96 int fd; /* Current ringbuffer file descriptor */
100 static ringbuf_data rb_data;
102 static int ringbuf_open_file(rb_file *rfile, int *err)
108 if (rfile->name != NULL) {
109 if (rb_data.unlimited == FALSE) {
110 /* remove old file (if any, so ignore error) */
119 current_time = time(NULL);
123 snprintf(filenum, sizeof(filenum), "%05d", rb_data.number);
124 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
125 rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
126 rb_data.fsuffix, NULL);
128 if (rfile->name == NULL) {
133 rb_data.fd = open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT, 0600);
135 if (rb_data.fd == -1 && err != NULL) {
143 * Initialize the ringbuffer data structures
146 ringbuf_init(const char *capfile_name, guint num_files)
149 char *pfx, *last_pathsep;
152 rb_data.files = NULL;
153 rb_data.curr_file_num = 0;
154 rb_data.fprefix = NULL;
155 rb_data.fsuffix = NULL;
156 rb_data.unlimited = FALSE;
161 /* just to be sure ... */
162 if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
163 rb_data.num_files = num_files;
165 rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
168 /* Check file name */
169 if (capfile_name == NULL) {
170 /* ringbuffer does not work with temporary files! */
174 /* set file name prefix/suffix */
176 save_file = g_strdup(capfile_name);
177 last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
178 pfx = strrchr(save_file,'.');
179 if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
180 /* The pathname has a "." in it, and it's in the last component
181 of the pathname (because there is either only one component,
182 i.e. last_pathsep is null as there are no path separators,
183 or the "." is after the path separator before the last
186 Treat it as a separator between the rest of the file name and
187 the file name suffix, and arrange that the names given to the
188 ring buffer files have the specified suffix, i.e. put the
189 changing part of the name *before* the suffix. */
191 rb_data.fprefix = g_strdup(save_file);
192 pfx[0] = '.'; /* restore capfile_name */
193 rb_data.fsuffix = g_strdup(pfx);
195 /* Either there's no "." in the pathname, or it's in a directory
196 component, so the last component has no suffix. */
197 rb_data.fprefix = g_strdup(save_file);
198 rb_data.fsuffix = NULL;
203 /* allocate rb_file structures (only one if unlimited since there is no
204 need to save all file names in that case) */
206 if (num_files == RINGBUFFER_UNLIMITED_FILES) {
207 rb_data.unlimited = TRUE;
208 rb_data.num_files = 1;
211 rb_data.files = g_malloc(rb_data.num_files * sizeof(rb_file));
212 if (rb_data.files == NULL) {
216 for (i=0; i < rb_data.num_files; i++) {
217 rb_data.files[i].name = NULL;
220 /* create the first file */
221 if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
222 ringbuf_error_cleanup();
230 * Calls wtap_dump_fdopen() for the current ringbuffer file
233 ringbuf_init_wtap_dump_fdopen(int filetype, int linktype, int snaplen, int *err)
236 rb_data.filetype = filetype;
237 rb_data.linktype = linktype;
238 rb_data.snaplen = snaplen;
240 rb_data.pdh = wtap_dump_fdopen(rb_data.fd, filetype, linktype, snaplen, err);
246 * Switches to the next ringbuffer file
249 ringbuf_switch_file(capture_file *cf, wtap_dumper **pdh, int *err)
252 rb_file *next_rfile = NULL;
254 /* close current file */
256 if (!wtap_dump_close(rb_data.pdh, err)) {
257 close(rb_data.fd); /* XXX - the above should have closed this already */
258 rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
266 /* get the next file number and open it */
268 next_file_num = (rb_data.curr_file_num + 1) % rb_data.num_files;
269 next_rfile = &rb_data.files[next_file_num];
271 if (ringbuf_open_file(next_rfile, err) == -1) {
275 if (ringbuf_init_wtap_dump_fdopen(rb_data.filetype, rb_data.linktype,
276 rb_data.snaplen, err) == NULL) {
280 /* switch to the new file */
281 rb_data.curr_file_num = next_file_num;
282 cf->save_file = next_rfile->name;
283 cf->save_file_fd = rb_data.fd;
284 (*pdh) = rb_data.pdh;
290 * Calls wtap_dump_close() for the current ringbuffer file
293 ringbuf_wtap_dump_close(capture_file *cf, int *err)
295 gboolean ret_val = TRUE;
297 /* close current file, if it's open */
298 if (rb_data.pdh != NULL) {
299 if (!wtap_dump_close(rb_data.pdh, err)) {
308 /* set the save file name to the current file */
309 cf->save_file = rb_data.files[rb_data.curr_file_num].name;
314 * Frees all memory allocated by the ringbuffer
321 if (rb_data.files != NULL) {
322 for (i=0; i < rb_data.num_files; i++) {
323 if (rb_data.files[i].name != NULL) {
324 g_free(rb_data.files[i].name);
325 rb_data.files[i].name = NULL;
328 g_free(rb_data.files);
329 rb_data.files = NULL;
331 if (rb_data.fprefix != NULL) {
332 g_free(rb_data.fprefix);
333 rb_data.fprefix = NULL;
335 if (rb_data.fsuffix != NULL) {
336 g_free(rb_data.fsuffix);
337 rb_data.fsuffix = NULL;
342 * Frees all memory allocated by the ringbuffer
345 ringbuf_error_cleanup(void)
349 /* try to close via wtap */
350 if (rb_data.pdh != NULL) {
351 if (wtap_dump_close(rb_data.pdh, NULL)) {
357 /* close directly if still open */
358 /* XXX - it shouldn't still be open; "wtap_dump_close()" should leave the
359 file closed even if it fails */
360 if (rb_data.fd != -1) {
365 if (rb_data.files != NULL) {
366 for (i=0; i < rb_data.num_files; i++) {
367 if (rb_data.files[i].name != NULL) {
368 unlink(rb_data.files[i].name);
372 /* free the memory */
376 #endif /* HAVE_LIBPCAP */