From owner-freebsd-hackers@FreeBSD.ORG Wed Jun 13 16:16:01 2007 Return-Path: X-Original-To: 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 B59BC16A41F for ; Wed, 13 Jun 2007 16:16:01 +0000 (UTC) (envelope-from rick@kiwi-computer.com) Received: from kiwi-computer.com (keira.kiwi-computer.com [63.224.10.3]) by mx1.freebsd.org (Postfix) with SMTP id 4549013C44C for ; Wed, 13 Jun 2007 16:15:56 +0000 (UTC) (envelope-from rick@kiwi-computer.com) Received: (qmail 83837 invoked by uid 2001); 13 Jun 2007 16:15:52 -0000 Date: Wed, 13 Jun 2007 11:15:52 -0500 From: "Rick C. Petty" To: Garrett Cooper Message-ID: <20070613161552.GA83292@keira.kiwi-computer.com> References: <466F86C6.7010006@u.washington.edu> <20070613123213.GE98927@bunrab.catwhisker.org> <46700CAE.6020902@u.washington.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <46700CAE.6020902@u.washington.edu> User-Agent: Mutt/1.4.2.1i Cc: hackers@freebsd.org Subject: Re: Using shell commands versus C equivalents X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: rick-freebsd@kiwi-computer.com List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jun 2007 16:16:01 -0000 On Wed, Jun 13, 2007 at 08:26:38AM -0700, Garrett Cooper wrote: > > vsystem("/bin/chmod +x %s", filename); > > and I replaced it with: > > chmod(filename, (mode_t) ( S_IXUSR | S_IXGRP | S_IXOTH )); Another improvement made by using stat(2)/chmod(2) over chmod(1) using system(3) variants is the protection against malicious filenames. The original code should have used fork/execv instead anyway. But yeah, saving the fork, time to load and execute the shell, and parse and perform the chmod is probably worth the effort to use the syscalls directly from C, especially if this operation happens often enough. I agree with the other poster(s) who said that if it prevents complex code, it might be worth it. Your case isn't complex enough to warrant the spawning of a shell process, especially when one of your primary goals is to reduce total runtime. -- Rick C. Petty