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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
############################################################################
# sync-engine
#
# Entry point and manager for sync-engine
#
# Adapted by Dr J A Gow 12/2007 from the original sync-engine
#
# Original sync-engine copyright (C) 2006 Ole André Vadla Ravnås
# <oleavr@gmail.com>
#
# 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.
############################################################################
version = (0, 1, 1)
import gobject
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from dbus.mainloop.glib import threads_init
import os
import signal
import sys
import threading
import logging
import getopt
import SyncEngine.config as Config
from SyncEngine.kernel import SyncEngine
logger = None
mainloop = None
#
# TerminationHandler
#
# Called when a request to terminate has been received, used to shut down
# the main loop
def TerminationHandler(signum, frame):
logger.info("Received termination signal. Exiting")
engine.StopSessions()
gobject.idle_add(mainloop.quit)
#
# MAIN ENTRY POINT
#
if __name__ == "__main__":
try:
progopts = Config.ValidateCommandOptions(sys.argv[1:])
except Exception,e:
if str(e) != "usage":
print
print "Command line error"
print
exit(0)
print "SynCE sync-engine starting up"
configObj = Config.Config(progopts)
# now, if we need to log, open our log file
if configObj.logfile != None:
try:
logstream = open(configObj.logfile,"wb")
except:
print "error: unable to open logfile %s, logging redirected to stdout" % configObj.logfile
logstream = sys.stdout
else:
logstream = sys.stdout
# now fork, if we need to
if configObj.fork==True:
try:
forkPid = os.fork()
if forkPid > 0:
print "SynCE sync-engine started as PID %d" % forkPid
sys.exit(0)
except OSError,e:
print "error: failed to fork - running in foreground (%s)" % e.strerror
pass
# if we get here, we're running in the daemon thread
# decouple from our terminal
os.chdir("/")
os.setsid()
# Redirect stdout and stderr to the logfile - unless the logfile is itself
# stdout in which case redirect it to /dev/null
f=os.open("/dev/null",os.O_RDWR)
if logstream != sys.stdout:
os.dup2(logstream.fileno(),1)
os.dup2(logstream.fileno(),2)
else:
os.dup2(f,1)
os.dup2(f,2)
# Redirect stdin to /dev/null
os.dup2(f,0)
if f>2:
os.close(f)
#
# Here, we are either the child process, or we are running in the foreground
logging.basicConfig(level=configObj.loglevel,
stream=logstream,
format='%(asctime)s %(levelname)s %(name)s : %(message)s')
logger = logging.getLogger("syncengine")
DBusGMainLoop(set_as_default=True)
logger.debug("running main loop")
gobject.threads_init()
dbus.mainloop.glib.threads_init()
mainloop = gobject.MainLoop()
logger.debug("creating SyncEngine object")
engine = SyncEngine(configObj,mainloop)
logger.debug("installing signal handlers")
signal.signal(signal.SIGTERM, TerminationHandler)
signal.signal(signal.SIGINT, TerminationHandler)
try:
mainloop.run()
except KeyboardInterrupt:
pass
logger.debug("waiting for engine to clean up")
engine.WaitForStoppingSessions()
|