From owner-freebsd-hackers@FreeBSD.ORG Mon Nov 3 08:53:13 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 03E0284F for ; Mon, 3 Nov 2014 08:53:13 +0000 (UTC) Received: from ustc.edu.cn (email6.ustc.edu.cn [IPv6:2001:da8:d800::8]) by mx1.freebsd.org (Postfix) with ESMTP id 11F98E94 for ; Mon, 3 Nov 2014 08:53:11 +0000 (UTC) Received: from freebsd (unknown [58.211.218.74]) by newmailweb.ustc.edu.cn (Coremail) with SMTP id LkAmygCnVzxsQldU4j7HAA--.18549S2; Mon, 03 Nov 2014 16:53:05 +0800 (CST) Date: Mon, 3 Nov 2014 16:52:35 +0800 From: Tiwei Bie To: Mateusz Guzik Subject: Re: [PATCH] Finish the task 'sysctl reporting current working directory' Message-ID: <20141103085235.GA85851@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> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20141103080526.GE29497@dft-labs.eu> User-Agent: Mutt/1.5.23 (2014-03-12) X-CM-TRANSID: LkAmygCnVzxsQldU4j7HAA--.18549S2 X-Coremail-Antispam: 1UD129KBjvJXoW7Zw43tFy3uFWxCr1UZw45Awb_yoW8ZF17pF ZxArZ8XF4UJF4UGF4qvrs8G3Z0ywnaqr1vgrW0v3ZxAFn8Zr4fZr42kr1j9Fyru3y5Z395 Cw48Ka1Iyw1UKaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkYb7Iv0xC_Zr1lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwV C2z280aVCY1x0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC 0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Cr0_Gr 1UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvEwIxGrwCY02Avz4vE14v_GFyl42xK82IY c2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s 026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1Y6r17MIIYrxkI7VAKI48JMIIF 0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0x vE42xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2 jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU8gdbUUUUUU== X-CM-SenderInfo: xewzqzxdloh3xvwfhvlgxou0/1tbiAQURAVQhl8yZCAAMsn Cc: 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: Mon, 03 Nov 2014 08:53:13 -0000 On Mon, Nov 03, 2014 at 09:05:26AM +0100, Mateusz Guzik wrote: > > > > #2. Patch for tmux: > > > > diff --git a/osdep-freebsd.c b/osdep-freebsd.c > > index d596eab..4178f01 100644 > > --- a/osdep-freebsd.c > > +++ b/osdep-freebsd.c > > @@ -132,6 +132,46 @@ error: > > return (NULL); > > } > > > > +#ifdef KERN_PROC_CWD > > +char * > > +osdep_get_cwd(int fd) > > +{ > > + static char wd[PATH_MAX]; > > + pid_t pgrp; > > + int mib[4]; > > + size_t len; > > + struct kinfo_file *info; > > + char *buf; > > + int error; > > + > > + if ((pgrp = tcgetpgrp(fd)) == -1) > > + return (NULL); > > + > > + mib[0] = CTL_KERN; > > + mib[1] = KERN_PROC; > > + mib[2] = KERN_PROC_CWD; > > Take a look at osdep-openbsd. This should be done along with > declaration. > > > + mib[3] = pgrp; > > + > > + error = sysctl(mib, 4, NULL, &len, NULL, 0); > > + if (error) > > + return (NULL); > > + > > + buf = malloc(len); > > + if (buf == NULL) > > + return (NULL); > > + error = sysctl(mib, 4, buf, &len, NULL, 0); > > + if (error) { > > + free(buf); > > + return (NULL); > > + } > > + > > + info = (struct kinfo_file *)buf; > > + strlcpy(wd, info->kf_path, sizeof wd); > > + > > + free(buf); > > Why? Just have static kinfo_file. There is no need to allocate or copy > anything, nor to query for size. > Sorry, my patch for tmux is messy... :-( My new patch: diff --git a/osdep-freebsd.c b/osdep-freebsd.c index d596eab..46f6f3f 100644 --- a/osdep-freebsd.c +++ b/osdep-freebsd.c @@ -132,6 +132,21 @@ error: 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) != 0) + return (NULL); + return (info.kf_path); +} +#else /* !KERN_PROC_CWD */ char * osdep_get_cwd(int fd) { @@ -157,6 +172,7 @@ osdep_get_cwd(int fd) free(info); return (NULL); } +#endif /* KERN_PROC_CWD */ struct event_base * osdep_event_init(void) -- 2.1.0 Tiwei Bie