PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/fstmerge/examples/wicd/rev519-537/right-branch-537/wicd/wicd-daemon.py

https://github.com/RoDaniel/featurehouse
Python | 185 lines | 144 code | 0 blank | 41 comment | 26 complexity | 8b670d13ca851046a715b939f9681ac5 MD5 | raw file
  1. """ wicd - wireless connection daemon implementation.
  2. This module implements the wicd daemon that provides network
  3. connection management, for both wireless and wired networks. The daemon
  4. must be run as root to control the networks, however the user interface
  5. components should be run as a normal user.
  6. class WicdDaemon -- The main DBus daemon for Wicd.
  7. def usage() -- Print usage information.
  8. def daemonize() -- Daemonize the current process with a double fork.
  9. def main() -- The wicd daemon main loop.
  10. """
  11. import sys, os, optparse, signal
  12. import gobject
  13. import dbus
  14. import dbus.service
  15. if getattr(dbus, 'version', (0, 0, 0)) < (0, 80, 0):
  16. import dbus.glib
  17. else:
  18. from dbus.mainloop.glib import DBusGMainLoop
  19. DBusGMainLoop(set_as_default=True)
  20. import wpath
  21. import misc
  22. from logfile import ManagedStdio
  23. from interfacemanager import InterfaceManager
  24. if __name__ == '__main__':
  25. wpath.chdir(__file__)
  26. misc.RenameProcess("wicd")
  27. class WicdDaemon(dbus.service.Object):
  28. def __init__(self, bus_name, options, object_path="/org/wicd/daemon"):
  29. ''' Creates a new WicdDaemon object. '''
  30. dbus.service.Object.__init__(self, bus_name=bus_name,
  31. object_path=object_path)
  32. self.interface_manager = InterfaceManager()
  33. self.options = options
  34. if not options.no_load_configuration:
  35. self.LoadConfiguration()
  36. @dbus.service.method('org.wicd.daemon')
  37. def GetVersion(self):
  38. """ Returns the version number.
  39. This number is major-minor-micro. Major is the major version of Wicd.
  40. Minor in incremented if Wicd undergoes major changes.
  41. Minor can be anything >= 0. Micro is for everything else, and micro
  42. may be anything >= 0. This number is effective starting wicd v1.2.0.
  43. """
  44. version = 'VPB 0.1.0'
  45. return version
  46. @dbus.service.method('org.wicd.daemon.interface')
  47. def GetInterfaces(self):
  48. ''' Updates the current state of the interfaces.
  49. In a wireless interface this might involve
  50. scanning for networks. In a wired network, this
  51. might involve check for a wire.
  52. '''
  53. return self.interface_manager.get_all_names()
  54. @dbus.service.method('org.wicd.daemon.interface')
  55. def CreateNewInterface(self, type, name):
  56. self.interface_manager.create(type, name)
  57. def _has_data(self, data_tuple):
  58. ''' Used to convert tuples sent over DBus to real tuples. '''
  59. if data_tuple in [('dbusdoesntallowemptytuples',), None, ()]:
  60. return None
  61. else:
  62. return data_tuple
  63. @dbus.service.method('org.wicd.daemon.interface')
  64. def GetInterfaceData(self, interface_name, method_name, data):
  65. ''' Gets the specified data from the specified interface. '''
  66. return self._interface_action(interface_name, method_name, data, 'get_')
  67. @dbus.service.method('org.wicd.daemon.interface')
  68. def SetInterfaceData(self, interface_name, method_name, data):
  69. ''' Sets the specified data on the specified interface. '''
  70. self._interface_action(interface_name, method_name, data, 'set_')
  71. @dbus.service.method('org.wicd.daemon.interface')
  72. def DoInterfaceAction(self, interface_name, method_name, data):
  73. ''' Runs the specified command on the specified interface. '''
  74. self._interface_action(interface_name, method_name, data, 'do_')
  75. def _interface_action(self, interface_name, method_name, data, prefix):
  76. ''' Runs a specified action on a specified method that starts with prefix. '''
  77. interface = self.interface_manager.get(interface_name)
  78. method = getattr(interface, prefix + method_name)
  79. print '%s interface action calling %s' % (prefix[:-1], method)
  80. if self._has_data(data):
  81. return method(*data)
  82. else:
  83. return method()
  84. @dbus.service.method('org.wicd.daemon')
  85. def SaveConfiguration(self):
  86. ''' Saves the state of the daemon. '''
  87. print 'saving configuration...'
  88. self.interface_manager.save()
  89. @dbus.service.method('org.wicd.daemon')
  90. def LoadConfiguration(self):
  91. ''' Loads the saved state of the daemon. '''
  92. print 'loading configuration...'
  93. self.interface_manager.load()
  94. def daemonize():
  95. """ Disconnect from the controlling terminal.
  96. Fork twice, once to disconnect ourselves from the parent terminal and a
  97. second time to prevent any files we open from becoming our controlling
  98. terminal.
  99. For more info see:
  100. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
  101. """
  102. try:
  103. pid = os.fork()
  104. if pid > 0:
  105. sys.exit(0)
  106. except OSError, e:
  107. print >> sys.stderr, "Fork #1 failed: %d (%s)" % (e.errno, e.strerror)
  108. sys.exit(1)
  109. os.setsid()
  110. os.umask(0)
  111. try:
  112. pid = os.fork()
  113. if pid > 0:
  114. dirname = os.path.dirname(wpath.pidfile)
  115. if not os.path.exists(dirname):
  116. os.makedirs(dirname)
  117. pidfile = open(wpath.pidfile, 'w')
  118. pidfile.write(str(pid) + '\n')
  119. pidfile.close()
  120. sys.exit(0)
  121. except OSError, e:
  122. print >> sys.stderr, "Fork #2 failed: %d (%s)" % (e.errno, e.strerror)
  123. sys.exit(1)
  124. sys.stdout.flush()
  125. sys.stderr.flush()
  126. os.close(sys.__stdin__.fileno())
  127. os.close(sys.__stdout__.fileno())
  128. os.close(sys.__stderr__.fileno())
  129. sys.stdin = open('/dev/null', 'r')
  130. def main(argv):
  131. """ The main daemon program.
  132. Keyword arguments:
  133. argv -- The arguments passed to the script.
  134. """
  135. global child_pid
  136. do_daemonize = True
  137. redirect_stderr = True
  138. redirect_stdout = True
  139. auto_connect = True
  140. p = optparse.OptionParser()
  141. p.add_option('--no-daemon', '-f', action='store_true')
  142. p.add_option('--no-stdout', '-o', action='store_true')
  143. p.add_option('--no-stderr', '-e', action='store_true')
  144. p.add_option('--no-autoconnect', '-a', action='store_true')
  145. p.add_option('--no-load-configuration', '-n', action='store_true')
  146. p.add_option('--add-current-dir-to-pythonpath', '-p', action='store_true')
  147. options, arguments = p.parse_args()
  148. if not options.no_daemon: daemonize()
  149. if (not options.no_stdout) or (not options.no_stderr):
  150. logpath = os.path.join(wpath.log, 'wicd.log')
  151. output = ManagedStdio(logpath)
  152. if os.path.exists(logpath):
  153. try:
  154. os.chmod(logpath, 0600)
  155. except:
  156. print 'unable to chmod log file to 0600'
  157. if not options.no_stdout: sys.stdout = output
  158. if not options.no_stderr: sys.stderr = output
  159. if options.add_current_dir_to_pythonpath:
  160. print 'adding',os.getcwd(),'to path'
  161. sys.path.insert(0, os.getcwd())
  162. print 'Wicd starting...'
  163. bus = dbus.SystemBus()
  164. wicd_bus = dbus.service.BusName('org.wicd.daemon', bus=bus)
  165. daemon = WicdDaemon(wicd_bus, options)
  166. gobject.threads_init()
  167. signal.signal(signal.SIGTERM, sigterm_caught)
  168. mainloop = gobject.MainLoop()
  169. mainloop.run()
  170. def sigterm_caught(sig, frame):
  171. """ Called when a SIGTERM is caught, kills monitor.py before exiting. """
  172. global child_pid
  173. print 'SIGTERM caught, killing wicd-monitor...'
  174. os.kill(child_pid, signal.SIGTERM)
  175. print 'Removing PID file...'
  176. if os.path.exists(wpath.pidfile):
  177. os.remove(wpath.pidfile)
  178. print 'Shutting down...'
  179. sys.exit(0)
  180. if __name__ == '__main__':
  181. if os.getuid() != 0:
  182. print ("Root priviledges are required for the daemon to run properly." +
  183. " Exiting.")
  184. sys.exit(1)
  185. main(sys.argv)