Of course I forgot to test the normal/default case (where no --with arguments
[metze/wireshark/wip.git] / ringbuffer.c
index 94efe0b5c710801bcf0f7925e5335c9e24f10a4f..f9f1e6d67883f2d76d57fc13c0d09c30555596bd 100644 (file)
@@ -19,7 +19,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 /*
@@ -42,9 +42,7 @@
  *
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #ifdef HAVE_LIBPCAP
 
@@ -65,7 +63,6 @@
 
 #include <glib.h>
 
-#include "pcapio.h"
 #include "ringbuffer.h"
 #include <wsutil/file_util.h>
 
@@ -86,6 +83,7 @@ typedef struct _ringbuf_data {
 
   int           fd;                 /* Current ringbuffer file descriptor */
   FILE         *pdh;
+  gboolean      group_read_access;   /* TRUE if files need to be opened with group read access */
 } ringbuf_data;
 
 static ringbuf_data rb_data;
@@ -113,17 +111,19 @@ static int ringbuf_open_file(rb_file *rfile, int *err)
 #endif
   current_time = time(NULL);
 
-  g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % 100000);
+  g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % RINGBUFFER_MAX_NUM_FILES);
   strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
   rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
                            rb_data.fsuffix, NULL);
 
   if (rfile->name == NULL) {
-    *err = ENOMEM;
+    if (err != NULL)
+      *err = ENOMEM;
     return -1;
   }
 
-  rb_data.fd = ws_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT, 0600);
+  rb_data.fd = ws_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT, 
+                            rb_data.group_read_access ? 0640 : 0600);
 
   if (rb_data.fd == -1 && err != NULL) {
     *err = errno;
@@ -136,7 +136,7 @@ static int ringbuf_open_file(rb_file *rfile, int *err)
  * Initialize the ringbuffer data structures
  */
 int
-ringbuf_init(const char *capfile_name, guint num_files)
+ringbuf_init(const char *capfile_name, guint num_files, gboolean group_read_access)
 {
   unsigned int i;
   char        *pfx, *last_pathsep;
@@ -149,6 +149,7 @@ ringbuf_init(const char *capfile_name, guint num_files)
   rb_data.unlimited = FALSE;
   rb_data.fd = -1;
   rb_data.pdh = NULL;
+  rb_data.group_read_access = group_read_access;
 
   /* just to be sure ... */
   if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
@@ -225,12 +226,17 @@ const gchar *ringbuf_current_filename(void)
 }
 
 /*
- * Calls libpcap_fdopen() for the current ringbuffer file
+ * Calls ws_fdopen() for the current ringbuffer file
  */
 FILE *
 ringbuf_init_libpcap_fdopen(int *err)
 {
-  rb_data.pdh = libpcap_fdopen(rb_data.fd, err);
+  rb_data.pdh = ws_fdopen(rb_data.fd, "wb");
+  if (rb_data.pdh == NULL) {
+    if (err != NULL) {
+      *err = errno;
+    }
+  }
   return rb_data.pdh;
 }
 
@@ -245,7 +251,10 @@ ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
 
   /* close current file */
 
-  if (!libpcap_dump_close(rb_data.pdh, err)) {
+  if (fclose(rb_data.pdh) == EOF) {
+    if (err != NULL) {
+      *err = errno;
+    }
     ws_close(rb_data.fd);      /* XXX - the above should have closed this already */
     rb_data.pdh = NULL;        /* it's still closed, we just got an error while closing */
     rb_data.fd = -1;
@@ -278,7 +287,7 @@ ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
 }
 
 /*
- * Calls libpcap_dump_close() for the current ringbuffer file
+ * Calls fclose() for the current ringbuffer file
  */
 gboolean
 ringbuf_libpcap_dump_close(gchar **save_file, int *err)
@@ -287,11 +296,13 @@ ringbuf_libpcap_dump_close(gchar **save_file, int *err)
 
   /* close current file, if it's open */
   if (rb_data.pdh != NULL) {
-    if (!libpcap_dump_close(rb_data.pdh, err)) {
+    if (fclose(rb_data.pdh) == EOF) {
+      if (err != NULL) {
+        *err = errno;
+      }
       ws_close(rb_data.fd);
       ret_val = FALSE;
     }
-
     rb_data.pdh = NULL;
     rb_data.fd  = -1;
   }
@@ -305,7 +316,7 @@ ringbuf_libpcap_dump_close(gchar **save_file, int *err)
  * Frees all memory allocated by the ringbuffer
  */
 void
-ringbuf_free()
+ringbuf_free(void)
 {
   unsigned int i;
 
@@ -339,15 +350,13 @@ ringbuf_error_cleanup(void)
 
   /* try to close via wtap */
   if (rb_data.pdh != NULL) {
-    if (libpcap_dump_close(rb_data.pdh, NULL)) {
+    if (fclose(rb_data.pdh) == 0) {
       rb_data.fd = -1;
     }
     rb_data.pdh = NULL;
   }
 
   /* close directly if still open */
-  /* XXX - it shouldn't still be open; "libpcap_dump_close()" should leave the
-     file closed even if it fails */
   if (rb_data.fd != -1) {
     ws_close(rb_data.fd);
     rb_data.fd = -1;