I therefore decided to create both a daemon module and a http server to test it.
Code availability
Both modules are available on GitHubThe daemon module is called daemon.py and the httpserver is called restrictedhttpserver.py. Both have a fair amount of comments in their source code that are the result of me trying to fully understand what is required to get things working.
A small word of warning here: I only tested it with Python3 on Ubuntu and Raspbian and I do not expect it to work on any other operating system (although most *nix like systems probably will work) and in my world Python2 is no longer a valid option.
Also, the Daemon
class makes use of an undocumented attribute in the logging.FileHandler class (the stream.fileno attribute) which is necessary in order not to close this stream when forking a child process. Theoretically this might break in the future but I did want all logging to happen outside the chroot jail so that the process running as a daemon can log but does not have access to the logging. Whether this is a valid approach remains to be seen as this also prevents proper log rotation.
The Daemon class
The basic requirement are all implemented, an instance of a Daemon class can:- run in a chrooted jail
- so it cannot access files outside a given directory
- run with lowered privileges
- so the risk of accessing files not owned by a specific user is lowered
- can have a umask of your choice
- which will ensure that newly created files by the daemon are not open to all
- log outside its jail
- so that we can log events without offering the process managed by daemon any way of altering the logging
- and maintains a configurable pid file
- so that we can prevent the daemon from starting more than once and providing a way the fond out the process id so that we can terminate the daemon
Daemon
class follow the Singleton pattern, i.e. there can only be one instanced object of the class. This is sensible because a process can only be daemonized once. We also implemented to required methods to facilitate use as a context manager. A typical, minimal code snippet would look something like:from daemon import Daemon dm = Daemon() with dm: ... do something forever ...All configurable options have sensible defaults, but a call could look like this:
from daemon import Daemon dm = Daemon(user='httpserver', rootdir='/var/www', umask=0o27, pidfile='/var/lockhttpserver.pid', logfile='/var/log/httpserver.log', name="Http Server") with dm: ... do something forever ...It is important to understand that when the Daemon object is created, the process is not immediately daemonized. This is handled by the context manager (or you could call the
daemonize()
method directly). There is also a stop()
method provided that will check for a running daemon process and terminate it. This could look like this:from daemon import Daemon import argparse parser = argparse.ArgumentParser(description="Example Daemon") parser.add_argument('-s', '--stop', action='store_true', help='stop a running server') args = parser.parse_args() dm = Daemon() if args.stop: dm.stop() else: with dm: ... do something forever ...
Thank you for providing useful information and its best blog for the students to make good career.learn Python training course.
ReplyDeletePython Training in Hyderabad
Thank you for sharing valuable article with good information in this blog.learn Data science course and make good career.
ReplyDeleteData Science Training in Hyderabad
Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!! You have been helping many application.
ReplyDeletebest selenium training in chennai | best selenium training institute in chennai selenium training in chennai | best selenium training in chennai | selenium training in Velachery | selenium training in chennai omr | quora selenium training in chennai | selenium testing course fees | java and selenium training in chennai | best selenium training institute in chennai | best selenium training center in chennai
A befuddling web diary I visit this blog, it's incredibly grand. Strangely, in this present blog's substance made motivation behind fact and sensible. The substance of information is instructive
ReplyDeleteOracle Fusion Financials Online Training
Oracle Fusion HCM Online Training
Oracle Fusion SCM Online Training
Hi, thanks for your blog, if you want to learn about programming languages like java, php, android app, embedded system etc. I think this training institute is the best one.
ReplyDeletebest python training in coimbatore
Android training in coimbatore
Networking training in coimbatore
Thank you so much for these kinds of informative blogs.
ReplyDeletewe also providesseo services
graphic designing company in gurgaon
website company in gurgaon
best website designing company in india
top website designing company in india
website designing company in gurgaon
website development company in gurgaon
web development company in gurgaon
best website designing company in gurgaon
website designing services in gurgaon
web design company in gurgaon
website company in gurgaon
Website design Company in gurgaon
website designing in gurgaon
website designing company in gurgaon
website design in gurgaon
website design company in gurgaon
website design services in gurgaon
website design service in gurgaon
website designing in gurgaon
web design services in gurgaon
best website design company in gurgaon
best web design company in gurgaon
best website design in gurgaon
best website design services in gurgaon
best website designing services in gurgaon
best web design in gurgaon
best web design company in gurgaon
best web designing services in gurgaon
best web design services in gurgaon
I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
ReplyDeleteDelhi University BCOM 1st, 2nd & 3rd Year TimeTable 2020
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
ReplyDeletehttp://chennaitraining.in/creo-training-in-chennai/
http://chennaitraining.in/building-estimation-and-costing-training-in-chennai/
http://chennaitraining.in/machine-learning-training-in-chennai/
http://chennaitraining.in/data-science-training-in-chennai/
http://chennaitraining.in/rpa-training-in-chennai/
http://chennaitraining.in/blueprism-training-in-chennai/
This professional hacker is absolutely reliable and I strongly recommend him for any type of hack you require. I know this because I have hired him severally for various hacks and he has never disappointed me nor any of my friends who have hired him too, he can help you with any of the following hacks:
ReplyDelete-Phone hacks (remotely)
-Credit repair
-Bitcoin recovery (any cryptocurrency)
-Make money from home (USA only)
-Social media hacks
-Website hacks
-Erase criminal records (USA & Canada only)
-Grade change
-funds recovery
Email: onlineghosthacker247@ gmail .com
I am happy to peruse this post, it's a decent one. I am continually searching for quality posts and articles and this is the thing that I discovered here, much obliged for sharing. social media marketing agency in chennai Exceptionally Informative and very much expressed Thanks for imparting this data to us.
ReplyDeleteThank you for sharing your info. I truly appreciate your efforts and
ReplyDeleteI will be waiting for your next post thank you once again.
Histogram Grayscale opencv python
how to insert unicode characters in mysql using php
how to display PDF in HTML using Javascript
Histogram Equalization OpenCV
Matplotlib pie chart
Password hash and verify in PHP
CRUD operations in Python using MySQL
Export data from MySQL table to CSV file using PHP
Actually I read it yesterday I looked at most of your posts but I had some ideas about it . This article is probably where I got the most useful information for my research and today I wanted to read it again because it is so well written.
ReplyDeleteData Science Course in Bangalore
i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
ReplyDeletedata science training in bangalore
Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
ReplyDeleteData Science Training in Bangalore
I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
ReplyDeletedata analytics training in bangalore