Skip to content

Commit 4f33a45

Browse files
Improve root permissions acquisition
1 parent 2d8f59c commit 4f33a45

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

lib/inputstreamhelper.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,25 @@ def _parse_chromeos_offset(self, bin_path):
152152
self._log('Failed to calculate losetup offset.')
153153
return False
154154

155-
def _run_cmd(self, cmd):
155+
def _run_cmd(self, cmd, sudo=False, ask=True):
156+
dialog = xbmcgui.Dialog()
157+
if ask and not dialog.yesno(self._language(30001), self._language(30030), yeslabel=self._language(30029), nolabel=self._language(30028)):
158+
self._log('User refused to give sudo permissions.')
159+
return cmd
160+
if sudo and os.getuid() != 0 and self._cmd_exists('sudo'):
161+
cmd.insert(0, 'sudo')
162+
156163
try:
157164
subprocess.check_output(cmd)
158165
self._log('{0} cmd executed successfully.'.format(cmd))
159-
return True
166+
success = True
160167
except subprocess.CalledProcessError, error:
161168
self._log('cmd failed with output: {0}'.format(error.output))
162-
return False
169+
success = False
170+
if 'sudo' in cmd:
171+
subprocess.check_output(['sudo -k']) # reset timestamp
172+
173+
return success
163174

164175
def _set_loop_dev(self):
165176
"""Set an unused loop device that's available for use."""
@@ -171,7 +182,7 @@ def _set_loop_dev(self):
171182
def _losetup(self, bin_path):
172183
"""Setup Chrome OS loop device."""
173184
cmd = ['losetup', self._loop_dev, bin_path, '-o', self._parse_chromeos_offset(bin_path)]
174-
success = self._run_cmd(cmd)
185+
success = self._run_cmd(cmd, sudo=True, ask=True)
175186
if success:
176187
self._attached_loop_dev = True
177188
return True
@@ -180,15 +191,8 @@ def _losetup(self, bin_path):
180191

181192
def _mnt_loop_dev(self):
182193
"""Mount loop device to self._mnt_path()"""
183-
dialog = xbmcgui.Dialog()
184194
cmd = ['mount', '-t', 'ext2', self._loop_dev, '-o', 'ro', self._mnt_path()]
185-
if os.getuid() != 0 and self._cmd_exists('sudo'): # ask for permission to wrap cmd in sudo
186-
if dialog.yesno(self._language(30001), self._language(30030), yeslabel=self._language(30029), nolabel=self._language(30028)):
187-
cmd.insert(0, 'sudo')
188-
else:
189-
self._log('User refused to give sudo permission.')
190-
191-
success = self._run_cmd(cmd)
195+
success = self._run_cmd(cmd, sudo=True, ask=False)
192196
if success:
193197
self._mounted = True
194198
return True
@@ -495,14 +499,12 @@ def _cleanup(self):
495499
"""Clean up after Widevine DRM installation."""
496500
if self._mounted:
497501
cmd = ['umount', self._mnt_path()]
498-
if os.getuid() != 0 and self._cmd_exists('sudo'): # no need to ask for permission again
499-
cmd.insert(0, 'sudo')
500-
unmount_success = self._run_cmd(cmd)
502+
unmount_success = self._run_cmd(cmd, sudo=True, ask=False)
501503
if unmount_success:
502504
self._mounted = False
503505
if self._attached_loop_dev:
504506
cmd = ['losetup', '-d', self._loop_dev]
505-
unattach_success = self._run_cmd(cmd)
507+
unattach_success = self._run_cmd(cmd, sudo=True, ask=False)
506508
if unattach_success:
507509
self._loop_dev = False
508510

0 commit comments

Comments
 (0)