Exemplo n.º 1
0
def run_server(debug_gc=False):
    """ Main function for firewall server. Handles D-Bus and GLib mainloop.
    """
    service = None
    if debug_gc:
        from pprint import pformat
        import gc
        gc.enable()
        gc.set_debug(gc.DEBUG_LEAK)

        gc_timeout = 10

        def gc_collect():
            gc.collect()
            if len(gc.garbage) > 0:
                print("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                      ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
                print("GARBAGE OBJECTS (%d):\n" % len(gc.garbage))
                for x in gc.garbage:
                    print(
                        type(x),
                        "\n  ",
                    )
                    print(pformat(x))
                print("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
                      "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
            GLib.timeout_add_seconds(gc_timeout, gc_collect)

    try:
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        bus = dbus.SystemBus()
        name = dbus.service.BusName(config.dbus.DBUS_INTERFACE, bus=bus)
        service = FirewallD(name, config.dbus.DBUS_PATH)

        mainloop = GLib.MainLoop()
        if debug_gc:
            GLib.timeout_add_seconds(gc_timeout, gc_collect)

        # use unix_signal_add if available, else unix_signal_add_full
        if hasattr(GLib, 'unix_signal_add'):
            unix_signal_add = GLib.unix_signal_add
        else:
            unix_signal_add = GLib.unix_signal_add_full

        unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP, sighup, service)
        unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM, sigterm, mainloop)

        mainloop.run()

    except KeyboardInterrupt:
        log.debug1("Stopping..")

    except SystemExit:
        log.error("Raising SystemExit in run_server")

    except Exception as e:
        log.error("Exception %s: %s", e.__class__.__name__, str(e))

    if service:
        service.stop()
Exemplo n.º 2
0
def run_server(debug_gc=False, persistent=False):
    """ Main function for rolekit server. Handles D-Bus and GLib mainloop.
    """
    service = None
    if debug_gc:
        from pprint import pformat
        import gc
        gc.enable()
        gc.set_debug(gc.DEBUG_LEAK)

        gc_timeout = 10
        def gc_collect():
            gc.collect()
            if len(gc.garbage) > 0:
                print("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                      ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
                print("GARBAGE OBJECTS (%d):\n" % len(gc.garbage))
                for x in gc.garbage:
                    print(type(x),"\n  ",)
                    print(pformat(x))
                print("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
                      "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
            GLib.timeout_add_seconds(gc_timeout, gc_collect)

    try:
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        bus = dbus.SystemBus()
        name = dbus.service.BusName(DBUS_INTERFACE, bus=bus)
        service = RoleD(name, DBUS_PATH, persistent=persistent)

        mainloop = GLib.MainLoop()
        slip.dbus.service.set_mainloop(mainloop)
        if debug_gc:
            GLib.timeout_add_seconds(gc_timeout, gc_collect)

        # use unix_signal_add if available, else unix_signal_add_full
        if hasattr(GLib, 'unix_signal_add'):
            unix_signal_add = GLib.unix_signal_add
        else:
            unix_signal_add = GLib.unix_signal_add_full

        unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP,
                        sighup, None)
        unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM,
                        sigterm, mainloop)

        mainloop.run()

    except KeyboardInterrupt as e:
        pass

    except SystemExit as e:
        log.error("Raising SystemExit in run_server")

    except Exception as e:
        log.error("Exception %s: %s", e.__class__.__name__, str(e))

    if service:
        service.stop()
Exemplo n.º 3
0
def run_server():
    """ Main function for firewall server. Handles D-BUS and GLib mainloop.
    """
    service = None

    try:
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        bus = dbus.SystemBus()
        name = dbus.service.BusName(DBUS_INTERFACE, bus=bus)
        service = FirewallD(name, DBUS_PATH)

        mainloop = GLib.MainLoop()
        slip.dbus.service.set_mainloop(mainloop)

        # use unix_signal_add if available, else unix_signal_add_full
        if hasattr(GLib, 'unix_signal_add'):
            unix_signal_add = GLib.unix_signal_add
        else:
            unix_signal_add = GLib.unix_signal_add_full

        unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP,
                        sighup, None)
        unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM,
                        sigterm, mainloop)

        mainloop.run()

    except KeyboardInterrupt as e:
        log.info1("Stopping..")

    except SystemExit as e:
        log.error("Raising SystemExit in run_server")

    except Exception as e:
        log.error("Exception %s: %s", e.__class__.__name__, str(e))

    if service:
        service.stop()
Exemplo n.º 4
0
    @dbus.service.method(dbus_interface='pl.glorpen.PySolar',
                         in_signature='s',
                         out_signature='u')
    def GetLastCharge(self, dj_path):
        if dj_path in self.manager.djs:
            return self.manager.djs[dj_path].last_charge
        else:
            raise Exception("Path %s was not registered" % dj_path)

    def report_handler(self, dj, num, report):
        if report.has_lightness():
            self.LightnessEvent(dj.devpath, num, report.get_charge(),
                                report.get_lightness())
        else:
            self.ChargeEvent(dj.devpath, num, report.get_charge())


if __name__ == "__main__":
    logger.setLevel(logging.DEBUG)
    gobject.threads_init()

    service = SolarDBus()
    try:
        service.start()
    except KeyboardInterrupt:
        pass
    except Exception as e:
        logger.error("error: %s" % e)
    finally:
        service.stop()
Exemplo n.º 5
0
def run_client():

    # parse arguments
    parser = optparse.OptionParser(usage="pynetkey [options]")
    parser.add_option("--open",
                      action="store_true",
                      dest="open",
                      default=False,
                      help="open firewall")
    parser.add_option("--close",
                      action="store_true",
                      dest="close",
                      default=False,
                      help="close firewall")

    #~ parser.add_option("--start", action="store_true", dest="start", default=False, help="start a Pynetkey process")
    parser.add_option("--stop",
                      action="store_true",
                      dest="stop",
                      default=False,
                      help="shutdown all Pynetkey processes")
    parser.add_option("--kill",
                      action="store_true",
                      dest="kill",
                      default=False,
                      help="kill all Pynetkey processes (%s)" % pkill_command)

    #~ group = optparse.OptionGroup(parser, "Wait options", "will block until true")
    #~ group.add_option("--wait_until_open", action="store_true", dest="wait_until_open", default=False, help="block until firewall open")
    #~ group.add_option("--wait_until_closed", action="store_true", dest="wait_until_closed", default=False, help="block until firewall closed")
    #~ group.add_option("--wait_until_started", action="store_true", dest="wait_until_started", default=False, help="block until pynetkey started")
    #~ group.add_option("--wait_until_stopped", action="store_true", dest="wait_until_stopped", default=False, help="block until pynetkey stopped")
    #~ parser.add_option_group(group)

    group = optparse.OptionGroup(parser, "Query options",
                                 "print status to stdout")
    group.add_option("--pid",
                     action="store_true",
                     dest="pid",
                     default=False,
                     help="print process ID to stdout")
    group.add_option(
        "--json",
        action="store_true",
        dest="json_status",
        default=False,
        help="print status as returned by firewall to stdout (as json)")
    group.add_option("--status",
                     action="store_true",
                     dest="status",
                     default=False,
                     help="print firewall status to stdout")
    #~ group.add_option("--usage", action="store_true", dest="usage", default=False, help="print current user's usage to stdout")
    group.add_option("--user",
                     action="store_true",
                     dest="user",
                     default=False,
                     help="print current inetkey user to stdout")
    parser.add_option_group(group)

    options, args = parser.parse_args()

    if options.kill:
        os.system(pkill_command)
        return

    bus = dbus.SessionBus()
    try:
        proxy = bus.get_object(bus_name, object_path)
    except dbus.exceptions.DBusException:
        service = None
    else:
        service = dbus.Interface(proxy, bus_name)

    #~ if options.start:
    #~ if service:
    #~ print "pynetkey already started"
    #~ return
    #~ pid = os.fork() #XXX not completely correct
    #~ print "pid", pid
    #~ if pid == 0:
    #~ os.system(r"./pynetkey.py &> \dev\null")
    #~ return

    if not service:
        print "pynetkey not started."
        parser.print_help()
        return

    if options.stop:
        print "closing firewall and stopping pynetkey process. ctrl-c will kill pynetkey process."
        try:
            service.stop()
        except KeyboardInterrupt:
            print "keyboard interrupt. running %s" % str(pkill_command)
            os.system(pkill_command)
    #~ elif options.wait_until_started:
    #~ pass
    #~ elif options.wait_until_stopped:
    #~ pass
    #~ elif options.wait_until_open or options.wait_until_closed:
    #~ status_to_wait_for = dict(wait_until_open="open", wait_until_closed="closed")["wait_until_open"]
    #~ interval = 1
    #~ timeout = None # in minutes
    #~ status = None # in seconds
    #~ start_time = time.time()
    #~ while 1:
    #~ status = service.status()
    #~ if status == status_to_wait_for:
    #~ break
    #~ if timeout is not None and timeout*60 < abs(time.time() - start_time): # take abs to handle system time change
    #~ break
    #~ time.sleep(interval)
    #~ print status
    elif options.open:
        service.open()
    elif options.close:
        service.close()
    elif options.pid:  # output must be clean to allow usage by other scripts
        print service.pid()
    elif options.status:  # output must be clean to allow usage by other scripts
        print service.status()
    elif options.json_status:  # output must be clean to allow usage by other scripts
        print service.json_status()
    elif options.user:  # output must be clean to allow usage by other scripts
        print service.user()
    else:
        #~ print "nothing to do"
        parser.print_help()