Can someone help me with skeleton C to handle SIGTERM

H

Henry Law

I'm trying to debug a Perl problem whose symptoms are that a running
Perl process either doesn't receive, or fails inexplicably to handle, a
SIGTERM sent from the OS during shutdown (Linux Fedora Core 4, if it
matters). One way to find out whether it's a Perl problem or an OS
problem is for me to run a program in some other language and see if it
receives SIGTERM when Linux shuts down (as it's supposed to, according
to the manuals).

I have written C in my time but I'm very rusty, and I'm hoping that
someone could either sketch out, or point me at, some skeleton code
which I could then perfect to do the job. I'll be OK with debugging and
so forth, but I know that I could spend ages rootling around looking for
the right #include statements and constants for the signalling and the
syslogging.

All I need is a program that can be run in the background (CRON) and
will syslog a message when it gets a SIGTERM. The original Perl program
is this (which most C programmers will understand, I hope).

#! /usr/bin/perl
use strict;
use warnings;
use Sys::Syslog;
use sigtrap 'handler' => \&terminate, 'normal-signals';
my $loop_count = 0;
my $timer = 0;
my $sleep_size = 10;
my $loop_size = 6;
openlog 'clpm.pl', 'cons,pid', 'user';
syslog 'info', 'starting up';
while (1) {
while (++$loop_count < $loop_size) {
sleep $sleep_size;
}
$timer += $loop_count*$sleep_size;
$loop_count = 0;
syslog 'info', "Running for $timer seconds";
}
syslog 'info', 'How did we get here?';

sub terminate {
syslog 'info', 'Normal signal received';
exit 0;
}

All help gratefully received.
 
B

Ben C

I'm trying to debug a Perl problem whose symptoms are that a running
Perl process either doesn't receive, or fails inexplicably to handle, a
SIGTERM sent from the OS during shutdown (Linux Fedora Core 4, if it
matters). One way to find out whether it's a Perl problem or an OS
problem is for me to run a program in some other language and see if it
receives SIGTERM when Linux shuts down (as it's supposed to, according
to the manuals).

I have written C in my time but I'm very rusty, and I'm hoping that
someone could either sketch out, or point me at, some skeleton code
which I could then perfect to do the job. I'll be OK with debugging and
so forth, but I know that I could spend ages rootling around looking for
the right #include statements and constants for the signalling and the
syslogging.

All I need is a program that can be run in the background (CRON) and
will syslog a message when it gets a SIGTERM.

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void handler(int v)
{
signal(SIGTERM, handler);
fprintf(stderr, "Signal handler called with %d\n", v);
}

int main(int argc, char **argv)
{
signal(SIGTERM, handler);

while (1)
sleep(3600);

return 0;
}

HTH. The proper NG would be comp.unix.programmer.
 
H

Henry Law

Ben said:
#include <stdio.h>

Wonderful; that'll be enough to get me going. Thank you very much.
HTH. The proper NG would be comp.unix.programmer.

Sorry; since I particularly wanted C (because I used to speak a bit) I
thought this was a better place.
 
D

Default User

Henry said:
Wonderful; that'll be enough to get me going. Thank you very much.


Sorry; since I particularly wanted C (because I used to speak a bit)
I thought this was a better place.

The problem is, there's no way to do in C. None of "background",
"CRON", "SIGTERM", or "syslog" are topical here.

An extension like POSIX might help, hence Ben's suggestion that you
move to comp.unix.programmer where they discuss things like that.




Brian
 
S

Spiros Bousbouras

Default said:
The problem is, there's no way to do in C. None of "background",
"CRON", "SIGTERM", or "syslog" are topical here.

That would be standard C without Unix specific functions
and definitions.
 
D

Default User

Spiros said:
Default User wrote:

That would be standard C without Unix specific functions
and definitions.


You mean the topic of this newsgroup? Yeah.





Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top