aboutsummaryrefslogtreecommitdiff
path: root/win32/process.c (follow)
Commit message (Collapse)AuthorAgeFilesLines
* win32: further fix incorrect path search in spawnvpRon Yorston2023-04-161-1/+3
| | | | | | | | Commit 9581d2396 (win32: fix incorrect path search in spawnvp) fixed the unwanted PATH search for relative or absolute paths but broke the desired PATH search for Unix-style paths. (GitHub issue #310)
* win32: fix incorrect path search in spawnvpRon Yorston2023-04-141-3/+1
| | | | | | | | | Commit 26ba73098e (win32: search PATH for missing Unix-style executables) rearranged the code of mingw_spawnvp(). As a result commands with a relative or absolute path could be incorrectly searched for on PATH. (GitHub issue #310)
* win32: ignore ctrl-c in parent of execve(2)Ron Yorston2023-04-051-4/+14
| | | | | | | | | | | | | | | | | | The execve(2) system call is emulated for Microsoft Windows. This requires the creation of a new process. The old process remains active, waiting for the "execed" child to exit so it can pass on its exit status. Previously this was achieved using P_WAIT mode in the call to spawnve(). However the parent of the execve(2) process may still be able to catch Ctrl-C interrupts. This can lead to unwanted behaviour, such as a shell and its children competing for input. Force the waiting process to ignore Ctrl-C interrupts. Costs 64-80 bytes. (GitHub issue #303)
* runuser,drop: code shrinkRon Yorston2023-03-191-3/+2
| | | | | | | | | Make quote_arg() always return an allocated string so we can free it unconditionally. Always use argv[1] as the first part of the command string. Saves 48 bytes.
* runuser: add 'drop' as an alias for runuserRon Yorston2023-03-161-1/+1
| | | | | | | | | | | | | | | | | | | The 'drop' alias for 'runuser' relaxes a number of constraints that were introduced for compatibility: - It works even if the current process doesn't have elevated privileges. - It isn't necessary to specify the name of the user. - Any command can be invoked, not just the BusyBox shell. - If the command doesn't specify a path 'drop' will first look for a BusyBox applet then search PATH. Adds 320-336 when built along with runuser. (GitHub issue #240)
* win32: code shrink detection of executablesRon Yorston2023-03-161-7/+4
| | | | | | | | | | | | Add a function, file_is_win32_exe(), to detect if a path refers to an executable. It tries adding extensions if necessary. Use this in a number of places to replace common code of the form path = alloc_ext_space(cmd); if (add_win32_extension(path) || file_is_executable(path)) Saves 32-48 bytes.
* runuser: new appletRon Yorston2023-03-131-1/+1
| | | | | | | | | | | | | | | | | | | | | Add a cut down, Windows-specific implementation of `runuser` from util-linux. This allows elevated privileges to be dropped when running in an SSH session. It also works when using `su` or starting busybox-w32 'as administrator'. There are complications: - The method used to drop privileges leaves the access token in the TokenIsElevated state. Detecting this is likely to be fragile. - The unprivileged shell is started by CreateProcessAsUserA(). In older versions of Windows this has to be loaded dynamically. Adds about 900 bytes. (GitHub issue #240)
* win32: revert special treatment of Unix-style absolute pathsRon Yorston2022-10-261-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | Commit 605972390 (win32: handle Unix-style absolute paths for executables) added special treatment of paths for executables starting with a slash. Such paths are absolute on Unix but are relative to the current drive on Windows. On reflection this commit did more than necessary. Later commits provided special treatment only for paths starting with locations traditionally used to contain binaries on Unix. This is probably sufficient. Problems introduced by commit 605972390 include: - If the current drive isn't the system drive tab completion of a command starting with a slash confusingly references the system drive. - Building busybox-w32 with w64devkit fails on drives other than the system drive. Revert the changes introduced by commit 605972390. This saves 192 bytes. (GitHub issue #239)
* win32: search PATH for missing Unix-style executablesRon Yorston2022-05-061-29/+39
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Commit 41ef232fc5 (win32: use built-in applets for non-existent binaries with Unix-style paths) alters what happens when trying to find an executable. If all of the following apply: - the pathname starts with one of the standard directories for Unix executables (/bin, /usr/bin, /sbin, /usr/sbin); - the file isn't found relative to the system root; - the basename matches an applet then the applet is run. Further extend the procedure so that if the first two conditions are met and either: - the PREFER_APPLETS and SH_STANDALONE features are enabled and the basename *doesn't* match an applet or - the PREFER_APPLETS and SH_STANDALONE features are disabled then PATH is searched for the basename. This affects: - how interpreters and binaries are spawned by mingw_spawn_interpreter() and mingw_spawnvp(); - how 'which' and the shell search for binaries. Special steps need to be taken in the shell to avoid treating shell built-ins and functions as applets. As a consequence of this change: - An executable that isn't an applet, say curl.exe, can be run as /usr/bin/curl so long as it's in a directory in PATH. It doesn't have to be in C:/usr/bin. - If the PREFER_APPLETS and SH_STANDALONE features are disabled binaries can be run using paths referring to standard Unix directories even if they're installed elsewhere in PATH.
* win32: better fix for empty environment variablesRon Yorston2022-05-051-9/+4
| | | | | | | | | | | | | | | | | | | It appears the CRT and OS each have a copy of the environment. mingw_putenv() fools the CRT into accepting an empty environment variable by calling _putenv("V=0") then truncating the new value by hand. But _putenv() also updates the OS environment with the fake 'V=0' value. Commit 5b48ca53b (win32: pass NULL to spawnve, not environ) resulted in this fake value being used and hence empty variables getting the value '0'. - Add a call to SetEnvironmentVariable() in mingw_putenv() to update the OS environment. - Restore the use of NULL environment pointers in mingw_spawnvp(). - Add a test. (GitHub issue #250)
* win32: revert changes related to environment variablesRon Yorston2022-05-041-4/+9
| | | | | | | | | | Revert the change to mingw_putenv() in the previous commit. When compiling for MSVCRT (i.e. not for UCRT) revert some of the changes from commit 5b48ca53b (win32: pass NULL to spawnve, not environ). (GitHub issue #250)
* win32: conditional compilation in process.cRon Yorston2022-04-241-10/+10
| | | | | | | Drop the use of ENABLE_FEATURE_SH_STANDALONE in process.c In mingw_spawn_interpreter() check for an applet *before* trying to run the interpreter using the path provided.
* win32: add link to LWN article on shebangRon Yorston2021-11-141-1/+2
|
* win32: rename mingw_spawn_1Ron Yorston2021-11-011-8/+8
| | | | | | | | | | The name of the function mingw_spawn_1() wasn't particularly meaningful. Remove its envp argument (as all of its current callers pass a NULL pointer) and rename it mingw_spawnvp() to better reflect what it does. The path search it performs isn't the standard one: it has features specific to busybox-w32.
* win32: pass NULL to spawnve, not environRon Yorston2021-10-281-4/+4
| | | | | | | | | | | | Building busybox-w32 for use with UCRT results in mysterious failures. (GitHub issue #234) These are somehow related to the environment values passed to spawnve. In several places the global environ pointer was being passed to spawnve so the child would inherit its environment from the parent. This can also be achieved by passing a NULL pointer. This prevents the failures in at least some cases and also makes the binary smaller.
* win32: handle -1 return status from execve(2)Ron Yorston2021-06-191-2/+3
| | | | | | | | The implementations of execve(2) and execvp(3) were unable to distinguish between failure and a program returning a status of -1. Check the error return code to discriminate between these cases. See GitHub issue #218.
* ash: code shrinkRon Yorston2021-02-141-9/+0
| | | | | Since there's only one call to mingw_spawn_forkshell() we might as well just call spawnve() directly from ash.
* win32: code shrink Unix-style path handlingRon Yorston2020-08-231-19/+9
| | | | | | | | Replace auto_add_system_drive() with alloc_system_drive() which leaves space for a possible filename extension. This makes it possible to drop alloc_win32_extension() and auto_win32_extension(). Saves 144 bytes.
* win32: use built-in applets for non-existent binaries with Unix-style pathsRon Yorston2020-08-131-2/+13
| | | | | | | | | | | | | | | | | | | | Shell scripts moved from Unix may contain hard-coded paths to binaries such as /bin/sh. A recent commit made it possible to execute such binaries reliably, but that does require them to be installed. As an alternative solution: if a binary with a standard Unix path prefix can't be found but is available as a built-in applet, run the applet. Add the function unix_path() to detect paths starting with /bin, /usr/bin, /sbin or /usr/sbin. Use this function in: - the 'which' applet - shellexec(), describe_command() and find_command() in ash - mingw_spawn_1() See GitHub issue #195.
* win32: handle Unix-style absolute paths for executablesRon Yorston2020-08-131-1/+10
| | | | | | | | | | | | | | | | | | | | | | As noted in commit 548ec7045 (win32: interpret absolute paths as relative to %SYSTEMDRIVE%) a path starting with a '/' in the Unix world is treated as relative to the current drive by Windows. To avoid ambiguity that commit considered certain such paths to be relative to %SYSTEMDRIVE%. Extend this to paths representing executables. Add the functions need_system_drive() and auto_add_system_drive() to detect the need for a system drive prefix and to add it if necessary. Use these functions in: - the 'which' applet - the find_executable() function - tab-completion code - PATH look-up, shellexec(), describe_command() and find_command() in ash - parse_interpreter() and mingw_spawn_1() With these changes executable paths starting with a slash are handled consistently, whatever the current drive.
* win32: code shrinkRon Yorston2020-08-131-1/+1
| | | | | | | | | | Add a new function, has_path(), to detect that an executable name doesn't require a path look-up. Also, since is_absolute_path() is now only used in shell/ash.c move its definition there from include/mingw.h. Saves 128 bytes.
* win32: code shrink kill(2)Ron Yorston2020-07-251-31/+17
| | | | | | | | - Drop exit_code argument from kill_SIGTERM_by_handle() - Pass signal number rather than exit code to other functions - Merge kill_SIGKILL() and kill_SIGTEST() Saves 112 bytes.
* win32: update link to Microsoft document on command-line argumentsRon Yorston2020-07-211-1/+1
|
* win32: minor improvements to process spawningRon Yorston2020-06-121-3/+2
| | | | | | | | | | | | | | The function mingw_spawn_forkshell() was introduced to handle spawning a forkshell process. Since we know that: - the binary being executed doesn't meet any of the special cases handled by spawnveq() - the arguments don't require quoting we can call spawnve() directly instead of spawnveq(). Also, use xzalloc() to allocate new_argv so we don't need to set the final NULL pointer explicitly.
* time: detect when spawn failsRon Yorston2020-06-041-1/+1
| | | | | | | In the 64-bit build the time applet reported garbage when it was unable to run the program to be timed. The error return from mingw_spawn_pid() was incorrect.
* win32: use lazy loading for certain DLLsRon Yorston2020-06-021-2/+13
| | | | | | | | | Only a handful of functions are used from shell32.dll, userenv.dll and psapi.dll. Mostly these functions are in out of the way places. By loading the functions only when required we can avoid the startup cost of linking the three DLLs in the common case that they aren't needed.
* ash: simplify spawning during forkshellRon Yorston2020-06-011-0/+9
| | | | | | | | | | | | | spawn_forkshell() uses mingw_spawn_proc() to start the child shell. mingw_spawn_proc() then calls mingw_spawn_1() which determines that "sh" is an applet, thus calling mingw_spawn_applet() which finally calls spawnveq(). Not only is this convoluted it also won't work if PREFER_APPLETS and SH_STANDALONE aren't enabled. Simplify matters by adding a new function, mingw_spawn_forkshell(), which is tailored for just this case.
* win32: changes to '#!' supportRon Yorston2020-05-241-10/+12
| | | | | | | | | | | | | Since the earliest days of busybox-w32 '#!' has searched PATH for the interpreter. This doesn't seem to be supported by precedent. In testing I also found that additional code was needed for the case where PATH has been altered in the current shell or is modified on the command line: PATH has to be extracted from envp rather than getenv("PATH"). Drop this non-standard feature. *NIX implementations disagree on whether the interpreter can itself be a script. Follow Linux and allow this with a limit of four levels of nesting.
* time: WIN32 portRon Yorston2020-04-061-7/+34
| | | | | | | | Port the time applet to WIN32. This requires the implemntation of a replacement for wait3(2). Only elapsed, user and system times are supported, not the memory and i/o statistics reported by GNU time.
* win32: new functions: getpeername(2), mingw_spawn_detach()Ron Yorston2020-04-051-3/+15
| | | | | | | Implement getpeername(2). Add mingw_spawn_detach() to allow the spawned process to detach from the console.
* win32: more improvements to 'c:file' pathsRon Yorston2020-03-291-2/+3
| | | | | | | | Tab-completion of 'c:file' paths was leaking memory and incorrectly included BusyBox applets and shell built-ins. Fix handling of 'c:file' paths used as arguments to spawn functions. Otherwise things like 'timeout 5 c:busybox sleep 99' don't work.
* win32: trim leading and trailing spaces from shebang optionsRon Yorston2019-05-031-0/+4
| | | | | | | | | | | | Commit 97e2c4a05 (win32: changes to treatment of scripts) attempted to use strtok(3) to simplify the parsing of shebang lines. Unfortunately it resulted in leading and trailing whitespace being left in the option string. Fix this by trimming the options before they're returned. Reported-by: Niklas DAHLQUIST <niklas.dahlquist@st.com> Signed-off-by: Ron Yorston <rmy@pobox.com>
* win32: add function to convert slashes to backslashesRon Yorston2019-03-151-5/+2
| | | | | | | | | There are now two places where slashes are converted to backslashes throughout a string so it makes sense to create a function to do this. To avoid confusion rename convert_slashes() to bs_to_slash() and call the new function slash_to_bs().
* win32: drop argument from err_win_to_posix()Ron Yorston2019-03-061-2/+2
|
* win32: make more use of common lazy loading codeRon Yorston2019-02-271-17/+5
|
* win32: don't try to run non-existent scriptRon Yorston2019-02-071-1/+1
| | | | | | In parse_interpreter return an error when the file doesn't exist. The current code tries to run non-existent scripts with a .sh suffix, thus breaking the test ash-misc/exec.tests.
* ps: fix long output when desktop compatibility is disabledRon Yorston2019-01-081-0/+3
| | | | | | | | In desktop mode the state column is disabled and doesn't affect output. When desktop mode is disabled the state column is displayed in long output. Put some fake data in the column to improve the display.
* ps: display interpreted script name in comm columnRon Yorston2019-01-081-1/+2
| | | | | | | | | | | When an interpreted script is being run the comm column in ps should display the name of the script not the name of the interpreter. Use a fake applet pathname to indicate which argument contains the script. This also allows pidof to obtain the pid of a script.
* ps: add support for the args columnRon Yorston2019-01-081-11/+29
| | | | | | | | | Implement read_cmdline() for WIN32 by storing the command line in the same way as the applet name. The applet name is actually used for the comm column which is truncated to COMM_LEN. Using this as the size of the bb_comm array avoids the need to calculate MAX_APPLET_NAME_LEN.
* ps: add user and group columnsRon Yorston2019-01-081-1/+10
|
* win32: special treatment for PATHRon Yorston2018-12-141-20/+0
| | | | | | | | | | | | | | | | | The PATH shell variable is a special case. It can be exported to the environment where it might be interpreted by native applications which assume the separator is ';'. Hence: - require that the separator used in PATH is ';' - enforce this by intercepting calls to setvareq() that set PATH and adjusting its value if necessary. As a result of this the code to parse PATH can be simplified by replacing the hardcoded Unix ':' path separator by the platform- dependent macro PATH_SEP. The MANPATH variable is also required to use ';' as its separator but since it's less likely to be used this isn't enforced.
* win32: special treatment for files with the '.sh' extensionRon Yorston2018-12-091-27/+36
| | | | | | | | | | | | Files with the extension '.sh' are considered to be executable. Those that start with a '#!' line will be run using the specified interpreter. If no '#!' is present the script will be run by the shell. When searching for an executable the '.sh' extension will be tested in the same way as the standard extensions understood by spawnve(). '.sh' takes precedence over the standard extensions.
* win32: use open_read_close() where possibleRon Yorston2018-12-091-6/+2
|
* win32: allow execution of empty batch file to succeedRon Yorston2018-12-081-0/+8
| | | | | | | Microsoft Windows' spawnve returns ERROR_BAD_EXE_FORMAT when passed an empty batch file. Work around this by skipping spawnve and returning success.
* win32: improve execution of batch filesRon Yorston2018-12-071-26/+8
| | | | | | | | | It appears that when a batch file is executed the first argument must contain backslashes if it's a relative path. Absolute paths work either way. In both cases the extension is optional. This allows for a considerable simplification of the special case in spawnveq.
* win32: rework adding of extensions to filenamesRon Yorston2018-12-061-2/+2
| | | | | | | | | | | | | | | | | | | | | | | Previously there was one function to handle adding extensions to executable filenames, add_win32_extension(). Refactor this into three functions: add_win32_extension() appends the suffix to the argument string in-place. The argument must be long enough to cope with this, as is the case in ash where path_advance() adds 4 bytes to each filename for just this reason. alloc_win32_extension() is equivalent to the old add_win32_extension(). It allocates a string to hold the new filename then calls the new add_win32_extension() function. The caller is responsible for managing the returned string. auto_win32_extension() calls alloc_win32_extension() and saves the resulting string using auto_string(). It's used where the new filename is consumed immediately or the actual value isn't needed. Rewrite code to use the most appropriate function. Also reorder some code in find_executable() and find_command().
* ash: fix a couple of test casesRon Yorston2018-12-041-3/+1
| | | | | | | | | | | | | | The ash tests exitcode_EACCES and exitcode_ENOENT both failed. In commit 92dbd3c09 a test was added to tryexec to check that the file being run existed and was executable. The error codes returned by this test were incorrect. The slightly later commit f5783ef14 added a similar test in spawnveq which got the error codes right. Remove the test from tryexec and some superfluous error messages from spawnveq.
* win32: save a few bytes in process scanningRon Yorston2018-11-271-14/+7
| | | | | | Recalculate the system boot time on every pass through the process scanning loop. It's less efficient than storing the value in a static variable but not noticeably so and it saves a few bytes.
* win32: use string_array_len to obtain size of argv arrayRon Yorston2018-10-261-8/+4
| | | | Saves 16 bytes.
* win32: better error message in kill(2)Ron Yorston2018-07-121-0/+2
| | | | | | When the process architecture of busybox-w32 didn't match that of the target process the error reported was 'Function not implemented'. Change this to 'Permission denied'.