Two things:<p>1. Replacing the current executable is <i>generally</i> okay on Unix and Unix-likes, but it's not particularly well-defined. Linux handles it gracefully, others may ETXTBSY or similar depending on how the replacement is done.<p>2. The hack being used on Windows is <i>awful</i>: it involves patching the underlying C runtime, can fail in uncontrolled ways (including in an `atexit` handler), and is fundamentally racy. The state it leaves a failed operation in isn't self-healing, and surfaces confusing filesystem state to the user on failure.<p>If you want to replace a running executable on Windows, either ask the user (and have them affirmatively kill the running program) or do the "cooperative rename" trick[1]. It doesn't require any unsafety, much less patching the runtime underneath you.<p>[1]: <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/07fb6078-c41d-4829-a62d-f22dd112360c/how-to-replace-exe-file-with-same-updated-exe-file?forum=csharpgeneral" rel="nofollow">https://social.msdn.microsoft.com/Forums/vstudio/en-US/07fb6...</a>
I don't understand why people think this is a good idea to do at all. Yes it makes updates easier. But each software doing that has to run with the privileges to update itself. Which is in my book a big security downside. Separating rights to install, update, and remove software from the right to run and use it should be complete separate.<p>I admit this is not very nice for private Windows users. But this is one of the strength of Linux distributions.
A little known fact about executables on Windows is that while it's not possible to remove a running executable, it's possible to <i>rename</i> it.<p>I use this in Clyde [1]: on Windows, when clyde needs to upgrade itself (which means uninstalling vN and installing vN+1) it renames itself from `clyde.exe` to `_clyde.exe`. I leave the old `_clyde.exe` around, but a nicer implementation could remove on the next start.<p>[1]: <a href="https://github.com/agateau/clyde">https://github.com/agateau/clyde</a>
Crazy idea, just make your EXE into a stub that loads your actual program, which is implemented in a DLL. When it's time to update, drop a newer DLL in there that the EXE will load instead. Hopefully you won't have to update the stub very often.
This is interesting.<p>Google Chrome can patch binaries and the Linux kernel can be patched while running.<p>I would like this capability for general purpose software, hot code reloading. Can do seamless non-breaking changes to a live server without a full restart.<p>Would be useful for Function as a Service and WASM runtimes if you could live patch code running.<p>I generally rely on Kubernetes or CI to do a rolling deploy of software to switch to the new version, but if we could patch a server while running that would be interesting. Would have interesting interactions with socket connections using the old code and connections using the new code and all the complicated states you can get into, if your data structures are different.<p>How do you move data if you added a field to your data structure? Presumably if you added a field, there isn't enough memory allocates in the existing data structures... Your malloc is for the wrong size.
When I had written my shell to the point where I could execute simple commands, this was the first thing that came to my mind using it.<p>Execute the shell -> Modify the source code of the shell with vim -> Remake the shell -> Execute the newly built shell with the old shell<p>Just for fun :)
the windows implementation seems awfully complex. what's wrong with something like _popen("find \"\" & del " + argv[0])? the problem to solve is "we can't delete ourselves, so we need something else to do it for us", but there are a myriad options to choose from, you don't have to write it in rust. hell, you've already got the dubious sleep(100) in there, you can even write _popen("ping 127.0.0.1 & del " + argv[0]) as suggested by stack overflow?
How does it relate to KSplice [1], from a technical point of view?<p>The first time I heard about it was when Jessica McKellar [2] explained what it is and what it does, in a video right before KSplice, Inc. was acquired by Oracle.<p>[1] <a href="https://en.wikipedia.org/wiki/Ksplice" rel="nofollow">https://en.wikipedia.org/wiki/Ksplice</a><p>[2] <a href="https://en.wikipedia.org/wiki/Jessica_McKellar" rel="nofollow">https://en.wikipedia.org/wiki/Jessica_McKellar</a>