DCE: problem when using sleep() in signal handler

143 views
Skip to first unread message

Thomas Burger

unread,
Mar 26, 2014, 6:08:25 AM3/26/14
to ns-3-...@googlegroups.com
Dear all,

I tried to use sleep() or sem_timedwait() within a signal handler, which works fine under Linux - e.g. run example test_sighandler.c and interrupt it with Strg+C. Although in practice one would rarely do this, I guess it is allowed to do so - see https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers

BTW, I wanted to implement an artificial "processing delay" with this sleeps...

Please see attached files for a simple example which produces the error. As far as I can see, the error is caused in UtilsDoSignal, which calls the signal handler again and again, so that there is no chance to reach sigdelset(). By temporary saving i->signal, and calling sigdelset() before i->handler() the problem seems to be gone, but I do not know whether this okay in all cases (side effects?).

I appreciate your opinion!

Best regards,
Thomas
dce-test_sighandler.cc
test_sighandler.c

Hajime Tazaki

unread,
Mar 31, 2014, 10:25:29 AM3/31/14
to ns-3-...@googlegroups.com

Hi, sorry to be late.

I tried with your code but can't reproduce the problem you
mentioned.

the obtained output from the execution was:

% ./waf --run dce-test_sighandler
mySendSignal will search for process 53512 (hard coded, FIXME!!!)
Found a process (ptr 0x1452950), calling UtilsSendSignal with SIGUSR2
%

and files-0/var/log/53512/stdout contains:

Testprogram for sighandler
main loop...
main loop...
main loop...
mySignalHandler received SIGUSR2
Doing a 1second sleep()
(snip)

which version of dce are you using ?
if you're using dce-dev, could you let me know which
revision of ns-3-dce you are using ?

thank you for your time.
-- Hajime

At Wed, 26 Mar 2014 03:08:25 -0700 (PDT),
Thomas Burger wrote:
>
> [1 <multipart/alternative (7bit)>]
> [1.1 <text/plain; UTF-8 (7bit)>]
> --
> You received this message because you are subscribed to the Google Groups "ns-3-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
> To post to this group, send email to ns-3-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/ns-3-users.
> For more options, visit https://groups.google.com/d/optout.
> [1.2 <text/html; UTF-8 (quoted-printable)>]
>
> [2 dce-test_sighandler.cc <text/x-c++src; US-ASCII (7bit)>]
> #include "ns3/core-module.h"
> #include "ns3/dce-module.h"
> #include "../../model/utils.h" //UtilsGetNodeId()
> #include "ns3/node-list.h" //NodeList::GetNode()
> #include <signal.h> //SIGINT
>
> using namespace ns3;
>
> void mySendSignal(int pid)
> {
> //53512
> printf ("mySendSignal will search for process 53512 (hard coded, FIXME!!!)\n"); //FIXME
>
> Ptr<DceManager> dceManager = NodeList::GetNode (0)->GetObject<DceManager> ();
> Process* dceProcess = dceManager->SearchProcess (53512);
> if (dceProcess)
> {
> printf ("Found a process (ptr 0x%x), calling UtilsSendSignal with SIGUSR2\n", (void*)dceProcess);
> UtilsSendSignal (dceProcess, SIGUSR2);
> }
> else
> {
> printf ("dceProcess not found\n");
> NS_ASSERT (false);
> }
> }
>
> int main (int argc, char *argv[])
> {
> NodeContainer nodes;
> nodes.Create (1);
>
> DceManagerHelper dceManager;
> dceManager.Install (nodes);
>
> DceApplicationHelper dce;
> ApplicationContainer apps;
>
> dce.SetStackSize (1<<20);
>
> dce.SetBinary ("test_sighandler");
> dce.ResetArguments ();
> apps = dce.Install (nodes.Get (0));
> apps.Start (Seconds (0.0));
>
> //XXX Why is here a ScheduleWithContext() required?
> // A normal Schedule() leads to SIGSEGV because m_scheduler is NULL pointer (m_scheduler->Enqueue in TaskManager::Wakeup)
> Simulator::ScheduleWithContext (0, Seconds (30), &mySendSignal, 123456789);
>
> Simulator::Stop (Seconds(300.0));
> Simulator::Run ();
> Simulator::Destroy ();
> }
> [3 test_sighandler.c <text/x-csrc; US-ASCII (7bit)>]
> #include <unistd.h>
> #include <stdio.h>
> #include <stdint.h>
> #include <stdlib.h> //EXIT_FAILURE
> #include <signal.h>
>
> void mySignalHandler(int signo)
> {
>
> if (signo == SIGINT)
> {
> printf("mySignalHandler received SIGINT\n");
> printf("Doing a 1second sleep()\n");
> sleep(1);
> printf("1second sleep() finished\n");
> }
> else if (signo == SIGUSR2)
> {
> printf("mySignalHandler received SIGUSR2\n");
> printf("Doing a 1second sleep()\n");
> sleep(1); // XXX this sleep causes infinite loop within DCE, and a SIGSEGV in the end
> printf("1second sleep() finished\n");
> }
> else
> {
> printf("mySignalHandler received unknown signal\n");
> }
>
> }
>
> int main(int argc, char **argv)
> {
> sleep (1);
>
> printf("Testprogram for sighandler \n");
>
> if (signal(SIGINT, mySignalHandler) == SIG_ERR)
> {
> printf("signal(SIGINT, ...) failed \n");
> }
>
> if (signal(SIGUSR2, mySignalHandler) == SIG_ERR)
> {
> printf("signal(SIGUSR2, ...) failed \n");
> }
>
> while(1)
> {
> printf("main loop...\n");
> sleep(10);
> }
>
> return 1;
> }

Thomas Burger

unread,
Mar 31, 2014, 3:42:06 PM3/31/14
to ns-3-...@googlegroups.com
Dear Hajime,

thanks a lot for your answer!

I've created the example with dce-linux-1.2, where I get a SIGSEGV.

With dce-ns3-dev, there is no immediate SIGSEGV given the provided files,
but I think the Problem is still existing, please try setting a smaller dce.SetStackSize(1<<10)
or running the simulation longer Simulator::Stop(Seconds(3000))

Your stdout is snipped somehow unfortunate, does it continue with calling the signal handler?
In my case, the signal handler is called again and again (possibly leading to a SIGSEGV), which IMHO is wrong?
 
% files-0/var/log/53512/stdout


Testprogram for sighandler
main loop...
main loop...
main loop...
mySignalHandler received SIGUSR2
Doing a 1second sleep()
mySignalHandler received SIGUSR2
Doing a 1second sleep()
mySignalHandler received SIGUSR2
Doing a 1second sleep()
mySignalHandler received SIGUSR2
Doing a 1second sleep()
...

Thanks for your time and checking the files!

Best regards,
Thomas

Thomas Burger

unread,
Mar 31, 2014, 4:00:11 PM3/31/14
to ns-3-...@googlegroups.com
BTW, I use Ubuntu 12.04.3 LTS

Hajime Tazaki

unread,
Apr 2, 2014, 10:15:34 AM4/2/14
to ns-3-...@googlegroups.com

Hi,

At Mon, 31 Mar 2014 12:42:06 -0700 (PDT),
Thomas Burger wrote:
>
> [1 <text/plain; UTF-8 (quoted-printable)>]
> Dear Hajime,
>
> thanks a lot for your answer!
>
> I've created the example with dce-linux-1.2, where I get a SIGSEGV.
>
> With dce-ns3-dev, there is no immediate SIGSEGV given the provided files,
> but I think the Problem is still existing, please try setting a smaller
> dce.SetStackSize(1<<10)
> or running the simulation longer Simulator::Stop(Seconds(3000))

I successfully (?) reproduced SIGSEGV with small stack size. thanks.

> Your stdout is snipped somehow unfortunate, does it continue with calling
> the signal handler?
> In my case, the signal handler is called again and again (possibly leading
> to a SIGSEGV), which IMHO is wrong?

you're right.

after changes to UtilsDoSignal () as you suggested, the
following is the full output in my laptop. before that,
signal handler is called repeatedly as you mentioned.

Testprogram for sighandler
main loop...
main loop...
main loop...
mySignalHandler received SIGUSR2
Doing a 1second sleep()
1second sleep() finished
main loop...
main loop...
main loop...
main loop...
(snip, repeated about 10 times)

so, the problem might be:

- signal handler should not be invoked during handler's
context

is it correct understanding ?

I will look at the changes in detail and fix it in the dce
repo.

It'll be great if you filled a bugzilla entry for the issue.

https://www.nsnam.org/bugzilla/

thank you.
-- Hajime
> > an email to ns-3-users+...@googlegroups.com <javascript:>.
> > > To post to this group, send email to ns-3-...@googlegroups.com<javascript:>.
> [2 <text/html; UTF-8 (quoted-printable)>]
>

Thomas Burger

unread,
Apr 7, 2014, 4:31:06 AM4/7/14
to ns-3-...@googlegroups.com
Hi,

yes you're right, the signal handler should be called just once.

I've created a bugzilla entry https://www.nsnam.org/bugzilla/show_bug.cgi?id=1896
and referred to this newsgroup posting, I hope this is okay.

Thanks again for your time,
Thomas

Hajime Tazaki

unread,
Apr 23, 2014, 8:50:54 PM4/23/14
to ns-3-...@googlegroups.com

thanks for the bug entry.
i will look at the patch and merge it once test should be done.

-- Hajime

At Mon, 7 Apr 2014 01:31:06 -0700 (PDT),
> > > [2 <text/html; UTF-8 (quoted-printable)>]
Reply all
Reply to author
Forward
0 new messages