Moving on

Dec. 4th, 2002 10:29 am
lnr: Halloween 2023 (Default)
[personal profile] lnr
I a better mood today after a nice evening in with Richard. Though I can't help having bitter little thoughts that he could have just as well had the same nice evening in with someone else. That's the thing I find hard being in a poly relationship: still feeling special. And no not in a BELM! sort of way Jan. Silly really because I know Richard is no less special to me despite the fact I'm seeing Jan too, so why it should be any different for him I dunno. Another case of the theory being fine but the emotions not keeping up in practice. I think it just gets harder when I'm down.

It was a nice evening anyway. Making the most of having lots of sins left at this time of the week we shared a bottle of wine over dinner then spent a while doing a jigsaw which I picked up for Richard at Erotica, with much giggling and trying to work out what was going on where. Definitely a good buy, even if we never bother doing it again. I sometimes feel bad about spending money on that sort of thing, but really it was barely more than the price of a couple of pints of cider these days, and I got just as much enjoyment out of it. And I'm still drinking lots less than I used to anyway because of the diet.

Our lack of getting round to going to bed at a sensible time does mean I'm still knackered today, and slightly damp from the cycle in. Thankfully I decided to play it safe and had a change of skirt or I'd be really very soggy indeed. Still, think I'm awake enough to faff a bit with some Perl today at least. If anyone has a good example of signal handling done in perl I wouldn't mind a pointer. It's all very well reading about it in the Camel book, but the only example I have is in perl 4 and has lines of code which are unreachable and commented as being only there to shut -w up!

Cut and paste from Emacs makes this much more pleasant to type, but I do wish I knew what the problem with LJ and Mozilla was, I've not seen it anywhere except under Windows.

Oh, and thanks to all who tried to cheer me up. Much appreciated, and sorry for being such a grumpy thing.

Date: 2002-12-04 05:14 am (UTC)
From: [identity profile] simonb.livejournal.com

Firstly *HUGS*

As for signal handling under perl... personally I used the POSIX signal handling stuff since its what I prefer having done systems coding in C. The following should help... it works with warnings and use strict in place.

#!/usr/bin/perl -w

use strict;

use POSIX;

##### Begin signal handlers

##
## We need this to allow interrupted system calls to be
## restarted automatically. The Perl POSIX module doesn't
## actually provide this since its not POSIX, but SYSV stuff.
## If you didn't do this you'd need to put wrappers around every
## system call so that if the system call failed and the error
## number was EAGAIN you'd repeat the system call.
##
## Its currently commented out as you don't actually need it under
## Solaris. This may not be true for other Unix varients.
##
#sub SA_RESTART {
#  return 0x00000004;
#}


##
## When we get a SIGINT (ie ^C) or a SIGTERM we need to
## shutdown gracefully
##
sub SigShutdown {
  print STDERR "Caught SIGINT/SIGTERM - shutting down\n";
  exit 0;
}

##
## We can ignore a number of signals - but we show that we've got them
##
sub SigIgnore {
  my $signame = shift;
  print STDERR "Caught and ignored SIG$signame\n";
}

##### End signal handlers

##### Start of installation of signal handlers
##
## When you use POSIX signals you have a two step process which you
## need to go through. The first thing to do is create a SigAction
## object which is the equivalent to the C sigaction structure. The
## arguments are:
##   o Fully qualified name of the subroutine which performs the
##     the signal handling
##   o A POSIX::SigSet object which allows you to define groups of
##     signals which will be handled by the signal handler
##   o Flags for the signal - ie if system calls are to be restarted,
##     etc.
##
## The next thing you need to do is install the SigAction object for
## a signal via the sigaction() routine. Its arguments are:
##   o The signal name
##   o The SigAction object
##   o The original SigAction object
##

##
## We shutdown if sent a SIGINT or SIGTERM
##
my $sigshut = new POSIX::SigAction 'main::SigShutdown',0,0;
sigaction(&SIGINT,$sigshut) ||
  die "Failed to run sigaction for SIGINT - $!\n";
sigaction(&SIGTERM,$sigshut)||
  die "Failed to run sigaction for SIGTERM - $!\n";

##
## Signals we ignore, but which can cause system calls to need to be
## restarted - this is why you see the SA_RESTART which is SysVR4 specific
##
## Note that the eval() below *is* evil however its the only way to get
## around using "use strict" and code like:
##
##   my $foo="SIGHUP";
##   sigaction(&$foo,....);
##
## Since the &$foo is banned when strict refs is in play. The eval() gets
## around this by the code perl looking at going from:
##
##   $sig="SIGHUP";
##   sigaction(&$sig,\$sigign)
##
## which breaks strict refs to:
##
##   sigaction(&SIGHUP,\$sigign)
##
## which doesn't.
##
## Note that you *may* need to remove the SA_RESTART on Unix varients
## other than Solaris/SYSV.
##
my $sigign = new POSIX::SigAction 'main::SigIgnore',0,&SA_RESTART;
foreach (qw( SIGHUP SIGUSR1 SIGUSR2 SIGCHLD SIGPIPE SIGQUIT SIGILL SIGABRT SIGFPE SIGSEGV )) {
  my $code="sigaction(&$_,\$sigign) || die \"Failed to run sigaction for $_ - \$!\\n\";";
  eval($code) || die "$0: Failed to eval($code) $@ - $!\n";
  ($@) && die "$0: Failed to eval($code) $@ - $!\n";
}

##### End of installation of signal handlers

## Show our PID
print "Our PID: $$\n";

## Now loop and show stuff
while(1) {
  sleep(1);
}

When this runs it looks like the following:

simonbu@killegray[511]% ./t.pl
Our PID: 21230
Caught and ignored SIGHUP
Caught and ignored SIGUSR1
Caught and ignored SIGPIPE
Caught and ignored SIGSEGV
Caught SIGINT/SIGTERM - shutting down
simonbu@killegray[513]%

The above was it responding to the following set of commands in another window:

simonbu@killegray[501]% kill -HUP 21230
simonbu@killegray[503]% kill -USR1 21230
simonbu@killegray[504]% kill -PIPE 21230
simonbu@killegray[506]% kill -SEGV 21230
simonbu@killegray[505]% kill -TERM 21230

Hope that the above makes sense and is helpful.

Date: 2002-12-04 05:34 am (UTC)
ext_8103: (Default)
From: [identity profile] ewx.livejournal.com
Why not just use %SIG?

Date: 2002-12-04 05:41 am (UTC)
From: [identity profile] simonb.livejournal.com
*shrug*

I prefer the control which the POSIX stuff gives you. As the comments in the code say, you can do fun things like not having to worry about restarting interrupted system calls under Solaris.

Date: 2002-12-04 06:53 am (UTC)
ext_8103: (Default)
From: [identity profile] ewx.livejournal.com
If you mean "use SA_RESTART" then %SIG does that anyway. You still have to retry interrupted system calls though, because SA_RESTART doesn't actually make all system calls restartable.

November 2025

S M T W T F S
      1
2345678
9101112131415
16171819 202122
23242526272829
30      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 31st, 2025 08:09 pm
Powered by Dreamwidth Studios