From bcfb3009c0b37750b6491dc75f3c529ad7c87b23 Mon Sep 17 00:00:00 2001 From: Brandon Heller Date: Tue, 13 Nov 2012 21:32:41 -0800 Subject: [PATCH] small refactor: put function to ensure root in util Two benefits: - One place to change if in the future, a more granular method of root access is used (like the BigSwitch patch). - Makes this reusable by stuff like examples/baresshd.py that use the low-level Mininet API. --- mininet/net.py | 9 ++------- mininet/util.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/mininet/net.py b/mininet/net.py index 6a091c3..8b6cf94 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -96,7 +96,7 @@ from mininet.cli import CLI from mininet.log import info, error, debug, output from mininet.node import Host, OVSKernelSwitch, Controller from mininet.link import Link, Intf -from mininet.util import quietRun, fixLimits, numCores +from mininet.util import quietRun, fixLimits, numCores, ensureRoot from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd from mininet.term import cleanUpScreens, makeTerms @@ -571,12 +571,7 @@ class Mininet( object ): "Initialize Mininet" if cls.inited: return - if os.getuid() != 0: - # Note: this script must be run as root - # Probably we should only sudo when we need - # to as per Big Switch's patch - print "*** Mininet must run as root." - exit( 1 ) + ensureRoot() fixLimits() cls.inited = True diff --git a/mininet/util.py b/mininet/util.py index 0c7c870..3461a1d 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -9,6 +9,7 @@ from subprocess import call, check_call, Popen, PIPE, STDOUT import re from fcntl import fcntl, F_GETFL, F_SETFL from os import O_NONBLOCK +import os # Command execution support @@ -456,3 +457,13 @@ def buildTopo( topos, topoStr ): if topo not in topos: raise Exception( 'Invalid topo name %s' % topo ) return topos[ topo ]( *args, **kwargs ) + +def ensureRoot(): + """Ensure that we are running as root. + + Probably we should only sudo when needed as per Big Switch's patch. + """ + if os.getuid() != 0: + print "*** Mininet must run as root." + exit( 1 ) + return