Signals and recvfrom() behavior

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Signals and recvfrom() behavior

    Hello,


    I have a program which is waiting for data on a socket in recvfrom(). If a
    signal (SIGTERM) is sent to the program, should recvfrom() return with
    errno set to EINTR? It does not return on my system (Linux 2.4.18), but I
    have seen older example code which checks for EINTR. Is there a way to
    cause recvfrom() to return when a signal occurs?


    -Karl Robillard
    Karl Robillard Guest

  2. Similar Questions and Discussions

    1. Net::Telnet, fork and signals..
      Hello, I've been trying to figure this out for the past couple of days but am hitting a wall. I am using Net::Telnet 3.03 with a modification...
    2. Signals and Net::Telnet::command
      Hello, I had a question regarding signals and Net::Telnet and was wondering if someone had an insight into it. My script looks like:...
    3. More on 5.8 and signals
      Hi, I am pretty desparate to get this working, and if anyone wants to earn some cash helping me fix things PLEASE call me at 250 655-9513. ...
    4. Usable signals for own needs
      On 22 Jul 2003, Peteris Krumins wrote: Not portably. BTW, SIGHUP is the usual "reload config" signal. Maybe you need to rethink your...
    5. recvfrom returns with an error code of 14, EFAULT "Bad Address"
      I'll post the code at the bottom of the post. Whenever I try to retreive a udp packet sent via broadcast I get the errorcode EFAULT(bad address)....
  3. #2

    Default Re: Signals and recvfrom() behavior

    Karl Robillard [email]krobillard@cox.net[/email] wrote:
    > I have a program which is waiting for data on a socket in recvfrom(). If a
    > signal (SIGTERM) is sent to the program, should recvfrom() return with
    > errno set to EINTR? It does not return on my system (Linux 2.4.18), but I
    > have seen older example code which checks for EINTR. Is there a way to
    > cause recvfrom() to return when a signal occurs?
    If you are ignoring it then you won't interrupt the system
    call. As a check, specifically unignore the signal before
    recvfrom() and see what happens.

    --
    Thamer Al-Harbash [url]http://www.whitefang.com/[/url]
    shadows Guest

  4. #3

    Default Re: Signals and recvfrom() behavior


    "Karl Robillard" <krobillard@cox.net> wrote in message
    news:%JgYa.63006$zy.45967@fed1read06...
    > Hello,
    >
    >
    > I have a program which is waiting for data on a socket in recvfrom(). If
    a
    > signal (SIGTERM) is sent to the program, should recvfrom() return with
    > errno set to EINTR? It does not return on my system (Linux 2.4.18), but I
    > have seen older example code which checks for EINTR. Is there a way to
    > cause recvfrom() to return when a signal occurs?
    >
    >
    > -Karl Robillard
    It works for me:

    $ cat sock.c

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>

    void die(const char *a)
    {
    fprintf(stderr, "%s\n", a);
    exit(0);
    }

    int main(void)
    {
    int sock;
    struct sockaddr_in ad;
    sock=socket(AF_INET, SOCK_DGRAM, 0);
    if(sock<0) die("Unable to get socket");
    ad.sin_family=AF_INET;
    ad.sin_port=0;
    ad.sin_addr.s_addr=0;
    if(bind(sock, (struct sockaddr *)&ad, sizeof(ad))!=0)
    {
    close(sock);
    die("bind");
    }
    recvfrom(sock, &ad, sizeof(ad), 0, NULL, NULL);
    }

    $ gcc sock.c -o sock
    $ ./sock
    <pauses, from another console, I send this process a SIGTERM>
    Terminated

    Maybe you're doing something odd, like catching SIGTERM?

    DS


    David Schwartz Guest

  5. #4

    Default Re: Signals and recvfrom() behavior

    David Schwartz wrote:
    > $ gcc sock.c -o sock
    > $ ./sock
    > <pauses, from another console, I send this process a SIGTERM>
    > Terminated
    >
    > Maybe you're doing something odd, like catching SIGTERM?
    >
    > DS

    Thats just it. I am catching SIGTERM. However, I'd like the program to
    continue from the recvfrom() call. The single line of documentation on
    EINTR makes it sound as if recvfrom() will return when a signal occurs.
    This does not seem to be the case.


    -Karl
    Karl Robillard Guest

  6. #5

    Default Re: Signals and recvfrom() behavior

    shadows wrote:
    > Karl Robillard [email]krobillard@cox.net[/email] wrote:
    >
    >> I have a program which is waiting for data on a socket in recvfrom(). If
    >> a signal (SIGTERM) is sent to the program, should recvfrom() return with
    >> errno set to EINTR? It does not return on my system (Linux 2.4.18), but
    >> I
    >> have seen older example code which checks for EINTR. Is there a way to
    >> cause recvfrom() to return when a signal occurs?
    >
    > If you are ignoring it then you won't interrupt the system
    > call. As a check, specifically unignore the signal before
    > recvfrom() and see what happens.
    >

    I am catching the signal. My signal handler is called but recvfrom()
    continues to wait. Is there a way to get recvfrom() to return?


    -Karl
    Karl Robillard Guest

  7. #6

    Default Re: Signals and recvfrom() behavior


    "Karl Robillard" <krobillard@cox.net> wrote in message
    news:xJkYa.63275$zy.18740@fed1read06...
    > I am catching the signal. My signal handler is called but recvfrom()
    > continues to wait. Is there a way to get recvfrom() to return?
    How are you catching the signal? With 'sigaction'?

    Also, you can always 'longjmp' from the signal handler.

    DS


    David Schwartz Guest

  8. #7

    Default Re: Signals and recvfrom() behavior

    Karl Robillard [email]krobillard@cox.net[/email] wrote:
    > I am catching the signal. My signal handler is called but recvfrom()
    > continues to wait. Is there a way to get recvfrom() to return?
    OK that changes things. Are you using sigaction() or the older
    signal()? sigaction will let you specify whether or not you want
    restartable system calls. This is done via the SA_RESTART flag in
    the struct sigaction you pass to sigaction(). Just make sure it's
    cleared.

    --
    Thamer Al-Harbash [url]http://www.whitefang.com/[/url]
    shadows Guest

  9. #8

    Default Re: Signals and recvfrom() behavior

    David Schwartz wrote:
    >
    > "Karl Robillard" <krobillard@cox.net> wrote in message
    > news:xJkYa.63275$zy.18740@fed1read06...
    >
    >> I am catching the signal. My signal handler is called but recvfrom()
    >> continues to wait. Is there a way to get recvfrom() to return?
    >
    > How are you catching the signal? With 'sigaction'?
    >
    > Also, you can always 'longjmp' from the signal handler.
    >
    > DS

    David,


    No, I'm using 'signal'. I simply need to catch SIGTERM to cleanly shut down
    my program (write a shutdown message to a log file, etc.). Here's an
    outline of my program flow:


    term(){
    running = false;
    }

    main(){
    init_stuff();
    socket();

    running = true;
    signal( SIGTERM, term() );

    while( running ){
    recvfrom();
    process_data();
    }

    close();
    cleanup_stuff();
    }


    If necessary, I'll cleanup everything and exit from the signal handler. The
    description of EINTR in the recvfrom man page made me think that this
    program flow would work. It does not, so I am wondering under what
    conditions recvfrom() will return with errno set to EINTR. I was looking at
    someone else's code where they use alarm() (SIGALRM) to break out of
    recvfrom(). I wrote a test to do this but my code never exited from
    recvfrom(). Can signals cause recvfrom() to return?


    -Karl
    Karl Robillard Guest

  10. #9

    Default Re: Signals and recvfrom() behavior

    shadows wrote:
    > Karl Robillard [email]krobillard@cox.net[/email] wrote:
    >
    >> I am catching the signal. My signal handler is called but recvfrom()
    >> continues to wait. Is there a way to get recvfrom() to return?
    >
    > OK that changes things. Are you using sigaction() or the older
    > signal()? sigaction will let you specify whether or not you want
    > restartable system calls. This is done via the SA_RESTART flag in
    > the struct sigaction you pass to sigaction(). Just make sure it's
    > cleared.
    >

    Using sigaction() rather than signal() gives the expected behavior. Thank
    you Shadows (and David) for your help.

    I have a dream that someday all man pages will clearly state, up front, that
    a call is obsolete or deprecated.


    Cheers,
    -Karl
    Karl Robillard Guest

  11. #10

    Default Re: Signals and recvfrom() behavior


    "Karl Robillard" <krobillard@cox.net> wrote in message
    news:ZxmYa.63288$zy.46938@fed1read06...
    > > How are you catching the signal? With 'sigaction'?
    > No, I'm using 'signal'. I simply need to catch SIGTERM to cleanly shut
    down
    > my program (write a shutdown message to a log file, etc.). Here's an
    > outline of my program flow:
    Do not use 'signal', use 'sigaction'. You are requesting BSD signal
    semantics, which is definitely not what you want.

    DS


    David Schwartz Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139