Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Dec 2012 18:26:43 -0600
From:      Brooks Davis <brooks@FreeBSD.org>
To:        Mark Johnston <markjdb@gmail.com>
Cc:        Ian Lepore <freebsd@damnhippie.dyndns.org>, Brooks Davis <brooks@FreeBSD.org>, d@delphij.net, Jilles Tjoelker <jilles@stack.nl>, Garrett Cooper <yanegomi@gmail.com>, "svn-src-all@freebsd.org" <svn-src-all@FreeBSD.org>, Alfred Perlstein <bright@mu.org>, "src-committers@freebsd.org" <src-committers@FreeBSD.org>, Xin LI <delphij@FreeBSD.org>, "svn-src-head@freebsd.org" <svn-src-head@FreeBSD.org>, Andrey Zonov <zont@FreeBSD.org>
Subject:   Re: svn commit: r244198 - in head: etc/rc.d sbin/sysctl
Message-ID:  <20121220002643.GA41556@lor.one-eyed-alien.net>
In-Reply-To: <20121219235917.GD8399@oddish>
References:  <1355932607.1198.206.camel@revolution.hippie.lan> <F158CD10-B65B-4BBA-BCAD-6A52BC5C8FDF@mu.org> <50D2128A.7030205@delphij.net> <20121219210418.GA83983@stack.nl> <CAGH67wRTdBCZLq1R8-yqD6EBZ%2B=F228-0o_8TvU3b0KZD6vUfg@mail.gmail.com> <CAGH67wT1L_9ia7-hdHST6q8iCdtmFifZcHixCQhBTiZEH8YAcw@mail.gmail.com> <50D23961.7090803@delphij.net> <20121219225854.GA8399@oddish> <20121219232140.GA40927@lor.one-eyed-alien.net> <20121219235917.GD8399@oddish>

next in thread | previous in thread | raw e-mail | index | archive | help

--d6Gm4EdcadzBjdND
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Dec 19, 2012 at 06:59:17PM -0500, Mark Johnston wrote:
> 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
> > > >=20
> > > > On 12/19/12 13:12, Garrett Cooper wrote:
> > > > > On Wed, Dec 19, 2012 at 1:10 PM, Garrett Cooper
> > > > > <yanegomi@gmail.com> wrote:
> > > > >=20
> > > > > ...
> > > > >=20
> > > > >> find -exec / echo | xargs ? Seems like there's a better way to
> > > > >> solve this.
> > > > >=20
> > > > > Of course we also might be overengineering the problem (my=20
> > > > > suggestion definitely was overengineered). Why not pass in the=20
> > > > > appropriate arguments via sysctl_args in /etc/rc.conf ? Thanks,
> > > >=20
> > > > 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 wro=
ng,
> > > > they just remove the pack rather than changing /etc/rc.conf.
> > >=20
> > > 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 w=
ay
> > > we can have a directory, say /etc/syslog.d, into which package install
> > > scripts can easily drop config files.
> > >=20
> > > 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.
> >=20
> > I don't have a strong opinion about the usefulness of this feature.  It
> > seems useful particularly in conjunction with supporting multiple -f's.
>=20
> I don't really either. Just thought I'd suggest it.
>=20
> >=20
> > I do have a few comments on the implementation below.
> >=20
>=20
> Thanks! I didn't know about openat(). Here's the regenerated diff.
>=20
> 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[] =3D
>  #endif /* not lint */
> =20
>  #include <sys/param.h>
> +#include <sys/types.h>
>  #include <sys/time.h>
>  #include <sys/resource.h>
>  #include <sys/stat.h>
> @@ -49,8 +50,11 @@ static const char rcsid[] =3D
>  #include <sys/vmmeter.h>
> =20
>  #include <ctype.h>
> +#include <dirent.h>
>  #include <err.h>
>  #include <errno.h>
> +#include <fcntl.h>
> +#include <fnmatch.h>
>  #include <inttypes.h>
>  #include <locale.h>
>  #include <stdio.h>
> @@ -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;
> =20
> +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)
> =20
>  	warncount =3D 0;
>  	if (conffile !=3D NULL)
> -		warncount +=3D parsefile(conffile);
> +		warncount +=3D handlefile(conffile);
> =20
>  	while (argc-- > 0)
>  		warncount +=3D parse(*argv++, 0);
> @@ -402,15 +407,55 @@ parse(const char *string, int lineno)
>  }
> =20
>  static int
> -parsefile(const char *filename)
> +handlefile(const char *filename)
> +{
> +	DIR *dir;
> +	struct dirent *de;
> +	struct stat sb;
> +	int fd, warncount =3D 0;
> +
> +	fd =3D 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));

This leaks the fd.  I might just keep the close() in parsefile() so that
in consumes the fd.

-- Brooks

> +	} else if (!S_ISDIR(sb.st_mode))
> +		errx(EX_USAGE, "invalid input file '%s'", filename);
> +
> +	dir =3D fdopendir(fd);
> +	if (dir =3D=3D NULL)
> +		err(EX_NOINPUT, "%s", filename);
> +	while ((de =3D readdir(dir)) !=3D NULL) {
> +		if (fnmatch("*.conf", de->d_name, 0) !=3D 0)
> +			continue;
> +		fd =3D openat(fd, de->d_name, O_RDONLY);
> +		if (fd < 0 || fstat(fd, &sb) !=3D 0) {
> +			warn("%s", de->d_name);
> +			continue;
> +		} else if (!S_ISREG(sb.st_mode))
> +			continue;
> +
> +		warncount +=3D parsefile(fd);
> +		close(fd);
> +	}
> +	closedir(dir);
> +
> +	return (warncount);
> +}
> +
> +static int
> +parsefile(int fd)
>  {
>  	FILE *file;
>  	char line[BUFSIZ], *p, *pq, *pdq;
>  	int warncount =3D 0, lineno =3D 0;
> =20
> -	file =3D fopen(filename, "r");
> +	file =3D fdopen(fd, "r");
>  	if (file =3D=3D NULL)
> -		err(EX_NOINPUT, "%s", filename);
> +		err(EX_OSERR, "fdopen()");
>  	while (fgets(line, sizeof(line), file) !=3D NULL) {
>  		lineno++;
>  		p =3D line;
> @@ -446,7 +491,6 @@ parsefile(const char *filename)
>  		else
>  			warncount +=3D parse(p, lineno);
>  	}
> -	fclose(file);
> =20
>  	return (warncount);
>  }
>=20

--d6Gm4EdcadzBjdND
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (FreeBSD)

iD8DBQFQ0ltDXY6L6fI4GtQRAsdYAKCV5A0A0zPBHxtymlxL+JgzcxEolQCguuBi
rQJqGziPFhuUsHBJetVcqcQ=
=cJdi
-----END PGP SIGNATURE-----

--d6Gm4EdcadzBjdND--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20121220002643.GA41556>