diff options
Diffstat (limited to 'toolbox')
-rw-r--r-- | toolbox/Android.mk | 1 | ||||
-rw-r--r-- | toolbox/chown.c | 62 | ||||
-rw-r--r-- | toolbox/insmod.c | 23 | ||||
-rw-r--r-- | toolbox/mkdosfs.c | 9 | ||||
-rw-r--r-- | toolbox/top.c | 72 |
5 files changed, 140 insertions, 27 deletions
diff --git a/toolbox/Android.mk b/toolbox/Android.mk index b0c241e..5a8dc0b 100644 --- a/toolbox/Android.mk +++ b/toolbox/Android.mk @@ -41,6 +41,7 @@ TOOLS := \ printenv \ smd \ chmod \ + chown \ mkdosfs \ netstat \ ioctl \ diff --git a/toolbox/chown.c b/toolbox/chown.c new file mode 100644 index 0000000..13617db --- /dev/null +++ b/toolbox/chown.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <dirent.h> +#include <errno.h> +#include <pwd.h> +#include <grp.h> + +#include <unistd.h> +#include <time.h> + +int chown_main(int argc, char **argv) +{ + int i; + + if (argc < 3) { + fprintf(stderr, "Usage: chown <USER>[.GROUP] <FILE1> [FILE2] ...\n"); + return 10; + } + + // Copy argv[1] to 'user' so we can truncate it at the period + // if a group id specified. + char user[32]; + char *group = NULL; + strncpy(user, argv[1], sizeof(user)); + if ((group = strchr(user, '.')) != NULL) { + *group++ = '\0'; + } + + // Lookup uid (and gid if specified) + struct passwd *pw; + struct group *grp = NULL; + uid_t uid; + gid_t gid = -1; // passing -1 to chown preserves current group + + pw = getpwnam(user); + if (pw == NULL) { + fprintf(stderr, "No such user '%s'\n", user); + return 10; + } + uid = pw->pw_uid; + + if (group != NULL) { + grp = getgrnam(group); + if (grp == NULL) { + fprintf(stderr, "No such group '%s'\n", group); + return 10; + } + gid = grp->gr_gid; + } + + for (i = 2; i < argc; i++) { + if (chown(argv[i], uid, gid) < 0) { + fprintf(stderr, "Unable to chmod %s: %s\n", argv[i], strerror(errno)); + return 10; + } + } + + return 0; +} + diff --git a/toolbox/insmod.c b/toolbox/insmod.c index d084403..44b9847 100644 --- a/toolbox/insmod.c +++ b/toolbox/insmod.c @@ -45,10 +45,12 @@ bail: return buffer; } +#define min(x,y) ((x) < (y) ? (x) : (y)) int insmod_main(int argc, char **argv) { void *file; - ssize_t size; + ssize_t size = 0; + char opts[1024]; int ret; /* make sure we've got an argument */ @@ -64,9 +66,24 @@ int insmod_main(int argc, char **argv) return -1; } + opts[0] = '\0'; + if (argc > 2) { + int i, len; + char *end = opts + sizeof(opts) - 1; + char *ptr = opts; + + for (i = 2; (i < argc) && (ptr < end); i++) { + len = min(strlen(argv[i]), end - ptr); + memcpy(ptr, argv[i], len); + ptr += len; + *ptr++ = ' '; + *ptr++ = '\0'; + } + *(ptr - 1) = '\0'; + } + /* pass it to the kernel */ - /* XXX options */ - ret = init_module(file, size, ""); + ret = init_module(file, size, opts); if (ret != 0) { fprintf(stderr, "insmod: init_module '%s' failed (%s)\n", diff --git a/toolbox/mkdosfs.c b/toolbox/mkdosfs.c index 9ba9409..744aad1 100644 --- a/toolbox/mkdosfs.c +++ b/toolbox/mkdosfs.c @@ -387,9 +387,8 @@ mkdosfs_main(int argc, char *argv[]) exit(1); } - lseek(fd1, 0, SEEK_SET); - off_t length = lseek(fd1, 0, SEEK_END); - fprintf(stderr, "lseek returned %ld\n", length); + lseek64(fd1, 0, SEEK_SET); + loff_t length = lseek64(fd1, 0, SEEK_END); if (length > 0) { bpb.bsec = length / bpb.bps; bpb.spt = bpb.bsec; @@ -615,8 +614,8 @@ mkdosfs_main(int argc, char *argv[]) fat == 32 && bpb.bkbs != MAXU16 && bss <= bpb.bkbs && x >= bpb.bkbs) { x -= bpb.bkbs; - if (!x && lseek(fd1, 0, SEEK_SET)) - fprintf(stderr, "lseek failed for %s\n", bname); + if (!x && lseek64(fd1, 0, SEEK_SET)) + fprintf(stderr, "lseek64 failed for %s\n", bname); } if (opt_B && x < bss) { if ((n = read(fd1, img, bpb.bps)) == -1) diff --git a/toolbox/top.c b/toolbox/top.c index 0f40a0c..dcc0843 100644 --- a/toolbox/top.c +++ b/toolbox/top.c @@ -41,15 +41,20 @@ struct cpu_info { long unsigned utime, ntime, stime, itime; + long unsigned iowtime, irqtime, sirqtime; }; +#define PROC_NAME_LEN 64 +#define THREAD_NAME_LEN 32 + struct proc_info { struct proc_info *next; pid_t pid; pid_t tid; uid_t uid; gid_t gid; - char name[256]; + char name[PROC_NAME_LEN]; + char tname[THREAD_NAME_LEN]; char state; long unsigned utime; long unsigned stime; @@ -69,7 +74,7 @@ struct proc_list { #define die(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); } #define INIT_PROCS 50 -#define THREAD_MULT 4 +#define THREAD_MULT 8 static struct proc_info **old_procs, **new_procs; static int num_old_procs, num_new_procs; static struct proc_info *free_procs; @@ -228,7 +233,8 @@ static void read_procs(void) { file = fopen("/proc/stat", "r"); if (!file) die("Could not open /proc/stat.\n"); - fscanf(file, "cpu %lu %lu %lu %lu", &new_cpu.utime, &new_cpu.ntime, &new_cpu.stime, &new_cpu.itime); + fscanf(file, "cpu %lu %lu %lu %lu %lu %lu %lu", &new_cpu.utime, &new_cpu.ntime, &new_cpu.stime, + &new_cpu.itime, &new_cpu.iowtime, &new_cpu.irqtime, &new_cpu.sirqtime); fclose(file); proc_num = 0; @@ -237,7 +243,9 @@ static void read_procs(void) { continue; pid = atoi(pid_dir->d_name); - + + struct proc_info cur_proc; + if (!threads) { proc = alloc_proc(); @@ -254,6 +262,12 @@ static void read_procs(void) { proc->num_threads = 0; } else { + sprintf(filename, "/proc/%d/cmdline", pid); + read_cmdline(filename, &cur_proc); + + sprintf(filename, "/proc/%d/status", pid); + read_status(filename, &cur_proc); + proc = NULL; } @@ -275,11 +289,9 @@ static void read_procs(void) { sprintf(filename, "/proc/%d/task/%d/stat", pid, tid); read_stat(filename, proc); - sprintf(filename, "/proc/%d/task/%d/cmdline", pid, tid); - read_cmdline(filename, proc); - - sprintf(filename, "/proc/%d/task/%d/status", pid, tid); - read_status(filename, proc); + strcpy(proc->name, cur_proc.name); + proc->uid = cur_proc.uid; + proc->gid = cur_proc.gid; add_proc(proc_num++, proc); } else { @@ -315,8 +327,9 @@ static int read_stat(char *filename, struct proc_info *proc) { if (!open_paren || !close_paren) return 1; *open_paren = *close_paren = '\0'; - strcpy(proc->name, open_paren + 1); - + strncpy(proc->tname, open_paren + 1, THREAD_NAME_LEN); + proc->tname[THREAD_NAME_LEN-1] = 0; + /* Scan rest of string. */ sscanf(close_paren + 1, " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " "%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld", @@ -347,8 +360,11 @@ static int read_cmdline(char *filename, struct proc_info *proc) { if (!file) return 1; fgets(line, MAX_LINE, file); fclose(file); - if (strlen(line) > 0) - strcpy(proc->name, line); + if (strlen(line) > 0) { + strncpy(proc->name, line, PROC_NAME_LEN); + proc->name[PROC_NAME_LEN-1] = 0; + } else + proc->name[0] = 0; return 0; } @@ -391,16 +407,34 @@ static void print_procs(void) { } } - total_delta_time = (new_cpu.utime + new_cpu.ntime + new_cpu.stime + new_cpu.itime) - - (old_cpu.utime + old_cpu.ntime + old_cpu.stime + old_cpu.itime); + total_delta_time = (new_cpu.utime + new_cpu.ntime + new_cpu.stime + new_cpu.itime + + new_cpu.iowtime + new_cpu.irqtime + new_cpu.sirqtime) + - (old_cpu.utime + old_cpu.ntime + old_cpu.stime + old_cpu.itime + + old_cpu.iowtime + old_cpu.irqtime + old_cpu.sirqtime); qsort(new_procs, num_new_procs, sizeof(struct proc_info *), proc_cmp); printf("\n\n\n"); + printf("User %ld%%, System %ld%%, IOW %ld%%, IRQ %ld%%\n", + ((new_cpu.utime + new_cpu.ntime) - (old_cpu.utime + old_cpu.ntime)) * 100 / total_delta_time, + ((new_cpu.stime ) - (old_cpu.stime)) * 100 / total_delta_time, + ((new_cpu.iowtime) - (old_cpu.iowtime)) * 100 / total_delta_time, + ((new_cpu.irqtime + new_cpu.sirqtime) + - (old_cpu.irqtime + old_cpu.sirqtime)) * 100 / total_delta_time); + printf("User %ld + Nice %ld + Sys %ld + Idle %ld + IOW %ld + IRQ %ld + SIRQ %ld = %ld\n", + new_cpu.utime - old_cpu.utime, + new_cpu.ntime - old_cpu.ntime, + new_cpu.stime - old_cpu.stime, + new_cpu.itime - old_cpu.itime, + new_cpu.iowtime - old_cpu.iowtime, + new_cpu.irqtime - old_cpu.irqtime, + new_cpu.sirqtime - old_cpu.sirqtime, + total_delta_time); + printf("\n"); if (!threads) printf("%5s %4s %1s %5s %7s %7s %-8s %s\n", "PID", "CPU%", "S", "#THR", "VSS", "RSS", "UID", "Name"); else - printf("%5s %5s %4s %1s %7s %7s %-8s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "UID", "Name"); + printf("%5s %5s %4s %1s %7s %7s %-8s %-15s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "UID", "Thread", "Proc"); for (i = 0; i < num_new_procs; i++) { proc = new_procs[i]; @@ -423,10 +457,10 @@ static void print_procs(void) { } if (!threads) printf("%5d %3ld%% %c %5d %6ldK %6ldK %-8.8s %s\n", proc->pid, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads, - proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->name); + proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->name[0] != 0 ? proc->name : proc->tname); else - printf("%5d %5d %3ld%% %c %6ldK %6ldK %-8.8s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state, - proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->name); + printf("%5d %5d %3ld%% %c %6ldK %6ldK %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state, + proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->tname, proc->name); } } |