Commit graph

28 commits

Author SHA1 Message Date
GustavoLCR
2fffab66b4 Windows: Subprocess fixes
* Use `_get_osfhandle()` to get std handles
* Call `CancelIO()` if timed out
* Always try to read once even with 0 timeout
2022-07-24 21:00:51 +08:00
GustavoLCR
05df29c226
Windows: Fix for subprocess API handle inheriting (#2773)
Prevents child processes from inheriting all open inheritable handles. This commonly happens when multiple threads are creating processes simultaneously.
Uses the proper API on Windows >= Vista or a lock for older versions.
2022-07-08 09:25:21 +08:00
Giovanni
a988941bf2
Refactor thread code and add RzThreadQueue (#2683)
* Kill the thread only if is alive.
* Remove rz_th_lock_guard.
* Use rz_sys_usleep since pthread_yield/sched_yield are not portable.
* Kill threads only on error.
* Avoid killing already-dead threads.
* RzThreadFunction now returns void* and added test_thread_queue.
* Removed pthread_exit
2022-06-19 21:07:13 +08:00
Florian Märkl
059b291b80 Avoid closing -1 in rz_subprocess_wait()
The stdin_fd may be -1 whenever a subprocess does not have its stdin piped.
This appeared as a failed assertion on Mac OS X 10.5, where rz_sys_pipe_close()
uses a hashtable to track fds.
2022-03-29 08:44:37 +08:00
GustavoLCR
170ab3eb3b
Remove windows.h include from rz_types.h (#2410)
* Create `rz_windows.h`
* Move `RzThread` structure definitions to internal header
* Create and use `rz_th_get_user()` API to avoid exposing `RzThread` layout in external header
* Make `rz_th_self()` an IPI
* Use `_wmkdir()` in `rz_sys_mkdir()` on Windows
* Remove unused `winkd_break_read` function
* Remove `RzIORap` definition from `rz_io.h`
* Remove include of `Windows.h` from `rz_types.h`
* Use `rz_windows.h` when necessary
* Remove `RzCoreRtrHost` definition from `rz_core.h`
* Remove `rz_socket.h` include from `rz_core.h`
2022-03-16 13:54:41 +01:00
GustavoLCR
7035e2c468 Support long paths on Windows with subprocess API 2022-02-13 21:31:03 +08:00
GustavoLCR
b048ea8ee1 Fix starting a process without an ".exe" at the end on Windows 2022-02-13 21:31:03 +08:00
GustavoLCR
570eb371ab Save/restore console mode in subprocess API on Windows 2022-02-13 21:31:03 +08:00
Dhruv Maroo
34d345d884 Format according to clang-format-13 2021-11-25 15:44:28 +08:00
Giovanni
8ace435328
Fix coverity bugs (#1654)
* CID 316614: Use after free (USE_AFTER_FREE)
* CID 316535: Use after free (USE_AFTER_FREE)
* CID 316652: Use after free (USE_AFTER_FREE)
* CID 316504: Unsigned compared against 0 (NO_EFFECT)
* CID 316439: Resource leak (RESOURCE_LEAK)
* CID 356127: Dereference after null check (name can never be NULL)
* CID 320881: Resource leak (RESOURCE_LEAK)
* CID 320885: Missing break in switch (MISSING_BREAK)
* CID 316679: Resource leak (RESOURCE_LEAK)
* CID 316574: Explicit null dereferenced (FORWARD_NULL)
* CID 356105: Resource leak (RESOURCE_LEAK)
* Fixed uninitialized building issue
2021-09-11 09:30:57 +02:00
Riccardo Schirone
6aef990eef Fix few clang-cl warnings 2021-09-04 12:06:07 +02:00
Giovanni
71e4e4d50e
Fix #1507 - Ignore sigpipe (#1526)
* ignore pipe only when writing and restore default signal once written
2021-09-01 02:18:39 +02:00
GustavoLCR
d7e7bae79e
Use wide version of Windows APIs (#1531)
* Use wide version of Windows APIs in subprocess functions
* Use `wmain` on Windows to get UTF16 arguments
* Use wide version of APIs in `rz_cons`
* Always try to set console codepage to UTF8

Co-authored-by: Riccardo Schirone <ret2libc@users.noreply.github.com>
2021-08-27 15:23:26 +08:00
David Carlier
29b68266e5 build fixes warning proposal. 2021-08-23 09:18:10 +02:00
Giovanni
8fe4d0b0a5
Convert ! commands to newshell (#1235)
* Updated core_plugin_example.c
* Removed RZ_BYTES and RZ_BLOCK
Co-authored-by: Riccardo Schirone <sirmy15@gmail.com>
Co-authored-by: Anton Kochkov <anton.kochkov@gmail.com>
2021-07-28 10:33:33 +02:00
Anton Kochkov
ec81933113
Detect presence of extern char **environ (#953)
* Detect presence of extern char **environ
* Set environ properly
2021-04-07 01:09:39 +08:00
Riccardo Schirone
53bf5c497f SPDX Copyright text for all files based on history
- Add LICENSES directory
- Download additional licenses used
- Add .reuse directory
- Add doc about SPDX/reuse

Co-authored-by: Florian Märkl <info@florianmaerkl.de>
2021-03-05 19:39:15 +08:00
Riccardo Schirone
10f72d818c
Implement rz_subprocess_start_opt API to choose pipes (#243) 2021-02-22 12:57:31 +00:00
Alexis Ehret
259bcf237f Fix dead nested assignment librz/util/subprocess.c 2021-02-07 13:43:20 -01:00
Khairul Azhar Kasmiran
4b9e575cac
subprocess.c: Fix format (#519) 2021-02-04 18:25:52 +08:00
Khairul Azhar Kasmiran
f22b290279
Windows subprocess: Remove \r only if followed by \n or not part of esc seq (#512) 2021-02-04 18:00:48 +08:00
Anton Kochkov
a0be4e2e64 Run clang-format 2021-01-21 22:05:44 +08:00
Khairul Azhar Kasmiran
d5601cc2fe
Replace naked rz_sys_system/write/read/freopen calls with their wrapped counterparts (#388) 2021-01-19 00:07:19 +08:00
Riccardo Schirone
a0e8808994
Use async-signal-safe functions after fork in multithread program (#207)
* Do not use setenv after fork, it's not async-signal-safe
* Do not use mutexes after fork

If pipe2 is not available on a system, we try to simulate the atomic
setting of O_CLOEXEC in different ways (depending on whether O_CLOEXEC
is at least available or not). However this involves using mutexes. When
forking, only the calling thread is forked, so it is important to lock
the mutex before the fork. However, in the child between the fork and
the exec* it is recommended to use only async-signal-safe functions. mutex
operations are not async-signal-safe, so we just keep track of whether we are
in the child or not and avoid using the mutex as necessary.

* Replace pthread_atfork with rz_sys_fork
2020-12-18 19:32:06 +01:00
Riccardo Schirone
5d6426508a Make rz_subprocess_lock/unlock private 2020-12-15 10:18:50 +01:00
Riccardo Schirone
286f930b93 Use O_CLOEXEC when creating pipes so they are closed on exec
By default, pipes file descriptors are not closed on exec. This means
they are inherited by the child process. However, this may be a problem
in particular in multi-threading programs (e.g. rz-test), because EOF is
not sent to a pipe until the last pipe file descriptor is closed.

This commit makes sure that by default pipes are created with O_CLOEXEC,
so they are closed whenever an exec syscall is executed. Usually pipe
file descriptors are duplicated to be stdin/stdout/stderr of the
children process and those dupped file descriptors won't be closed on
exec.

In case pipe2 is not available (e.g. MacOS), we do a pipe + fcntl, which
is in theory racy (see man open(2)). For this reason pipe, exec, system,
etc. are wrapped in rz_sys and they use a global mutex to make sure a
pipe is either created and set as O_CLOEXEC or not created at all before
an exec operation is done.

On very old systems, O_CLOEXEC may not be available at all. In those
cases, which should be rare, we just keep a list of file descriptors
created with rz_sys_pipe that we need to close before executing another
process.

Interaction with external libs may be tricky. We expect external libs to
create files/pipes that need to be closed on exec with the right flags
and we expect rizin code to manually close pipes file descriptors as
appropriate if the exec syscall is executed by an external library
(non-rizin code).

Make sure execl is found by acr/make
2020-12-15 10:18:50 +01:00
Riccardo Schirone
5ea4697402 Use RzSubprocess in rz_test 2020-12-15 10:18:50 +01:00
Riccardo Schirone
09d305ec10 Extract subprocess API used in rz-test into RzUtil 2020-12-15 10:18:50 +01:00