From owner-freebsd-current@FreeBSD.ORG Tue Sep 16 16:05:51 2008 Return-Path: Delivered-To: current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C54161065673; Tue, 16 Sep 2008 16:05:51 +0000 (UTC) (envelope-from ache@nagual.pp.ru) Received: from nagual.pp.ru (nagual.pp.ru [194.87.13.69]) by mx1.freebsd.org (Postfix) with ESMTP id 53D828FC1A; Tue, 16 Sep 2008 16:05:45 +0000 (UTC) (envelope-from ache@nagual.pp.ru) Received: from nagual.pp.ru (ache@localhost [127.0.0.1]) by nagual.pp.ru (8.14.3/8.14.3) with ESMTP id m8GG5gib041098; Tue, 16 Sep 2008 20:05:42 +0400 (MSD) (envelope-from ache@nagual.pp.ru) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nagual.pp.ru; s=default; t=1221581143; bh=U+sLeLy+G7XsvVX/TlyS9ina285e//T/Zp9iWvp d6Vs=; l=2810; h=Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:In-Reply-To; b=cnI8J62kI0kQmtWY+ml86FZ+c pFl6oCrx88Oc05gaz+l5L1dkMNTKswg5DdWcGavK0a6r0NY70MsjTNfEtqtsVtWWJ0v P9OZBirsk5bcxf8PzQruC6HFeQY3BGqIGnxx4M5GHwdpNwloZnNy+1DLYo1u0poOsLy LI/lECnEHXE4= Received: (from ache@localhost) by nagual.pp.ru (8.14.3/8.14.3/Submit) id m8GG5edj041097; Tue, 16 Sep 2008 20:05:40 +0400 (MSD) (envelope-from ache) Date: Tue, 16 Sep 2008 20:05:37 +0400 From: Andrey Chernov To: Daniel Eischen Message-ID: <20080916160535.GA40676@nagual.pp.ru> Mail-Followup-To: Andrey Chernov , Daniel Eischen , Attilio Rao , current@freebsd.org References: <20080916140319.GA34447@nagual.pp.ru> <20080916144502.GA39765@nagual.pp.ru> <3bbf2fe10809160753o7e5e8a78q7c6bd44c02bfd5c2@mail.gmail.com> <20080916150120.GA40087@nagual.pp.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.18 (2008-05-17) Cc: Attilio Rao , current@freebsd.org Subject: Re: Is fork() hook ever possible? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Sep 2008 16:05:51 -0000 On Tue, Sep 16, 2008 at 11:36:03AM -0400, Daniel Eischen wrote: > On Tue, 16 Sep 2008, Andrey Chernov wrote: > > > On Tue, Sep 16, 2008 at 04:53:54PM +0200, Attilio Rao wrote: > >> 2008/9/16, Andrey Chernov : > >>> On Tue, Sep 16, 2008 at 03:38:16PM +0100, Bob Bishop wrote: > >>> > Hi, > >>> > >>>> > >>> > On 16 Sep 2008, at 15:03, Andrey Chernov wrote: > >>> > > >>> >> I need some sort of fork() hook to detect that pid is changed to re- > >>> >> stir > >>> >> ar4random() after that (in the child), simple flag variable with > >>> >> child's pid is needed. > >>> >> > >>> >> Currently OpenBSD does almost that checking getpid() every time > >>> >> arc4random() called, but it is very slow way to use getpid() syscall > >>> >> repeatedly, about 12-15 times slower than just arc4random() without > >>> >> getpid(). > >>> >> > >>> >> Any ideas? > >>> > > >>> > >>>> How about something hacky using mmap()/minherit()? > >>> > >>> Could you please provide working low cost example to detect that we are in > >>> the child (pid changed or something else)? Calling getpid() as OpenBSD > >>> does definitely is very high cost. :( > >> > >> An idea would be to implement a shared page between process and system > >> which exports such informations. > >> I'm sure we have a SoC project (2007) implementing this and perforce > >> branches for it, I'm just not sure how far it did end. > > > > Please keep in mind that the hook itself must be invisible to user > > application, we have standard API only - fork() and arc4random() family, > > no additional setup or functions are possible outside of existen API. I.e. > > the low cost hack must be completely inside ether the fork() wrapper or > > arc4random(). > > Well, you could speed up getpid() by having libc wrap all fork() > variants. The idea is that getpid() would only call __sys_getpid() > the first time it was called and then only after a fork(). It > would return the saved process id for all other cases. > > This wouldn't work if the application made its own syscall > without going through libc. > > The shared page between process and system has been tossed around > before and would probably be more benficial. Having access to > time without making a syscall would be nice. Yes, speeding up getpid() by caching its pid is nice idea. But I am completely unaware how to create syscall wrappers inside libc. :( I think about something like that: __weak_reference(_fork, fork); pid_t _fork(void); pid_t _fork(void) { pid_t ret; if ((ret = __sys_fork()) == 0) _curr_pid = -1; return (ret); } But how it will coexists with the same __weak in thread/thr_fork.c ? Are some threading locks required in this code? -- http://ache.pp.ru/