-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbusgpiod
executable file
·87 lines (69 loc) · 3.15 KB
/
dbusgpiod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# GPL. (C) 2013 Paolo Patruno.
# Authors: Paolo Patruno <[email protected]>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import os,dbusgpio.daemon as daemon
from dbusgpio import _version_
import dbusgpio.settings
import dbusgpio.interface
dbusgpiod = daemon.Daemon(
stdin="/dev/null",
stdout=dbusgpio.settings.logfiledbusgpio,
stderr=dbusgpio.settings.errfiledbusgpio,
pidfile=dbusgpio.settings.lockfiledbusgpio,
user=dbusgpio.settings.userdbusgpio,
group=dbusgpio.settings.groupdbusgpio
)
def main ():
import logging,logging.handlers
handler = logging.handlers.RotatingFileHandler(dbusgpio.settings.logfiledbusgpio, maxBytes=5000000, backupCount=10)
formatter=logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
# Add the log message handler to the root logger
logging.getLogger('dbusgpiod').addHandler(handler)
logging.getLogger('').setLevel(logging.DEBUG)
logging.info('Starting up dbusgpiod version '+_version_)
# # Use logging for ouput at different *levels*.
# #
# logging.getLogger().setLevel(logging.INFO)
# log = logging.getLogger("dbusgpiod")
# handler = logging.StreamHandler(sys.stderr)
# log.addHandler(handler)
dbusgpio.interface.main(pinlist=dbusgpio.settings.pinlistdbusgpio,busaddress=dbusgpio.settings.busaddressdbusgpio)
if __name__ == '__main__':
# main()# (this code was run as script)
import sys, os
# this is a triky for ubuntu and debian that remove /var/run every boot
# ATTENTION, this should be a security problem
path=os.path.dirname(dbusgpio.settings.lockfiledbusgpio)
if (not os.path.lexists(path) and path == "/var/run/dbusgpio" ):
os.mkdir(path)
if (os.getuid() == 0):
user=dbusgpio.settings.userdbusgpio
group=dbusgpio.settings.groupdbusgpio
if user is not None and group is not None:
from pwd import getpwnam
from grp import getgrnam
uid = getpwnam(user)[2]
gid = getgrnam(group)[2]
os.chown(path,uid,gid)
if dbusgpiod.service(noptions=1000):
sys.stdout.write("dbusgpiod version "+_version_+"\n")
sys.stdout.write("Daemon started with pid %d\n" % os.getpid())
sys.stdout.write("Daemon stdout output\n")
sys.stderr.write("Daemon stderr output\n")
sys.exit(main()) # (this code was run as script)