Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Create pid file to avoid having two daemons. #126

Merged
merged 3 commits into from
Aug 14, 2019

Conversation

jjnicola
Copy link
Member

Adds the --pid-file option in the parser.
Add method to create and remove the pid file.
Set the signal handler and atexit to remove the pid file when the daemon finishes.

How to:
by default, the pid path is /run/ospd/ospd.py. You will have to create the directory and set the right permissions.

sudo mkdir /run/ospd`
sudo chown jnicola:jnicola /run/ospd

Or you can run ospd-daemon with the option --pid-file and use an existing directory with rw permissions:

ospd-openvas --pid-file /tmp/ospd-openvas.pid

This is also valid to do it with the config file, adding under the right section the option. E.g.:

[OSPD - openvas]
log_level = DEBUG
socket_mode = 0o770
unix_socket = /tmp/openvas.sock
pid_file = /tmp/openvas.sock

Adds the --pid-file option in the parser.
Add method to create and remove the pid file.
Set the signal handler and atexit to remove the pid file when the daemon finishes.
@codecov
Copy link

codecov bot commented Aug 14, 2019

Codecov Report

Merging #126 into master will decrease coverage by 0.89%.
The diff coverage is 16.12%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #126     +/-   ##
=========================================
- Coverage   69.67%   68.78%   -0.9%     
=========================================
  Files          12       12             
  Lines        1672     1701     +29     
=========================================
+ Hits         1165     1170      +5     
- Misses        507      531     +24
Impacted Files Coverage Δ
ospd/main.py 0% <0%> (ø) ⬆️
ospd/parser.py 86.48% <100%> (+0.37%) ⬆️
ospd/misc.py 75.33% <15%> (-5.95%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 20fdaba...97b05e0. Read the comment docs.

ospd/misc.py Outdated

pid = str(os.getpid())

if os.path.isfile(pidfile):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use pathlib instead

Suggested change
if os.path.isfile(pidfile):
if Path(pidfile).is_file():

ospd/misc.py Show resolved Hide resolved
ospd/misc.py Outdated

def remove_pidfile(pidfile, signum=None, frame=None):
""" Removes the pidfile before ending the daemon. """
if os.path.isfile(pidfile):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the pathlib here too. Maybe it is even better to already pass pidfile as a Path object to the functions but i am not 100% sure about that.

ospd/misc.py Outdated
""" Removes the pidfile before ending the daemon. """
if os.path.isfile(pidfile):
LOGGER.debug("Finishing daemon process")
os.remove(pidfile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be done via a Path instance

pidpath = Path(pidfile)
if pidfile.is_file():
  ...
  pidfile.unlink()

ospd/main.py Show resolved Hide resolved
Now the pid in the pid file is the right one.
Also initialize the daemon after going to background, to not hang in the console during initialization.
Copy link
Contributor

@bjoernricks bjoernricks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@bjoernricks bjoernricks merged commit b9b81b5 into greenbone:master Aug 14, 2019
@jjnicola
Copy link
Member Author

Thank you @bjoernricks !

@jjnicola jjnicola deleted the pid-file branch August 14, 2019 10:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants