ThinkPads and KnockAge.pl — problem solved

December 24, 2013 | 3 Minute Read

Hello there!

A couple of days ago i remembered of the old days using my glorious ThinkPad T42, one of the most awesome laptop I ever had.

Among other things I used to run, there also was the knockAge script.

KnockAge is well explained in this article: “Knock-based commands for your Linux laptop“. KnockAge is a tool written to exploit functionalities of ThinkPads’ HDAPS (Hard Disk Active Protection System), allowing ThinkPad owners to define sequences of knocks on their laptop and bind commands to them, to be executed when a certain sequence of knocks is detected.

Installation of the script is simple,and discussed in the linked article, so I won’t repeat it here.

What I want to write about is that some users including me experienced a bug in the knockAge.pl. The bug comes out as a stuble error message:

Use of uninitialized value in system at ./knockAge-0.1.pl line 236.

I found at least five posts on the Internet asking for help about this.

I decided to dig the sources, face the bug and fix it.

So, what’s in line #236 ? There’s a call to system, and an access to a hash data structure. In order to see what’s going wrong, I need to see where is the undefine data. To do so, I used Data::Dumper and feature ‘say’ (it’s handy). Data::Dumper will do the trick.

Let’s give a look at it (line #240 is actually line #236 after some import and call to Dumper):

Schermata del 2013-12-24 17:05:23Now that I added data-dumping code, let’s go and see the output (error message + debugging output):

Schermata del 2013-12-24 17:06:13Can you see those $VAR5 and  $VAR6? Where do they come from?

They almost came out from nowhere!

The bug is in config-file reading subroutine: my ~/.knockFile ends with a newline (‘\n’), and the script treats that completely blank line as a regular line, trying to parse it as any other regular line and thus filling the hash-based data structure with undefined data.

Let’s fix it! In order to fix it, I have to check the line is not an empty line.

Here is my version, patched:

Schermata del 2013-12-24 17:09:26Line 182-185 were added by me.

Complete, patched source code can be found at:

https://github.com/esantoro/knockage/

Hope it helps!