Kismet Wireless

Kismet Forums

 

Posted by:jean2
Subject:Kismet patch : log update
Date:00:57:33 29/08/2007

Hi,

I started using Kismet on OpenWRT, and I discovered that
writing the network log is currently sub-optimal.
On that platform, writing the network log can take a long
time, especially when you have in excess of 150 APs. While
kismet_server is updating the log, the log file is empty. So, if you
want to use the log in a script, you are stuck, because an empty log
file is a valid (but rare) condition, and if the log file is empty you
don't have anything to fallback to. Dealing with that is a huge
headache.
Fortunately, the fix for that in Kismet is trivial, and does
not have any downside. With a few changes, while kismet_server is
updating the log, the old log is still available. And we have an
atomic update of the log.
Patch was tested with 2006-04-R1, and I verified that is patch
with minimal offset in 2007-01-R1b.

Have fun...

Jean

-----------------------------------------------------

--- packetracker.j1.cc 2007-08-28 16:53:04.000000000 -0700
+++ packetracker.cc 2007-08-28 17:07:26.000000000 -0700
@@ -1280,7 +1280,12 @@ int Packetracker::WriteNetworks(string i

FILE *netfile;

- if ((netfile = fopen(in_fname.c_str(), "w+")) == NULL) {
+ /* Test if we can open the file, using read+write mode (r+).
+ * Using write mode (w+) would truncate the file, which means that during
+ * update the log would be empty.
+ * Using apend mode (a+) would force to seek to the end of file, which
+ * is slower. Jean II */
+ if ((netfile = fopen(in_fname.c_str(), "r+")) == NULL) {
snprintf(errstr, 1024, "Could not open %s for writing: %s", in_fname.c_str(),
strerror(errno));
return -1;
@@ -1456,14 +1461,10 @@ int Packetracker::WriteNetworks(string i

fclose(netfile);

- if (unlink(in_fname.c_str()) == -1) {
- if (errno != ENOENT) {
- snprintf(errstr, 1024, "Unable to unlink %s even though we could write to it: %s",
- in_fname.c_str(), strerror(errno));
- return -1;
- }
- }
-
+ /* Move the temporary file on the regular file in one "atomic" operation.
+ * The idea here is that the log file is always available and always
+ * a complete and valid file, users of that file can access it with
+ * confidence. The old file will be unlinked automatically. Jean II */
if (rename(fname_temp.c_str(), in_fname.c_str()) == -1) {
snprintf(errstr, 1024, "Unable to rename %s to %s: %s", fname_temp.c_str(), in_fname.c_str(),
strerror(errno));
@@ -1479,7 +1480,12 @@ int Packetracker::WriteCisco(string in_f

FILE *netfile;

- if ((netfile = fopen(in_fname.c_str(), "w+")) == NULL) {
+ /* Test if we can open the file, using read+write mode (r+).
+ * Using write mode (w+) would truncate the file, which means that during
+ * update the log would be empty.
+ * Using apend mode (a+) would force to seek to the end of file, which
+ * is slower. Jean II */
+ if ((netfile = fopen(in_fname.c_str(), "r+")) == NULL) {
snprintf(errstr, 1024, "Could not open %s for writing: %s", in_fname.c_str(),
strerror(errno));
return -1;
@@ -1567,14 +1573,10 @@ int Packetracker::WriteCisco(string in_f

fclose(netfile);

- if (unlink(in_fname.c_str()) == -1) {
- if (errno != ENOENT) {
- snprintf(errstr, 1024, "Unable to unlink %s even though we could write to it: %s",
- in_fname.c_str(), strerror(errno));
- return -1;
- }
- }
-
+ /* Move the temporary file on the regular file in one "atomic" operation.
+ * The idea here is that the log file is always available and always
+ * a complete and valid file, users of that file can access it with
+ * confidence. The old file will be unlinked automatically. Jean II */
if (rename(fname_temp.c_str(), in_fname.c_str()) == -1) {
snprintf(errstr, 1024, "Unable to rename %s to %s: %s", fname_temp.c_str(), in_fname.c_str(),
strerror(errno));
@@ -1608,7 +1610,12 @@ int Packetracker::WriteCSVNetworks(strin

FILE *netfile;

- if ((netfile = fopen(in_fname.c_str(), "w+")) == NULL) {
+ /* Test if we can open the file, using read+write mode (r+).
+ * Using write mode (w+) would truncate the file, which means that during
+ * update the log would be empty.
+ * Using apend mode (a+) would force to seek to the end of file, which
+ * is slower. Jean II */
+ if ((netfile = fopen(in_fname.c_str(), "r+")) == NULL) {
snprintf(errstr, 1024, "Could not open %s for writing: %s", in_fname.c_str(),
strerror(errno));
return -1;
@@ -1853,14 +1860,10 @@ int Packetracker::WriteCSVNetworks(strin

fclose(netfile);

- if (unlink(in_fname.c_str()) == -1) {
- if (errno != ENOENT) {
- snprintf(errstr, 1024, "Unable to unlink %s even though we could write to it: %s",
- in_fname.c_str(), strerror(errno));
- return -1;
- }
- }
-
+ /* Move the temporary file on the regular file in one "atomic" operation.
+ * The idea here is that the log file is always available and always
+ * a complete and valid file, users of that file can access it with
+ * confidence. The old file will be unlinked automatically. Jean II */
if (rename(fname_temp.c_str(), in_fname.c_str()) == -1) {
snprintf(errstr, 1024, "Unable to rename %s to %s: %s", fname_temp.c_str(), in_fname.c_str(),
strerror(errno));
@@ -1894,7 +1897,12 @@ int Packetracker::WriteXMLNetworks(strin

FILE *netfile;

- if ((netfile = fopen(in_fname.c_str(), "w+")) == NULL) {
+ /* Test if we can open the file, using read+write mode (r+).
+ * Using write mode (w+) would truncate the file, which means that during
+ * update the log would be empty.
+ * Using apend mode (a+) would force to seek to the end of file, which
+ * is slower. Jean II */
+ if ((netfile = fopen(in_fname.c_str(), "r+")) == NULL) {
snprintf(errstr, 1024, "Could not open %s for writing: %s", in_fname.c_str(),
strerror(errno));
return -1;
@@ -2271,14 +2279,10 @@ int Packetracker::WriteXMLNetworks(strin

fclose(netfile);

- if (unlink(in_fname.c_str()) == -1) {
- if (errno != ENOENT) {
- snprintf(errstr, 1024, "Unable to unlink %s even though we could write to it: %s",
- in_fname.c_str(), strerror(errno));
- return -1;
- }
- }
-
+ /* Move the temporary file on the regular file in one "atomic" operation.
+ * The idea here is that the log file is always available and always
+ * a complete and valid file, users of that file can access it with
+ * confidence. The old file will be unlinked automatically. Jean II */
if (rename(fname_temp.c_str(), in_fname.c_str()) == -1) {
snprintf(errstr, 1024, "Unable to rename %s to %s: %s", fname_temp.c_str(), in_fname.c_str(),
strerror(errno));


Reply to this message