Half-baked idea: Programs can be libraries

For more half-baked ideas, see the ideas tag.

Wouldn’t it be great if qemu was a library. Instead of forking qemu in order to run a guest, you’d just create a new “qemu handle” in your program:

/* gcc program.c -o program -lqemu */
qemu_h *guest = qemu_new (/* args */);
qemu_guest_run (guest);
qemu_destroy (guest);

There would be some sort of “librarifier” for ELF binaries that turns them into ELF shared libraries, hiding all the symbols, and creating a basic API that lets you create new instances, pass command line arguments over, and destroy instances.

For bonus points, programs could become “library-aware” and offer more formal APIs.

9 Comments

Filed under Uncategorized

9 responses to “Half-baked idea: Programs can be libraries

  1. Frank Ch. Eigler

    Do such executables have header files for program.c to #include?

    • rich

      This is one of the things the “librarifier” could do, as well as writing the binding code, managing threads, etc.

      • The big problem is memory management. Programs tend to assume that the operating system will clean up after them when they exit, and there are even some style manuals recommending to rely on that rather than calling cleanup functions on exit, because it saves a few milliseconds. As a result, when librarified, if you call program_new and program_destroy repeatedly, each time you will leak some memory.

        Another problem is global/static variables persisting between runs of the librarified program, which can also confuse some programs. (For example, I know from experience that, even if you build that libgdb.a “library” GDB provides, running gdb_main more than once will cause a lot of trouble. In the program where I needed to do that for reasons I don’t want to go into here, I ended up having to add several kludges to skip some initializations in gdb_main on non-first calls, and there was still the occasional report of crashes due to rerunning gdb_main.)

        And of course, let’s not even START getting into reentrancy: There’s just no way you can run more than one instance of the typical program in the same process at the same time.

  2. Without discussing the general idea, isn’t the exact case you’re referencing (i.e. qemu), isn’t that what libvirt(d) does?

    Regarding the general case, it probably belongs in dbus or systemd.

    • rich

      libvirt is a library, but it isn’t running qemu as a library. libvirtd manages a bunch of qemu processes.

      And dbus … like COM, this is an extremely heavyweight solution compared to using a library.

      • Yes, I know what libvirt is and how it works. My point is that it’s a model for doing what you want while keeping in mind the limitations pointed out by Kevin.

        In terms of where it belongs, dbus does the thing you’re suggesting right now, except it has much richer semantics than the quasi-OO duplicate of system(2) that you’re talking about.

        Though ideologically the “launch this application” code may belong in systemd…

      • rich

        I think the crucial difference is that I want to actually have qemu linked into my program, avoiding the fork/exec of qemu which causes all sorts of issues with signal handling and cleanup.

  3. Alexander Boström

    Wow, the macro symbol and linker symbol name mess that’d cause. A proper module system really is number one on my C wish list.

  4. Sean Halle

    We’re trying to use QEMU as a library in our research. We have created a parallel language for simulating parallel activities. The language controls the suspension and re-animation of threads, in such a way that physical memory contents remains consistent with the order of events in simulation time.
    We want to simulate processors, using QEMU for their behavior. This means the language has to control suspending QEMU and re-animating it. Which works fine if QEMU is called, in one of the language’s threads, as a library.
    I don’t think libvirt will work — our language doesn’t have control over OS threads — and the overhead of OS threads is around 50x higher than our language, more on larger machines.
    Your proposal sounds like exactly what it needs. Is there any way to work together to make it reality?
    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s