From 96fe5fbb83ac132b59f67c96cc217c2fadd535bf Mon Sep 17 00:00:00 2001
From: Stefan Laudemann <thisco@webcake.de>
Date: Fri, 20 Feb 2015 09:38:36 +0100
Subject: [PATCH] Reimplements the transmission of the report using nixio
 (instead of netcat).

As the old implementation lead to rather inconsistent notifications on the
screen due to some incorrect return value handlings of os.execute(cat ...),
which as well produced an inconsistent output (an error message of netcat
got printed to the screen for the first host without the information that
there is a second one to which the report can be send; if the transmission
to that host succeeds, one will see the irritating sequence "failure" -->
"report has been sent" on the screen).
As error handling and output-redirection via os.execute(...) would have re-
quired some refactoring anyway, the logic is now implemented using nixio
sockets, as their behaviour is easier to control from within a Lua-script,
and - additionally - enables us to keep the debug data in memory (i.e. we
don't have to temporarily write it to /tmp/debug-report.txt anymore). The
report only needs to be stored permanetely, if the transmission fails for
whatever reason. Thus, as this part of the script had to be changed as well,
we now make use of the nixio library to interact with files.

Signed-off-by: Stefan Laudemann <thisco@webcake.de>
---
 ffpb/ffpb-debug/files/bin/ffpb-debug | 53 ++++++++++++++++++----------
 1 file changed, 34 insertions(+), 19 deletions(-)

diff --git a/ffpb/ffpb-debug/files/bin/ffpb-debug b/ffpb/ffpb-debug/files/bin/ffpb-debug
index 834a1e6..a193d1f 100755
--- a/ffpb/ffpb-debug/files/bin/ffpb-debug
+++ b/ffpb/ffpb-debug/files/bin/ffpb-debug
@@ -2,6 +2,9 @@
 
 debugdata = ""
 ATH9K_DEBUGFS_DIR="/sys/kernel/debug/ieee80211/phy0/ath9k"
+PATH_DBG_REPORT='/tmp/debug-report.txt'
+
+local nixio = require('nixio'), require('nixio.util'), require('nixio.fs')
 
 -- wrapper for calling systemcommands
 function cmd(_command)
@@ -40,9 +43,9 @@ if arg[1] == '-l' then
 	localMode = true
 end
 
--- search for existing debugreport.txt
-local debugFile = io.open("/tmp/debugreport.txt", "r")
-if debugFile==nil then
+-- search for existing debug report
+local oldReport = nixio.open(PATH_DBG_REPORT, "r")
+if oldReport==nil then
 	-- no existing debugreport, let's generate a new one
 	
 	-- inform the User ;)
@@ -107,13 +110,10 @@ if debugFile==nil then
 	os.execute("killall -USR1 fastd 2>/dev/null")
 	debugdata = debugdata .. cmd("logread")
 	debugdata = debugdata .. "---- END BATMAN AND FASTD STATUS ----\n\n"
-	
-	-- write debugreport to file
-	debugFile = io.open("/tmp/debugreport.txt", "w")
-	debugFile:write(debugdata)
-	debugFile:close()
 else
 	print('I found an old debugreport.')
+	debugdata = oldReport:readall() 
+	oldReport:close()
 end
 
 -- if local mode is requested print the report, otherwise send it to the admin team
@@ -121,27 +121,42 @@ siteConfig = require("gluon.site_config")
 if localMode then
 	print ('As requested, i will not send the report, here it is:')
 	print (debugdata)
-	os.execute("rm /tmp/debugreport.txt")
-	os.exit(0)
+	nixio.fs.unlink(PATH_DBG_REPORT)
 else
+	local nixio = require('nixio'), require('nixio.util')
 	print('Sending report to Admin-Team ...')
-	local reportSent = false
+	local sent = 0
+	local reportname = nil
 	local port = siteConfig.debugserver.port
 	for host in list_iter(siteConfig.debugserver.host) do
-		print('Trying ' .. host)
-		if os.execute("cat /tmp/debugreport.txt | nc " .. host .. " " .. port) and reportSent == false then
-			reportSent = true
+		print('Trying to deliver debug-report to: ' .. host)
+		local sock = nixio.connect(host, port, "inet6", "stream")
+		if sock then
+			sock:setopt('socket', 'sndtimeo', 30.0)
+			sock:setopt('socket', 'rcvtimeo', 30.0)
+			sent = sock:writeall(debugdata)
+			if sent == debugdata:len() then
+				-- half-side close to indicate the end of our transmission
+				sock:shutdown('wr')
+				print('Transmission succeeded. Waiting for report-name.')
+				reportname = sock:readall(256)
+			end
+			sock:close()
+			if reportname ~= nil then break end
 		end
 	end
 
-	if reportSent then
-		print()
-		os.execute("rm /tmp/debugreport.txt")
-		print('Report was sent, the Adminteam has been informed.')
-		print('My job is done, good bye')
+	if reportname ~= nil then
+		print('\nYour report has been stored at the debug-server with the name: ' .. reportname)
+		print('I also notified some gurus to take care of the issue. My job is done here, good bye!.')
 	else
 		print('Sorry, i could\'t send the report. I will try it again')
 		print('when this script will be invoked the next time.')
 		print('Good bye')
+		local f = nixio.open(PATH_DBG_REPORT, 'w')
+		f:writeall(debugdata)
+		f:close()
 	end
 end
+
+os.exit(0)
-- 
GitLab