From owner-freebsd-hackers@FreeBSD.ORG Fri Mar 20 06:36:30 2015 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 46C81988 for ; Fri, 20 Mar 2015 06:36:30 +0000 (UTC) Received: from ustc.edu.cn (email6.ustc.edu.cn [IPv6:2001:da8:d800::8]) by mx1.freebsd.org (Postfix) with ESMTP id 942C29E2 for ; Fri, 20 Mar 2015 06:36:29 +0000 (UTC) Received: from freebsd (unknown [58.211.218.74]) by newmailweb.ustc.edu.cn (Coremail) with SMTP id LkAmygB3JzzkvwtVxwu4Aw--.49200S2; Fri, 20 Mar 2015 14:36:25 +0800 (CST) Date: Fri, 20 Mar 2015 14:36:04 +0800 From: Tiwei Bie To: Prasad Joshi Subject: Re: [PATCH] Finish the task 'Fix corefilename race' Message-ID: <20150320063604.GA69307@freebsd> References: <1426749223-18118-1-git-send-email-btw@mail.ustc.edu.cn> <20150319101019.GZ2379@kib.kiev.ua> <20150319113530.GA33176@freebsd> <20150319144004.GD2379@kib.kiev.ua> <20150320000418.GA78913@freebsd> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-CM-TRANSID: LkAmygB3JzzkvwtVxwu4Aw--.49200S2 X-Coremail-Antispam: 1UD129KBjvJXoWxurWUtFWfAFWktw1fAr13Jwb_yoWrWrWDpF WrCFyrArsakF45Cr1av3WrAF1Yy34ktr4UW34UXr1YyrnYqrykXF10qry0gFykWrn2gryD ta1UXry3Kryjv3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk0b7Iv0xC_KF4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I 8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI 64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVW8JVWxJw Am72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lc2xSY4AK67AK6r47MxAIw28IcxkI 7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxV Cjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY 6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6x AIw20EY4v20xvaj40_WFyUJVCq3wCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv 6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUx_-BDUUUU X-CM-SenderInfo: xewzqzxdloh3xvwfhvlgxou0/1tbiAQUOAVQhl-nmmAABsv Cc: Konstantin Belousov , freebsd-hackers@freebsd.org, mjguzik@gmail.com 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: Fri, 20 Mar 2015 06:36:30 -0000 On Fri, Mar 20, 2015 at 11:18:27AM +0530, Prasad Joshi wrote: > On Fri, Mar 20, 2015 at 5:34 AM, Tiwei Bie wrote: > > On Thu, Mar 19, 2015 at 04:40:04PM +0200, Konstantin Belousov wrote: > >> On Thu, Mar 19, 2015 at 07:35:30PM +0800, Tiwei Bie wrote: > >> > On Thu, Mar 19, 2015 at 12:10:19PM +0200, Konstantin Belousov wrote: > >> > > On Thu, Mar 19, 2015 at 03:13:43PM +0800, Tiwei Bie wrote: > >> > > > Hi, Mateusz! > >> > > > > >> > > > I have finished the task: Fix corefilename race [1]. > >> > > > > >> > > > Following is my patch: > >> > > > > >> > > > --- > >> > > > sys/kern/kern_sig.c | 22 ++++++++++++++++++++-- > >> > > > 1 file changed, 20 insertions(+), 2 deletions(-) > >> > > > > >> > > > diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c > >> > > > index 58d9707..a1421cb 100644 > >> > > > --- a/sys/kern/kern_sig.c > >> > > > +++ b/sys/kern/kern_sig.c > >> > > > @@ -3090,8 +3090,24 @@ static int compress_user_cores = 0; > >> > > > #endif > >> > > > > >> > > > static char corefilename[MAXPATHLEN] = {"%N.core"}; > >> > > > -SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RWTUN, corefilename, > >> > > > - sizeof(corefilename), "Process corefile name format string"); > >> > > > + > >> > > > +static struct sx corefilename_lock; > >> > > > +SX_SYSINIT(corefilename_init, &corefilename_lock, "corefilename lock"); > >> > > > + > >> > > > +static int > >> > > > +sysctl_kern_corefile(SYSCTL_HANDLER_ARGS) > >> > > > +{ > >> > > > + int error; > >> > > > + > >> > > > + sx_xlock(&corefilename_lock); > >> > > > + error = sysctl_handle_string(oidp, corefilename, MAXPATHLEN, req); > > Hello, > > Though I am not an expert FreeBSD developer. In my humble opinion, > MAXPATHLEN must be replaced with sizeof(corefilename). For example, > the SYSCTL_STRING line (above) removed in this patch, preferred to use > sizeof(corefilename) instead of MAXPATHLEN. > Because corefilename is defined as `char corefilename[MAXPATHLEN]', sizeof(corefilename) equals MAXPATHLEN. So, technically speaking, both of them are right. But one of them may be preferred to keep the coding style consistent. I also think sizeof(corefilename) is a better choice. Thanks for pointing it out, I updated my patch: --- sys/kern/kern_sig.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c index 58d9707..8410d9d 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -3089,9 +3089,28 @@ SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RWTUN, static int compress_user_cores = 0; #endif +/* + * Protect the access to corefilename[] by allproc_lock. + */ +#define corefilename_lock allproc_lock + static char corefilename[MAXPATHLEN] = {"%N.core"}; -SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RWTUN, corefilename, - sizeof(corefilename), "Process corefile name format string"); + +static int +sysctl_kern_corefile(SYSCTL_HANDLER_ARGS) +{ + int error; + + sx_xlock(&corefilename_lock); + error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename), + req); + sx_xunlock(&corefilename_lock); + + return (error); +} +SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RWTUN | + CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A", + "Process corefile name format string"); /* * corefile_open(comm, uid, pid, td, compress, vpp, namep) @@ -3120,6 +3139,7 @@ corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); indexpos = -1; (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); + sx_slock(&corefilename_lock); for (i = 0; format[i] != '\0'; i++) { switch (format[i]) { case '%': /* Format character */ @@ -3162,6 +3182,7 @@ corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, break; } } + sx_sunlock(&corefilename_lock); free(hostname, M_TEMP); if (compress) sbuf_printf(&sb, GZ_SUFFIX); -- 2.1.2 Best regards, Tiwei Bie