From owner-freebsd-hackers@FreeBSD.ORG Fri Jul 6 12:23:04 2007 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 3A42D16A468 for ; Fri, 6 Jul 2007 12:23:04 +0000 (UTC) (envelope-from ighighi@gmail.com) Received: from ik-out-1112.google.com (ik-out-1112.google.com [66.249.90.176]) by mx1.freebsd.org (Postfix) with ESMTP id C66B513C45D for ; Fri, 6 Jul 2007 12:23:03 +0000 (UTC) (envelope-from ighighi@gmail.com) Received: by ik-out-1112.google.com with SMTP id c21so189849ika for ; Fri, 06 Jul 2007 05:23:02 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=Lu94sOoem+hY2d9v1UjWOuelwz7e92Yy+hiwmFZjeUMeXrCRzxATn/zLHYSsmuBhexfDXTIfEi9nbVZe6wqTPQ34ME34mhSVBKuZQGPOOcTeOTXOdXawtkMuinVOdPp1J2Ca341PTkkW0sY0AwaZCX5zeVahOn/T1GzLXhDMEw8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=tHbq+/ZwQD3myQ+3VTFKtIHmVaKo1VreWMK/9EVzCHuJRE7pYvnCD7D6DdkInR0bGXQnlZGH00NAUVxgZL3WDaTrdssIaBTYxSUddJXfjjNDPXVEysp8nB3cW8MARrNrXf5Q5QOlxCqd8WaKLyuMt8WGIU5ymYFRoPY0LN/3QfU= Received: by 10.78.160.2 with SMTP id i2mr263214hue.1183724582368; Fri, 06 Jul 2007 05:23:02 -0700 (PDT) Received: by 10.78.51.18 with HTTP; Fri, 6 Jul 2007 05:23:02 -0700 (PDT) Message-ID: Date: Fri, 6 Jul 2007 08:23:02 -0400 From: "Ighighi Ighighi" To: d@delphij.net In-Reply-To: <468CD5E9.7060000@delphij.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <468CD5E9.7060000@delphij.net> X-Mailman-Approved-At: Fri, 06 Jul 2007 12:35:23 +0000 Cc: freebsd-hackers@freebsd.org Subject: Re: add closefrom() call X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Jul 2007 12:23:04 -0000 > LI Xin wrote: > Here is my implementation for FreeBSD. Some difference between my and > DragonFly's implementation: > > - closefrom(-1) would be no-op on DragonFly, my version would close all > open files (From my understanding of OpenSolaris's userland > implementation, this is Solaris's behavior). > - my version closefrom(very_big_fd) would result in EBADF. I am not > very sure whether this is correct, but it does not hurt for applications > that thinks closefrom() would return void. Why not follow current practice and return EBADF for -1 ? It'd be dangerous to close all open files with -1 (a closefrom(0) would suffice), and weird to ignore very_big_fd. I also agree that using fcntl() would be better. Here's the code I'm using to emulate this call on FreeBSD >= 5.0 anyway. int closefrom(int lowfd) { int mib[2] = { CTL_KERN, KERN_FILE }; struct xfile *files = NULL; pid_t pid = getpid(); int i, nfiles; size_t fsize; for (;;) { if (sysctl(mib, 2, files, &fsize, NULL, 0) == -1) { if (errno != ENOMEM) goto bad; else if (files != NULL) { free(files); files = NULL; } } else if (files == NULL) { files = (struct xfile *) malloc(fsize); if (files == NULL) return -1; } else break; } /* XXX This structure may change */ if (files->xf_size != sizeof(struct xfile) || fsize % sizeof(struct xfile)) { errno = ENOSYS; goto bad; } nfiles = fsize / sizeof(struct xfile); for (i = 0; i < nfiles; i++) if (files[i].xf_pid == pid && files[i].xf_fd >= lowfd) close(files[i].xf_fd); free(files); return 0; bad: if (files != NULL) { int save_errno = errno; free(files); errno = save_errno; } return -1; }