Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line-buffered stdout logging of elevated process does not appear to work (osascript issue) #16

Open
ForestJohnson opened this issue Oct 17, 2021 · 1 comment

Comments

@ForestJohnson
Copy link

Hello,

I am writing a python desktop GUI application and I wanted to embed a small installer into it. The idea is, when the user clicks on the install button, the GUI application will call itself with a special "run installer" argument. Then instead of launching another copy of the GUI, it would call elevate and then execute the installer script:

    if "run_macos_background_service_installer" in sys.argv:

      elevate(graphical=True)

      if os.getuid() != 0:
        print(f"elevate() failed: not running as root. uid={os.getuid()}")
        sys.exit(1)

      # bufsize=1 means line-buffered.. ? https://docs.python.org/3/library/subprocess.html#popen-constructor
      proccess = subprocess.Popen(["bash", app_context.get_resource('install_daemon.sh')],
                           bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

      for line in iter(proccess.stdout.readline, b''):
        print(line.rstrip().decode("utf-8"))
        sys.stdout.flush()

      proccess.wait(10)

      sys.exit(proccess.returncode)
    else:
      ... normal app stuff... 

I noticed that posix.py will do nothing if its already running as root:
https://github.com/barneygale/elevate/blob/master/elevate/posix.py#L25-L27

I also noticed that if I run this app as a normal user, the logs from install_daemon.sh wont appear until after the process exits. However if I run the app as root (causing elevate to exit early instead of calling os.execlp(...)) then the logs appear in real time. So far I have only tested in Mac OS. If you wish to see the full example my app is here, sorry about the messy code:

https://git.sequentialread.com/forest/greenhouse-desktop/src/commit/aae851dcbac899360c449ab1e24594197c34582b

For now this issue is not going to block me, I will simply output to a temp file and then poll that file instead of doing stdout pipe.

@ForestJohnson
Copy link
Author

ForestJohnson commented Oct 17, 2021

Never mind, it looks like this might be a limitation of osascript, not an elevate issue. See: https://stackoverflow.com/questions/49171769/how-where-to-best-retrieve-sudo-password-via-a-native-gui-on-a-macos-python-ba

For now I have solved this by writing to a temp file and then polling it.

However if it would be possible to have an option for tell terminal to run xyz or whatever where it would actually do the line-by-line piping (without actually opening a terminal window), that might be cool. I never tried it.

@ForestJohnson ForestJohnson changed the title Line-buffered stdout logging of elevated process does not appear to work Line-buffered stdout logging of elevated process does not appear to work (osascript issue) Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant