LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-07-2007, 10:54 AM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Question sigint custom handler


Hello. I have a program with a listening socket. I usually close it with ctrl-c. So I decided to set up sigint_handler and to make it close the socket when called. So far so good. The listening socket was, initially declared locally to main. Now, not wanting to dig further, I decided to simply make the listening socket global so sigint_handler can have access to it. But guess what. It doesn't work. When a client connects to the server, the server immediately exits with Segmentation fault. It will only work with a local listening socket, don't know why. Now, my question is: how can I add to sigint a handler that takes another argument (I want to pass the listening_socket). Thank you in advance.

Last edited by nc3b; 05-08-2007 at 07:55 AM.
 
Old 05-07-2007, 03:51 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Where to begin? Where to begin? Let's take these in reverse order.
Quote:
how can I add to sigint a handler that takes another argument (I want to pass the listening_socket)
You can't. In normal operation, you never actually call the interrupt handler yourself. (But see below.) It's the interrupt handling mechanism that does this. How is the interrupt handling mechanism to know what to pass as that additional parameter? It can't. So additional parameters are not allowable.
Quote:
When a client connects to the server, the server immediately exits with Segmentation fault. I will only work with a local listening socket, don't know why.
That segmentation fault is what you need to fix. Narrow in on that. There's nothing to keep you from faking a SIGINT by calling your interrupt handler yourself, without waiting for SIGINT to happen. Use gdb or something to figure out what's going on.
Quote:
I decided to set up sigint_handler and to make it close the socket when called.
Wait. That's basically all that's done with your interrupt handler? There's nothing wrong with closing the socket in the interrupt handler, but what happens when control is returned to your main program? Is it expected to just continue as though nothing had happened? How is it to know that the interrupt handler closed the socket?

For every function involving the socket such as accept() or read(), you should check the function's return value. (Consult the man page for each such function for more info.) If the return value from the function indicates an error, look at errno. If the value of errno (immediately upon return from the function) is EINTR, then you know that the function terminated because of a signal. Then you can decide in your main program what to do about that.

Quote:
I have a program with a listening socket. I usually close it with ctrl-c.
What's wrong with doing just that? Don't use an interrupt handler at all. If you don't use one, ctrl-C will cause your program to exit, and as part of that, the listening socket will be closed.

Or am I missing something here?
 
Old 05-08-2007, 07:55 AM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Post

Yup you are missing something. It's because of my faulty writing, I meant " It will only work with a local listening socket, don't know why". So it does work (if the listening socket is local), clients can connect and everything goes pretty much well. I close the program with ctrl-c. If I turn it back on right away, no clients can connect to it. So I checked netstat, and I saw it waits for something on that port. Of course, it ends the wait after a minute or so.

I don't think that interrupt handler is so bad. It closes the listening socket (or at least it tries to do so), and then exits. So the program doesn't need to find out I closed the socket. It exit(0)'s and that's that. Do you know why it still waits after I close the program? Thank you.

Last edited by nc3b; 05-08-2007 at 07:57 AM.
 
Old 05-08-2007, 12:17 PM   #4
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
You should set the SO_REUSEADDR socket option, it will let you bind even if the socket is in TIME_WAIT state. And you have to fix your segfalut. Use gdb, valgrind, electric fence or whatever you want to locate your errors.
 
Old 05-08-2007, 01:52 PM   #5
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Thank you. This works quite nicely.
 
Old 05-09-2007, 08:27 AM   #6
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
krizzz, I know you didn't mean to do this, but you gave me the gift of a new word: segfalut. I'm not sure why it's going to be useful, but I know it will be. (grin)
 
Old 05-09-2007, 08:39 AM   #7
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
Wink

Hehe, fat fingers... but wth, just taking a part in creating etymology
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SIGSEGV handler (segmentation fauld handler) myp Programming 8 03-08-2011 02:17 PM
overriding SIGINT xface66 Programming 3 04-02-2007 01:32 PM
SIGINT handler with Fork() xface66 Programming 1 04-02-2007 12:15 PM
Custom Timer Interrupt Handler Linux 2.4x ratwings Linux - Software 2 01-13-2005 01:51 AM
<0>Kernel panic: Aiee, killing interrupt handler! In interrupt handler - not syncing mrb Linux - Newbie 2 01-09-2005 09:47 AM


All times are GMT -5. The time now is 01:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration