[Gambas-user] Running Sudo using Exec acts very differently from distro to distro!!
Benoît Minisini
gambas at ...1...
Tue Apr 2 22:43:22 CEST 2013
Le 02/04/2013 22:33, Willy Raets a écrit :
> It is getting more crazy by the minute:
>
> This is part of the code of previous version posted (version 0.0.5):
>
> Public Sub btnSudo_Click()
>
> If IsNull(txtPassword.Text) Then
> Message.Info("First enter a password")
> Else
> txaOutput.Clear
> txaOutput.Text = "Ready to rumble..\n\n"
> $hProcess = Exec [System.Shell, "-c", "sudo -s id -u"] For Input
> Output As "Process"
> Print #$hProcess, txtPassword.Text
> Endif
>
> End
>
> To pin point where it goes wrong I added a output to txaOutput
> (TextArea) before and after the passing of the password.
> See ExpectedOutcome.png for what is should show when run successfully.
>
> Changed the code to (version 0.0.6):
>
> Public Sub btnSudo_Click()
>
> If IsNull(txtPassword.Text) Then
> Message.Info("First enter a password")
> Else
> txaOutput.Clear
> txaOutput.Text = "Ready to rumble..\n"
> $hProcess = Exec [System.Shell, "-c", "sudo -s id -u"] For Input
> Output As "Process"
> txaOutput.Text &= "Passing password..\n" '<-- ADDED
> Print #$hProcess, txtPassword.Text
> txaOutput.Text &= "Password passed..\n\n" '<-- ADDED
> Endif
>
> End
I didn't read all your project yet, but by seing that code, I can tell
you that what you wrote cannot work reliably.
Why? As the process execution is not synchronous, 'sudo' may not be yet
ready when the "Print #$hProcess,..." line is executed.
Instead, you must catch the output of 'sudo' in the Process_Read event,
and only sends the password after sudo has emitted its "Password:" prompt.
Look the 'VersionControl.Run' method in the IDE source code: it runs a
subversion command that way. In the Process_Read, it watches the output
of the svn command, and sends the password as soon as the command emits
a password prompt (which may come almost at random with the 'svn' command!)
So rewrite your code the way I told you before doing all your checks again.
Regards,
--
Benoît Minisini
More information about the User
mailing list