Author: trasz
Date: Wed Apr 29 16:41:49 2015
New Revision: 282236
URL: https://svnweb.freebsd.org/changeset/base/282236

Log:
  Make rctl(8) more user-friendly when RACCT/RCTL is disabled for some reason.
  
  MFC after:    1 month
  Sponsored by: The FreeBSD Foundation

Modified:
  head/usr.bin/rctl/rctl.c

Modified: head/usr.bin/rctl/rctl.c
==============================================================================
--- head/usr.bin/rctl/rctl.c    Wed Apr 29 16:41:48 2015        (r282235)
+++ head/usr.bin/rctl/rctl.c    Wed Apr 29 16:41:49 2015        (r282236)
@@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$");
 
 #include <sys/types.h>
 #include <sys/rctl.h>
+#include <sys/sysctl.h>
 #include <assert.h>
 #include <ctype.h>
 #include <err.h>
@@ -305,13 +306,37 @@ print_rules(char *rules, int hflag, int 
 }
 
 static void
+enosys(void)
+{
+       int error, racct_enable;
+       size_t racct_enable_len;
+
+       racct_enable_len = sizeof(racct_enable);
+       error = sysctlbyname("kern.racct.enable",
+           &racct_enable, &racct_enable_len, NULL, 0);
+
+       if (error != 0) {
+               if (errno == ENOENT)
+                       errx(1, "RACCT/RCTL support not present in kernel; see 
rctl(8) for details.");
+
+               err(1, "sysctlbyname");
+       }
+
+       if (racct_enable == 0)
+               errx(1, "RACCT/RCTL present, but disabled; enable using 
kern.racct.enable=1 tunable");
+}
+
+static void
 add_rule(char *rule)
 {
        int error;
 
        error = rctl_add_rule(rule, strlen(rule) + 1, NULL, 0);
-       if (error != 0)
+       if (error != 0) {
+               if (errno == ENOSYS)
+                       enosys();
                err(1, "rctl_add_rule");
+       }
        free(rule);
 }
 
@@ -330,8 +355,11 @@ show_limits(char *filter, int hflag, int
 
                error = rctl_get_limits(filter, strlen(filter) + 1, outbuf,
                    outbuflen);
-               if (error && errno != ERANGE)
+               if (error && errno != ERANGE) {
+                       if (errno == ENOSYS)
+                               enosys();
                        err(1, "rctl_get_limits");
+               }
        } while (error && errno == ERANGE);
 
        print_rules(outbuf, hflag, nflag);
@@ -345,8 +373,11 @@ remove_rule(char *filter)
        int error;
 
        error = rctl_remove_rule(filter, strlen(filter) + 1, NULL, 0);
-       if (error != 0)
+       if (error != 0) {
+               if (errno == ENOSYS)
+                       enosys();
                err(1, "rctl_remove_rule");
+       }
        free(filter);
 }
 
@@ -399,8 +430,11 @@ show_usage(char *filter, int hflag)
 
                error = rctl_get_racct(filter, strlen(filter) + 1, outbuf,
                    outbuflen);
-               if (error && errno != ERANGE)
+               if (error && errno != ERANGE) {
+                       if (errno == ENOSYS)
+                               enosys();
                        err(1, "rctl_get_racct");
+               }
        } while (error && errno == ERANGE);
 
        while ((tmp = strsep(&outbuf, ",")) != NULL) {
@@ -439,8 +473,11 @@ show_rules(char *filter, int hflag, int 
                        err(1, "realloc");
 
                error = rctl_get_rules(filter, filterlen, outbuf, outbuflen);
-               if (error && errno != ERANGE)
+               if (error && errno != ERANGE) {
+                       if (errno == ENOSYS)
+                               enosys();
                        err(1, "rctl_get_rules");
+               }
        } while (error && errno == ERANGE);
 
        print_rules(outbuf, hflag, nflag);
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to