Journal Journal: systemd 12 - how large is pid 1? 15
Systemd pid 1 takes up 1.5 megabytes on my Fedora system hard drive. For comparison, sysvinit compiles down to 40k. For security purposes, smaller is better, but getting the interfaces right is far more important.
So what is systemd doing with that 1.5 megabytes of compiled code? To answer that question, we find a good place to start, src/core/main.c, which is 2085 lines long. That is not necessarily too long, but if you are a fan of short functions, you will be horrified to learn that the main function is 825 lines long.
Typically a long function starts out well organized, but as people add to it over time, it becomes more and more chaotic, and that is starting to happen here. It is not always clear the order of why things happen, for example. If I were going to refactor this, it would look something like this:
main() {
init();
main_loop();
cleanup();
}
So what is it doing with all those 825 lines of code? I went through and made a rough outline, which I wrote in code form. It could be further simplified, of course, but this will give you a basic understanding of what is in the main function, and it's better than 825 lines of chaotic code:
int main(void) {
getTimestamp();
parseSomeCommandlineArgs();
setPIDNameToSystemd();
setUMaskToZeroIfRequired();
initFilesystems();
initSELinux();
initSMACK();
initIMA();
initKernelClocks();
initLogging();
initDefaultUnit();
initJoinControllers();
mountKernelVirtualFilesystems();
initSignalHandlers();
parse_Config_Commandline_Environment_and_KernelCommandLine();
daemonize();
initIfRunningAsManagerSystem();
initIfRunningAsManagerUser();
managerSetup();
setCapabilityBounding();
enforceSyscallArchitectures();
queueDefaultJob();
do{
runMainLoop();
}while(handleStateChange());
reexecuteIfNeeded();
cleanupAndShutdown();
}
I tried pasting the original function here so you could compare, but Slashdot apparently doesn't allow posts that long and chopped it off. If you want to see it, you'll have to put some extra effort in and click on this link.
When code gets too long, you need to break it into smaller pieces, so the human brain can see how the code is organized. When you refactor like this, it's important to make sure that the refactored code shows the overall structure, rather than obscuring it.
As always, please comment or correct me below, I am interested in hearing what you think!
(I've written other reviews, too, check them out).