Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
connman, preup and postup functions...
View unanswered posts
View posts from last 24 hours

Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message

Joined: 10 Aug 2014
Posts: 23
Location: France

PostPosted: Sun Aug 10, 2014 8:24 pm    Post subject: connman, preup and postup functions... Reply with quote

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 :


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])
                        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)
                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__':

        bus = dbus.SystemBus()







        mainloop = gobject.MainLoop()

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 :

# 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 !
Back to top
View user's profile Send private message

Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Thu Aug 14, 2014 2:21 pm    Post subject: Reply with quote

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.

Back to top
View user's profile Send private message

Joined: 10 Aug 2014
Posts: 23
Location: France

PostPosted: Thu Aug 14, 2014 4:55 pm    Post subject: Reply with quote

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 !
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum