www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.process - avoid interaction with parent shell

reply Steven Schveighoffer <schveiguy gmail.com> writes:
I am doing some scripting via D, and using std.process.execute to git 
clone things.

I don't want any user interaction. Occasionally, I get a repository that 
no longer exists (404). Then git comes up and asks for a 
username/password. I want it to just fail. Apparently git has no option 
to be non-interactive, it supposedly checks stdin to see if it's a tty, 
and only errors if it's not.

I tried redirecting /dev/null to stdin when executing my application 
(and I assumed that would pass onto the process child), but it still 
asks. What am I doing wrong?

e.g.:

myapp < /dev/null
Username for 'https://github.com':
Program pauses, waits for my input.

-Steve
Jul 20 2020
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Jul 20, 2020 at 04:55:52PM -0400, Steven Schveighoffer via
Digitalmars-d-learn wrote:
 I am doing some scripting via D, and using std.process.execute to git
 clone things.
 
 I don't want any user interaction. Occasionally, I get a repository
 that no longer exists (404). Then git comes up and asks for a
 username/password. I want it to just fail. Apparently git has no
 option to be non-interactive, it supposedly checks stdin to see if
 it's a tty, and only errors if it's not.
Try --no-pager perhaps? Not sure if that would help, since this isn't technically a pager that's prompting you. Another way is to take a look at std.process.execute's implementation. I believe it's just a wrapper around spawnProcess. What you want is to adapt that implementation so that it closes stdin before fork-n-exec'ing git; that should stop any prompts. One thing to be aware of is that it may not necessarily be git itself that's prompting you; it could be a helper program like a password manager that creates the prompt. In that case you probably have to find out what it is, and disable it somehow (usually by overriding some environment variable that gets passed to the git child process). T -- Ph.D. = Permanent head Damage
Jul 20 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/20/20 5:24 PM, H. S. Teoh wrote:
 On Mon, Jul 20, 2020 at 04:55:52PM -0400, Steven Schveighoffer via
Digitalmars-d-learn wrote:
 I am doing some scripting via D, and using std.process.execute to git
 clone things.

 I don't want any user interaction. Occasionally, I get a repository
 that no longer exists (404). Then git comes up and asks for a
 username/password. I want it to just fail. Apparently git has no
 option to be non-interactive, it supposedly checks stdin to see if
 it's a tty, and only errors if it's not.
Try --no-pager perhaps? Not sure if that would help, since this isn't technically a pager that's prompting you. Another way is to take a look at std.process.execute's implementation. I believe it's just a wrapper around spawnProcess. What you want is to adapt that implementation so that it closes stdin before fork-n-exec'ing git; that should stop any prompts.
I ran the git command from the shell directly with < /dev/null and it still can ask for username/password. I don't know if it's possible to prevent it.
 
 One thing to be aware of is that it may not necessarily be git itself
 that's prompting you; it could be a helper program like a password
 manager that creates the prompt. In that case you probably have to find
 out what it is, and disable it somehow (usually by overriding some
 environment variable that gets passed to the git child process).
I think you might be right. I don't know how it's accessing my terminal, but clearly it can keep doing so even without any handles open. I'm even using ctrl-D and it continues to come up with prompts and wait for input. I was able to solve it by backgrounding the process, and then quitting the parent shell, then it had no option but to error ;) I'm still interested in knowing how this works, if anyone knows. Searching for things like "how does git access my terminal when I closed stdin" doesn't give me much information. -Steve
Jul 20 2020
parent Paul Backus <snarwin gmail.com> writes:
On Monday, 20 July 2020 at 21:44:31 UTC, Steven Schveighoffer 
wrote:
 I think you might be right. I don't know how it's accessing my 
 terminal, but clearly it can keep doing so even without any 
 handles open.
Probably /dev/tty
Jul 20 2020
prev sibling next sibling parent reply Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Monday, 20 July 2020 at 20:55:52 UTC, Steven Schveighoffer 
wrote:
 I tried redirecting /dev/null to stdin when executing my 
 application (and I assumed that would pass onto the process 
 child), but it still asks. What am I doing wrong?
Generically, I think you want to detach the program from the current terminal (as well as doing the above). I think setsid can be used for this purpose. Specifically, checking git's source code, I see that setting the environment variable GIT_TERMINAL_PROMPT to 0 will disable password prompts.
Jul 20 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/20/20 6:04 PM, Vladimir Panteleev wrote:
 On Monday, 20 July 2020 at 20:55:52 UTC, Steven Schveighoffer wrote:
 I tried redirecting /dev/null to stdin when executing my application 
 (and I assumed that would pass onto the process child), but it still 
 asks. What am I doing wrong?
Generically, I think you want to detach the program from the current terminal (as well as doing the above). I think setsid can be used for this purpose.
Would be a good option possibly to put into std.process.
 
 Specifically, checking git's source code, I see that setting the 
 environment variable GIT_TERMINAL_PROMPT to 0 will disable password 
 prompts.
 
Thanks to you and FreeSlave, that seems to work. I had already solved it by just quitting the terminal with it still running. But good to know for the future! -Steve
Jul 21 2020
prev sibling parent FreeSlave <freeslave93 gmail.com> writes:
On Monday, 20 July 2020 at 20:55:52 UTC, Steven Schveighoffer 
wrote:
 I don't want any user interaction. Occasionally, I get a 
 repository that no longer exists (404). Then git comes up and 
 asks for a username/password. I want it to just fail. 
 Apparently git has no option to be non-interactive, it 
 supposedly checks stdin to see if it's a tty, and only errors 
 if it's not.


 -Steve
Try setting GIT_TERMINAL_PROMPT=0 as an environment variable.
Jul 20 2020