Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Helper script to perform actions as a super-user on ChromeOS. | 6 """Helper script to perform actions as a super-user on ChromeOS. |
| 7 | 7 |
| 8 Needs to be run with superuser privileges, typically using the | 8 Needs to be run with superuser privileges, typically using the |
| 9 suid_python binary. | 9 suid_python binary. |
| 10 | 10 |
| 11 Usage: | 11 Usage: |
| 12 sudo python suid_actions.py --action=CleanFlimflamDirs | 12 sudo python suid_actions.py --action=CleanFlimflamDirs |
| 13 """ | 13 """ |
| 14 | 14 |
| 15 import logging | 15 import logging |
|
bartfab (slow)
2012/07/30 08:59:45
Nit: "logging" is no longer needed by this file
| |
| 16 import optparse | 16 import optparse |
| 17 import os | 17 import os |
| 18 import shutil | 18 import shutil |
| 19 import subprocess | 19 import subprocess |
| 20 import sys | 20 import sys |
| 21 import time | 21 import time |
| 22 | 22 |
| 23 sys.path.append('/usr/local') # to import autotest libs. | 23 sys.path.append('/usr/local') # to import autotest libs. |
| 24 from autotest.cros import constants | 24 from autotest.cros import constants |
| 25 from autotest.cros import cryptohome | 25 from autotest.cros import cryptohome |
| 26 | 26 |
| 27 # TODO(bartfab): Remove when crosbug.com/20709 is fixed. | 27 TEMP_BACKCHANNEL_FILE = '/tmp/pyauto_network_backchannel_file' |
| 28 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) | |
| 29 from pyauto import AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE | |
| 30 | 28 |
| 31 TEMP_BACKCHANNEL_FILE = '/tmp/pyauto_network_backchannel_file' | |
| 32 | 29 |
| 33 class SuidAction(object): | 30 class SuidAction(object): |
| 34 """Helper to perform some super-user actions on ChromeOS.""" | 31 """Helper to perform some super-user actions on ChromeOS.""" |
| 35 | 32 |
| 36 def _ParseArgs(self): | 33 def _ParseArgs(self): |
| 37 parser = optparse.OptionParser() | 34 parser = optparse.OptionParser() |
| 38 parser.add_option( | 35 parser.add_option( |
| 39 '-a', '--action', help='Action to perform.') | 36 '-a', '--action', help='Action to perform.') |
| 40 self._options = parser.parse_args()[0] | 37 self._options = parser.parse_args()[0] |
| 41 if not self._options.action: | 38 if not self._options.action: |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 begin = time.time() | 79 begin = time.time() |
| 83 while not os.path.exists(constants.RESOLV_CONF_FILE): | 80 while not os.path.exists(constants.RESOLV_CONF_FILE): |
| 84 if time.time() - begin > 10: | 81 if time.time() - begin > 10: |
| 85 raise RuntimeError('Timeout while waiting for flimflam/shill start.') | 82 raise RuntimeError('Timeout while waiting for flimflam/shill start.') |
| 86 time.sleep(.25) | 83 time.sleep(.25) |
| 87 | 84 |
| 88 def RemoveAllCryptohomeVaults(self): | 85 def RemoveAllCryptohomeVaults(self): |
| 89 """Remove any existing cryptohome vaults.""" | 86 """Remove any existing cryptohome vaults.""" |
| 90 cryptohome.remove_all_vaults() | 87 cryptohome.remove_all_vaults() |
| 91 | 88 |
| 92 def TryToDisableLocalStateAutoClearing(self): | |
| 93 """Try to disable clearing of the local state on session manager startup. | |
| 94 | |
| 95 This will fail if rootfs verification is on. | |
| 96 TODO(bartfab): Remove this method when crosbug.com/20709 is fixed. | |
| 97 """ | |
| 98 os.system('mount -o remount,rw /') | |
| 99 os.remove(AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE) | |
| 100 os.system('mount -o remount,ro /') | |
| 101 if os.path.exists(AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE): | |
| 102 logging.debug('Failed to remove %s. Session manager will clear local ' | |
| 103 'state on startup.' % AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE) | |
| 104 else: | |
| 105 logging.debug('Removed %s. Session manager will not clear local state on ' | |
| 106 'startup.' % AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE) | |
| 107 | |
| 108 def TryToEnableLocalStateAutoClearing(self): | |
| 109 """Try to enable clearing of the local state on session manager startup. | |
| 110 | |
| 111 This will fail if rootfs verification is on. | |
| 112 TODO(bartfab): Remove this method when crosbug.com/20709 is fixed. | |
| 113 """ | |
| 114 os.system('mount -o remount,rw /') | |
| 115 open(AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE, 'w').close() | |
| 116 os.system('mount -o remount,ro /') | |
| 117 if os.path.exists(AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE): | |
| 118 logging.debug('Created %s. Session manager will clear local state on ' | |
| 119 'startup.' % AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE) | |
| 120 else: | |
| 121 logging.debug('Failed to create %s. Session manager will not clear local ' | |
| 122 'state on startup.' % AUTO_CLEAR_LOCAL_STATE_MAGIC_FILE) | |
| 123 | |
| 124 def _GetEthInterfaces(self): | 89 def _GetEthInterfaces(self): |
| 125 """Returns a list of the eth* interfaces detected by the device.""" | 90 """Returns a list of the eth* interfaces detected by the device.""" |
| 126 # Assumes ethernet interfaces all have "eth" in the name. | 91 # Assumes ethernet interfaces all have "eth" in the name. |
| 127 import pyudev | 92 import pyudev |
| 128 return sorted([iface.sys_name for iface in | 93 return sorted([iface.sys_name for iface in |
| 129 pyudev.Context().list_devices(subsystem='net') | 94 pyudev.Context().list_devices(subsystem='net') |
| 130 if 'eth' in iface.sys_name]) | 95 if 'eth' in iface.sys_name]) |
| 131 | 96 |
| 132 def _Renameif(self, old_iface, new_iface, mac_address): | 97 def _Renameif(self, old_iface, new_iface, mac_address): |
| 133 """Renames the interface with mac_address from old_iface to new_iface. | 98 """Renames the interface with mac_address from old_iface to new_iface. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 | 145 |
| 181 with open(TEMP_BACKCHANNEL_FILE, 'r') as fp: | 146 with open(TEMP_BACKCHANNEL_FILE, 'r') as fp: |
| 182 eth_iface, mac_address = fp.read().split(',') | 147 eth_iface, mac_address = fp.read().split(',') |
| 183 | 148 |
| 184 self._Renameif('eth_test', eth_iface, mac_address) | 149 self._Renameif('eth_test', eth_iface, mac_address) |
| 185 os.remove(TEMP_BACKCHANNEL_FILE) | 150 os.remove(TEMP_BACKCHANNEL_FILE) |
| 186 | 151 |
| 187 | 152 |
| 188 if __name__ == '__main__': | 153 if __name__ == '__main__': |
| 189 sys.exit(SuidAction().Run()) | 154 sys.exit(SuidAction().Run()) |
| OLD | NEW |