summary refs log tree commit diff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/C/glib/monitor-nm-running-gdbus.c6
-rw-r--r--examples/C/glib/monitor-nm-state-gdbus.c6
-rwxr-xr-xexamples/dispatcher/10-ifcfg-rh-routes.sh106
-rw-r--r--examples/python/nmex.py72
4 files changed, 87 insertions, 103 deletions
diff --git a/examples/C/glib/monitor-nm-running-gdbus.c b/examples/C/glib/monitor-nm-running-gdbus.c
index ad05a9e4..16e1cfad 100644
--- a/examples/C/glib/monitor-nm-running-gdbus.c
+++ b/examples/C/glib/monitor-nm-running-gdbus.c
@@ -32,8 +32,8 @@
 
 static void
 on_name_appeared (GDBusConnection *connection,
-                  const char      *name,
-                  const char      *name_owner,
+                  const gchar     *name,
+                  const gchar     *name_owner,
                   gpointer         user_data)
 {
 	g_print ("Name '%s' on the system bus is owned by %s => NM is running\n",
@@ -42,7 +42,7 @@ on_name_appeared (GDBusConnection *connection,
 
 static void
 on_name_vanished (GDBusConnection *connection,
-                  const char      *name,
+                  const gchar     *name,
                   gpointer         user_data)
 {
 	g_print ("Name '%s' does not exist on the system bus => NM is not running\n", name);
diff --git a/examples/C/glib/monitor-nm-state-gdbus.c b/examples/C/glib/monitor-nm-state-gdbus.c
index 4593f1a6..fdb4d95d 100644
--- a/examples/C/glib/monitor-nm-state-gdbus.c
+++ b/examples/C/glib/monitor-nm-state-gdbus.c
@@ -59,15 +59,15 @@ nm_state_to_string (NMState state)
 
 static void
 on_signal (GDBusProxy *proxy,
-           char       *sender_name,
-           char       *signal_name,
+           gchar      *sender_name,
+           gchar      *signal_name,
            GVariant   *parameters,
            gpointer    user_data)
 {
 	guint32 new_state;
 
 	/* Print all signals */
-	//char *parameters_str;
+	//gchar *parameters_str;
 	//parameters_str = g_variant_print (parameters, TRUE);
 	//g_print (" *** Received Signal: %s: %s\n", signal_name, parameters_str);
 	//g_free (parameters_str);
diff --git a/examples/dispatcher/10-ifcfg-rh-routes.sh b/examples/dispatcher/10-ifcfg-rh-routes.sh
index 147506e4..b1d0314d 100755
--- a/examples/dispatcher/10-ifcfg-rh-routes.sh
+++ b/examples/dispatcher/10-ifcfg-rh-routes.sh
@@ -1,49 +1,15 @@
-#!/bin/bash
+#!/bin/sh
+
+# This ifcfg-rh-specific script runs
+# /etc/sysconfig/network-scripts/ifup-routes when bringing up
+# interfaces that have routing rules associated with them that can't
+# be expressed by NMSettingIPConfig. (Eg, policy-based routing.)
 
-# This script applies policy-based routing rules defined for the
-# connection in the /etc/sysconfig/network-scripts/ directory.
-#
 # This should be installed in both dispatcher.d/ and
 # dispatcher.d/pre-up.d/
-#
+
 # pre-up scripts delay activation of the device. To reduce the delay,
 # it is advised to install the script as symlink to no-wait.d directory.
-#
-# This file is derived from scripts 'if{up,down}-routes' from
-# Fedora/RHEL initscripts.
-
-MATCH='^[[:space:]]*(\#.*)?$'
-
-handle_file () {
-    . $1
-    routenum=0
-    while [ "x$(eval echo '$'ADDRESS$routenum)x" != "xx" ]; do
-        eval $(ipcalc -p $(eval echo '$'ADDRESS$routenum) $(eval echo '$'NETMASK$routenum))
-        line="$(eval echo '$'ADDRESS$routenum)/$PREFIX"
-        if [ "x$(eval echo '$'GATEWAY$routenum)x" != "xx" ]; then
-            line="$line via $(eval echo '$'GATEWAY$routenum)"
-        fi
-        line="$line dev $2"
-        /sbin/ip route add $line
-        routenum=$(($routenum+1))
-    done
-}
-
-handle_ip_file() {
-    local f t type= file=$1 proto="-4"
-    f=${file##*/}
-    t=${f%%-*}
-    type=${t%%6}
-    if [ "$type" != "$t" ]; then
-        proto="-6"
-    fi
-    { cat "$file" ; echo ; } | while read line; do
-        if [[ ! "$line" =~ $MATCH ]]; then
-            /sbin/ip $proto $type add $line
-        fi
-    done
-}
-
 
 if [ "$2" != "pre-up" -a "$2" != "down" ]; then
     exit 0
@@ -53,73 +19,19 @@ dir=$(dirname "$CONNECTION_FILENAME")
 if [ "$dir" != "/etc/sysconfig/network-scripts" ]; then
     exit 0
 fi
-
 profile=$(basename "$CONNECTION_FILENAME" | sed -ne 's/^ifcfg-//p')
 if [ -z "$profile" ]; then
     exit 0
 fi
-
 if ! [ -f "$dir/rule-$profile" -o -f "$dir/rule6-$profile" ]; then
     exit 0
 fi
 
 case "$2" in
     pre-up)
-        # Routes
-        FILES="/etc/sysconfig/network-scripts/route-$DEVICE_IP_IFACE"
-        FILES="$FILES /etc/sysconfig/network-scripts/route6-$DEVICE_IP_IFACE"
-        if [ "$profile" != "$DEVICE_IP_IFACE" ]; then
-            FILES="$FILES /etc/sysconfig/network-scripts/route-$profile"
-            FILES="$FILES /etc/sysconfig/network-scripts/route6-$profile"
-        fi
-
-        for file in $FILES; do
-            if [ -f "$file" ]; then
-                if grep -Eq '^[[:space:]]*ADDRESS[0-9]+=' $file ; then
-                    # new format
-                    handle_file $file ${1%:*}
-                else
-                    # older format
-                    handle_ip_file $file
-                fi
-            fi
-        done
-
-        # Rules
-        FILES="/etc/sysconfig/network-scripts/rule-$DEVICE_IP_IFACE"
-        FILES="$FILES /etc/sysconfig/network-scripts/rule6-$DEVICE_IP_IFACE"
-        if [ "$profile" != "$DEVICE_IP_IFACE" ]; then
-            FILES="$FILES /etc/sysconfig/network-scripts/rule-$profile"
-            FILES="$FILES /etc/sysconfig/network-scripts/rule6-$profile"
-        fi
-
-        for file in $FILES; do
-            if [ -f "$file" ]; then
-                handle_ip_file $file
-            fi
-        done
+        /etc/sysconfig/network-scripts/ifup-routes "$DEVICE_IP_IFACE" "$profile"
         ;;
     down)
-        # Routes are deleted by NetworkManager
-        # Rules
-        FILES="/etc/sysconfig/network-scripts/rule-$DEVICE_IP_IFACE"
-        FILES="$FILES /etc/sysconfig/network-scripts/rule6-$DEVICE_IP_IFACE"
-        if [ "$profile" != "$DEVICE_IP_IFACE" ]; then
-            FILES="$FILES /etc/sysconfig/network-scripts/rule-$profile"
-            FILES="$FILES /etc/sysconfig/network-scripts/rule6-$profile"
-        fi
-        for file in $FILES; do
-            if [ -f "$file" ]; then
-                proto=
-                if [ "$file" != "${file##*/rule6-}" ]; then
-                    proto="-6"
-                fi
-                { cat "$file" ; echo ; } | while read line; do
-                    if [[ ! "$line" =~ $MATCH ]]; then
-                        /sbin/ip $proto rule del $line
-                    fi
-                done
-            fi
-        done
+        /etc/sysconfig/network-scripts/ifdown-routes "$DEVICE_IP_IFACE" "$profile"
         ;;
 esac
diff --git a/examples/python/nmex.py b/examples/python/nmex.py
new file mode 100644
index 00000000..a85eecaf
--- /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 <http://www.gnu.org/licenses/>.
+#
+# 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
+
+###############################################################################