Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
42fa09a6
Commit
42fa09a6
authored
7 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
dhtnode: graceful signal handling in service and daemon modes
parent
673884a4
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
tools/dhtnode.cpp
+1
-2
1 addition, 2 deletions
tools/dhtnode.cpp
tools/tools_common.h
+25
-1
25 additions, 1 deletion
tools/tools_common.h
with
26 additions
and
3 deletions
tools/dhtnode.cpp
+
1
−
2
View file @
42fa09a6
...
...
@@ -386,8 +386,7 @@ main(int argc, char **argv)
}
if
(
params
.
daemonize
or
params
.
service
)
{
while
(
true
)
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
30
));
while
(
runner
.
wait
());
}
else
{
cmd_loop
(
dht
,
params
);
}
...
...
This diff is collapsed.
Click to expand it.
tools/tools_common.h
+
25
−
1
View file @
42fa09a6
...
...
@@ -18,6 +18,7 @@
*/
// Common utility methods used by C++ OpenDHT tools.
#pragma once
#include
<opendht.h>
#ifndef WIN32_NATIVE
...
...
@@ -37,6 +38,8 @@
#include
<string>
#include
<vector>
#include
<chrono>
#include
<mutex>
#include
<condition_variable>
#include
<iostream>
#include
<sstream>
#include
<fstream>
...
...
@@ -204,13 +207,33 @@ readLine(const char* prefix = PROMPT)
return
line_read
?
std
::
string
(
line_read
)
:
std
::
string
(
"
\0
"
,
1
);
}
struct
ServiceRunner
{
bool
wait
()
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
m
);
cv
.
wait
(
lock
,
[
&
]{
return
terminate
;});
return
!
terminate
;
}
void
kill
()
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
m
);
terminate
=
true
;
cv
.
notify_all
();
}
private
:
std
::
condition_variable
cv
;
std
::
mutex
m
;
bool
terminate
=
false
;
};
ServiceRunner
runner
;
void
signal_handler
(
int
sig
)
{
switch
(
sig
)
{
case
SIGHUP
:
break
;
case
SIGINT
:
case
SIGTERM
:
exit
(
EXIT_SUCCESS
);
runner
.
kill
(
);
break
;
}
}
...
...
@@ -223,6 +246,7 @@ void setupSignals()
signal
(
SIGTTOU
,
SIG_IGN
);
signal
(
SIGTTIN
,
SIG_IGN
);
signal
(
SIGHUP
,
signal_handler
);
/* catch hangup signal */
signal
(
SIGINT
,
signal_handler
);
/* catch interrupt signal */
signal
(
SIGTERM
,
signal_handler
);
/* catch kill signal */
#endif
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment