diff options
Diffstat (limited to 'toolbox')
-rw-r--r-- | toolbox/Android.mk | 24 | ||||
-rw-r--r-- | toolbox/clear.c | 41 | ||||
-rw-r--r-- | toolbox/df.c | 16 | ||||
-rw-r--r-- | toolbox/du.c | 4 | ||||
-rw-r--r-- | toolbox/id.c | 7 | ||||
-rw-r--r-- | toolbox/ls.c | 12 | ||||
-rw-r--r-- | toolbox/renice.c | 64 | ||||
-rw-r--r-- | toolbox/setsebool.c | 39 |
8 files changed, 119 insertions, 88 deletions
diff --git a/toolbox/Android.mk b/toolbox/Android.mk index 086ba0d..2ecb626 100644 --- a/toolbox/Android.mk +++ b/toolbox/Android.mk @@ -57,11 +57,8 @@ TOOLS := \ touch \ lsof \ du \ - md5 - -ifeq ($(HAVE_SELINUX),true) - -TOOLS += \ + md5 \ + clear \ getenforce \ setenforce \ chcon \ @@ -71,9 +68,6 @@ TOOLS += \ setsebool \ load_policy -endif - - ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT))) TOOLS += r endif @@ -90,17 +84,13 @@ LOCAL_SRC_FILES := \ cp/cp.c cp/utils.c \ grep/grep.c grep/fastgrep.c grep/file.c grep/queue.c grep/util.c -LOCAL_SHARED_LIBRARIES := libcutils libc libusbhost - LOCAL_C_INCLUDES := bionic/libc/bionic -ifeq ($(HAVE_SELINUX),true) - -LOCAL_CFLAGS += -DHAVE_SELINUX -LOCAL_SHARED_LIBRARIES += libselinux -LOCAL_C_INCLUDES += external/libselinux/include - -endif +LOCAL_SHARED_LIBRARIES := \ + libcutils \ + libc \ + libusbhost \ + libselinux LOCAL_MODULE := toolbox diff --git a/toolbox/clear.c b/toolbox/clear.c new file mode 100644 index 0000000..df46ad2 --- /dev/null +++ b/toolbox/clear.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2012, The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google, Inc. nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdio.h> + +int clear_main(int argc, char **argv) { + /* This prints the clear screen and move cursor to top-left corner control + * characters for VT100 terminals. This means it will not work on + * non-VT100 compliant terminals, namely Windows' cmd.exe, but should + * work on anything unix-y. */ + fputs("\x1b[2J\x1b[H", stdout); + return 0; +} diff --git a/toolbox/df.c b/toolbox/df.c index 63940a1..9cd0743 100644 --- a/toolbox/df.c +++ b/toolbox/df.c @@ -9,16 +9,22 @@ static int ok = EXIT_SUCCESS; static void printsize(long long n) { char unit = 'K'; - n /= 1024; - if (n > 1024) { + long long t; + + n *= 10; + + if (n > 1024*1024*10) { n /= 1024; unit = 'M'; } - if (n > 1024) { + + if (n > 1024*1024*10) { n /= 1024; unit = 'G'; } - printf("%4lld%c", n, unit); + + t = (n + 512) / 1024; + printf("%4lld.%1lld%c", t/10, t%10, unit); } static void df(char *s, int always) { @@ -41,7 +47,7 @@ static void df(char *s, int always) { } int df_main(int argc, char *argv[]) { - printf("Filesystem Size Used Free Blksize\n"); + printf("Filesystem Size Used Free Blksize\n"); if (argc == 1) { char s[2000]; FILE *f = fopen("/proc/mounts", "r"); diff --git a/toolbox/du.c b/toolbox/du.c index 06374a4..fc7c943 100644 --- a/toolbox/du.c +++ b/toolbox/du.c @@ -62,7 +62,7 @@ __RCSID("$NetBSD: du.c,v 1.33 2008/07/30 22:03:40 dsl Exp $"); int linkchk(dev_t, ino_t); void prstat(const char *, int64_t); -void usage(void); +static void usage(void); long blocksize; @@ -312,7 +312,7 @@ linkchk(dev_t dev, ino_t ino) return 0; } -void +static void usage(void) { diff --git a/toolbox/id.c b/toolbox/id.c index bc79288..8ec79c1 100644 --- a/toolbox/id.c +++ b/toolbox/id.c @@ -4,10 +4,7 @@ #include <sys/types.h> #include <pwd.h> #include <grp.h> - -#ifdef HAVE_SELINUX #include <selinux/selinux.h> -#endif static void print_uid(uid_t uid) { @@ -34,9 +31,7 @@ int id_main(int argc, char **argv) { gid_t list[64]; int n, max; -#ifdef HAVE_SELINUX char *secctx; -#endif max = getgroups(64, list); if (max < 0) max = 0; @@ -53,12 +48,10 @@ int id_main(int argc, char **argv) print_gid(list[n]); } } -#ifdef HAVE_SELINUX if (getcon(&secctx) == 0) { printf(" context=%s", secctx); free(secctx); } -#endif printf("\n"); return 0; } diff --git a/toolbox/ls.c b/toolbox/ls.c index a4db99c..e530521 100644 --- a/toolbox/ls.c +++ b/toolbox/ls.c @@ -5,9 +5,7 @@ #include <dirent.h> #include <errno.h> -#ifdef HAVE_SELINUX #include <selinux/selinux.h> -#endif #include <sys/stat.h> #include <unistd.h> @@ -260,11 +258,7 @@ static int listfile_maclabel(const char *path, int flags) return -1; } -#ifdef HAVE_SELINUX lgetfilecon(path, &maclabel); -#else - maclabel = strdup("-"); -#endif if (!maclabel) { return -1; } @@ -276,12 +270,12 @@ static int listfile_maclabel(const char *path, int flags) switch(s.st_mode & S_IFMT) { case S_IFLNK: { char linkto[256]; - int len; + ssize_t len; len = readlink(path, linkto, sizeof(linkto)); if(len < 0) return -1; - if(len > sizeof(linkto)-1) { + if((size_t)len > sizeof(linkto)-1) { linkto[sizeof(linkto)-4] = '.'; linkto[sizeof(linkto)-3] = '.'; linkto[sizeof(linkto)-2] = '.'; @@ -307,7 +301,7 @@ static int listfile_maclabel(const char *path, int flags) static int listfile(const char *dirname, const char *filename, int flags) { - if ((flags & LIST_LONG | LIST_SIZE | LIST_CLASSIFY | LIST_MACLABEL) == 0) { + if ((flags & (LIST_LONG | LIST_SIZE | LIST_CLASSIFY | LIST_MACLABEL)) == 0) { printf("%s\n", filename); return 0; } diff --git a/toolbox/renice.c b/toolbox/renice.c index 978b329..9dfeb51 100644 --- a/toolbox/renice.c +++ b/toolbox/renice.c @@ -35,11 +35,12 @@ #include <sys/time.h> #include <sys/resource.h> #include <sched.h> +#include <getopt.h> static void usage(const char *s) { - fprintf(stderr, "USAGE: %s [[-r] priority pids ...] [-g pid]\n", s); + fprintf(stderr, "USAGE: %s [[-r] [-t TYPE] priority pids ...] [-g pid]\n", s); exit(EXIT_FAILURE); } @@ -74,32 +75,49 @@ void print_prio(pid_t pid) sched_get_priority_min(sched), sched_get_priority_max(sched)); } +int get_sched(char *str) +{ + if (strcasecmp(str, "RR") == 0) + return SCHED_RR; + else if (strcasecmp(str, "FIFO") == 0) + return SCHED_FIFO; + else if (strcasecmp(str, "NORMAL") == 0) + return SCHED_OTHER; + else if (strcasecmp(str, "OTHER") == 0) + return SCHED_OTHER; + return SCHED_RR; +} + int renice_main(int argc, char *argv[]) { int prio; int realtime = 0; + int opt; + int sched = SCHED_RR; char *cmd = argv[0]; - // consume command name - argc--; - argv++; - - if (argc < 1) - usage(cmd); - - if(strcmp("-r", argv[0]) == 0) { - // do realtime priority adjustment - realtime = 1; - argc--; - argv++; - } - - if(strcmp("-g", argv[0]) == 0) { - if (argc < 2) + do { + opt = getopt(argc, argv, "rt:g:"); + if (opt == -1) + break; + switch (opt) { + case 'r': + // do realtime priority adjustment + realtime = 1; + break; + case 't': + sched = get_sched(optarg); + break; + case 'g': + print_prio(atoi(optarg)); + return 0; + default: usage(cmd); - print_prio(atoi(argv[1])); - return 0; - } + } + } while (1); + + argc -= optind; + argv += optind; if (argc < 1) usage(cmd); @@ -122,7 +140,7 @@ int renice_main(int argc, char *argv[]) struct sched_param sp = { .sched_priority = prio }; int ret; - ret = sched_setscheduler(pid, SCHED_RR, &sp); + ret = sched_setscheduler(pid, sched, &sp); if (ret) { perror("sched_set_scheduler"); exit(EXIT_FAILURE); @@ -137,8 +155,6 @@ int renice_main(int argc, char *argv[]) } } } - + return 0; } - - diff --git a/toolbox/setsebool.c b/toolbox/setsebool.c index 4a3d87d..f79a612 100644 --- a/toolbox/setsebool.c +++ b/toolbox/setsebool.c @@ -9,35 +9,26 @@ #include <errno.h> static int do_setsebool(int nargs, char **args) { - SELboolean *b = alloca(nargs * sizeof(SELboolean)); - char *v; - int i; + const char *name = args[1]; + const char *value = args[2]; + SELboolean b; if (is_selinux_enabled() <= 0) return 0; - for (i = 1; i < nargs; i++) { - char *name = args[i]; - v = strchr(name, '='); - if (!v) { - fprintf(stderr, "setsebool: argument %s had no =\n", name); - return -1; - } - *v++ = 0; - b[i-1].name = name; - if (!strcmp(v, "1") || !strcasecmp(v, "true") || !strcasecmp(v, "on")) - b[i-1].value = 1; - else if (!strcmp(v, "0") || !strcasecmp(v, "false") || !strcasecmp(v, "off")) - b[i-1].value = 0; - else { - fprintf(stderr, "setsebool: invalid value %s\n", v); - return -1; - } + b.name = name; + if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on")) + b.value = 1; + else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off")) + b.value = 0; + else { + fprintf(stderr, "setsebool: invalid value %s\n", value); + return -1; } - if (security_set_boolean_list(nargs - 1, b, 0) < 0) + if (security_set_boolean_list(1, &b, 0) < 0) { - fprintf(stderr, "setsebool: unable to set booleans: %s", strerror(errno)); + fprintf(stderr, "setsebool: could not set %s to %s: %s", name, value, strerror(errno)); return -1; } @@ -46,8 +37,8 @@ static int do_setsebool(int nargs, char **args) { int setsebool_main(int argc, char **argv) { - if (argc < 2) { - fprintf(stderr, "Usage: %s name=value...\n", argv[0]); + if (argc != 3) { + fprintf(stderr, "Usage: %s name value\n", argv[0]); exit(1); } |