From owner-svn-src-head@FreeBSD.ORG Wed Dec 19 23:59:27 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id C94C4921; Wed, 19 Dec 2012 23:59:27 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: from mail-ie0-f182.google.com (mail-ie0-f182.google.com [209.85.223.182]) by mx1.freebsd.org (Postfix) with ESMTP id 3CF228FC16; Wed, 19 Dec 2012 23:59:26 +0000 (UTC) Received: by mail-ie0-f182.google.com with SMTP id s9so3826857iec.27 for ; Wed, 19 Dec 2012 15:59:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=kjnWCRDOQU08kwYFK+WlRA26GQmRzndrJrC0ib18HVc=; b=L5Hb0IN8hcUMyrh2aXxikTVzIv5bOdQGK3JOUJ20irrluYI/WIa/xpxI8ecFrOrxCc 05aU/PzxgDZ9odwlPV+qSUPWRRfM+LzOvwNR/V5JfS5UeObJ0M8YenwNw7HriOF0yrqe Xo0Nx8QZ1mwBki7DTyY3fw1applGy32uWfmwb+P73TZwi/xgxSpzW+feRkmAiYmBfbRB /hNsdXaVOCStYEL307uKbkJCWmXgj9qqH1z1PnfTKN+KZj8y3X5tOXul11Q5ta+vsDQw D2b/3ER0PNZ1vbe+7a4qxqfgCqZyimy45V1620KpJt6piHDI2bA5BPn7+b3INzrN9El3 NzcA== X-Received: by 10.50.242.73 with SMTP id wo9mr8993466igc.36.1355961560767; Wed, 19 Dec 2012 15:59:20 -0800 (PST) Received: from oddish ([66.11.160.25]) by mx.google.com with ESMTPS id gs6sm5480266igc.11.2012.12.19.15.59.19 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 19 Dec 2012 15:59:20 -0800 (PST) Date: Wed, 19 Dec 2012 18:59:17 -0500 From: Mark Johnston To: Brooks Davis Subject: Re: svn commit: r244198 - in head: etc/rc.d sbin/sysctl Message-ID: <20121219235917.GD8399@oddish> References: <05CC5BAD-B968-4A7A-8097-A3344D970D63@mu.org> <1355932607.1198.206.camel@revolution.hippie.lan> <50D2128A.7030205@delphij.net> <20121219210418.GA83983@stack.nl> <50D23961.7090803@delphij.net> <20121219225854.GA8399@oddish> <20121219232140.GA40927@lor.one-eyed-alien.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20121219232140.GA40927@lor.one-eyed-alien.net> User-Agent: Mutt/1.5.21 (2010-09-15) X-Mailman-Approved-At: Thu, 20 Dec 2012 00:19:45 +0000 Cc: Ian Lepore , "src-committers@freebsd.org" , d@delphij.net, Jilles Tjoelker , Garrett Cooper , "svn-src-all@freebsd.org" , Alfred Perlstein , Xin LI , "svn-src-head@freebsd.org" , Andrey Zonov X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 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: Wed, 19 Dec 2012 23:59:27 -0000 On Wed, Dec 19, 2012 at 05:21:40PM -0600, Brooks Davis wrote: > On Wed, Dec 19, 2012 at 05:58:54PM -0500, Mark Johnston wrote: > > On Wed, Dec 19, 2012 at 02:02:09PM -0800, Xin Li wrote: > > > -----BEGIN PGP SIGNED MESSAGE----- > > > Hash: SHA256 > > > > > > On 12/19/12 13:12, Garrett Cooper wrote: > > > > On Wed, Dec 19, 2012 at 1:10 PM, Garrett Cooper > > > > wrote: > > > > > > > > ... > > > > > > > >> find -exec / echo | xargs ? Seems like there's a better way to > > > >> solve this. > > > > > > > > Of course we also might be overengineering the problem (my > > > > suggestion definitely was overengineered). Why not pass in the > > > > appropriate arguments via sysctl_args in /etc/rc.conf ? Thanks, > > > > > > Irrelevant. Consider this (extreme) situation: someone distributes > > > several sets of sysctl values tuned for certain situations, like > > > tcp.conf, supermicro.conf, ... and wants to put them together in a > > > directory, it's useful to source from the directory without having to > > > do a generation of command line on boot, so when something goes wrong, > > > they just remove the pack rather than changing /etc/rc.conf. > > > > At work I've changed the -f flag of syslogd and newsyslog to accept a > > directory which gets non-recursively searched for input files. This way > > we can have a directory, say /etc/syslog.d, into which package install > > scripts can easily drop config files. > > > > For something like sysctl this might be a bit much, but it's just a > > thought. The example diff below is what I have in mind. > > I don't have a strong opinion about the usefulness of this feature. It > seems useful particularly in conjunction with supporting multiple -f's. I don't really either. Just thought I'd suggest it. > > I do have a few comments on the implementation below. > Thanks! I didn't know about openat(). Here's the regenerated diff. diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index 8fad089..9e453f6 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -42,6 +42,7 @@ static const char rcsid[] = #endif /* not lint */ #include +#include #include #include #include @@ -49,8 +50,11 @@ static const char rcsid[] = #include #include +#include #include #include +#include +#include #include #include #include @@ -64,8 +68,9 @@ static const char *conffile; static int aflag, bflag, dflag, eflag, hflag, iflag; static int Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag; +static int handlefile(const char *); static int oidfmt(int *, int, char *, u_int *); -static int parsefile(const char *); +static int parsefile(int); static int parse(const char *, int); static int show_var(int *, int); static int sysctl_all(int *oid, int len); @@ -165,7 +170,7 @@ main(int argc, char **argv) warncount = 0; if (conffile != NULL) - warncount += parsefile(conffile); + warncount += handlefile(conffile); while (argc-- > 0) warncount += parse(*argv++, 0); @@ -402,15 +407,55 @@ parse(const char *string, int lineno) } static int -parsefile(const char *filename) +handlefile(const char *filename) +{ + DIR *dir; + struct dirent *de; + struct stat sb; + int fd, warncount = 0; + + fd = open(filename, O_RDONLY); + if (fd < 0) + err(EX_NOINPUT, "%s", filename); + if (fstat(fd, &sb)) + err(EX_NOINPUT, "%s", filename); + + if (S_ISREG(sb.st_mode)) { + return (parsefile(fd)); + } else if (!S_ISDIR(sb.st_mode)) + errx(EX_USAGE, "invalid input file '%s'", filename); + + dir = fdopendir(fd); + if (dir == NULL) + err(EX_NOINPUT, "%s", filename); + while ((de = readdir(dir)) != NULL) { + if (fnmatch("*.conf", de->d_name, 0) != 0) + continue; + fd = openat(fd, de->d_name, O_RDONLY); + if (fd < 0 || fstat(fd, &sb) != 0) { + warn("%s", de->d_name); + continue; + } else if (!S_ISREG(sb.st_mode)) + continue; + + warncount += parsefile(fd); + close(fd); + } + closedir(dir); + + return (warncount); +} + +static int +parsefile(int fd) { FILE *file; char line[BUFSIZ], *p, *pq, *pdq; int warncount = 0, lineno = 0; - file = fopen(filename, "r"); + file = fdopen(fd, "r"); if (file == NULL) - err(EX_NOINPUT, "%s", filename); + err(EX_OSERR, "fdopen()"); while (fgets(line, sizeof(line), file) != NULL) { lineno++; p = line; @@ -446,7 +491,6 @@ parsefile(const char *filename) else warncount += parse(p, lineno); } - fclose(file); return (warncount); }