Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
62e912d3
Commit
62e912d3
authored
Feb 01, 2016
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(configd) add logging handle to daemonize
+ upgrade daemonize => 2.4.2 + rc script require fix
parent
8f92a6a2
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
108 additions
and
69 deletions
+108
-69
configd
src/etc/rc.d/configd
+1
-1
configd.py
src/opnsense/service/configd.py
+12
-1
daemonize.py
src/opnsense/service/modules/daemonize.py
+95
-67
No files found.
src/etc/rc.d/configd
View file @
62e912d3
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
# $FreeBSD$
# $FreeBSD$
#
#
# PROVIDE: configd
# PROVIDE: configd
# REQUIRE:
LOGIN
# REQUIRE:
REQUIRE
# KEYWORD: shutdown
# KEYWORD: shutdown
#
#
...
...
src/opnsense/service/configd.py
View file @
62e912d3
...
@@ -35,6 +35,7 @@ __author__ = 'Ad Schellevis'
...
@@ -35,6 +35,7 @@ __author__ = 'Ad Schellevis'
import
os
import
os
import
sys
import
sys
import
logging
import
modules.processhandler
import
modules.processhandler
import
modules.csconfigparser
import
modules.csconfigparser
from
modules.daemonize
import
Daemonize
from
modules.daemonize
import
Daemonize
...
@@ -102,8 +103,18 @@ if len(sys.argv) > 1 and 'console' in sys.argv[1:]:
...
@@ -102,8 +103,18 @@ if len(sys.argv) > 1 and 'console' in sys.argv[1:]:
else
:
else
:
proc_handler
.
run
()
proc_handler
.
run
()
else
:
else
:
# bind log handle to syslog to catch messages from Daemonize()
loghandle
=
logging
.
getLogger
(
"configd.py"
)
loghandle
.
setLevel
(
logging
.
INFO
)
handler
=
logging
.
handlers
.
SysLogHandler
(
address
=
"/var/run/log"
,
facility
=
logging
.
handlers
.
SysLogHandler
.
LOG_DAEMON
)
handler
.
setFormatter
(
logging
.
Formatter
(
"
%(name)
s
%(message)
s"
))
loghandle
.
addHandler
(
handler
)
# daemonize process
# daemonize process
daemon
=
Daemonize
(
app
=
__file__
.
split
(
'/'
)[
-
1
]
.
split
(
'.py'
)[
0
],
pid
=
cnf
.
get
(
'main'
,
'pid_filename'
),
action
=
proc_handler
.
run
)
daemon
=
Daemonize
(
app
=
__file__
.
split
(
'/'
)[
-
1
]
.
split
(
'.py'
)[
0
],
pid
=
cnf
.
get
(
'main'
,
'pid_filename'
),
action
=
proc_handler
.
run
,
logger
=
loghandle
)
daemon
.
start
()
daemon
.
start
()
sys
.
exit
(
0
)
sys
.
exit
(
0
)
src/opnsense/service/modules/daemonize.py
View file @
62e912d3
#!/usr/local/bin/python2.7
# #!/usr/bin/python
# original source from https://github.com/thesharp/daemonize
import
fcntl
import
fcntl
import
os
import
os
...
@@ -14,25 +12,36 @@ import atexit
...
@@ -14,25 +12,36 @@ import atexit
from
logging
import
handlers
from
logging
import
handlers
__version__
=
"2.4.2"
class
Daemonize
(
object
):
class
Daemonize
(
object
):
""" Daemonize object
"""
Object constructor expects three arguments:
Daemonize object.
- app: contains the application name which will be sent to syslog.
- pid: path to the pidfile.
Object constructor expects three arguments.
- action: your custom function which will be executed after daemonization.
- keep_fds: optional list of fds which should not be closed.
:param app: contains the application name which will be sent to syslog.
- auto_close_fds: optional parameter to not close opened fds.
:param pid: path to the pidfile.
- privileged_action: action that will be executed before drop privileges if user or
:param action: your custom function which will be executed after daemonization.
:param keep_fds: optional list of fds which should not be closed.
:param auto_close_fds: optional parameter to not close opened fds.
:param privileged_action: action that will be executed before drop privileges if user or
group parameter is provided.
group parameter is provided.
If you want to transfer anything from privileged_action to action, such as
If you want to transfer anything from privileged_action to action, such as
opened privileged file descriptor, you should return it from
opened privileged file descriptor, you should return it from
privileged_action function and catch it inside action function.
privileged_action function and catch it inside action function.
- user: drop privileges to this user if provided.
:param user: drop privileges to this user if provided.
- group: drop privileges to this group if provided.
:param group: drop privileges to this group if provided.
- verbose: send debug messages to logger if provided.
:param verbose: send debug messages to logger if provided.
- logger: use this logger object instead of creating new one, if provided.
:param logger: use this logger object instead of creating new one, if provided.
:param foreground: stay in foreground; do not fork (for debugging)
:param chdir: change working directory if provided or /
"""
"""
def
__init__
(
self
,
app
,
pid
,
action
,
keep_fds
=
None
,
auto_close_fds
=
True
,
privileged_action
=
None
,
user
=
None
,
group
=
None
,
verbose
=
False
,
logger
=
None
):
def
__init__
(
self
,
app
,
pid
,
action
,
keep_fds
=
None
,
auto_close_fds
=
True
,
privileged_action
=
None
,
user
=
None
,
group
=
None
,
verbose
=
False
,
logger
=
None
,
foreground
=
False
,
chdir
=
"/"
):
self
.
app
=
app
self
.
app
=
app
self
.
pid
=
pid
self
.
pid
=
pid
self
.
action
=
action
self
.
action
=
action
...
@@ -43,9 +52,11 @@ class Daemonize(object):
...
@@ -43,9 +52,11 @@ class Daemonize(object):
self
.
logger
=
logger
self
.
logger
=
logger
self
.
verbose
=
verbose
self
.
verbose
=
verbose
self
.
auto_close_fds
=
auto_close_fds
self
.
auto_close_fds
=
auto_close_fds
self
.
foreground
=
foreground
self
.
chdir
=
chdir
def
sigterm
(
self
,
signum
,
frame
):
def
sigterm
(
self
,
signum
,
frame
):
"""
sigterm method
"""
These actions will be done after SIGTERM.
These actions will be done after SIGTERM.
"""
"""
self
.
logger
.
warn
(
"Caught signal
%
s. Stopping daemon."
%
signum
)
self
.
logger
.
warn
(
"Caught signal
%
s. Stopping daemon."
%
signum
)
...
@@ -53,7 +64,7 @@ class Daemonize(object):
...
@@ -53,7 +64,7 @@ class Daemonize(object):
sys
.
exit
(
0
)
sys
.
exit
(
0
)
def
exit
(
self
):
def
exit
(
self
):
"""
exit method
"""
Cleanup pid file at exit.
Cleanup pid file at exit.
"""
"""
self
.
logger
.
warn
(
"Stopping daemon."
)
self
.
logger
.
warn
(
"Stopping daemon."
)
...
@@ -61,8 +72,8 @@ class Daemonize(object):
...
@@ -61,8 +72,8 @@ class Daemonize(object):
sys
.
exit
(
0
)
sys
.
exit
(
0
)
def
start
(
self
):
def
start
(
self
):
"""
start method
"""
Main
daemonization process.
Start
daemonization process.
"""
"""
# If pidfile already exists, we should read pid from there; to overwrite it, if locking
# If pidfile already exists, we should read pid from there; to overwrite it, if locking
# will fail, because locking attempt somehow purges the file contents.
# will fail, because locking attempt somehow purges the file contents.
...
@@ -86,6 +97,8 @@ class Daemonize(object):
...
@@ -86,6 +97,8 @@ class Daemonize(object):
pidfile
.
write
(
old_pid
)
pidfile
.
write
(
old_pid
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
# skip fork if foreground is specified
if
not
self
.
foreground
:
# Fork, creating a new process for the child.
# Fork, creating a new process for the child.
process_id
=
os
.
fork
()
process_id
=
os
.
fork
()
if
process_id
<
0
:
if
process_id
<
0
:
...
@@ -144,7 +157,7 @@ class Daemonize(object):
...
@@ -144,7 +157,7 @@ class Daemonize(object):
# We will continue with syslog initialization only if actually have such capabilities
# We will continue with syslog initialization only if actually have such capabilities
# on the machine we are running this.
# on the machine we are running this.
if
os
.
path
.
isfile
(
syslog_address
):
if
os
.
path
.
exists
(
syslog_address
):
syslog
=
handlers
.
SysLogHandler
(
syslog_address
)
syslog
=
handlers
.
SysLogHandler
(
syslog_address
)
if
self
.
verbose
:
if
self
.
verbose
:
syslog
.
setLevel
(
logging
.
DEBUG
)
syslog
.
setLevel
(
logging
.
DEBUG
)
...
@@ -163,20 +176,35 @@ class Daemonize(object):
...
@@ -163,20 +176,35 @@ class Daemonize(object):
# Change to a known directory. If this isn't done, starting a daemon in a subdirectory that
# Change to a known directory. If this isn't done, starting a daemon in a subdirectory that
# needs to be deleted results in "directory busy" errors.
# needs to be deleted results in "directory busy" errors.
os
.
chdir
(
"/"
)
os
.
chdir
(
self
.
chdir
)
# Execute privileged action
# Execute privileged action
privileged_action_result
=
self
.
privileged_action
()
privileged_action_result
=
self
.
privileged_action
()
if
not
privileged_action_result
:
if
not
privileged_action_result
:
privileged_action_result
=
[]
privileged_action_result
=
[]
# Change gid
# Change owner of pid file, it's required because pid file will be removed at exit.
uid
,
gid
=
-
1
,
-
1
if
self
.
group
:
if
self
.
group
:
try
:
try
:
gid
=
grp
.
getgrnam
(
self
.
group
)
.
gr_gid
gid
=
grp
.
getgrnam
(
self
.
group
)
.
gr_gid
except
KeyError
:
except
KeyError
:
self
.
logger
.
error
(
"Group {0} not found"
.
format
(
self
.
group
))
self
.
logger
.
error
(
"Group {0} not found"
.
format
(
self
.
group
))
sys
.
exit
(
1
)
sys
.
exit
(
1
)
if
self
.
user
:
try
:
uid
=
pwd
.
getpwnam
(
self
.
user
)
.
pw_uid
except
KeyError
:
self
.
logger
.
error
(
"User {0} not found."
.
format
(
self
.
user
))
sys
.
exit
(
1
)
if
uid
!=
-
1
or
gid
!=
-
1
:
os
.
chown
(
self
.
pid
,
uid
,
gid
)
# Change gid
if
self
.
group
:
try
:
try
:
os
.
setgid
(
gid
)
os
.
setgid
(
gid
)
except
OSError
:
except
OSError
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment