Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Cleaning up after CTRL-C when using Net::DNS::Nameserver on Windows?

by elwarren (Curate)
on Jan 03, 2005 at 21:07 UTC ( #419058=perlquestion: print w/ replies, xml ) Need Help??
elwarren has asked for the wisdom of the Perl Monks concerning the following question:

I've used the Net::DNS::Nameserver module to run a local nameserver on my windows workstation. After teaching it a few tricks and getting it to do what I want, I thought I would get fancy and have it register itself as my dns server. Win32::Ipconfig does this very nicely.

I store my current dns servers in a hash then set my adapter to use my local nameserver. When my server stops, I set the adapter back to the servers I saved. I put this step in the END block, but it doesn't seem to get called when I CTRL-C. In fact, nothing happens when I hit CTRL-C when I set my own signal handler.

After much gnashing of teeth and blaming signal handling on windows, I realized that Net::DNS::Nameserver must be handling signals in its own loop. I was hoping that the END in my code would execute after the modules cleanup, but it doesn't seem to be.

My question is simply, can I catch CTRL-C and cleanup, or somehow make sure my end code always runs? I have put together a small example below to illustrate the problem.

TIA
#!/usr/bin/perl use warnings; use strict; use Net::DNS; use Net::DNS::Nameserver; sub replyhandler { 1 } sub gotint { my $sig = shift; die "die because I caught: SIG<$sig>\n"; } #$SIG{'INT'} = \&gotint; my $ns = Net::DNS::Nameserver->new( LocalAddr => '127.0.0.1', ReplyHan +dler => \&replyhandler); $ns->main_loop; while ( 1 ) { sleep 1 } END { print "oh no this is the END\n" }

Comment on Cleaning up after CTRL-C when using Net::DNS::Nameserver on Windows?
Download Code
Re: Cleaning up after CTRL-C when using Net::DNS::Nameserver on Windows?
by jfroebe (Vicar) on Jan 03, 2005 at 21:42 UTC

    Hi,

    Without modifying Net::DNS::Nameserver, you could perform a fork (works ok with 5.8 and 2k+), where the child process becomes your dns server and the parent will be the controlling process. That way, you can safely keep both signal handlers. :-)

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

      I would rather not fork on windows. I'm trying to keep this code light on memory, and I've had problems with fork and reading from sockets on windows in the past.

      Setting this forking discussion aside, this still does't tell me why my END block doesn't run as I expect/want.

      It is windows, so I'm thinking about just calling my perl script from a batch file and reseting the interface with a second script that runs after the first one exits. Not as clean of a solution as I'd like, but eventually I expect this to run as a service, so the CTRL-C portion will be moot at that point...
Re: Cleaning up after CTRL-C when using Net::DNS::Nameserver on Windows?
by sgifford (Prior) on Jan 04, 2005 at 03:00 UTC
    I seem to recall that under Windows I had to catch the BREAK signal insted of INT. That might be worth a try.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://419058]
Approved by Arunbear
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (15)
As of 2014-08-27 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (238 votes), past polls