Bump to version 1.22.1
[platform/upstream/busybox.git] / selinux / sestatus.c
index ec39afc..e594318 100644 (file)
@@ -4,16 +4,24 @@
  * Ported to busybox: KaiGai Kohei <kaigai@ak.jp.nec.com>
  *
  * Copyright (C) KaiGai Kohei <kaigai@ak.jp.nec.com>
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
+//usage:#define sestatus_trivial_usage
+//usage:       "[-vb]"
+//usage:#define sestatus_full_usage "\n\n"
+//usage:       "       -v      Verbose"
+//usage:     "\n       -b      Display current state of booleans"
+
 #include "libbb.h"
 
 extern char *selinux_mnt;
 
-#define OPT_VERBOSE    (1 << 0)
-#define OPT_BOOLEAN    (1 << 1)
+#define OPT_VERBOSE  (1 << 0)
+#define OPT_BOOLEAN  (1 << 1)
 
-#define COL_FMT                "%-31s "
+#define COL_FMT  "%-31s "
 
 static void display_boolean(void)
 {
@@ -33,7 +41,7 @@ static void display_boolean(void)
                if (pending < 0)
                        goto skip;
                printf(COL_FMT "%s",
-                      bools[i], active == 0 ? "off" : "on");
+                               bools[i], active == 0 ? "off" : "on");
                if (active != pending)
                        printf(" (%sactivate pending)", pending == 0 ? "in" : "");
                bb_putchar('\n');
@@ -47,31 +55,14 @@ static void display_boolean(void)
 
 static void read_config(char **pc, int npc, char **fc, int nfc)
 {
-       char buf[256];
-       FILE *fp;
+       char *buf;
+       parser_t *parser;
        int pc_ofs = 0, fc_ofs = 0, section = -1;
 
        pc[0] = fc[0] = NULL;
 
-       fp = fopen("/etc/sestatus.conf", "rb");
-       if (fp == NULL)
-               return;
-
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               int i, c;
-
-               /* kills comments */
-               for (i = 0; (c = buf[i]) != '\0'; i++) {
-                       if (c == '#') {
-                               buf[i] = '\0';
-                               break;
-                       }
-               }
-               trim(buf);
-
-               if (buf[0] == '\0')
-                       continue;
-
+       parser = config_open("/etc/sestatus.conf");
+       while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
                if (strcmp(buf, "[process]") == 0) {
                        section = 1;
                } else if (strcmp(buf, "[files]") == 0) {
@@ -86,7 +77,7 @@ static void read_config(char **pc, int npc, char **fc, int nfc)
                        }
                }
        }
-       fclose(fp);
+       config_close(parser);
 }
 
 static void display_verbose(void)
@@ -129,7 +120,8 @@ static void display_verbose(void)
        /* files contexts */
        puts("\nFile contexts:");
 
-       cterm = ttyname(0);
+       cterm = xmalloc_ttyname(0);
+//FIXME: if cterm == NULL, we segfault!??
        puts(cterm);
        if (cterm && lgetfilecon(cterm, &con) >= 0) {
                printf(COL_FMT "%s\n", "Controlling term:", con);
@@ -137,7 +129,7 @@ static void display_verbose(void)
                        freecon(con);
        }
 
-       for (i=0; fc[i] != NULL; i++) {
+       for (i = 0; fc[i] != NULL; i++) {
                struct stat stbuf;
 
                if (lgetfilecon(fc[i], &con) < 0)
@@ -159,13 +151,13 @@ static void display_verbose(void)
 }
 
 int sestatus_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int sestatus_main(int argc ATTRIBUTE_UNUSED, char **argv)
+int sestatus_main(int argc UNUSED_PARAM, char **argv)
 {
        unsigned opts;
        const char *pol_path;
        int rc;
 
-       opt_complementary = "?0";       /* no arguments are required. */
+       opt_complementary = "?0";  /* no arguments are required. */
        opts = getopt32(argv, "vb");
 
        /* SELinux status: line */