X-Original-To: pgsql-hackers-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 6B76B52A5A for ; Fri, 4 Mar 2005 05:30:15 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 33174-06 for ; Fri, 4 Mar 2005 05:30:06 +0000 (GMT) Received: from mailbox.samurai.com (mailbox.samurai.com [205.207.28.82]) by svr1.postgresql.org (Postfix) with ESMTP id A8EDA52A50 for ; Fri, 4 Mar 2005 05:30:05 +0000 (GMT) Received: from localhost (mailbox.samurai.com [205.207.28.82]) by mailbox.samurai.com (Postfix) with ESMTP id 16A9318CCAC for ; Fri, 4 Mar 2005 00:30:05 -0500 (EST) Received: from mailbox.samurai.com ([205.207.28.82]) by localhost (mailbox.samurai.com [205.207.28.82]) (amavisd-new, port 10024) with LMTP id 26637-01-10 for ; Fri, 4 Mar 2005 00:30:03 -0500 (EST) Received: from [61.88.101.19] (unknown [61.88.101.19]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mailbox.samurai.com (Postfix) with ESMTP id D864B18CC1D for ; Fri, 4 Mar 2005 00:30:02 -0500 (EST) Message-ID: <4227F258.9040709@samurai.com> Date: Fri, 04 Mar 2005 16:30:00 +1100 From: Neil Conway User-Agent: Debian Thunderbird 1.0 (X11/20050116) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-hackers Subject: refactoring fork() and EXEC_BACKEND Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at mailbox.samurai.com X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.04 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/111 X-Sequence-Number: 64913 While going through the usual motions needed to fork a child process of the postmaster, it occurred to me that there's a fair bit of duplicated code involved. There are also #ifdef for various situations (BeOS, LINUX_PROFILE, and EXEC_BACKEND), which makes the code yet more ugly. I think we could make this a lot cleaner. I'd like to define an API like so: pid_t fork_process(int proc_type); pid_t fork_backend(Port *port); If the process needs to add a lot of private information to the argv in the case of EXEC_BACKEND, they could invoke a third variant: #ifdef EXEC_BACKEND pid_t forkexec_process(int proc_type, int argc, char **argv); #endif (Or possibly using varargs, if that is cleaner for most call-sites). Hopefully most call sites could just use fork_process(). These functions would then take care of all the necessary platform-specific judo: - flush stdout, stderr - invoke BeOS hooks as necessary - save and restore profiling timer, if necessary - if EXEC_BACKEND, use proc_type to lay out the argv for the new process and then invoke internal_forkexec() - otherwise, just invoke fork() - return result to client So, most call sites would be quite nice: pid_t result = fork_process(PROC_TYPE_FOO); if (result == -1) { /* fork failed, in parent */ } else if (result == 0) { /* in child */ } else { /* in parent, `result' is pid of child */ } I'd also like to move the implementation of fork_process() and friends, as well as internal_forkexec(), into a separate file -- I'd rather not clutter up postmaster.c with it. Comments? -Neil