From owner-freebsd-hackers@FreeBSD.ORG Thu Nov 6 11:40:01 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 278B6BF4 for ; Thu, 6 Nov 2014 11:40:01 +0000 (UTC) Received: from ustc.edu.cn (email6.ustc.edu.cn [IPv6:2001:da8:d800::8]) by mx1.freebsd.org (Postfix) with ESMTP id 328D3F75 for ; Thu, 6 Nov 2014 11:39:59 +0000 (UTC) Received: from freebsd (unknown [58.211.218.74]) by newmailweb.ustc.edu.cn (Coremail) with SMTP id LkAmygCnVzwAXltUiGfdAA--.19608S2; Thu, 06 Nov 2014 19:39:52 +0800 (CST) Date: Thu, 6 Nov 2014 19:39:16 +0800 From: Tiwei Bie To: Mateusz Guzik Subject: Re: [PATCH] Finish the task 'sysctl reporting current working directory' Message-ID: <20141106113916.GA13209@freebsd> References: <1414987325-23280-1-git-send-email-btw@mail.ustc.edu.cn> <20141103051908.GC29497@dft-labs.eu> <20141103064052.GA1739@freebsd> <20141103080526.GE29497@dft-labs.eu> <20141103085235.GA85851@freebsd> <20141106081330.GA12284@dft-labs.eu> <20141106085750.GA52132@freebsd> <20141106092134.GL53947@kib.kiev.ua> <20141106100032.GA80996@freebsd> <20141106111358.GF12284@dft-labs.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20141106111358.GF12284@dft-labs.eu> User-Agent: Mutt/1.5.23 (2014-03-12) X-CM-TRANSID: LkAmygCnVzwAXltUiGfdAA--.19608S2 X-Coremail-Antispam: 1UD129KBjvJXoWxCFW7Cr4kZw43Cry8JFy7KFg_yoW5tF1Upr Z7AF4UXF4kJF1UGF4qywsYgFnYywnxtr95WrWru3ZxAFn0qr1furs0kr4j9Fyfu3yrZ34k Cw4UGw1Syw47t3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkYb7Iv0xC_KF4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwV C2z280aVCY1x0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC 0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Cr0_Gr 1UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvEwIxGrwCY02Avz4vE14v_Gw4l42xK82IY c2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s 026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1Y6r17MIIYrxkI7VAKI48JMIIF 0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0x vE42xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2 jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU8I38UUUUUU== X-CM-SenderInfo: xewzqzxdloh3xvwfhvlgxou0/1tbiAQUTAVQhl82hrQAYst Cc: Konstantin Belousov , freebsd-hackers@freebsd.org X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Nov 2014 11:40:01 -0000 On Thu, Nov 06, 2014 at 12:13:58PM +0100, Mateusz Guzik wrote: > On Thu, Nov 06, 2014 at 06:00:32PM +0800, Tiwei Bie wrote: > > On Thu, Nov 06, 2014 at 11:21:34AM +0200, Konstantin Belousov wrote: > > > On Thu, Nov 06, 2014 at 04:57:50PM +0800, Tiwei Bie wrote: > > > > On Thu, Nov 06, 2014 at 09:13:31AM +0100, Mateusz Guzik wrote: > > > > > On Mon, Nov 03, 2014 at 04:52:35PM +0800, Tiwei Bie wrote: > > > > > > > > > > kernel changes got in in r274167 > > > > > > > > > > Please submit tmux patch to bugzilla. > > > > > > > > > > > > > Thanks! I have submitted the tmux patch to bugzilla [1]. > > > > > > > > [1] https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=194858 > > > > > > This is incorrect way to use kern_proc_cwd. You should try the new > > > sysctl, and on ENOENT fall back to the kern_proc_file. Expecting > > > that the binary is run on the same machine where it was build is > > > wrong. > > > > > > > Yeah, you are right. Following is my new patch, maybe it needs some > > style improvements. > > > > diff --git a/osdep-freebsd.c b/osdep-freebsd.c > > index d596eab..673ca98 100644 > > --- a/osdep-freebsd.c > > +++ b/osdep-freebsd.c > > @@ -133,7 +133,7 @@ error: > > } > > > > char * > > -osdep_get_cwd(int fd) > > +osdep_get_cwd_fallback(int fd) > > { > > static char wd[PATH_MAX]; > > struct kinfo_file *info = NULL; > > @@ -158,6 +158,31 @@ osdep_get_cwd(int fd) > > return (NULL); > > } > > > > +#ifdef KERN_PROC_CWD > > +char * > > +osdep_get_cwd(int fd) > > +{ > > + static struct kinfo_file info; > > + int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, 0 }; > > + size_t len = sizeof info; > > + > > + if ((name[3] = tcgetpgrp(fd)) == -1) > > + return (NULL); > > + if (sysctl(name, 4, &info, &len, NULL, 0) == -1) { > > + if (errno == ENOENT) > > + return (osdep_get_cwd_fallback(fd)); > > + return (NULL); > > + } > > + return (info.kf_path); > > > tmux calls this quite often and trying out this sysctl just to see it > fail each time is a waste. have a static var to indicate the failure and > to know when to fallback immediately. > > Also this probably should be reworked to obtain tcgetpgrp once and then > decide which sysctl to use, but that's largely a perference matter. > A fallback indicator has been added. New patch: diff --git a/osdep-freebsd.c b/osdep-freebsd.c index d596eab..74b5c91 100644 --- a/osdep-freebsd.c +++ b/osdep-freebsd.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -132,8 +133,8 @@ error: return (NULL); } -char * -osdep_get_cwd(int fd) +static char * +osdep_get_cwd_fallback(int fd) { static char wd[PATH_MAX]; struct kinfo_file *info = NULL; @@ -158,6 +159,38 @@ osdep_get_cwd(int fd) return (NULL); } +#ifdef KERN_PROC_CWD +char * +osdep_get_cwd(int fd) +{ + static struct kinfo_file info; + static bool fallback = false; + int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, 0 }; + size_t len = sizeof info; + + if (fallback) + return (osdep_get_cwd_fallback(fd)); + + if ((name[3] = tcgetpgrp(fd)) == -1) + return (NULL); + + if (sysctl(name, 4, &info, &len, NULL, 0) == -1) { + if (errno == ENOENT) { + fallback = true; + return (osdep_get_cwd_fallback(fd)); + } + return (NULL); + } + return (info.kf_path); +} +#else /* !KERN_PROC_CWD */ +char * +osdep_get_cwd(int fd) +{ + return (osdep_get_cwd_fallback(fd)); +} +#endif /* KERN_PROC_CWD */ + struct event_base * osdep_event_init(void) { -- 2.1.0 Tiwei Bie