I have written a small custom web server application in C running on Linux. When the application receives a request it calls fork() and handles the request in a separate process, which is chrooted into a specific directory containing the files I want to make available.
I want to port the application to Windows, but neither fork() nor chroot() are available on this platform, and there don’t seem to be any direct equivalents. Can you point me to a simple (and preferably well written) example of code that will provide this functionality in Windows? My C isn’t all that good, so the simpler the better.
First of all, the Windows equivalent of
chrootisRUNASwhich is documented here. If you need to do this from a program, then studying this C++ source code should help you understand how to use the Windows API. It is not precisely the same aschroot()but Windows folk use it to create something like a chroot jail by creating a user with extremely limited permissions and only giving that user read permission on the application folder, and write permission on one folder for data.You probably don’t want to exactly emulate
fork()on Windows because it doesn’t sound like you need to go that far. To understand the Windows API for creating processes and how it differs fromfork(), check Mr. Peabody Explains fork(). The actual current source code for Cygwin’s fork implementation shows you the current state of the art.The Microsoft documentation for
CreateProcess()andCreateThread()are the place to look for more info on the differences between them.And finally, if you don’t want to learn all the nitty-gritty platform details, just write portable programs that work on Windows and Unix, why not just use the Apache Portable Runtime library itself. Here are some docs on process creation with some sample code, in C, to create a new process.