nim-sys icon indicating copy to clipboard operation
nim-sys copied to clipboard

Implements the Windows version of sys/exec

Open alaviss opened this issue 4 years ago • 6 comments

Target: Vista+

Problems:

  • CMD and Windows has different parameter quoting algo (and all of them sucks).
  • Not everything in PATHEXT can be run directly (need confirmation on whether CreateProcess can launch .bat directly or cmd.exe has to be used).
  • inherits is racy on Windows since the inheritable state is global, and we need to restore it afterwards. Probably a lock is the best way atm.

alaviss avatar Feb 02 '21 18:02 alaviss

Depends on #2 and #1, we need a path library for findExe and NullessString for process execution.

alaviss avatar Feb 02 '21 19:02 alaviss

There are no parameter quoting rules on windows. Parameters are always a single string with all parameters. The CRT splits them up again in userspace for compatibility with C programs.

barcharcraz avatar Apr 29 '21 01:04 barcharcraz

There is a quoting rule to distinguish C:\Program Files and ["C:\Program", "Files"]. You can read more about it here. The cmd.exe unquoting algo is even worse

The stupid thing with Windows is that programs can implement their own un-quoting scheme, of which msiexec and cmd are the two widely used offenders as far as I know.

alaviss avatar Apr 29 '21 01:04 alaviss

yes, the quoting rules described above are properties of the CRT, if you don't use the CRT you don't get those rules and just get one big parameter string (it's actually similar to how linux deals with parameters passed to executable interpreters). Linux actually keeps parameter arrays separated by parameter as they work their way through the kernel from one process to another.

I've been trying to think of a way to make things easier, it would be good if the CRT's version of the posixey exec functiosn actually did the escaping correctly (they just join the arguments with spaces).

barcharcraz avatar Apr 30 '21 02:04 barcharcraz

yes, the quoting rules described above are properties of the CRT, if you don't use the CRT you don't get those rules and just get one big parameter string (it's actually similar to how linux deals with parameters passed to executable interpreters). Linux actually keeps parameter arrays separated by parameter as they work their way through the kernel from one process to another.

Yes but sys/exec has to cater to the most common case. I have already planned a Windows-specific API to call with your own argument string in case you have to deal with Windows application that doesn't parse like the CRT.

I've been trying to think of a way to make things easier, it would be good if the CRT's version of the posixey exec functiosn actually did the escaping correctly (they just join the arguments with spaces).

Unfortunately for the kind of control that sys/exec has to exert on process creation, we can't use the APIs exposed by the CRT.

alaviss avatar Apr 30 '21 03:04 alaviss

yeah. Even a function in windows would be easier than the "real" fix of getting an actual new syscall to do it. (instead of the current easiest syscall which is NtCreateUserProcess. (I'm actually not sure if that's a "real" syscall)

barcharcraz avatar Apr 30 '21 19:04 barcharcraz