diff --git a/Makefile.am b/Makefile.am index f52e1f0248..61b89bd9f8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1466,10 +1466,10 @@ src_libNetworkManager_la_CPPFLAGS = $(src_cppflags) src_libNetworkManager_la_SOURCES = \ \ - src/nm-checkpoint-manager.c \ - src/nm-checkpoint-manager.h \ src/nm-checkpoint.c \ src/nm-checkpoint.h \ + src/nm-checkpoint-manager.c \ + src/nm-checkpoint-manager.h \ \ src/devices/nm-device.c \ src/devices/nm-device.h \ diff --git a/Makefile.examples b/Makefile.examples index d49db6ce86..d5433395c3 100644 --- a/Makefile.examples +++ b/Makefile.examples @@ -150,6 +150,8 @@ EXTRA_DIST += \ examples/nm-conf.d/30-anon.conf \ examples/nm-conf.d/31-mac-addr-change.conf \ \ + examples/python/nmex.py \ + \ examples/python/dbus/nm-state.py \ examples/python/dbus/add-connection.py \ examples/python/dbus/add-connection-compat.py \ diff --git a/examples/python/gi/checkpoint.py b/examples/python/gi/checkpoint.py old mode 100644 new mode 100755 index 513e6c3a0a..7c36cdba57 --- a/examples/python/gi/checkpoint.py +++ b/examples/python/gi/checkpoint.py @@ -18,98 +18,153 @@ import gi gi.require_version('NM', '1.0') from gi.repository import GLib, NM +import os +os.sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +import nmex + +############################################################################### + def usage(): - print "Usage: %s [COMMAND [ARG]...]" % sys.argv[0] - print "" - print " COMMANDS: show" - print " create TIMEOUT [DEV]..." - print " destroy PATH|NUMBER" - print " rollback PATH|NUMBER" - print + print("Usage: %s [COMMAND [ARG]...]" % sys.argv[0]) + print("") + print(" COMMANDS: [show]") + print(" create TIMEOUT [--destroy-all|--delete-new-connections|--disconnect-new-devices|--allow-overlapping|DEV]...") + print(" destroy PATH|NUMBER") + print(" rollback PATH|NUMBER") + print(" adjust-rollback-timeout PATH|NUMBER TIMEOUT") + print("") sys.exit(1) -def create_cb(client, result, data): - try: - checkpoint = client.checkpoint_create_finish(result) - print("%s" % checkpoint.get_path()) - except Exception, e: - sys.stderr.write("Failed: %s\n" % e.message) - main_loop.quit() - -def do_create(client): - if len(sys.argv) < 3: - sys.exit("Failed: %s\n" % e.message) - - timeout = int(sys.argv[2]) - devices = [] - for arg in sys.argv[3:]: - d = client.get_device_by_iface(arg) - if d is None: - sys.exit("Unknown device %s" % arg) - devices.append(d) - - client.checkpoint_create_async(devices, timeout, 0, None, create_cb, None) - -def destroy_cb(client, result, data): - try: - if client.checkpoint_destroy_finish(result) == True: - print "Success" - except Exception, e: - sys.stderr.write("Failed: %s\n" % e.message) - main_loop.quit() - -def find_checkpoint(client, arg): - try: - num = int(arg) - path = "/org/freedesktop/NetworkManager/Checkpoint/%u" % num - except Exception, e: - path = arg +def show(c, ts = None): + cr = c.get_created() + rt = c.get_rollback_timeout() + print("%s:" % c.get_path()) + print(" created: %u%s" % (cr, "" if ts is None else (" (%s sec ago)" % ((ts - cr) / 1000.0)))) + if rt == 0: + print(" timeout: infinity") + else: + print(" timeout: %u seconds%s" % (rt, "" if ts is None else (" (circa %s sec left)" % ((cr + (rt * 1000) - ts) / 1000.0)))) + print(" devices: %s" % (' '.join(sorted(map(lambda x: x.get_iface(), c.get_devices()))))) +def find_checkpoint(client, path): for c in client.get_checkpoints(): if c.get_path() == path: return c return None +def validate_path(path, client): + try: + num = int(path) + path = "/org/freedesktop/NetworkManager/Checkpoint/%u" % (num) + except Exception as e: + pass + + if not path or path[0] != '/': + sys.exit('Invalid checkpoint path \"%s\"' % (path)) + + if client is not None: + checkpoint = find_checkpoint(client, path) + if checkpoint is None: + print('WARNING: no checkpoint with path "%s" found' % (path)) + + return path + +def do_create(client): + flags = NM.CheckpointCreateFlags.NONE + if len(sys.argv) < 3: + sys.exit("Failed: missing argument timeout") + + timeout = int(sys.argv[2]) + devices = [] + for arg in sys.argv[3:]: + if arg == '--destroy-all': + flags |= NM.CheckpointCreateFlags.DESTROY_ALL + elif arg == '--delete-new-connections': + flags |= NM.CheckpointCreateFlags.DELETE_NEW_CONNECTIONS + elif arg == '--disconnect-new-devices': + flags |= NM.CheckpointCreateFlags.DISCONNECT_NEW_DEVICES + elif arg == '--allow-overlapping': + flags |= NM.CheckpointCreateFlags.ALLOW_OVERLAPPING + else: + d = client.get_device_by_iface(arg) + if d is None: + sys.exit("Unknown device %s" % arg) + devices.append(d) + + def create_cb(client, result, data): + try: + checkpoint = client.checkpoint_create_finish(result) + print("%s" % checkpoint.get_path()) + except Exception as e: + sys.stderr.write("Failed: %s\n" % e.message) + main_loop.quit() + + client.checkpoint_create(devices, timeout, flags, None, create_cb, None) + def do_destroy(client): if len(sys.argv) < 3: sys.exit("Missing checkpoint path") - checkpoint = find_checkpoint(client, sys.argv[2]) - if checkpoint is None: - sys.exit("Uknown checkpoint %s" % sys.argv[2]) + path = validate_path(sys.argv[2], client) - client.checkpoint_destroy_async(checkpoint, None, destroy_cb, None) + def destroy_cb(client, result, data): + try: + if client.checkpoint_destroy_finish(result) == True: + print("Success") + except Exception as e: + sys.stderr.write("Failed: %s\n" % e.message) + main_loop.quit() -def rollback_cb(client, result, data): - try: - res = client.checkpoint_rollback_finish(result) - for path in res: - d = client.get_device_by_path(path) - if d is None: - iface = path - else: - iface = d.get_iface() - print "%s => %s" % (iface, "OK" if res[path] == 0 else "ERROR") - except Exception, e: - sys.stderr.write("Failed: %s\n" % e.message) - main_loop.quit() + client.checkpoint_destroy(path, None, destroy_cb, None) def do_rollback(client): if len(sys.argv) < 3: sys.exit("Missing checkpoint path") - checkpoint = find_checkpoint(client, sys.argv[2]) - if checkpoint is None: - sys.exit("Uknown checkpoint %s" % sys.argv[2]) + path = validate_path(sys.argv[2], client) - client.checkpoint_rollback_async(checkpoint, None, rollback_cb, None) + def rollback_cb(client, result, data): + try: + res = client.checkpoint_rollback_finish(result) + for path in res: + d = client.get_device_by_path(path) + if d is None: + iface = path + else: + iface = d.get_iface() + print("%s => %s" % (iface, "OK" if res[path] == 0 else "ERROR")) + except Exception as e: + sys.stderr.write("Failed: %s\n" % e.message) + main_loop.quit() + + client.checkpoint_rollback(path, None, rollback_cb, None) + +def do_adjust_rollback_timeout(client): + if len(sys.argv) < 3: + sys.exit("Missing checkpoint path") + if len(sys.argv) < 4: + sys.exit("Missing timeout") + try: + add_timeout = int(sys.argv[3]) + except: + sys.exit("Invalid timeout") + + path = validate_path(sys.argv[2], client) + + def adjust_rollback_timeout_cb(client, result, data): + try: + client.checkpoint_adjust_rollback_timeout_finish(result) + print("Success") + except Exception as e: + sys.stderr.write("Failed: %s\n" % e.message) + main_loop.quit() + + client.checkpoint_adjust_rollback_timeout(path, add_timeout, None, adjust_rollback_timeout_cb, None) def do_show(client): + ts = nmex.nm_boot_time_ms() for c in client.get_checkpoints(): - print "%s:" % c.get_path() - print " created: %u" % c.get_created() - print " timeout: %u seconds" % c.get_rollback_timeout() - print " devices:", ' '.join(sorted(map(lambda x: x.get_iface(), c.get_devices()))) + show(c, ts) if __name__ == '__main__': nm_client = NM.Client.new(None) @@ -124,6 +179,8 @@ if __name__ == '__main__': do_destroy(nm_client) elif sys.argv[1] == 'rollback': do_rollback(nm_client) + elif sys.argv[1] == 'adjust-rollback-timeout': + do_adjust_rollback_timeout(nm_client) else: usage() diff --git a/examples/python/nmex.py b/examples/python/nmex.py new file mode 100644 index 0000000000..a85eecaf87 --- /dev/null +++ b/examples/python/nmex.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +# vim: ft=python ts=4 sts=4 sw=4 et ai + +# 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Copyright 2018 Red Hat, Inc. + +############################################################################### +# nmex.py contains helper functions used by some examples. The helper functions +# should be simple and independent, so that the user can extract them easily +# when modifying the example to his needs. +############################################################################### + +def _sys_clock_gettime_ns_lazy(): + import ctypes + + class timespec(ctypes.Structure): + _fields_ = [ + ('tv_sec', ctypes.c_long), + ('tv_nsec', ctypes.c_long) + ] + + librt = ctypes.CDLL('librt.so.1', use_errno=True) + clock_gettime = librt.clock_gettime + clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] + + t = timespec() + def f(clock_id): + if clock_gettime(clock_id, ctypes.pointer(t)) != 0: + import os + errno_ = ctypes.get_errno() + raise OSError(errno_, os.strerror(errno_)) + return (t.tv_sec * 1000000000) + t.tv_nsec + return f + +_sys_clock_gettime_ns = None + +# call POSIX clock_gettime() and return it as integer (in nanoseconds) +def sys_clock_gettime_ns(clock_id): + global _sys_clock_gettime_ns + if _sys_clock_gettime_ns is None: + _sys_clock_gettime_ns = _sys_clock_gettime_ns_lazy() + return _sys_clock_gettime_ns(clock_id) + +def nm_boot_time_ns(): + # NetworkManager exposes some timestamps as CLOCK_BOOTTIME. + # Try that first (number 7). + try: + return sys_clock_gettime_ns(7) + except OSError as e: + # On systems, where this is not available, fallback to + # CLOCK_MONOTONIC (numeric 1). + # That is what NetworkManager does as well. + import errno + if e.errno == errno.EINVAL: + return sys_clock_gettime_ns(1) + raise +def nm_boot_time_us(): + return nm_boot_time_ns() / 1000 +def nm_boot_time_ms(): + return nm_boot_time_ns() / 1000000 +def nm_boot_time_s(): + return nm_boot_time_ns() / 1000000000 + +############################################################################### diff --git a/introspection/org.freedesktop.NetworkManager.xml b/introspection/org.freedesktop.NetworkManager.xml index 26a618c1bf..b8ad5911f2 100644 --- a/introspection/org.freedesktop.NetworkManager.xml +++ b/introspection/org.freedesktop.NetworkManager.xml @@ -251,6 +251,27 @@ + + + + + +