Browse Tag

Windows

Perl sig-int and open

skip to solution

While recently re-writing our build script into a simple perl script I came across a problem.

I wanted to have the script put output from the commands that it was executing onto both the screen and into a file at the same time.

No problem, in perl you can open commands like you can files, by simply putting a pipe ‘|’ as the first character.

open(OUT, ">log.txt")
    || die "can't open log: $!";
open(STATUS, "netstat -an 2>&1 |")
    || die "can't fork: $!";
while () {
    next if /^(tcp|udp)/;
    print;
    print OUT;
}
close STATUS || die "bad netstat: $! $?";

This code will output the lines to both stdout and to a log file.

Great nice simple code that works using standard perl operations. However one thing that this does not provide is that the forked processes do not receive ‘Ctrl-C’ or ‘Sig-Int’ if you are more of a Unix head.

Which is really quite bad when you are creating a build script you want all processes to die if you need to pull the plug on a build.

So next I turned to calling a direct windows api call GenerateConsoleCtrlEvent calling this function on all child processes in a perl sig-int handler to send CTRL_C_EVENT (0) to each of these processes.

use Win32::Process::Info;
use Win32::API;

$SIG{INT} = 'killChildren';

sub killChildren
{
    print "Killing process\n";
    my $pi = Win32::Process::Info->new ();

    Win32::API->Import('Kernel32.dll',
'BOOL GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,  DWORD dwProcessGroupId)');

    my %children = $pi->Subprocesses($$);
    foreach my $child (@{$children{$$}})
    {
        GenerateConsoleCtrlEvent(0, $child);
    }
    exit(1);
}

However if you read the documentation of GenerateConsoleCtrlEvent closely:

Sends a specified signal to a console process group

So then you realize that the process must be created in a process group by calling CreateProcess and passing a CREATE_NEW_PROCESS_GROUP flag. Which is getting to be way too much work there must be a simpler way surely.

The solution I eventually came up with was perl calling via the system command and running an inline script.

open(CMD, "$^X -e \"system(q{$cmd});\" |") || die "Failed to run \"$cmd\": $!\n";

However since the commands I were executing had ” in them as well there were too many variants that were requiring regex replacement to get the commands working correctly.

So like any good web monkey I ended up Base64 encoding the command I wish to execute and then un-encoding it in the sub perl process.

Final Solution

sub RunCommand
{
    my ($cmd, $dontStop) = @_;
	print "$cmd\n";
	print BUILDLOG "$cmd\n";
	# Run the command through another instance of perl so that it can all be
	# killed when Sig-C is sent to this build file.
	# However command contain characters such as \ and " which are hard
	# to pas through 2 interpreters so base64 endcode and decode the command
	# $^X is a special variable that is the path to perl
	my $encoded = encode_base64($cmd);
    open(CMD,
  "$^X -e \"use MIME::Base64;system(decode_base64(q{$encoded}));\" |")
  || die "Failed to run \"$cmd\": $!\n";

    while()
    {
        print "$_";
        print BUILDLOG "$_";
    }
    close(CMD);

    if(!$dontStop and $? != 0)
    {
        die "Command: $cmd\nFailed with code: $?\n";
    }

    return $?;
}

Virtualbox 4.0 dhcp not working

Update Skip to solution

Virtualbox is a wonderful tool for pc virtualization. I use it all the time for testing installers and just general I don’t want to hose my PC if this goes pear shape type stuff.

It’s great for devloping Windows software that needs to work on different versions of the OS too. I test XP, Vista, 7; x32 and x64 versions without breaking a sweat.

However once up upgraded to the new version 4.0 of the product the some networks were no longer operational. Specifically those using the dhcp on host only connections.

I thought this may be an issue with old guest additions which I then proceeded to upgrade. Unfortunately this did no turn out to be the issue so I proceeded to give up an just use other network types.

However today I stumbled upon the solution and it came from somewhere completely unrelated to virtualbox.

The posting of the solution can be found on a forum My Digital Live here.

There appears to be some bug within the source code which the user has devised a patch for.

The Solution

Windows versions of Virtualbox only

  1. Download the compressed patch from http://www.4shared.com/file/EFsSxJaV/vb4dhcp_patch.html
  2. Extract the program using 7-zip
  3. Open command prompt as Administrator and navigate to the directory where you extracted the program
  4. Run the extracted program vb4dhcp.exe passing a single argument to the VBoxC.dll
    vb4dhcp.exe "C:\Program Files\Oracle\VirtualBox\VBoxC.dll"

No you can run Virtualbox and it’s dhcp server runs again. 8)

Update

Virtualbox 4.0.4 has been released and this regression has been fixed. Download it now.

Floder Properties in Windows Vista

When using the explorer in Windows Vista I was having trouble finding the folder properties, for showing hidden files among other things. Like everything else in Vista it has moved and changed name. No longer can you find it under “Tools” -> “Folder Options…”

But now under “Organize” -> “Folder and Search Options”

Location of Folder Options in Windows Vista
Location of Folder Options in Windows Vista

It really shook me and the help only had a link to open the options and didn’t tell you how to actually get to the options directly.

Open Java and Windows Options

Well it’s officially been announced that Java J2SE, J2ME, J2EE will all be released by sun as reference implementations under the Gnu GPL + classpath exception. Which I’m sure will bring a new era of Java, especially to Linux since Java will be able to be shipped with distributions without concern just like Mono is today.

I really hope this means the MVM will get done faster it would be a great thing for the platform.

On a unrelated note one of my pet peeves about Windows is the way that Explorer.exe works. The problem is that it is the window manager as well as the file browser. What I usually find is that the file manager will lock up the window manager which is most annoying. However there is an option to make them work in separate processes which users more memory but for the stability of the system it’s well worth it.
Edit In Seperate Process

wikidPad

For a while now I have been waiting for tomboy to be ported to windows because I am stuck in windows land for now because of my sound problem. However on the net it is always talked about having tomboy for Windows but no one has ever released anything that could be considered usable.

So I just started to search for a desktop wiki for windows and I came across wikidPad. It’s written in python using wxwidgets, it is uncompromisingly geeky. In that it is uses a wiki style markup and stores everything in text files, ahh the geekness. It has full regex search and incremental searching of pages. It exports to HTML for showing others.

So like Accountants use Excel for recording their thoughts and ideas I can now record in a wiki.

Other useful features is auto save and tags so you can always find what you were working on. I thoroughly recommend it for anyone who needs to record their ideas and loves not using the mouse, everything has keyboard shortcuts and it is a syntax based markup so no trying to find bold just type the syntax.

Death to open source by virus scan

Today for the first time my virus scanner here at work started deleting files. Identifying them as the Prockill-DF virus. It wasn’t going to take any chances and deleted the files what was interesting was what files it identified:

Are you getting my drift here it’s after all the setup programs for open source products. The problem has been identified by McAfee in 4629 DAT but this is a simple way to rid the windows world of open source products. Get the anti-virus to take them out for us.

Just look at them all.

Thin Client Fun

After reformatting our old Celron 400 Laptop to Hoary Hedghog, Oh the joy of only having Linux on a computer. However since most of my time at the moment is taken up doing C# development and more importantly of the MS.NET kind rather than the Mono :(

But I was pleasantly surprised that Ubuntu comes with rdesktop a Unix remote terminal services client. So using my Windoz knowledge configured my main PC for remote goodness and then my trusty wireless laptop allowed me to use my main computer from anywhere in the house well use as much as 800×600 will let you these days :P

Sick of 16 color tray icons in win2k?

Here at work we I am running Windows 2000 and for the longest time the fact that tray icons could only have 16 colours has annoyed me for the longest time. With a lot of newer programs creating their icons to this high colour made the UI(User Interface) look really bad :(.

However to day I found a solution someone has patched explorer.exe for all service packs of win2k with a simple active x control. You can see it here. So right click with ie view for Firefox or if you are for some reason still using ie just click the link :P. But do please consider switching and enjoy your winXP style hi-color tray icons.

Longhorn faces another delay

Yet another delay has been announced for the next versoin of windows codenamed Longhorn. Will this give futher chance for Linux to gain a foothold in the Desktop market only time will tell. But by the time it is released in late 2006 we on the current 6 month release schedule we should see Gnome 2.16, if version 2 at all maybe into 3?? and probably Fedora Release 6.

Check the story at infoworld