From owner-svn-src-head@FreeBSD.ORG Tue Oct 21 23:08:48 2014 Return-Path: Delivered-To: svn-src-head@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 1A7A7C7; Tue, 21 Oct 2014 23:08:48 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 04183C66; Tue, 21 Oct 2014 23:08:48 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id s9LN8lku052002; Tue, 21 Oct 2014 23:08:47 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id s9LN8lm6051999; Tue, 21 Oct 2014 23:08:47 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <201410212308.s9LN8lm6051999@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Tue, 21 Oct 2014 23:08:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r273436 - in head/sys: i386/ibcs2 kern sys X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Oct 2014 23:08:48 -0000 Author: mjg Date: Tue Oct 21 23:08:46 2014 New Revision: 273436 URL: https://svnweb.freebsd.org/changeset/base/273436 Log: Eliminate unnecessary memory allocation in sys_getgroups and its ibcs2 counterpart. Modified: head/sys/i386/ibcs2/ibcs2_misc.c head/sys/kern/kern_prot.c head/sys/sys/syscallsubr.h Modified: head/sys/i386/ibcs2/ibcs2_misc.c ============================================================================== --- head/sys/i386/ibcs2/ibcs2_misc.c Tue Oct 21 23:07:30 2014 (r273435) +++ head/sys/i386/ibcs2/ibcs2_misc.c Tue Oct 21 23:08:46 2014 (r273436) @@ -659,33 +659,28 @@ ibcs2_getgroups(td, uap) struct thread *td; struct ibcs2_getgroups_args *uap; { + struct ucred *cred; ibcs2_gid_t *iset; - gid_t *gp; u_int i, ngrp; int error; - if (uap->gidsetsize < td->td_ucred->cr_ngroups) { - if (uap->gidsetsize == 0) - ngrp = 0; - else - return (EINVAL); - } else - ngrp = td->td_ucred->cr_ngroups; - gp = malloc(ngrp * sizeof(*gp), M_TEMP, M_WAITOK); - error = kern_getgroups(td, &ngrp, gp); - if (error) + cred = td->td_ucred; + ngrp = cred->cr_ngroups; + + if (uap->gidsetsize == 0) { + error = 0; goto out; - if (uap->gidsetsize > 0) { - iset = malloc(ngrp * sizeof(*iset), M_TEMP, M_WAITOK); - for (i = 0; i < ngrp; i++) - iset[i] = (ibcs2_gid_t)gp[i]; - error = copyout(iset, uap->gidset, ngrp * sizeof(ibcs2_gid_t)); - free(iset, M_TEMP); } - if (error == 0) - td->td_retval[0] = ngrp; + if (uap->gidsetsize < ngrp) + return (EINVAL); + + iset = malloc(ngrp * sizeof(*iset), M_TEMP, M_WAITOK); + for (i = 0; i < ngrp; i++) + iset[i] = (ibcs2_gid_t)cred->cr_groups[i]; + error = copyout(iset, uap->gidset, ngrp * sizeof(ibcs2_gid_t)); + free(iset, M_TEMP); out: - free(gp, M_TEMP); + td->td_retval[0] = ngrp; return (error); } Modified: head/sys/kern/kern_prot.c ============================================================================== --- head/sys/kern/kern_prot.c Tue Oct 21 23:07:30 2014 (r273435) +++ head/sys/kern/kern_prot.c Tue Oct 21 23:08:46 2014 (r273436) @@ -303,45 +303,24 @@ struct getgroups_args { int sys_getgroups(struct thread *td, register struct getgroups_args *uap) { - gid_t *groups; + struct ucred *cred; u_int ngrp; int error; - if (uap->gidsetsize < td->td_ucred->cr_ngroups) { - if (uap->gidsetsize == 0) - ngrp = 0; - else - return (EINVAL); - } else - ngrp = td->td_ucred->cr_ngroups; - groups = malloc(ngrp * sizeof(*groups), M_TEMP, M_WAITOK); - error = kern_getgroups(td, &ngrp, groups); - if (error) - goto out; - if (uap->gidsetsize > 0) - error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t)); - if (error == 0) - td->td_retval[0] = ngrp; -out: - free(groups, M_TEMP); - return (error); -} - -int -kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups) -{ - struct ucred *cred; - cred = td->td_ucred; - if (*ngrp == 0) { - *ngrp = cred->cr_ngroups; - return (0); + ngrp = cred->cr_ngroups; + + if (uap->gidsetsize == 0) { + error = 0; + goto out; } - if (*ngrp < cred->cr_ngroups) + if (uap->gidsetsize < ngrp) return (EINVAL); - *ngrp = cred->cr_ngroups; - bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t)); - return (0); + + error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t)); +out: + td->td_retval[0] = ngrp; + return (error); } #ifndef _SYS_SYSPROTO_H_ Modified: head/sys/sys/syscallsubr.h ============================================================================== --- head/sys/sys/syscallsubr.h Tue Oct 21 23:07:30 2014 (r273435) +++ head/sys/sys/syscallsubr.h Tue Oct 21 23:08:46 2014 (r273436) @@ -109,7 +109,6 @@ int kern_getdirentries(struct thread *td long *basep, ssize_t *residp, enum uio_seg bufseg); int kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize, enum uio_seg bufseg, int flags); -int kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups); int kern_getitimer(struct thread *, u_int, struct itimerval *); int kern_getppid(struct thread *); int kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,