Re: Patch to add -p (reading/parsing /etc/sysctl.conf) option to sysctl

2023-02-21 Thread jhx
Thanks to all the suggestions/hints from Crystal Kolipe and Janne 
Johansson I made a few changes to the patch.


This also fixes reading commented lines.

Thanks!

--- sysctl.c.dist   Tue Feb 21 12:17:57 2023
+++ sysctl.cTue Feb 21 13:41:13 2023
@@ -138,6 +138,9 @@
 /* Maximum size object to expect from sysctl(2) */
 #define SYSCTL_BUFSIZ  8192

+/* Default sysctl.conf location */
+#define SYSCTL_CONF "/etc/sysctl.conf"
+
 struct list {
struct  ctlname *list;
int size;
@@ -161,7 +164,7 @@
{ 0, 0 },   /* CTL_VFS */
 };

-intAflag, aflag, nflag, qflag;
+intAflag, aflag, nflag, pflag, qflag;

 time_t boottime;

@@ -192,6 +195,7 @@
 ssize_t parse_hex_string(unsigned char *, size_t, const char *);
 void parse(char *, int);
 void parse_baddynamic(int *, size_t, char *, void **, size_t *, int, int);
+void read_config(void);
 void usage(void);
 int findname(char *, char *, char **, struct list *);
 int sysctl_inet(char *, char **, int *, int, int *);
@@ -232,7 +236,7 @@
 {
int ch, lvl1;

-   while ((ch = getopt(argc, argv, "Aanqw")) != -1) {
+   while ((ch = getopt(argc, argv, "Aanpqw")) != -1) {
switch (ch) {

case 'A':
@@ -247,6 +251,10 @@
nflag = 1;
break;

+   case 'p':
+   pflag = 1;
+   break;
+
case 'q':
qflag = 1;
break;
@@ -268,9 +276,16 @@
err(1,"unveil %s", _PATH_DEVDB);
if (unveil("/dev", "r") == -1 && errno != ENOENT)
err(1, "unveil /dev");
+   if(unveil(SYSCTL_CONF, "r") == -1 && errno != ENOENT)
+   err(1, "unveil /etc/sysctl.conf");
if (unveil(NULL, NULL) == -1)
err(1, "unveil");

+   if (pflag) {
+   read_config();
+   return (0);
+   }
+
if (argc == 0 || (Aflag || aflag)) {
debuginit();
vfsinit();
@@ -278,9 +293,33 @@
listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
return (0);
}
+
for (; *argv != NULL; ++argv)
parse(*argv, 1);
return (0);
+}
+
+/*
+ * Read sysctl.conf and parse every line
+ */
+void
+read_config(void)
+{
+   char line[SYSCTL_BUFSIZ];
+   FILE *file;
+
+   file = fopen(SYSCTL_CONF, "r");
+   if(!file)
+   errx(1, "Cannot open %s", SYSCTL_CONF);
+
+   while(fgets(line, SYSCTL_BUFSIZ, file) != NULL) {
+   /* Ignore comments */
+   if (strncmp(line, "#", 1) == 0)
+   continue;
+   line[strlen(line)-1] = '\0';
+   parse(line, 1);
+   memset(line, '\0', SYSCTL_BUFSIZ);
+   }
 }

 /*


--- sysctl.8.dist   Tue Feb 21 12:18:21 2023
+++ sysctl.8Tue Feb 21 12:09:59 2023
@@ -38,7 +38,7 @@
 .Nd get or set kernel state
 .Sh SYNOPSIS
 .Nm sysctl
-.Op Fl Aanq
+.Op Fl Aanpq
 .Op Ar name Ns Op = Ns Ar value
 .Sh DESCRIPTION
 The
@@ -74,6 +74,8 @@
 For example, to set the psize shell variable to the pagesize of the 
hardware:

 .Pp
 .Dl # set psize=`sysctl -n hw.pagesize`
+.It Fl p
+Read and apply all settings from /etc/sysctl.conf.
 .It Fl q
 Suppress all output when setting a variable.
 This option overrides the behaviour of
@@ -133,6 +135,10 @@
 To retrieve information about the load average history:
 .Pp
 .Dl $ sysctl vm.loadavg
+.Pp
+To apply all settings from /etc/sysctl.conf
+.Pp
+.Dl # sysctl -p
 .Pp
 To make the
 .Xr chown 2



Patch to add -p (reading/parsing /etc/sysctl.conf) option to sysctl

2023-02-21 Thread jhx

Hello everyone,

this patch adds the "-p" option to sysctl which makes it possible to 
read/parse/apply settings from /etc/sysctl.conf at any time the user 
wishes. Normally, the sysctl.conf is only parsed during system boot via rc.
I've tested the patch on OpenBSD 7.2 amd64 and so far there have been no 
problems.


Thanks in advance!

PS: Sorry if any mistakes were made, first time trying to contribute.


--- sysctl.c.dist   Tue Feb 21 12:17:57 2023
+++ sysctl.cTue Feb 21 11:59:56 2023
@@ -138,6 +138,9 @@
 /* Maximum size object to expect from sysctl(2) */
 #define SYSCTL_BUFSIZ  8192

+/* Default sysctl.conf location */
+#define SYSCTL_CONF "/etc/sysctl.conf"
+
 struct list {
struct  ctlname *list;
int size;
@@ -161,7 +164,7 @@
{ 0, 0 },   /* CTL_VFS */
 };

-intAflag, aflag, nflag, qflag;
+intAflag, aflag, nflag, pflag, qflag;

 time_t boottime;

@@ -192,6 +195,7 @@
 ssize_t parse_hex_string(unsigned char *, size_t, const char *);
 void parse(char *, int);
 void parse_baddynamic(int *, size_t, char *, void **, size_t *, int, int);
+void read_config(void);
 void usage(void);
 int findname(char *, char *, char **, struct list *);
 int sysctl_inet(char *, char **, int *, int, int *);
@@ -232,7 +236,7 @@
 {
int ch, lvl1;

-   while ((ch = getopt(argc, argv, "Aanqw")) != -1) {
+   while ((ch = getopt(argc, argv, "Aanpqw")) != -1) {
switch (ch) {

case 'A':
@@ -247,6 +251,10 @@
nflag = 1;
break;

+   case 'p':
+   pflag = 1;
+   break;
+
case 'q':
qflag = 1;
break;
@@ -268,9 +276,16 @@
err(1,"unveil %s", _PATH_DEVDB);
if (unveil("/dev", "r") == -1 && errno != ENOENT)
err(1, "unveil /dev");
+   if(unveil("/etc", "r") == -1 && errno != ENOENT)
+   err(1, "unveil /etc");
if (unveil(NULL, NULL) == -1)
err(1, "unveil");

+   if (pflag) {
+   read_config();
+   return (0);
+   }
+
if (argc == 0 || (Aflag || aflag)) {
debuginit();
vfsinit();
@@ -278,9 +293,30 @@
listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
return (0);
}
+
for (; *argv != NULL; ++argv)
parse(*argv, 1);
return (0);
+}
+
+/*
+ * Read sysctl.conf and parse every line
+ */
+void
+read_config(void)
+{
+   char line[SYSCTL_BUFSIZ];
+   FILE *file;
+
+   file = fopen(SYSCTL_CONF, "r");
+   if(!file)
+   err(1, "fopen");
+
+   while(fgets(line, SYSCTL_BUFSIZ, file) != NULL) {
+   line[strlen(line)-1] = '\0';
+   parse(line, 1);
+   memset(line, '\0', SYSCTL_BUFSIZ);
+   }
 }

 /*

--- sysctl.8.dist   Tue Feb 21 12:18:21 2023
+++ sysctl.8Tue Feb 21 12:09:59 2023
@@ -38,7 +38,7 @@
 .Nd get or set kernel state
 .Sh SYNOPSIS
 .Nm sysctl
-.Op Fl Aanq
+.Op Fl Aanpq
 .Op Ar name Ns Op = Ns Ar value
 .Sh DESCRIPTION
 The
@@ -74,6 +74,8 @@
 For example, to set the psize shell variable to the pagesize of the 
hardware:

 .Pp
 .Dl # set psize=`sysctl -n hw.pagesize`
+.It Fl p
+Read and apply all settings from /etc/sysctl.conf.
 .It Fl q
 Suppress all output when setting a variable.
 This option overrides the behaviour of
@@ -133,6 +135,10 @@
 To retrieve information about the load average history:
 .Pp
 .Dl $ sysctl vm.loadavg
+.Pp
+To apply all settings from /etc/sysctl.conf
+.Pp
+.Dl # sysctl -p
 .Pp
 To make the
 .Xr chown 2