Hacking the OS X Kernel for Fun and Profiles
research.swtch.com
research.swtch.com
Betty:~ lelf$ dsymutil -s /mach_kernel |head
----------------------------------------------------------------------
Symbol table for: '/mach_kernel' (x86_64)
----------------------------------------------------------------------
Index n_strx n_type n_sect n_desc n_value
======== -------- ------------------ ------ ------ ----------------
[ 0] 00000004 0f ( SECT EXT) 08 0000 ffffff800084e79c '.constructors_used'
[ 1] 00000017 0f ( SECT EXT) 08 0000 ffffff800084e7a4 '.destructors_used'
[ 2] 00000029 0f ( SECT EXT) 01 0000 ffffff80005241b0 '_AddFileExtent'
[ 3] 00000038 0f ( SECT EXT) 01 0000 ffffff800051ef40 '_AllocateNode'
[ 4] 00000046 0f ( SECT EXT) 01 0000 ffffff800021d510 '_Assert'This is incorrect, because you can prevent fds from getting inherited even without "close on exec". Simply list the files in /dev/fd and you'll see all the file descriptors your program has open, and then you can close all of the ones that the exec'd program won't need. The same thing is done on Linux with /proc/self/fd. The whole facility gets wrapped in a library function (not a standard library function, sadly) since there is a minor trick involved in getting this right.
The problem with close on exec is that libraries would have to be modified to ensure they mark descriptors as close-on-exec. The current system is admittedly arcane and non-portable, but it does work.
Forking + threads is very messy, but it's not quite that pathological. I'd like to see an interface similar to posix_spawn() become the norm for spawning processes, with a fallback to fork()+exec() for more difficult use cases, but I don't think posix_spawn() is good enough.
Edit: upon re-reading the man page, this bit caught my eye:
"If you need to use these frameworks in the child process, you must exec. In this situation it is reasonable to exec yourself."
So that would be one (highly painful) way to handle this safely. Fork, then self-exec, passing in arguments that tell the newly execed process what you really want to run. The new process can then do the /dev/fd listing in peace, then call exec again. Eww.
Another silly way to do things is to have the parent process send a list of fds to close over a pipe, which at least doesn't require a second call to exec().
I think the problem with close-on-exec is always going to be simple though: you have to make sure every library you use sets the flag.
I suppose if I am not mistaken you could go the pre-readdir() route and open a directory with open() and read it with read(). Probably a portability mess though.
(In both cases, you can't count on currently acquired locks ever being released. In a signal handler, this is because it could be acquired on the current thread, and post-fork, this is because all other threads have been killed from the perspective of the child process. As such, no lock-based code can safely be called.)
http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_sample.htmlI have walked around this issue by installing Linux which allows me to tune the CPU governor manually, and it uncaps the artificial limit that the malfunctioning sensor puts.
Would this patch allow me to do something similar in Mountain Lion?
The change you want likely is such a change, as, to me, it looks like a 'replace an if by a NOP or a jump always' patch.
I guess Apple doesn't exactly shout from the rooftops how to do this but I see now that this is the top Google hit for "building xnu". So it's not too hidden.
[1] http://www.phrack.org/issues.html?id=14&issue=49 [2] http://english.stackexchange.com/questions/25205/what-is-the...
what impressed me most is that the hack worked flawlessly on one of my 10.8.5 pre-release kernels even though it had not been tested there yet.
the loadKernel function reads in the kernel and builds a symbol table looking for the bsd_ast and current_thread symbols here:
https://code.google.com/p/rsc/source/browse/cmd/pprof_mac_fi...
then the "apply" func writes the modified binary instructions code:
https://code.google.com/p/rsc/source/browse/cmd/pprof_mac_fi...