minstall: add a timeout when waiting for user input for elevation

Do not allow hanging forever.

It's fine to use selectors here, which are unix-only, because we require
Unix + a system that has e.g. sudo installed, anyway.
pull/11366/head
Eli Schwartz 2 years ago
parent 55abcbb8f6
commit 616bf1f79d
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 15
      mesonbuild/minstall.py

@ -18,6 +18,7 @@ from pathlib import Path
import argparse
import errno
import os
import selectors
import shlex
import shutil
import subprocess
@ -566,12 +567,22 @@ class Installer:
if rootcmd is not None:
print('Installation failed due to insufficient permissions.')
s = selectors.DefaultSelector()
s.register(sys.stdin, selectors.EVENT_READ)
ans = None
for attempt in range(5):
ans = input(f'Attempt to use {rootcmd} to gain elevated privileges? [y/n] ')
print(f'Attempt to use {rootcmd} to gain elevated privileges? [y/n] ', end='', flush=True)
if s.select(30):
# we waited on sys.stdin *only*
ans = sys.stdin.readline().rstrip('\n')
else:
print()
break
if ans in {'y', 'n'}:
break
else:
raise MesonException('Answer not one of [y/n]')
if ans is not None:
raise MesonException('Answer not one of [y/n]')
if ans == 'y':
os.execlp(rootcmd, rootcmd, sys.executable, main_file, *sys.argv[1:],
'-C', os.getcwd(), '--no-rebuild')

Loading…
Cancel
Save