Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(522)

Side by Side Diff: media/tools/constrained_network_server/traffic_control.py

Issue 9125022: Add root access check to cns server. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Minor changes. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/tools/constrained_network_server/cns.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Traffic control library for constraining the network configuration on a port. 5 """Traffic control library for constraining the network configuration on a port.
6 6
7 The traffic controller sets up a constrained network configuration on a port. 7 The traffic controller sets up a constrained network configuration on a port.
8 Traffic to the constrained port is forwarded to a specified server port. 8 Traffic to the constrained port is forwarded to a specified server port.
9 """ 9 """
10 10
11 import logging 11 import logging
12 import os
12 import re 13 import re
13 import subprocess 14 import subprocess
14 15
15 # The maximum bandwidth limit. 16 # The maximum bandwidth limit.
16 _DEFAULT_MAX_BANDWIDTH_KBIT = 1000000 17 _DEFAULT_MAX_BANDWIDTH_KBIT = 1000000
17 18
18 19
19 class TrafficControlError(BaseException): 20 class TrafficControlError(BaseException):
20 """Exception raised for errors in traffic control library. 21 """Exception raised for errors in traffic control library.
21 22
22 Attributes: 23 Attributes:
23 msg: User defined error message. 24 msg: User defined error message.
24 cmd: Command for which the exception was raised. 25 cmd: Command for which the exception was raised.
25 returncode: Return code of running the command. 26 returncode: Return code of running the command.
26 stdout: Output of running the command. 27 stdout: Output of running the command.
27 stderr: Error output of running the command. 28 stderr: Error output of running the command.
28 """ 29 """
29 30
30 def __init__(self, msg, cmd=None, returncode=None, output=None, 31 def __init__(self, msg, cmd=None, returncode=None, output=None,
31 error=None): 32 error=None):
32 BaseException.__init__(self, msg) 33 BaseException.__init__(self, msg)
33 self.msg = msg 34 self.msg = msg
34 self.cmd = cmd 35 self.cmd = cmd
35 self.returncode = returncode 36 self.returncode = returncode
36 self.output = output 37 self.output = output
37 self.error = error 38 self.error = error
38 39
39 40
41 def CheckRequirements():
42 """Checks if permissions are available to run traffic control commands.
43
44 Raises:
45 TrafficControlError: If permissions to run traffic control commands are not
46 available.
47 """
48 if os.geteuid() != 0:
49 _Exec(['sudo', '-n', 'tc', '-help'],
50 msg=('Cannot run traffic control \'tc\' command. '
DaleCurtis 2012/01/10 21:35:08 Better written as "Cannot run 'tc' command. Traffi
51 'Please check that you have root access.'))
52 _Exec(['sudo', '-n', 'iptables', '-help'],
53 msg=('Cannot run traffic control \'iptables\' command. '
54 'Please check that you have root access.'))
55
56
40 def CreateConstrainedPort(config): 57 def CreateConstrainedPort(config):
41 """Creates a new constrained port. 58 """Creates a new constrained port.
42 59
43 Imposes packet level constraints such as bandwidth, latency, and packet loss 60 Imposes packet level constraints such as bandwidth, latency, and packet loss
44 on a given port using the specified configuration dictionary. Traffic to that 61 on a given port using the specified configuration dictionary. Traffic to that
45 port is forwarded to a specified server port. 62 port is forwarded to a specified server port.
46 63
47 Args: 64 Args:
48 config: Constraint configuration dictionary, format: 65 config: Constraint configuration dictionary, format:
49 port: Port to constrain (integer 1-65535). 66 port: Port to constrain (integer 1-65535).
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 """ 345 """
329 cmd_list = [str(x) for x in command] 346 cmd_list = [str(x) for x in command]
330 cmd = ' '.join(cmd_list) 347 cmd = ' '.join(cmd_list)
331 logging.debug('Running command: %s', cmd) 348 logging.debug('Running command: %s', cmd)
332 349
333 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 350 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
334 output, error = p.communicate() 351 output, error = p.communicate()
335 if p.returncode != 0: 352 if p.returncode != 0:
336 raise TrafficControlError(msg, cmd, p.returncode, output, error) 353 raise TrafficControlError(msg, cmd, p.returncode, output, error)
337 return output.strip() 354 return output.strip()
OLDNEW
« no previous file with comments | « media/tools/constrained_network_server/cns.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698