# connman, preup and postup functions...

## zelurker

I am new to gentoo so maybe I missed something (tried it because of systemd in debian mainly !).

Anyway, I started to use connman which works quite well, except that it seems it never calls the preup / postup functions from /etc/conf.d/net

And there doesn't seem to be anyway to install this kind of callback with connman, unless I missed something.

So, are these functions limited to openrc only, or is there another way ? (maybe by using something else, like network manager ?).

EDIT : well I found a solution, maybe it could be usefull for some others :

I just installed the examples from the connman package, and took inspiration from the monitor-services python script.

It can be modified to run the functions from /etc/conf.d/net when an event occured.

I am not totally sure I took the right status to launch preup and predown, but I was more interested by postup anyway. It should work though.

Here is the modified script, I placed it in /usr/bin/connman-monitor-services :

```

#!/usr/bin/python2

import gobject

import dbus

import dbus.mainloop.glib

import os

global iface

iface = {}

def extract_values(values):

        val = "{"

        for key in values.keys():

                val += " " + key + "="

                if key in ["Servers", "Excludes"]:

                        val += extract_list(values[key])

                else:

                        val += str(values[key])

        val += " }"

        return val

def extract_list(list):

        val = "["

        for i in list:

                val += " " + str(i)

        val += " ]"

        return val

def property_changed(name, value, path):

        service = path[path.rfind("/") + 1:]

        if name in ["Services"]:

                val = "["

                for i in value:

                        val = val + " " + i[i.rfind("/") + 1:]

                val = val + " ]"

        elif name in ["IPv4", "IPv4.Configuration",

                        "IPv6", "IPv6.Configuration",

                        "Proxy", "Proxy.Configuration", "Ethernet", "Provider"]:

                val = extract_values(value)

        elif name in ["Nameservers", "Nameservers.Configuration",

                        "Domains", "Domains.Configuration",

                        "Timeservers", "Timeservers.Configuration", "Security"]:

                val = extract_list(value)

        elif name in ["Strength", "Priority"]:

                val = int(value)

        else:

                val = str(value)

        if name == "Ethernet":

            iface[service] = value["Interface"]

        print "[%s] %s = %s" % (service, name, val)

        if name == "State":

            action = ""

            if value == "idle":

                action = "postdown"

            elif value == "disconnect":

                action = "predown"

            elif value == "configuration":

                action = "preup"

            elif value == "online":

                action = "postup"

            if service and action and iface.get(service):

                print "*** launch net ",iface[service]," ",action

                os.system(". /etc/conf.d/net; export IFACE=%s; %s %s" % (iface[service],action,iface[service]))

def services_changed(services, removed):

        for i in services:

                service = i[0][i[0].rfind("/") + 1:]

                print "[%s] changed" % (service)

                for n in i[1].keys():

                        property_changed(n, i[1][n], i[0])

        for i in removed:

                service = i[i.rfind("/") + 1:]

                print "[%s] removed" % (service)

def technology_added(path, properties):

        technology = path[path.rfind("/") + 1:]

        print "[%s] added" % (technology)

        for n in properties.keys():

                property_changed(n, properties[n], technology)

def technology_removed(path):

        technology = path[path.rfind("/") + 1:]

        print "[%s] removed" % (technology)

if __name__ == '__main__':

        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

        bus = dbus.SystemBus()

        bus.add_signal_receiver(property_changed,

                                bus_name="net.connman",

                                dbus_interface="net.connman.Manager",

                                signal_name="PropertyChanged",

                                path_keyword="path")

        bus.add_signal_receiver(services_changed,

                                bus_name="net.connman",

                                dbus_interface="net.connman.Manager",

                                signal_name="ServicesChanged")

        bus.add_signal_receiver(property_changed,

                                bus_name="net.connman",

                                dbus_interface="net.connman.Service",

                                signal_name="PropertyChanged",

                                path_keyword="path")

        bus.add_signal_receiver(technology_added,

                                bus_name="net.connman",

                                dbus_interface="net.connman.Manager",

                                signal_name="TechnologyAdded")

        bus.add_signal_receiver(technology_removed,

                                bus_name="net.connman",

                                dbus_interface="net.connman.Manager",

                                signal_name="TechnologyRemoved")

        bus.add_signal_receiver(property_changed,

                                bus_name="net.connman",

                                dbus_interface="net.connman.Technology",

                                signal_name="PropertyChanged",

                                path_keyword="path")

        mainloop = gobject.MainLoop()

        mainloop.run()

```

It may seem a little long, but the modifications are very short.

And then just launch this with connman, I took the solution to modify /etc/init.d/connman and add it to the default runlevel (otherwise connman is launched by dbus apparently).

Here is the modified script :

```

#!/sbin/runscript

# Copyright 1999-2013 Gentoo Foundation

# Distributed under the terms of the GNU General Purpose License v2

# $Header: /var/cvsroot/gentoo-x86/net-misc/connman/files/connman.initd2,v 1.1 2013/03/14 12:51:31 chainsaw Exp $

depend() {

   need dbus

   provide net

}

start() {

   ebegin "Starting Connection Manager"

   start-stop-daemon --start --quiet --exec /usr/sbin/connmand -- ${CONNMAN_OPTS}

   start-stop-daemon --start -b -m -p /var/run/connman-monitor-services --quiet --exec /usr/bin/connman-monitor-services

   eend $?

}

stop() {

   ebegin "Stopping Connection Manager"

   start-stop-daemon --stop --quiet --exec /usr/sbin/connmand

   start-stop-daemon --stop -p /var/run/connman-monitor-services --quiet --exec /usr/bin/connman-monitor-services

   eend $?

}

# vim: set ft=gentoo-init-d ts=3 sw=3 et:

```

It's very fresh, but it seems ok, the functions from /etc/conf.d/net are now correctly called !

----------

## steveL

Nice work zelurker :-)

One minor point: I'd chain the commands you're executing with && like so:

```
  start-stop-daemon --start --quiet --exec /usr/sbin/connmand -- ${CONNMAN_OPTS} \

    && start-stop-daemon --start -b -m -p /var/run/connman-monitor-services --quiet --exec /usr/bin/connman-monitor-services

  eend $?
```

 because you want to terminate with the exit status of whichever fails first, and not execute the second when the first fails.

If you're using something like that, it's best to start the next line with the &&, not leave it at the end of the first; it helps maintenance as if the line gets moved out of position, you'll get an immediate syntax error and can correct it before someone else uses it. Though if it were any more complex I'd probably switch to an 'if' or something.

There are some functions you might find useful, if you're doing more messing around with initscripts.

Also, you might want to take a look at dhcpcd setup (dhcpd is by the guy who wrote openrc); I understand it can make wifi etc quite palatable; it manages wpa-supplicant for you, and routes traffic by whatever route is fastest, without losing connection; ie it'll switch back to using a fixed connection if it becomes available, though it is ofc up to you how you configure it. In any event it's very lightweight, so worth knowing about as an option.

HTH,

steveL.

----------

## zelurker

Yes thanks, good advice.

It's not perfect anyway when started together the dbus script seems to miss the initial connection at boot sometimes, probably because of a synchronization problem somewhere (which is annoying, you don't want to add a sleep in such a script... maybe there is a way to fix it without the sleep anyway, simply by changing the script so that it becomes a daemon by itself and returns once everything works).

But for now I can do without the scripts being launched so it's ok.

I haven't looked so far at the network configuration with openrc, my 1st impression was that it was a pity to write some configuration for some stuff which can/should work automatically (especially wired connection !). It might need to run a few scripts when the network becomes available though, so that's why I tried to improve this, but no other configuration for now.

I confess I didn't even test it with wifi yet !

----------

