1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
12 from telemetry.core.platform import desktop_platform_backend
13 from telemetry.core.platform import ps_util
16 class PosixPlatformBackend(desktop_platform_backend.DesktopPlatformBackend):
18 # This is an abstract class. It is OK to have abstract methods.
19 # pylint: disable=W0223
21 def _RunCommand(self, args):
22 return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]
24 def _GetFileContents(self, path):
25 with open(path, 'r') as f:
28 def _GetPsOutput(self, columns, pid=None):
29 """Returns output of the 'ps' command as a list of lines.
30 Subclass should override this function.
33 columns: A list of require columns, e.g., ['pid', 'pss'].
34 pid: If nont None, returns only the information of the process
38 args.extend(['-p', str(pid)] if pid != None else ['-e'])
40 args.extend(['-o', c + '='])
41 return self._RunCommand(args).splitlines()
43 def GetChildPids(self, pid):
44 """Returns a list of child pids of |pid|."""
45 ps_output = self._GetPsOutput(['pid', 'ppid', 'state'])
46 ps_line_re = re.compile(
47 '\s*(?P<pid>\d+)\s*(?P<ppid>\d+)\s*(?P<state>\S*)\s*')
49 for pid_ppid_state in ps_output:
50 m = ps_line_re.match(pid_ppid_state)
51 assert m, 'Did not understand ps output: %s' % pid_ppid_state
52 processes.append((m.group('pid'), m.group('ppid'), m.group('state')))
53 return ps_util.GetChildPids(processes, pid)
55 def GetCommandLine(self, pid):
56 command = self._GetPsOutput(['command'], pid)
57 return command[0] if command else None
59 def GetFlushUtilityName(self):
60 return 'clear_system_cache'
62 def CanLaunchApplication(self, application):
63 return bool(distutils.spawn.find_executable(application))
65 def LaunchApplication(
66 self, application, parameters=None, elevate_privilege=False):
67 assert application, 'Must specify application to launch'
69 if os.path.sep not in application:
70 application = distutils.spawn.find_executable(application)
71 assert application, 'Failed to find application in path'
76 assert isinstance(parameters, list), 'parameters must be a list'
80 return (os.stat(path).st_mode & stat.S_ISUID) == stat.S_ISUID
84 ['sudo', '-nv'], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
85 stderr=subprocess.STDOUT)
86 stdout = p.communicate()[0]
87 # Some versions of sudo set the returncode based on whether sudo requires
88 # a password currently. Other versions return output when password is
89 # required and no output when the user is already authenticated.
90 return not p.returncode and not stdout
92 if elevate_privilege and not IsSetUID(application):
93 args = ['sudo'] + args
95 print ('Telemetry needs to run %s under sudo. Please authenticate.' %
97 subprocess.check_call(['sudo', '-v']) # Synchronously authenticate.
99 prompt = ('Would you like to always allow %s to be run as the current '
100 'user without sudo? If so, Telemetry will '
101 '`sudo chmod +s %s`. (y/N)' % (application, application))
102 if raw_input(prompt).lower() == 'y':
103 subprocess.check_call(['sudo', 'chmod', '+s', application])
105 stderror_destination = subprocess.PIPE
106 if logging.getLogger().isEnabledFor(logging.DEBUG):
107 stderror_destination = None
109 return subprocess.Popen(
110 args, stdout=subprocess.PIPE, stderr=stderror_destination)