From 9739a279a328093b20fa59ff1bc8aa58434320c7 Mon Sep 17 00:00:00 2001 From: hondza Date: Tue, 2 Jun 2009 22:10:39 +0200 Subject: [PATCH] Initial commit. --- README | 139 ++++++++++ cache/Makefile | 29 ++ cache/cache.c | 109 ++++++++ crc32/crc32.c | 82 ++++++ crc32/crc32.h | 26 ++ createsparse/Makefile | 29 ++ createsparse/createsparse.c | 76 ++++++ fadvise/Makefile | 32 +++ fadvise/fadvise | 12 + fadvise/fadvise.c | 283 +++++++++++++++++++ fadvise/fadvise.h | 37 +++ fadvise/fadvise_test | 12 + getline/getline.c | 45 +++ getline/getline.h | 10 + mark/Makefile | 28 ++ mark/mark | 12 + mark/mark.c | 53 ++++ mark/mark.h | 15 + newns/Makefile | 29 ++ newns/newns.c | 80 ++++++ nodevrandom/Makefile | 28 ++ nodevrandom/nodevrandom | 12 + nodevrandom/nodevrandom.c | 125 +++++++++ nodevrandom/nodevrandom.h | 16 ++ nodevrandom/nodevrandom_test | 12 + setrlimit/Makefile | 26 ++ setrlimit/setrlimit.c | 201 ++++++++++++++ sha256sum/Makefile | 33 +++ sha256sum/Makefile.diet | 29 ++ sha256sum/Makefile.mingw32 | 30 ++ sha256sum/crc32.c | 1 + sha256sum/crc32.h | 1 + sha256sum/getline.c | 1 + sha256sum/getline.h | 1 + sha256sum/sha256sum.c | 637 +++++++++++++++++++++++++++++++++++++++++++ sparse/Makefile | 26 ++ sparse/sparse.c | 104 +++++++ tos/Makefile | 28 ++ tos/tos | 12 + tos/tos.c | 68 +++++ tos/tos.h | 17 ++ xtma/Makefile | 26 ++ xtma/xtma.c | 89 ++++++ 43 files changed, 2661 insertions(+) create mode 100644 README create mode 100644 cache/Makefile create mode 100644 cache/cache.c create mode 100644 crc32/crc32.c create mode 100644 crc32/crc32.h create mode 100644 createsparse/Makefile create mode 100644 createsparse/createsparse.c create mode 100644 fadvise/Makefile create mode 100755 fadvise/fadvise create mode 100644 fadvise/fadvise.c create mode 100644 fadvise/fadvise.h create mode 100755 fadvise/fadvise_test create mode 100644 getline/getline.c create mode 100644 getline/getline.h create mode 100644 mark/Makefile create mode 100755 mark/mark create mode 100644 mark/mark.c create mode 100644 mark/mark.h create mode 100644 newns/Makefile create mode 100644 newns/newns.c create mode 100644 nodevrandom/Makefile create mode 100755 nodevrandom/nodevrandom create mode 100644 nodevrandom/nodevrandom.c create mode 100644 nodevrandom/nodevrandom.h create mode 100755 nodevrandom/nodevrandom_test create mode 100644 setrlimit/Makefile create mode 100644 setrlimit/setrlimit.c create mode 100644 sha256sum/Makefile create mode 100644 sha256sum/Makefile.diet create mode 100644 sha256sum/Makefile.mingw32 create mode 120000 sha256sum/crc32.c create mode 120000 sha256sum/crc32.h create mode 120000 sha256sum/getline.c create mode 120000 sha256sum/getline.h create mode 100644 sha256sum/sha256sum.c create mode 100644 sparse/Makefile create mode 100644 sparse/sparse.c create mode 100644 tos/Makefile create mode 100755 tos/tos create mode 100644 tos/tos.c create mode 100644 tos/tos.h create mode 100644 xtma/Makefile create mode 100644 xtma/xtma.c diff --git a/README b/README new file mode 100644 index 0000000..25e5645 --- /dev/null +++ b/README @@ -0,0 +1,139 @@ + +This is a set of tools I originally made for myself. Some of them may be of use +for somebody, though. + +Things with no "foreign" code, written entirely by me (cache, createsparse, +fadvise, mark, newns, nodevrandom, setrlimit, sparse, tos, xtma) are hereby +placed in public domain (where appllicable). Read: do whatever you want with +it, but don't blame me. + +Some other things (sha256sum) use portions of GPL code, so due to the GPL virus +it's GPL. Non-win32 sha256sum doesn't need GPL'd getline.[ch] though... + + +A brief summary of the tools: + + +cache +----- + +posix_fadvise() frontend. May be used to "cache" or "uncache" given files. + + + +createsparse +------------ + +Creates a set of sparse files (as specified on the command line) of given size. +(Inside it is just fopen() folowed by fseek(), putc() and fclose(), nothing +more.) + +Usage: createsparse [filenames ...] + + + +fadvise +------- + +write(), fwrite{,_unlocked}(), read(), fread{,_unlocked}(), close(), fclose() +wrapper using posix_fadvise(). + +When a dynamically linked program is run via this, it will have changed the +caching behavior. It is rather crude (calling posix_fadvise() for every call of +the functions), yet useful. Especially the default "don't cache" mode. Imagine +you need to copy a huge directory from one disk to another, knowing that after +the copy is done, you won't use it anymore. This tool advises the kernel that +this file won't be needed anytime soon. + +Usage: fadvise program [that program options] + + + +mark +---- + +Very simple socket() wrapper. Calls setsockopt(... SO_MARK ...) for every +AF_INET socket() call. Mark is specified as an env. variable (or defaults to +1). + +Useful when you need some means to classify packets from usermode to later +apply policy routing (via fwmark). + +Usage: MARK=some_number mark program [that program options] + + + +newns +----- + +clone() frontend which sets the "unshare" flags (CLONE_NEWNS, CLONE_NEWPID, +CLONE_NEWUTS, CLONE_NEWUSER, CLONE_NEWIPC). Useful for playing with +*-namespaces (notably PID namespaces). + +Usage: newns [options] -- program [that program options] + + + +nodevrandom +----------- + +{,f}open{,64}() wrapper that intercepts opening of /dev/random and changes the +opened file to /dev/urandom, therefore preventing the program to block +"forever". Some bad program are written in a way that they open /dev/random +(which may block very very long for new entropy to arrive). + +Usage: nodevrandom program [that program options] + + + +setrlimit +--------- + +setrlimit() frontend. It does pretty much the same as shell's "ulimit" builtin +does, except that it is NOT shell builtin. Imagine you are in a VERY memory +restricted environment. Then even running a shell just to set some limit is way +too much overhead. This tool is handy for example for chroot environments ... + +Usage: setrlimit [options] -- /path/to/executable [arguments] + + + +sha256sum +--------- + +Chesumming tool. Does pretty much the same job as "vanilla" +{md5,sha1,sha256}sum, except that it supports crc32 as well (not much win +there, actually) and by default generates an auxiliary record that contains the +file size as well. + + + +sparse +------ + +Reads standard input and ouputs a file with "holes" (a sparse file) when the +filesystem supports it. Useful for disk image files kept on filesystem (e.g. +virtual machines, ...) + +Usage: sparse new_file < old_file + + + +tos +--- + +socket() wrapper similar to mark. Classifies packets by ToS (type of service). +Don't use unless necessary. Use mark instead where possible. + + + +xtma +---- + +Simple X program that draws a fullscreen black window (with no decoration +whatsoever). Useful when you play windows games in wine in pseudo-fullscreen +(vitual desktop). Just run xtma in some desktop, run the wine game, make it +fullscreen, switch to xtma and back and ... the pseudo-fullscreen doesn't +contain the window manager crap anymore. YMMV. + + diff --git a/cache/Makefile b/cache/Makefile new file mode 100644 index 0000000..528064c --- /dev/null +++ b/cache/Makefile @@ -0,0 +1,29 @@ + +CC=gcc +STRIP=strip +RM=rm -f +CFLAGS_COMMON+=-Wall -pedantic -Wno-long-long +CFLAGS_OPTI+=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG+=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS+=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS+=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=cache + +.PHONY: clean + +LIBS= +OBJS=cache.o + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o $(NAME) core + diff --git a/cache/cache.c b/cache/cache.c new file mode 100644 index 0000000..aded54d --- /dev/null +++ b/cache/cache.c @@ -0,0 +1,109 @@ +/****************************************************************************** + * cache.c + *****************************************************************************/ + +#define _FILE_OFFSET_BITS 64 +#define _GNU_SOURCE + +#include +#include +#include +#include + +#include + +#include /* solaris wants it */ + +#include + +#define STRINGIFY(x) #x + + +static unsigned long long get_special_number(char *in) +{ + unsigned long long ret; + char *unit; + + ret = strtoull(in, &unit, 10); + if(*unit == 'k') { ret *= 1024; } + else if(*unit == 'K') { ret *= 1000; } + else if(*unit == 'm') { ret *= 1024*1024; } + else if(*unit == 'M') { ret *= 1000*1000; } + else if(*unit == 'g') { ret *= 1024*1024*1024; } + else if(*unit == 'G') { ret *= 1000*1000*1000; } + + return ret; +} /* get_special_number() */ + + +int main(int argc, char ** argv) +{ + int err, i, ret=0, fd, c, advice=POSIX_FADV_DONTNEED, verbose=0; + char *adv_string = STRINGIFY(POSIX_FADV_DONTNEED); + off_t len=0, offset=0; + + while( (c = getopt(argc, argv, "a:nsrNwdo:l:v")) != -1 ) + { + switch(c) + { + case 'v': verbose = 1; break; + + case 'o': offset = get_special_number(optarg); break; + case 'l': len = get_special_number(optarg); break; + + case 'n': advice = POSIX_FADV_NORMAL; adv_string = STRINGIFY(POSIX_FADV_NORMAL); break; + case 's': advice = POSIX_FADV_SEQUENTIAL; adv_string = STRINGIFY(POSIX_FADV_SEQUENTIAL); break; + case 'r': advice = POSIX_FADV_RANDOM; adv_string = STRINGIFY(POSIX_FADV_RANDOM); break; + case 'N': advice = POSIX_FADV_NOREUSE; adv_string = STRINGIFY(POSIX_FADV_NOREUSE); break; + case 'w': advice = POSIX_FADV_WILLNEED; adv_string = STRINGIFY(POSIX_FADV_WILLNEED); break; + case 'd': advice = POSIX_FADV_DONTNEED; adv_string = STRINGIFY(POSIX_FADV_DONTNEED); break; + + case 'a': + if(strcasestr(optarg, "NORM")) { advice = POSIX_FADV_NORMAL; adv_string = STRINGIFY(POSIX_FADV_NORMAL); } + else if(strcasestr(optarg, "SEQ")) { advice = POSIX_FADV_SEQUENTIAL; adv_string = STRINGIFY(POSIX_FADV_SEQUENTIAL); } + else if(strcasestr(optarg, "RAND")) { advice = POSIX_FADV_RANDOM; adv_string = STRINGIFY(POSIX_FADV_RANDOM); } + else if(strcasestr(optarg, "NORE")) { advice = POSIX_FADV_NOREUSE; adv_string = STRINGIFY(POSIX_FADV_NOREUSE); } + else if(strcasestr(optarg, "WILL")) { advice = POSIX_FADV_WILLNEED; adv_string = STRINGIFY(POSIX_FADV_WILLNEED); } + else if(strcasestr(optarg, "DONT")) { advice = POSIX_FADV_DONTNEED; adv_string = STRINGIFY(POSIX_FADV_DONTNEED); } + else + { + fprintf(stderr, "Not a valid advice ('%s'), defaulting to POSIX_FADV_DONTNEED\n", optarg); + advice = POSIX_FADV_DONTNEED; + adv_string = STRINGIFY(POSIX_FADV_DONTNEED); + } + break; + + } /* switch argument */ + } /* while getopt() */ + + + if(optind >= argc) + { + fprintf(stderr, "Usage: cache [-o offset[kKmMgG]] [-l length[kKmMgG]] [-a NORM|SEQ|RAND|NORE|WILL|DONT] [-nsrNwd] [filenames ...]\n\n"); + exit(1); + } + + if(verbose) + fprintf(stderr, "Will use %s (offset=%llu, length=%llu)\n", adv_string, offset, len); + + for(i=optind; i < argc; i++) + { + fd = open(argv[i], O_RDONLY); + if(-1 == fd) + { + ret = 1; + fprintf(stderr, "%s: open() failed: %s\n", argv[i], strerror(errno)); + continue; + } + err = posix_fadvise(fd, offset, len, advice); + if(-1 == err) + { + ret = 1; + fprintf(stderr, "%s: posix_fadvise() failed: %s\n", argv[i], strerror(errno)); + } + close(fd); + } /* for i */ + + return ret; +} /* main() */ + diff --git a/crc32/crc32.c b/crc32/crc32.c new file mode 100644 index 0000000..e0b92e8 --- /dev/null +++ b/crc32/crc32.c @@ -0,0 +1,82 @@ +/* + * This is a tiny implementation of CRC-32. + * + * Written by Solar Designer in 1998, revised in + * 2005 for use in John the Ripper, and placed in the public domain. + * There's absolutely no warranty. + */ + +#include + +#include "crc32.h" + +#define POLY 0xEDB88320 +#define ALL1 0xFFFFFFFF + +/* static crc32_t *crc32_table = NULL; */ +static crc32_t crc32_table[0x100]; +static char crc32_table_init = 0; + +void crc32_init(crc32_t *value) +{ + unsigned int index, bit; + crc32_t entry; + + *value = ALL1; + + if (crc32_table_init) return; + crc32_table_init = 1; + /* crc32_table = malloc(sizeof(*crc32_table) * 0x100); */ + + for (index = 0; index < 0x100; index++) { + entry = index; + + for (bit = 0; bit < 8; bit++) + if (entry & 1) { + entry >>= 1; + entry ^= POLY; + } else + entry >>= 1; + + crc32_table[index] = entry; + } +} + + +void crc32_update(crc32_t *value, const void *data, const unsigned int size) +{ + unsigned char *ptr; + unsigned int count; + crc32_t result; + + result = *value; + ptr = (unsigned char *) data; + count = size; + + if (count) + do { + result = (result >> 8) ^ crc32_table[(result ^ *ptr++) & 0xFF]; + } while (--count); + + *value = result; +} + + +void crc32_final(crc32_t *value, unsigned char *out) +{ + *value = ~(*value); + out[3] = *value; + out[2] = *value >> 8; + out[1] = *value >> 16; + out[0] = *value >> 24; +} + + +void crc32_final2(crc32_t *value, unsigned char *out) +{ + *value = ~(*value); + out[0] = *value; + out[1] = *value >> 8; + out[2] = *value >> 16; + out[3] = *value >> 24; +} diff --git a/crc32/crc32.h b/crc32/crc32.h new file mode 100644 index 0000000..ccfc82a --- /dev/null +++ b/crc32/crc32.h @@ -0,0 +1,26 @@ +/* + * This is a tiny implementation of CRC-32. + * + * Written by Solar Designer in 1998, revised in + * 2005 for use in John the Ripper, and placed in the public domain. + * There's absolutely no warranty. + */ + +#ifndef _JOHN_CRC32_H +#define _JOHN_CRC32_H + +typedef unsigned int crc32_t; + +extern void crc32_init(crc32_t *value); +#define crc32_start crc32_init + +extern void crc32_update(crc32_t *value, const void *data, const unsigned int size); +#define crc32_process crc32_update + +extern void crc32_final(crc32_t *value, unsigned char *out); +#define crc32_finish + +extern void crc32_final2(crc32_t *value, unsigned char *out); +#define crc32_finish2 + +#endif diff --git a/createsparse/Makefile b/createsparse/Makefile new file mode 100644 index 0000000..8b68246 --- /dev/null +++ b/createsparse/Makefile @@ -0,0 +1,29 @@ + +CC=gcc +STRIP=strip +RM=rm -f +CFLAGS_COMMON+=-Wall -pedantic -Wno-long-long +CFLAGS_OPTI+=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG+=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS+=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS+=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=createsparse + +.PHONY: clean + +LIBS= +OBJS=createsparse.o + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o $(NAME) core + diff --git a/createsparse/createsparse.c b/createsparse/createsparse.c new file mode 100644 index 0000000..8a195dc --- /dev/null +++ b/createsparse/createsparse.c @@ -0,0 +1,76 @@ +/****************************************************************************** + * createsparse.c + * creates arbitrary sized sparse files + *****************************************************************************/ + +#define _FILE_OFFSET_BITS 64 +#define _GNU_SOURCE + +#include +#include +#include +#include + +#include + +static unsigned long long get_special_number(char *in) +{ + unsigned long long ret; + char *unit; + + ret = strtoull(in, &unit, 10); + if(*unit == 'k') { ret *= 1024; } + else if(*unit == 'K') { ret *= 1000; } + else if(*unit == 'm') { ret *= 1024*1024; } + else if(*unit == 'M') { ret *= 1000*1000; } + else if(*unit == 'g') { ret *= 1024*1024*1024; } + else if(*unit == 'G') { ret *= 1000*1000*1000; } + + return ret; +} /* get_special_number() */ + + +int main(int argc, char ** argv) +{ + size_t size; + FILE *f; + int err, i, ret=0; + + if(argc < 3) + { + fprintf(stderr, "Usage: createsparse [filenames ...]\n\n"); + exit(1); + } + + size = get_special_number(argv[1]); + + for(i=2; i < argc; i++) + { + f = fopen(argv[i], "w+"); + if(!f) + { + ret = 1; + fprintf(stderr, "%s: fopen() failed: %s\n", argv[i], strerror(errno)); + continue; + } + err = fseek(f, size-1, SEEK_SET); + if(-1 == err) + { + ret = 1; + fprintf(stderr, "%s: fseek() failed: %s\n", argv[i], strerror(errno)); + fclose(f); + continue; + } + err = putc(0, f); + if(EOF == err) + { + ret = 1; + fprintf(stderr, "%s: putc() failed: %s\n", argv[i], strerror(errno)); + fclose(f); + continue; + } + } /* for i */ + + return ret; +} /* main() */ + diff --git a/fadvise/Makefile b/fadvise/Makefile new file mode 100644 index 0000000..306a00a --- /dev/null +++ b/fadvise/Makefile @@ -0,0 +1,32 @@ + +CC=gcc +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -ansi +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math -DNDEBUG +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=libfadvise.so + +LIBS=-ldl +OBJS=fadvise.o + + +$(NAME): $(OBJS) $(LIBS) + $(CC) -fPIC -shared $(CFLAGS) -o $@ $^ +ifeq "$(ARG)" "opti" + strip --strip-debug $(NAME) +endif + + +%.o: %.c %.h + $(CC) -c -fPIC -shared $(CFLAGS) -o $@ $< + + +clean: + $(RM) *.o $(NAME) core + diff --git a/fadvise/fadvise b/fadvise/fadvise new file mode 100755 index 0000000..af4bfde --- /dev/null +++ b/fadvise/fadvise @@ -0,0 +1,12 @@ +#!/bin/mksh + +if [ $# = 0 ] ; then + echo " usage:" + echo " fadvise [args]" + exit +fi + +LD_PRELOAD="${LD_PRELOAD}:/usr/local/lib/libfadvise.so" +export LD_PRELOAD +exec "$@" + diff --git a/fadvise/fadvise.c b/fadvise/fadvise.c new file mode 100644 index 0000000..fa568fd --- /dev/null +++ b/fadvise/fadvise.c @@ -0,0 +1,283 @@ +/****************************************************************************** + * fadvise.c + *****************************************************************************/ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#include "fadvise.h" + +static real_close_t real_close; +static real_write_t real_write; +static real_read_t real_read; + +static real_fclose_t real_fclose; +static real_fwrite_t real_fwrite; +static real_fread_t real_fread; +static real_fwrite_t real_fwrite_unlocked; +static real_fread_t real_fread_unlocked; + +static pid_t mypid; + +static int fadvise_init_done=0; +static int fadvise_debug=0; +static int fadvise_advice=POSIX_FADV_DONTNEED; + +void do_log(char *str, ...) +{ + va_list arglist; + + va_start(arglist, str); + vfprintf(stderr, str, arglist); + fflush(stderr); + va_end(arglist); +} /* do_log() */ + + + +static void lib_init(void) +{ + char *temp; + + if(fadvise_init_done) return; + fadvise_init_done=1; + + *(void **) (&real_close) = dlsym(RTLD_NEXT, "close"); + if(!real_close) + { + fprintf(stderr, "(fadvise) Cannot load symbol 'close' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_write) = dlsym(RTLD_NEXT, "write"); + if(!real_write) + { + fprintf(stderr, "(fadvise) Cannot load symbol 'write' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_read) = dlsym(RTLD_NEXT, "read"); + if(!real_read) + { + fprintf(stderr, "(fadvise) Cannot load symbol 'read' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_fclose) = dlsym(RTLD_NEXT, "fclose"); + if(!real_fclose) + { + fprintf(stderr, "(fadvise) Cannot load symbol 'fclose' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_fwrite) = dlsym(RTLD_NEXT, "fwrite"); + if(!real_fwrite) + { + fprintf(stderr, "(fadvise) Cannot load symbol 'fwrite' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_fread) = dlsym(RTLD_NEXT, "fread"); + if(!real_fread) + { + fprintf(stderr, "(fadvise) Cannot load symbol 'fread' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_fwrite_unlocked) = dlsym(RTLD_NEXT, "fwrite_unlocked"); + if(!real_fwrite_unlocked) + { + fprintf(stderr, "(fadvise) Cannot load symbol 'fwrite_unlocked' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_fread_unlocked) = dlsym(RTLD_NEXT, "fread_unlocked"); + if(!real_fread_unlocked) + { + fprintf(stderr, "(fadvise) Cannot load symbol 'fread_unlocked' %s\n", dlerror()); + _exit(1); + } + + temp = getenv("FADVISE_ADVICE"); if(!temp) temp = getenv("FADV"); + if(temp) + { + if(strcasestr(temp, "NORM")) fadvise_advice = POSIX_FADV_NORMAL; + else if(strcasestr(temp, "SEQ")) fadvise_advice = POSIX_FADV_SEQUENTIAL; + else if(strcasestr(temp, "RAND")) fadvise_advice = POSIX_FADV_RANDOM; + else if(strcasestr(temp, "NORE")) fadvise_advice = POSIX_FADV_NOREUSE; + else if(strcasestr(temp, "WILL")) fadvise_advice = POSIX_FADV_WILLNEED; + else if(strcasestr(temp, "DONT")) fadvise_advice = POSIX_FADV_DONTNEED; + } + +#ifndef NDEBUG + temp = getenv("FADVISE_DEBUG"); if(!temp) temp = getenv("FADD"); + if(temp) fadvise_debug = strtoul(temp, NULL, 10); +#endif /* NDEBUG */ + + +} /* lib_init() */ + + + +ssize_t write(int fd, const void *buf, size_t count) +{ + ssize_t ret; + off_t pos; + + DEBUG("write(%d, ,%u)\n", fd, count); + if(!fadvise_init_done) lib_init(); + + ret = real_write(fd, buf, count); + + if(ret > 0) + { + pos = lseek(fd, 0, SEEK_CUR); + if((off_t)-1 == pos) pos = 0; + posix_fadvise(fd, 0, pos, fadvise_advice); + } + + return ret; +} + + + +size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t ret; + off_t pos; + + DEBUG("fwrite(, %u, %u, %d)\n", size, nmemb, stream ? fileno(stream) : -1); + if(!fadvise_init_done) lib_init(); + + ret = real_fwrite(ptr, size, nmemb, stream); + + if(ret > 0) + { + pos = (long) ftell(stream); + if(-1 == pos) pos = 0; + posix_fadvise(fileno(stream), 0, pos, fadvise_advice); + } + + return ret; +} + + + +size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t ret; + off_t pos; + + DEBUG("fwrite_unlocked(, %u, %u, %d)\n", size, nmemb, stream ? fileno(stream) : -1); + if(!fadvise_init_done) lib_init(); + + ret = real_fwrite_unlocked(ptr, size, nmemb, stream); + + if(ret > 0) + { + pos = (long) ftell(stream); + if(-1 == pos) pos = 0; + posix_fadvise(fileno(stream), 0, pos, fadvise_advice); + } + + return ret; +} + + + +ssize_t read(int fd, void *buf, size_t count) +{ + ssize_t ret; + off_t pos; + + DEBUG("read(%d, ,%u)\n", fd, count); + if(!fadvise_init_done) lib_init(); + + ret = real_read(fd, buf, count); + + if(ret > 0) + { + pos = lseek(fd, 0, SEEK_CUR); + if((off_t)-1 == pos) pos = 0; + posix_fadvise(fd, 0, pos, fadvise_advice); + } + + return ret; +} + + + +size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t ret; + off_t pos; + + DEBUG("fread(, %u, %u, %d)\n", size, nmemb, stream ? fileno(stream) : -1); + if(!fadvise_init_done) lib_init(); + + ret = real_fread(ptr, size, nmemb, stream); + + if(ret > 0) + { + pos = (long) ftell(stream); + if(-1 == pos) pos = 0; + posix_fadvise(fileno(stream), 0, pos, fadvise_advice); + } + + return ret; +} + + + +size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t ret; + off_t pos; + + DEBUG("fread_unlocked(, %u, %u, %d)\n", size, nmemb, stream ? fileno(stream) : -1); + if(!fadvise_init_done) lib_init(); + + ret = real_fread_unlocked(ptr, size, nmemb, stream); + + if(ret > 0) + { + pos = (long) ftell(stream); + if(-1 == pos) pos = 0; + posix_fadvise(fileno(stream), 0, pos, fadvise_advice); + } + + return ret; +} + + + + +int close(int fd) +{ + off_t pos; + + DEBUG("close(%d)\n", fd); + if(!fadvise_init_done) lib_init(); + + pos = lseek(fd, 0, SEEK_CUR); + if((off_t)-1 == pos) pos = 0; + posix_fadvise(fd, 0, pos, fadvise_advice); + return real_close(fd); +} + + + +int fclose(FILE *fp) +{ + off_t pos; + + DEBUG("fclose(%d)\n", fp ? fileno(fp) : -1); + if(!fadvise_init_done) lib_init(); + + pos = (long) ftell(fp); + if(-1 == pos) pos = 0; + posix_fadvise(fileno(fp), 0, pos, fadvise_advice); + return real_fclose(fp); +} + + diff --git a/fadvise/fadvise.h b/fadvise/fadvise.h new file mode 100644 index 0000000..3a16452 --- /dev/null +++ b/fadvise/fadvise.h @@ -0,0 +1,37 @@ +/****************************************************************************** + * fadvise.h + *****************************************************************************/ + +#ifndef _FADVISE_H_ +#define _FADVISE_H_ + + +#ifndef NDEBUG +#define DEBUG(p...) do { if(fadvise_debug) do_log(p); } while(0) +#else +#define DEBUG(p...) +#endif /* NDEBUG */ + +#undef fwrite_unlocked +#undef fread_unlocked + + +typedef int (*real_close_t) (int); + +typedef ssize_t (*real_write_t) (int, const void *, size_t); + +typedef ssize_t (*real_read_t) (int, void *, size_t); + +typedef size_t (*real_fwrite_t) (const void *, size_t, size_t, FILE *); + +typedef size_t (*real_fread_t) (void *, size_t, size_t, FILE *); + +typedef int (*real_fclose_t) (FILE *); + +static void lib_init(void) __attribute__((constructor)); + +/* static void lib_deinit(void) __attribute__((destructor)); */ + +#endif /* _FADVISE_H_ */ + + diff --git a/fadvise/fadvise_test b/fadvise/fadvise_test new file mode 100755 index 0000000..311d5d2 --- /dev/null +++ b/fadvise/fadvise_test @@ -0,0 +1,12 @@ +#!/bin/mksh + +if [ $# = 0 ] ; then + echo " usage:" + echo " fadvise [args]" + exit +fi + +LD_PRELOAD="${LD_PRELOAD} ./libfadvise.so" +export LD_PRELOAD +exec "$@" + diff --git a/getline/getline.c b/getline/getline.c new file mode 100644 index 0000000..daec68f --- /dev/null +++ b/getline/getline.c @@ -0,0 +1,45 @@ +/* getdelim() and getline() from dietlibc. GPLv2 */ + +#ifdef NEED_GETLINE + +#include +#include +#include +#include + +#include "getline.h" + +ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream) { + size_t i; + int x, tmp; + char *new; + + if (!lineptr || !n) { + errno=EINVAL; + return -1; + } + if (!*lineptr) *n=0; + for (i=0; ; ) { + x=fgetc(stream); + if (i>=*n) { + tmp=*n+100; + new=realloc(*lineptr,tmp); + if (!new) return -1; + *lineptr=new; *n=tmp; + } + if (x==EOF) { if (!i) return -1; (*lineptr)[i]=0; return i; } + (*lineptr)[i]=x; + ++i; + if (x==delim) break; + } + (*lineptr)[i]=0; + return i; +} + +ssize_t getline(char **lineptr, size_t *n, FILE *stream) { + return getdelim(lineptr,n,'\n',stream); +} + +#endif /* NEED_GETLINE */ + + diff --git a/getline/getline.h b/getline/getline.h new file mode 100644 index 0000000..6f2ddf9 --- /dev/null +++ b/getline/getline.h @@ -0,0 +1,10 @@ + +#ifdef NEED_GETLINE + +ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); + +ssize_t getline(char **lineptr, size_t *n, FILE *stream); + +#endif /* NEED_GETLINE */ + + diff --git a/mark/Makefile b/mark/Makefile new file mode 100644 index 0000000..4ec2645 --- /dev/null +++ b/mark/Makefile @@ -0,0 +1,28 @@ + +CC=gcc +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -ansi +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=libmark.so + +LIBS=-ldl +OBJS=mark.o + + +$(NAME): $(OBJS) $(LIBS) + $(CC) -fPIC -shared $(CFLAGS) -o $@ $^ + +%.o: %.c %.h + $(CC) -c -fPIC -shared $(CFLAGS) -o $@ $< + + +clean: + $(RM) *.o $(NAME) core + diff --git a/mark/mark b/mark/mark new file mode 100755 index 0000000..e0976c1 --- /dev/null +++ b/mark/mark @@ -0,0 +1,12 @@ +#!/bin/mksh + +if [ $# = 0 ] ; then + echo " usage:" + echo " mark [args]" + exit +fi + +LD_PRELOAD="${LD_PRELOAD} /usr/local/lib/libmark.so" +export LD_PRELOAD +exec "$@" + diff --git a/mark/mark.c b/mark/mark.c new file mode 100644 index 0000000..c11d739 --- /dev/null +++ b/mark/mark.c @@ -0,0 +1,53 @@ +/****************************************************************************** + * mark.c + *****************************************************************************/ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +#include "mark.h" + + +#define SO_MARK 36 + + +real_socket_t real_socket; +int default_mark = 1; + + +void lib_init(void) +{ + char *temp; + + *(void **) (&real_socket) = dlsym(RTLD_NEXT, "socket"); + if(!real_socket) + { + fprintf(stderr, "Cannot load symbol 'socket' %s\n", dlerror()); + _exit(1); + } + + temp = getenv("MARK"); + if(temp) default_mark = (int) strtoul(temp, NULL, 10); +} + + +int socket(int domain, int type, int protocol) +{ + int err2, mark = default_mark; + int err = real_socket(domain, type, protocol); + + if(domain != PF_INET || err == -1) return err; + + err2 = setsockopt(err, SOL_SOCKET, SO_MARK, (void *) &mark, sizeof (int)); + if(err2 == -1) { perror("setsockopt(... SO_MARK ...)"); exit(1); } + + return err; +} + + diff --git a/mark/mark.h b/mark/mark.h new file mode 100644 index 0000000..d3173b2 --- /dev/null +++ b/mark/mark.h @@ -0,0 +1,15 @@ +/****************************************************************************** + * tos.h + *****************************************************************************/ + +#ifndef _MARK_H_ +#define _MARK_H_ + +typedef int (*real_socket_t) (int, int, int); + +void lib_init(void) __attribute__((constructor)); + + +#endif /* _MARK_H_ */ + + diff --git a/newns/Makefile b/newns/Makefile new file mode 100644 index 0000000..b904a92 --- /dev/null +++ b/newns/Makefile @@ -0,0 +1,29 @@ + +CC=gcc +STRIP=strip +RM=rm -f +CFLAGS_COMMON+=-Wall -pedantic -Wno-long-long +CFLAGS_OPTI+=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG+=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS+=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS+=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=newns + +.PHONY: clean + +LIBS= +OBJS=newns.o + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o $(NAME) core + diff --git a/newns/newns.c b/newns/newns.c new file mode 100644 index 0000000..9491b5f --- /dev/null +++ b/newns/newns.c @@ -0,0 +1,80 @@ +/****************************************************************************** + * newns.c + * Roughly the same as: http://glandium.org/blog/?p=217 + *****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +void help() +{ + fputs("newns: launch a process in a new namespace\n\n" \ + "Usage: newns [-0npuUi] [command]\n\n" \ + "Options:\n" \ + "\t -0\t\treset previous (including default CLONE_NEWNS)\n" \ + "\t -n\t\t[CLONE_NEWNS] new mount namespace; the default\n" \ + "\t -p\t\t[CLONE_NEWPID] new PID namespace\n" \ + "\t -U\t\t[CLONE_NEWUTS] new UTS namespace\n" \ + "\t -u\t\t[CLONE_NEWUSER] new USER namespace\n" \ + "\t -i\t\t[CLONE_NEWIPC] new IPC namespace\n\n" \ + "Default namespace is mount namespace (CLONE_NEWNS); default command is a new shell (/bin/sh)\n\n" \ + , stderr); +} + + +int main(int argc, char *argv[]) +{ + int err, c, flag=CLONE_NEWNS; + + while( (c = getopt(argc, argv, "0unUiph")) != -1 ) + { + switch(c) + { + case '0': flag = 0; break; + case 'n': flag |= CLONE_NEWNS; break; + case 'u': flag |= CLONE_NEWUSER; break; + case 'U': flag |= CLONE_NEWUTS; break; + case 'p': flag |= CLONE_NEWPID; break; + case 'i': flag |= CLONE_NEWIPC; break; + case 'h': help(); exit(0); break; + case '?': help(); exit(1); break; + } /* switch argument */ + } /* while getopt() */ + +#if 0 + err = syscall(SYS_unshare, flag); + /* err = unshare(flag); */ + if(-1 == err) + { + perror("unshare() failed"); + exit(1); + } +#else + err = syscall(SYS_clone, SIGCHLD | flag, NULL); + if(-1 == err) + { + perror("clone() failed"); + exit(1); + } + else if(0 != err) + { + wait(NULL); + return 0; + } +#endif + + if(optind < argc) + return execvp(argv[optind], &argv[optind]); + + return execv("/bin/sh", NULL); +} diff --git a/nodevrandom/Makefile b/nodevrandom/Makefile new file mode 100644 index 0000000..334e3b8 --- /dev/null +++ b/nodevrandom/Makefile @@ -0,0 +1,28 @@ + +CC=gcc +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -ansi +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=libnodevrandom.so + +LIBS=-ldl +OBJS=nodevrandom.o + + +$(NAME): $(OBJS) $(LIBS) + $(CC) -fPIC -shared $(CFLAGS) -o $@ $^ + +%.o: %.c %.h + $(CC) -c -fPIC -shared $(CFLAGS) -o $@ $< + + +clean: + $(RM) *.o $(NAME) core + diff --git a/nodevrandom/nodevrandom b/nodevrandom/nodevrandom new file mode 100755 index 0000000..2c872f6 --- /dev/null +++ b/nodevrandom/nodevrandom @@ -0,0 +1,12 @@ +#!/bin/mksh + +if [ $# = 0 ] ; then + echo " usage:" + echo " nodevrandom [args]" + exit +fi + +LD_PRELOAD="${LD_PRELOAD} /usr/local/lib/libnodevrandom.so" +export LD_PRELOAD +exec "$@" + diff --git a/nodevrandom/nodevrandom.c b/nodevrandom/nodevrandom.c new file mode 100644 index 0000000..819ef1b --- /dev/null +++ b/nodevrandom/nodevrandom.c @@ -0,0 +1,125 @@ +/****************************************************************************** + * nodevrandom.c + *****************************************************************************/ + +#define _GNU_SOURCE + +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include + +#include "nodevrandom.h" + +static real_open_t real_open; +static real_open_t real_open64; +static real_fopen_t real_fopen; +static real_fopen_t real_fopen64; + +static int nodevrandom_init_done=0; + + +static void lib_init(void) +{ + if(nodevrandom_init_done) return; + nodevrandom_init_done=1; + + *(void **) (&real_open) = dlsym(RTLD_NEXT, "open"); + if(!real_open) + { + fprintf(stderr, "(NODEVRANDOM) Cannot load symbol 'open' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_open64) = dlsym(RTLD_NEXT, "open64"); + if(!real_open64) + { + fprintf(stderr, "(NODEVRANDOM) Cannot load symbol 'open64' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_fopen) = dlsym(RTLD_NEXT, "fopen"); + if(!real_fopen) + { + fprintf(stderr, "(NODEVRANDOM) Cannot load symbol 'fopen' %s\n", dlerror()); + _exit(1); + } + *(void **) (&real_fopen64) = dlsym(RTLD_NEXT, "fopen64"); + if(!real_fopen64) + { + fprintf(stderr, "(NODEVRANDOM) Cannot load symbol 'fopen64' %s\n", dlerror()); + _exit(1); + } +} /* lib_init() */ + + + + +int open(const char *pathname, int flags, ...) +{ + va_list list; + mode_t mode; + + if(!nodevrandom_init_done) lib_init(); + + if(0 == strcmp(pathname, "/dev/random")) + { + va_start(list, flags); + mode = va_arg(list, mode_t); + va_end(list); + return real_open("/dev/urandom", flags, mode); + } + else + return real_open(pathname, flags); +} + + +int open64(const char *pathname, int flags, ...) +{ + va_list list; + mode_t mode; + + if(!nodevrandom_init_done) lib_init(); + + if(0 == strcmp(pathname, "/dev/random")) + { + va_start(list, flags); + mode = va_arg(list, mode_t); + va_end(list); + return real_open("/dev/urandom", flags, mode); + } + else + return real_open(pathname, flags); +} + + + +FILE *fopen(const char *path, const char *mode) +{ + if(!nodevrandom_init_done) lib_init(); + + if(0 == strcmp(path, "/dev/random")) + return real_fopen("/dev/urandom", mode); + else + return real_fopen(path, mode); +} + + + + +FILE *fopen64(const char *path, const char *mode) +{ + if(!nodevrandom_init_done) lib_init(); + + if(0 == strcmp(path, "/dev/random")) + return real_fopen("/dev/urandom", mode); + else + return real_fopen(path, mode); +} + diff --git a/nodevrandom/nodevrandom.h b/nodevrandom/nodevrandom.h new file mode 100644 index 0000000..8b4325d --- /dev/null +++ b/nodevrandom/nodevrandom.h @@ -0,0 +1,16 @@ +/****************************************************************************** + * nodevrandom.h + *****************************************************************************/ + +#ifndef _NODEVRANDOM_H_ +#define _NODEVRANDOM_H_ + +typedef int (*real_open_t) (const char *, int, ...); + +typedef FILE *(*real_fopen_t) (const char *, const char *); + +static void lib_init(void) __attribute__((constructor)); + +#endif /* _NODEVRANDOM_H_ */ + + diff --git a/nodevrandom/nodevrandom_test b/nodevrandom/nodevrandom_test new file mode 100755 index 0000000..d5f1476 --- /dev/null +++ b/nodevrandom/nodevrandom_test @@ -0,0 +1,12 @@ +#!/bin/mksh + +if [ $# = 0 ] ; then + echo " usage:" + echo " nodevrandom [args]" + exit +fi + +LD_PRELOAD="${LD_PRELOAD} ./libnodevrandom.so" +export LD_PRELOAD +exec "$@" + diff --git a/setrlimit/Makefile b/setrlimit/Makefile new file mode 100644 index 0000000..a93c8b1 --- /dev/null +++ b/setrlimit/Makefile @@ -0,0 +1,26 @@ + +CC=gcc +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -ansi +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=setrlimit + +LIBS= +OBJS=setrlimit.o + +$(NAME): $(OBJS) $(LIBS) + $(CC) $(CFLAGS) -o $@ $^ + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o $(NAME) core + diff --git a/setrlimit/setrlimit.c b/setrlimit/setrlimit.c new file mode 100644 index 0000000..6af972f --- /dev/null +++ b/setrlimit/setrlimit.c @@ -0,0 +1,201 @@ +/****************************************************************************** + * setrlimit.c: execute program in with restricted resources + * Written by hondza. Public domain. + *****************************************************************************/ + +#define _BSD_SOURCE + +#include +#include +#include +#include + +#include + +#include +#include + + +#define SET_SOFT 1 +#define SET_HARD 2 + +static int both = 0; +static int relaxed = 0; + + +void help() +{ + fputs("Usage: setrlimit [options] -- /path/to/executable [arguments]\n", stderr); + fputs("\n(lower case sets soft limit, UPPER case hard limit)\n\n", stderr); + fputs("Options:\n", stderr); + fputs("-n , -N \tNOFILE\t\tmax file descriptors+1\n", stderr); + fputs("-p , -P \tNPROC\t\tmax number of processes\n", stderr); + fputs("-u , -U \tNPROC\t\tmax number of processes\n", stderr); + fputs("-c , -C \tCORE\t\tmax core file size (bytes)\n", stderr); + fputs("-f , -F \tFSIZE\t\tmax size of files written\n", stderr); + fputs("-i , -I \tSIGPENDING\tmax pending signals\n", stderr); + fputs("-s , -S \tSTACK\t\tmax stack size (bytes)\n", stderr); + fputs("-t , -T \tCPU\t\tmax cpu usage (seconds)\n", stderr); + fputs("-v , -V \tAS\t\tmax address space (bytes)\n\n", stderr); + fputs("-h\tthis help\n", stderr); + fputs("-r\trelaxed ({get,set}rlimit() mail fail)\n", stderr); + fputs("-b\tset both soft and HARD limit\n", stderr); + fputs("-x\tclose all file descriptors before execv()\n\n", stderr); +} /* help() */ + + +void do_set(const int resource, const char *arg, int opt) +{ + struct rlimit lim; + int err; + unsigned long l; + + opt = both ? (SET_SOFT | SET_HARD) : (opt); + + /* If I'm setting both, I don't need to know current ones */ + if(!both) + { + err = getrlimit(resource, &lim); + if(-1 == err) + { + if(!relaxed) + { + perror("getrlimit failed"); + exit(1); + } + else + { + /* FIXME */ + lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; + } + } + } + + l = strtoul(arg, NULL, 10); + + if(opt & SET_SOFT) lim.rlim_cur = l; + if(opt & SET_HARD) lim.rlim_max = l; + + err = setrlimit(resource, &lim); + if(-1 == err && !relaxed) + { + perror("setrlimit failed"); + exit(1); + } +} /* do_set() */ + + + +int main(int argc, char ** argv) +{ + int c, i, doclose=0; + + if(argc < 2) + { + help(); + exit(1); + } + + while( (c = getopt(argc, argv, "n:N:p:P:c:C:f:F:i:I:s:S:t:T:u:U:v:V:rbhx")) != -1 ) + { + switch(c) + { + case 'b': + both = 1; + break; + + case 'r': + relaxed = 1; + break; + + case 'x': + doclose = 1; + break; + + case 'h': + help(); + exit(0); + break; + + case 'n': + do_set(RLIMIT_NOFILE, optarg, SET_SOFT); + break; + case 'N': + do_set(RLIMIT_NOFILE, optarg, SET_HARD); + break; + + case 'p': case 'u': + do_set(RLIMIT_NPROC, optarg, SET_SOFT); + break; + case 'P': case 'U': + do_set(RLIMIT_NPROC, optarg, SET_HARD); + break; + + case 'c': + do_set(RLIMIT_CORE, optarg, SET_SOFT); + break; + case 'C': + do_set(RLIMIT_CORE, optarg, SET_HARD); + break; + + case 'f': + do_set(RLIMIT_FSIZE, optarg, SET_SOFT); + break; + case 'F': + do_set(RLIMIT_FSIZE, optarg, SET_HARD); + break; + + case 'i': + do_set(RLIMIT_SIGPENDING, optarg, SET_SOFT); + break; + case 'I': + do_set(RLIMIT_SIGPENDING, optarg, SET_HARD); + break; + + case 's': + do_set(RLIMIT_STACK, optarg, SET_SOFT); + break; + case 'S': + do_set(RLIMIT_STACK, optarg, SET_HARD); + break; + + case 't': + do_set(RLIMIT_CPU, optarg, SET_SOFT); + break; + case 'T': + do_set(RLIMIT_CPU, optarg, SET_HARD); + break; + + case 'v': + do_set(RLIMIT_AS, optarg, SET_SOFT); + break; + case 'V': + do_set(RLIMIT_AS, optarg, SET_HARD); + break; + + + default: help(); exit(1); + } /* switch argument */ + } /* while getopt() */ + + if(doclose) + for ((i = getdtablesize()); i >= 0 ; i--) close(i); + + if(optind < argc) + { + c = execv(argv[optind], argv+optind); + if(!doclose && -1 == c) + { + perror("execv() failed"); + } + exit(1); + } + else if(!doclose) + { + fputs("FATAL: nothing to exec!\n", stderr); + exit(1); + } + + return 0; +} /* main() */ + diff --git a/sha256sum/Makefile b/sha256sum/Makefile new file mode 100644 index 0000000..4fce2be --- /dev/null +++ b/sha256sum/Makefile @@ -0,0 +1,33 @@ + +CC=gcc +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -ansi +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=s2 + +LIBS=-ltomcrypt +OBJS=sha256sum.o crc32.o + +ifdef NEED_GETLINE + CFLAGS += -DNEED_GETLINE + OBJS += getline.o +endif + + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + + +clean: + $(RM) *.o $(NAME) core + diff --git a/sha256sum/Makefile.diet b/sha256sum/Makefile.diet new file mode 100644 index 0000000..c83eb75 --- /dev/null +++ b/sha256sum/Makefile.diet @@ -0,0 +1,29 @@ + +CC=diet gcc +STRIP=strip +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -Wno-long-long +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=s2_diet + +.PHONY: clean + +LIBS=-ltomcrypt -lcompat +OBJS=sha256sum.o crc32.o + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o $(NAME) core + diff --git a/sha256sum/Makefile.mingw32 b/sha256sum/Makefile.mingw32 new file mode 100644 index 0000000..be2a67e --- /dev/null +++ b/sha256sum/Makefile.mingw32 @@ -0,0 +1,30 @@ + +CC=i586-mingw32msvc-gcc +STRIP=i586-mingw32msvc-strip +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -Wno-long-long -I/usr/src/tomcrypt-win32/src/headers -DNEED_GETLINE +CFLAGS_OPTI=-O3 -fomit-frame-pointer -fno-strength-reduce -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=s2.exe + +.PHONY: clean + +LIBS=/usr/src/tomcrypt-win32/libtomcrypt.a +OBJS=sha256sum.o getline.o crc32.o + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) + $(STRIP) $(NAME) + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o $(NAME) core + diff --git a/sha256sum/crc32.c b/sha256sum/crc32.c new file mode 120000 index 0000000..04c90a9 --- /dev/null +++ b/sha256sum/crc32.c @@ -0,0 +1 @@ +../crc32/crc32.c \ No newline at end of file diff --git a/sha256sum/crc32.h b/sha256sum/crc32.h new file mode 120000 index 0000000..34a9bbc --- /dev/null +++ b/sha256sum/crc32.h @@ -0,0 +1 @@ +../crc32/crc32.h \ No newline at end of file diff --git a/sha256sum/getline.c b/sha256sum/getline.c new file mode 120000 index 0000000..aedf368 --- /dev/null +++ b/sha256sum/getline.c @@ -0,0 +1 @@ +../getline/getline.c \ No newline at end of file diff --git a/sha256sum/getline.h b/sha256sum/getline.h new file mode 120000 index 0000000..9936848 --- /dev/null +++ b/sha256sum/getline.h @@ -0,0 +1 @@ +../getline/getline.h \ No newline at end of file diff --git a/sha256sum/sha256sum.c b/sha256sum/sha256sum.c new file mode 100644 index 0000000..7146698 --- /dev/null +++ b/sha256sum/sha256sum.c @@ -0,0 +1,637 @@ +/****************************************************************************** + * {sha256,sha1,md5,crc32}sum, depends on argv[0] (default is sha256sum). + * Uses libtomcrypt (public domain) and dietlibc's getline() ... GLPv2. + * Due to GPLv2 virus it is GPLv2. + *****************************************************************************/ + +#define _FILE_OFFSET_BITS 64 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include + +#include /* solaris wants it */ + +#include + +#include "crc32.h" + + +#ifdef WIN32 +#define FOPEN_MODE "rb" +#define BUFSIZE_CONST +#else +#define FOPEN_MODE "r" +#include +#endif /* WIN32 */ + +#ifdef NEED_GETLINE +#include "getline.h" +#endif /* NEED_GETLINE */ + + +#define DEFAULT_BL 4096 +#define DEFAULT_LINE_LEN 4096 + +#define MAKE_CHECKSUM_ERR_OK 0 +#define MAKE_CHECKSUM_ERR_OPEN 1 +#define MAKE_CHECKSUM_ERR_READ 3 +#define MAKE_CHECKSUM_ERR_MEM 4 + +#define MD5 1 +#define SHA1 2 +#define SHA256 4 +#define CRC32 8 +#define HASH_ALL (MD5|SHA1|SHA256|CRC32) + + +#define RELEASE "r2" + + +static const unsigned char hexchar[] = "0123456789abcdef"; + + +/* long time ago there was a number of options :) */ +struct t_options { + int hashes; + int check_selected; +} opt; + +static int verbose=0; +static int comments=1; + +struct hash_out { + char *filename; + int hashes; + crc32_t crc32; + hash_state md5; + hash_state sha1; + hash_state sha256; + off_t written; + unsigned char md5_o[16]; + unsigned char sha1_o[20]; + unsigned char sha256_o[32]; + unsigned char crc32_o[4]; +}; + + + + +static void bin2hex(unsigned char *out, unsigned char *in, size_t size) +{ + for(; size; size--, in++) + { + *out = hexchar[((*in)>>4)]; out++; + *out = hexchar[((*in)&15)]; out++; + } + *out = '\0'; +} /* bin2hex() */ + + + +static void hex2bin(unsigned char *out, unsigned char *in, size_t size) +{ + for(; size; size-=2, in+=2, out++) + { + *out = ((((*in) >= 'a' && (*in) <= 'f') ? + ( (*in) - 'a' + 10 ) + : ( ((*in) >= 'A' && (*in) <= 'F') + ? ((*in) - 'A' + 10) + : ((*in) - '0'))) << 4) + + + ((((*(in+1)) >= 'a' && (*(in+1)) <= 'f') ? + ( (*(in+1)) - 'a' + 10 ) + : ( ((*(in+1)) >= 'A' && (*(in+1)) <= 'F') + ? ((*(in+1)) - 'A' + 10) + : ((*(in+1)) - '0'))) & 15); + } +} /* hex2bin() */ + + + +static int is_hex_string(char *in, size_t len) +{ + for(; len; len--, in++) + if(!isxdigit(*in)) return 0; + return 1; +} /* is_hex_string() */ + + + + +static size_t getbufsize(FILE *f) +{ +#ifdef BUFSIZE_CONST + return DEFAULT_BL; +#else + struct stat st; + int err; + err = fstat(fileno(f), &st); + if(err == -1 || !st.st_blksize) return DEFAULT_BL; + return st.st_blksize; +#endif +} /* getbufsize() */ + + + + +static int mark_filename_end(char *in) +{ + int i; + for(i=0; *in != '\0' && *in != '\n' && *in != '\r'; in++, i++) + ; + *in = '\0'; + return i; +} /* mark_filename_end() */ + + + + +static int mark_hash_end(char *in, char *c) +{ + int i; + for(i=0; isxdigit(*in); in++, i++) + ; + *c = *in; + *in = '\0'; + return i; +} /* mark_hash_end() */ + + + + +static int make_checksum(struct hash_out *out) +{ + size_t err, blks; + unsigned char *data; + FILE *f; + + if(!(out->filename)) f = stdin; + else + { + f = fopen(out->filename, FOPEN_MODE); + if(!f) return MAKE_CHECKSUM_ERR_OPEN; + } + + blks = getbufsize(f); + data = malloc(blks); + if(!data) + { + fclose(f); + return MAKE_CHECKSUM_ERR_MEM; + } + + /* init hashes */ + if(out->hashes & CRC32) crc32_init(&(out->crc32)); + if(out->hashes & MD5) md5_init(&(out->md5)); + if(out->hashes & SHA1) sha1_init(&(out->sha1)); + if(out->hashes & SHA256) sha256_init(&(out->sha256)); + + while(1) + { + err = fread(data, 1, blks, f); + /* process */ + if(err > 0) + { + out->written += err; + if(out->hashes & CRC32) crc32_update(&(out->crc32), data, err); + if(out->hashes & MD5) md5_process(&(out->md5), data, err); + if(out->hashes & SHA1) sha1_process(&(out->sha1), data, err); + if(out->hashes & SHA256) sha256_process(&(out->sha256), data, err); + } + else if(feof(f)) break; + else + { + if(out->hashes & CRC32) crc32_final(&(out->crc32), out->crc32_o); + if(out->hashes & MD5) md5_done(&(out->md5), out->md5_o); + if(out->hashes & SHA1) sha1_done(&(out->sha1), out->sha1_o); + if(out->hashes & SHA256) sha256_done(&(out->sha256), out->sha256_o); + fclose(f); + memset(data, 0, blks); free(data); + return MAKE_CHECKSUM_ERR_READ; + } + } /* while 1 */ + + fclose(f); + memset(data, 0, blks); free(data); + if(out->hashes & CRC32) crc32_final(&(out->crc32), out->crc32_o); + if(out->hashes & MD5) md5_done(&(out->md5), out->md5_o); + if(out->hashes & SHA1) sha1_done(&(out->sha1), out->sha1_o); + if(out->hashes & SHA256) sha256_done(&(out->sha256), out->sha256_o); + + return MAKE_CHECKSUM_ERR_OK; +} /* make_checksum() */ + + + + +void print_hashes(struct hash_out *in) +{ + unsigned char hex_form[65]; + unsigned char *bin_form; + + hex_form[64] = '0'; + + #define print_hash(a,b) do { \ + bin2hex(hex_form, bin_form, (a)); \ + if(comments) printf("# R %s %llu %*s %s\n", (b), in->written, 2*(a), hex_form, in->filename ? in->filename : "-"); \ + printf("%*s %s\n", 2*(a), hex_form, in->filename ? in->filename : "-"); \ + } while(0) + + if(opt.hashes & CRC32) + { + bin_form = in->crc32_o; + print_hash(sizeof(in->crc32_o), "CRC32"); + } + if(opt.hashes & MD5) + { + bin_form = in->md5_o; + print_hash(sizeof(in->md5_o), "MD5"); + } + if(opt.hashes & SHA1) + { + bin_form = in->sha1_o; + print_hash(sizeof(in->sha1_o), "SHA1"); + } + if(opt.hashes & SHA256) + { + bin_form = in->sha256_o; + print_hash(sizeof(in->sha256_o), "SHA256"); + } + } /* print_hash() */ + + + + +/* depends on "char *filename" */ +#define handle_make_checksum_err(x) do { \ + if((x) != MAKE_CHECKSUM_ERR_OK) \ + { \ + switch((x)) \ + { \ + case MAKE_CHECKSUM_ERR_OPEN: \ + fprintf(stderr, "fopen(\"%s\") failed: %s\n", filename, \ + strerror(errno)); \ + return 2; \ + case MAKE_CHECKSUM_ERR_MEM: \ + fprintf(stderr, "malloc() failed, file: '%s'\n", \ + filename); \ + return 2; \ + case MAKE_CHECKSUM_ERR_READ: \ + fprintf(stderr, "fread(\"%s\") failed: %s\n", filename, \ + strerror(errno)); \ + return 2; \ + } /* switch err */ \ + } \ + } while(0) + + + +int do_hash(char *filename) +{ + int err; + struct hash_out out; + unsigned char hex_form[64]; + + hex_form[64] = '\0'; + + memset(&out, 0, sizeof(out)); + out.hashes = opt.hashes; + out.filename = filename; + + err = make_checksum(&out); + handle_make_checksum_err(err); + + print_hashes(&out); + + return 0; +} /* do_hash() */ + + +#define check_failed(x, y) do { printf("%s: FAILED (%s)\n", ((x) ? (x) : "(stdin)"), (y)); } while(0) +#define check_ok(x, y) do { if(verbose) printf("%s: OK (%s)\n", ((x) ? (x) : "(stdin)"), (y)); } while(0) + + +int check_file(struct hash_out *what, struct hash_out *against) +{ + char *filename = what->filename; + int err, retval=0; + + err = make_checksum(what); + handle_make_checksum_err(err); + + if(what->hashes & CRC32) + { + if((memcmp(what->crc32_o, against->crc32_o, sizeof(what->crc32_o))) != 0) { check_failed(what->filename, "CRC32"); retval = 1; } + else check_ok(what->filename, "CRC32"); + } + if(what->hashes & MD5) + { + if((memcmp(what->md5_o, against->md5_o, sizeof(what->md5_o))) != 0) { check_failed(what->filename, "MD5"); retval = 1; } + else check_ok(what->filename, "MD5"); + } + if(what->hashes & SHA1) + { + if((memcmp(what->sha1_o, against->sha1_o, sizeof(what->sha1_o))) != 0) { check_failed(what->filename, "SHA1"); retval = 1; } + else check_ok(what->filename, "SHA1"); + } + if(what->hashes & SHA256) + { + if((memcmp(what->sha256_o, against->sha256_o, sizeof(what->sha256_o))) != 0) { check_failed(what->filename, "SHA256"); retval = 1; } + else check_ok(what->filename, "SHA256"); + } + + return retval; +} /* check_file() */ + + + + +int do_hash_check(char *in_filename) +{ + FILE *f; + char *temp_line, *filename; + char sep; + int retval=0, current_hash=0; + ssize_t err2; + size_t line_len = DEFAULT_LINE_LEN, lineno, hex_len, filename_len; + struct hash_out temp_out, to_compare; + + memset(&temp_out, 0, sizeof(temp_out)); + memset(&to_compare, 0, sizeof(to_compare)); + + if(!in_filename) f = stdin; + else + { + f = fopen(in_filename, FOPEN_MODE); + if(!f) + { + fprintf(stderr, "fopen(\"%s\") failed: %s\n", in_filename, + strerror(errno)); + return 2; + } + } + + temp_line = malloc(DEFAULT_LINE_LEN); + if(!temp_line) + { + fprintf(stderr, "malloc() failed, file: '%s'\n", in_filename ? in_filename : "(stdin)"); + if(f != stdin) fclose(f); + return 2; + } + + + for(lineno=1; ;lineno++) + { + hex_len=0; + filename=NULL; + + err2 = getline(&temp_line, &line_len, f); + if(err2 < 1) + { + /* we may have a pending check to do */ + if(temp_out.hashes) + { + /* make checksum */ + retval |= check_file(&temp_out, &to_compare); + /* clean */ + if(temp_out.filename) free(temp_out.filename); + memset(&temp_out, 0, sizeof(temp_out)); + memset(&to_compare, 0, sizeof(to_compare)); + } + + if(!feof(f)) retval |= 2; + if(temp_line) { memset(temp_line, 0, line_len); free(temp_line); } + fclose(f); + return retval; + } + + /* skip comment */ + if(*temp_line == '#' || *temp_line == ';') continue; + + if(!(filename = strstr(temp_line, " "))) filename = strstr(temp_line, " *"); + + if(!filename) + { + hex_len = mark_hash_end(temp_line, &sep); + /* skip malformed line */ + if(!(sep == '\n' || sep == '\r' || sep == '\0')) + { + if(verbose) fprintf(stderr, "malformed (sep) line %d in file: '%s'\n", lineno, in_filename ? in_filename : "(stdin)"); + continue; + } + } + else + { + *filename='\0'; + hex_len = filename - temp_line; + if(!is_hex_string(temp_line, hex_len)) + { + if(verbose) fprintf(stderr, "malformed (!is_hex_string) line %d in file: '%s'\n", lineno, in_filename ? in_filename : "(stdin)"); + continue; + } + } + + switch(hex_len) + { + case 8: current_hash = CRC32; break; + case 32: current_hash = MD5; break; + case 40: current_hash = SHA1; break; + case 64: current_hash = SHA256; break; + default: + { + if(verbose) fprintf(stderr, "malformed (hex_len) line %d in file: '%s'\n", lineno, in_filename ? in_filename : "(stdin)"); + continue; + } + } + + /* skip on '-C' and non-selected hash */ + if(opt.check_selected && !(current_hash & opt.hashes)) continue; + + if(filename) + { + filename += 2; + filename_len = mark_filename_end(filename); + /* XXX: perhaps allow it as stdin? */ + if(!filename_len) + { + if(verbose) fprintf(stderr, "malformed (nothing_after_sep) line %d in file: '%s'\n", lineno, in_filename ? in_filename : "(stdin)"); + } + else if(filename_len == 1 && *filename == '-') filename = NULL; + } + + +#define modify_to_compare() do { \ + switch(current_hash) \ + { \ + case CRC32: hex2bin(to_compare.crc32_o, (unsigned char *)temp_line, hex_len); break; \ + case MD5: hex2bin(to_compare.md5_o, (unsigned char *)temp_line, hex_len); break; \ + case SHA1: hex2bin(to_compare.sha1_o, (unsigned char *)temp_line, hex_len); break; \ + case SHA256: hex2bin(to_compare.sha256_o, (unsigned char *)temp_line, hex_len); break; \ + } \ + } while(0) + + + +#define allocate_filename() do { \ + if(filename) \ + { \ + temp_out.filename = to_compare.filename = strdup(filename); \ + if(!temp_out.filename) \ + { \ + perror("strdup() failed"); \ + if(f != stdin) fclose(f); \ + return 2; \ + } \ + } \ + } while(0) + + + /* first */ + if(!temp_out.hashes) + { + allocate_filename(); + temp_out.hashes = to_compare.hashes = current_hash; + modify_to_compare(); + } /* !temp_out.hashes */ + else /* not first */ + { + /* now I need to compare filenames */ + if( (!filename && !temp_out.filename) || (filename && temp_out.filename && (strcmp(filename, temp_out.filename) == 0)) ) + { + /* same files, just bitwise OR the hash */ + temp_out.hashes |= current_hash; + to_compare.hashes = temp_out.hashes; + modify_to_compare(); + } + else /* different files => make checksums & re-init temp_out */ + { + /* make checksum */ + retval |= check_file(&temp_out, &to_compare); + + /* clean */ + if(temp_out.filename) free(temp_out.filename); + memset(&temp_out, 0, sizeof(temp_out)); + memset(&to_compare, 0, sizeof(to_compare)); + + /* re-init */ + allocate_filename(); + temp_out.hashes = to_compare.hashes = current_hash; + modify_to_compare(); + } + } /* not first */ + + } /* for lineno */ + + return retval; +} /* do_hash_check() */ + + + + + +static void help() +{ + fputs("{sha256,sha1,md5,crc32}sum: generates or verifies checksums (message digests)\n", stderr); + fputs("Usage: {sha256,sha1,md5,crc32}sum [-01235CachqvV] [file...] \n\n", stderr); + fputs("Options:\n" + "\t-c\t\tcheck mode\n" + "\t-C\t\tcheck mode (check against selected hashes)\n" + "\t-0\t\tZero previous hashes\n" + "\t-1\t\tSHA1\n" + "\t-2\t\tSHA256\n" + "\t-3\t\tCRC32\n" + "\t-5\t\tMD5\n" + "\t-a\t\tall hashes\n" + "\t-m \t\tselected hashes\n" + "\t-h\t\thelp\n" + "\t-v\t\tverbose\n" + "\t-q\t\tcreate comments\n" + "\t-V\t\tversion\n" + "\n", stderr); +} /* help() */ + + + + +static void version() +{ + fprintf(stderr, "s2-%s\n\n", RELEASE); +} /* version() */ + + + + +/* samotny programek */ +int main(int argc, char ** argv) +{ + int check = 0, retval = 0, c, temp; + + opt.hashes = SHA256; + opt.check_selected = 0; + + if( (strstr(argv[0], "s1")) || (strstr(argv[0], "sha1sum")) ) opt.hashes = SHA1; + else if( (strstr(argv[0], "s2")) || (strstr(argv[0], "sha256sum")) ) opt.hashes = SHA256; + else if( (strstr(argv[0], "md5")) ) opt.hashes = MD5; + else if( (strstr(argv[0], "crc32")) ) opt.hashes = CRC32; + else if( (strstr(argv[0], "md")) ) opt.hashes = HASH_ALL; + else opt.hashes = 0; + + while( (c = getopt(argc, argv, "ahvVcCq01235m:")) != -1 ) + { + switch(c) + { + case 'c': check = 1; break; + case 'q': comments = 0; break; + case 'C': check = opt.check_selected = 1; break; + case 'v': verbose=1; break; + case 'V': version(); exit(0); + case 'h': help(); exit(0); + case 'a': opt.hashes = HASH_ALL; break; + case '0': opt.hashes = 0; break; + case '2': opt.hashes |= SHA256; break; + case '1': opt.hashes |= SHA1; break; + case '3': opt.hashes |= CRC32; break; + case '5': opt.hashes |= MD5; break; + case 'm': temp = strtoul(optarg, NULL, 10); if((temp & HASH_ALL)) opt.hashes = temp; break; + default: help(opt.hashes); exit(1); + } /* switch argument */ + } /* while getopt() */ + + if(!(opt.hashes & HASH_ALL)) opt.hashes = SHA256; + + if(optind < argc) + { + if(check) + { + while(optind < argc) + retval |= (do_hash_check(argv[optind++])); + } /* if check */ + else + { + while(optind < argc) + retval |= (do_hash(argv[optind++])); + } /* not check */ + + return retval; + } + else + { + if(check) { retval = do_hash_check(NULL); } + else + { retval = (do_hash(NULL)); } + + return retval; + } /* no file => stdin */ + + + /* + * 0 .. OK, 1 .. bad checksum, 2 .. other error (fopen(), ...) + * bitwise ORed + */ + return retval; +} /* main() */ + diff --git a/sparse/Makefile b/sparse/Makefile new file mode 100644 index 0000000..db42a4f --- /dev/null +++ b/sparse/Makefile @@ -0,0 +1,26 @@ + +CC=gcc +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -ansi +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=sparse + +LIBS= +OBJS=sparse.o + +$(NAME): $(OBJS) $(LIBS) + $(CC) $(CFLAGS) -o $@ $^ + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o $(NAME) core + diff --git a/sparse/sparse.c b/sparse/sparse.c new file mode 100644 index 0000000..a7d342f --- /dev/null +++ b/sparse/sparse.c @@ -0,0 +1,104 @@ +/****************************************************************************** + * sparse.c + *****************************************************************************/ + +#define _FILE_OFFSET_BITS 64 +#define _GNU_SOURCE + +#include +#include +#include + +#include + +#define THRESHOLD 1023 + +void help() +{ + fputs("sparse: create a sparse file from stdin\n\n", stderr); + fputs("Usage: sparse [-v] [-c threshold] \n\n", stderr); +} /* help() */ + + +int main(int argc, char ** argv) +{ + int c, i, verbose=0; + long offset=0; + unsigned long threshold = THRESHOLD, seeks=0; + FILE *fo; + + while( (c = getopt(argc, argv, "vhc:")) != -1 ) + { + switch(c) + { + case 'c': + /* 0 is acceptable */ + threshold = strtoul(optarg, NULL, 10); + break; + + case 'v': + verbose = 1; + break; + + case 'h': + help(); + return 0; + + default: + help(); + return 1; + } + } + + + if(optind >= argc) + { + help(); + return 1; + } + + fo = fopen(argv[optind], "w"); + if(!fo) + { + perror("fopen() failed"); + return 1; + } + + while((c = getc(stdin)) != EOF) + { + if(0 == c) offset++; + else + { + if(offset > threshold) + { + if(verbose) fprintf(stderr, "seek: offset CUR + %ld\n", offset); + seeks++; + fseek(fo, offset, SEEK_CUR); + offset = 0; + } + else if(offset > 0) + { + for(i=0; i 0) + { + if(verbose) fprintf(stderr, "seek: offset CUR + %ld\n", offset-1); + seeks++; + fseek(fo, offset-1, SEEK_CUR); + putc(0, fo); + } + + fclose(fo); + + if(verbose) fprintf(stderr, "total seeks: %lu\n", seeks); + + return 0; +} /* main() */ + diff --git a/tos/Makefile b/tos/Makefile new file mode 100644 index 0000000..12ac0e0 --- /dev/null +++ b/tos/Makefile @@ -0,0 +1,28 @@ + +CC=gcc +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -ansi +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=libtos.so + +LIBS=-ldl +OBJS=tos.o + + +$(NAME): $(OBJS) $(LIBS) + $(CC) -fPIC -shared $(CFLAGS) -o $@ $^ + +%.o: %.c %.h + $(CC) -c -fPIC -shared $(CFLAGS) -o $@ $< + + +clean: + $(RM) *.o $(NAME) core + diff --git a/tos/tos b/tos/tos new file mode 100755 index 0000000..cee4d52 --- /dev/null +++ b/tos/tos @@ -0,0 +1,12 @@ +#!/bin/mksh + +if [ $# = 0 ] ; then + echo " usage:" + echo " tos [args]" + exit +fi + +LD_PRELOAD="${LD_PRELOAD} /usr/local/lib/libtos.so" +export LD_PRELOAD +exec "$@" + diff --git a/tos/tos.c b/tos/tos.c new file mode 100644 index 0000000..7b4fe8c --- /dev/null +++ b/tos/tos.c @@ -0,0 +1,68 @@ +/****************************************************************************** + * tos.c + *****************************************************************************/ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +#include "tos.h" + + +real_socket_t real_socket; +real_setsockopt_t real_setsockopt; +int default_tos = 0x18; + + + +void lib_init(void) +{ + char *temp; + + *(void **) (&real_socket) = dlsym(RTLD_NEXT, "socket"); + if(!real_socket) + { + fprintf(stderr, "Cannot load symbol 'socket' %s\n", dlerror()); + _exit(1); + } + + *(void **) (&real_setsockopt) = dlsym(RTLD_NEXT, "setsockopt"); + if(!real_setsockopt) + { + fprintf(stderr, "Cannot load symbol 'setsockopt' %s\n", dlerror()); + _exit(1); + } + + temp = getenv("TOS"); + if(temp) default_tos = (int) strtoul(temp, NULL, 10); +} + + + +int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen) +{ + if(level == SOL_IP && optname == IP_TOS) return 0; + return real_setsockopt(s, level, optname, optval, optlen); +} + + + +int socket(int domain, int type, int protocol) +{ + int err2, tos = default_tos; + int err = real_socket(domain, type, protocol); + + if(domain != PF_INET || err == -1) return err; + + err2 = real_setsockopt(err, SOL_IP, IP_TOS, (void *) &tos, sizeof (int)); + if(err2 == -1) { perror("setsockopt(... IP_TOS ...)"); exit(1); } + + return err; +} + + diff --git a/tos/tos.h b/tos/tos.h new file mode 100644 index 0000000..c4da7b6 --- /dev/null +++ b/tos/tos.h @@ -0,0 +1,17 @@ +/****************************************************************************** + * tos.h + *****************************************************************************/ + +#ifndef _TOS_H_ +#define _TOS_H_ + +typedef int (*real_setsockopt_t) (int, int, int, const void *, socklen_t ); + +typedef int (*real_socket_t) (int, int, int); + +void lib_init(void) __attribute__((constructor)); + + +#endif /* _TOS_H_ */ + + diff --git a/xtma/Makefile b/xtma/Makefile new file mode 100644 index 0000000..42af9b8 --- /dev/null +++ b/xtma/Makefile @@ -0,0 +1,26 @@ + +CC=gcc +RM=rm -f +CFLAGS_COMMON=-Wall -pedantic -ansi +CFLAGS_OPTI=-O3 -fomit-frame-pointer -ffast-math +CFLAGS_DEBUG=-ggdb3 +ifeq "$(ARG)" "opti" + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_OPTI) +else + export CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG) +endif + +NAME=xtma + +LIBS=-lX11 +OBJS=xtma.o + +$(NAME): $(OBJS) $(LIBS) + $(CC) $(CFLAGS) -o $@ $^ + +%.o: %.c %.h + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o $(NAME) core + diff --git a/xtma/xtma.c b/xtma/xtma.c new file mode 100644 index 0000000..c44410a --- /dev/null +++ b/xtma/xtma.c @@ -0,0 +1,89 @@ +/****************************************************************************** + * xtma.c + *****************************************************************************/ + +#include +#include +#include +#include + +#include +#include + + +int main(int argc, char ** argv) +{ + Display *dpy; + int screen; + int dispw; + int disph; + Window win; + int run = 1; + XEvent xev; + Atom wm_state, fs; + Pixmap bm_no; + Colormap cmap; + Cursor no_ptr; + XColor black, dummy; + static char bm_no_data[] = {0, 0, 0, 0, 0, 0, 0, 0}; + + close(0); close(1); close(2); + + while(1) + { + dpy = XOpenDisplay(""); + if(NULL != dpy) break; + sleep(1); + } + + screen = DefaultScreen(dpy); + dispw = XDisplayWidth(dpy, screen); + disph = XDisplayHeight(dpy, screen); + + win = XCreateSimpleWindow(dpy, RootWindow(dpy, screen), 0, 0, 1, 1, 0, WhitePixel(dpy, screen), BlackPixel(dpy, screen)); + + /* disable the cursor: http://www.linuxforums.org/forum/linux-programming-scripting/59012-xlib-hide-mouse-pointer.html */ + cmap = DefaultColormap(dpy, screen); + XAllocNamedColor(dpy, cmap, "black", &black, &dummy); + bm_no = XCreateBitmapFromData(dpy, win, bm_no_data, 8, 8); + no_ptr = XCreatePixmapCursor(dpy, bm_no, bm_no, &black, &black, 0, 0); + XDefineCursor(dpy, win, no_ptr); + + /* name */ + XStoreName(dpy, win, "xtma"); + + /* show the window */ + XMapRaised(dpy, win); + + /* listen for mouse and kbd */ + XSelectInput(dpy, win, KeyPressMask | ButtonPressMask); + + /* fullscreen */ + wm_state = XInternAtom(dpy, "_NET_WM_STATE", True); + fs = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", True); + memset(&xev, 0, sizeof(xev)); + xev.type = ClientMessage; + xev.xclient.serial = 0; + xev.xclient.send_event=True; + xev.xclient.window = win; + xev.xclient.message_type = wm_state; + xev.xclient.format = 32; + xev.xclient.data.l[0] = 1; + xev.xclient.data.l[1] = fs; + xev.xclient.data.l[2] = 0; + XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev); + XFlush(dpy); + + + while(run) + { + XNextEvent(dpy, &xev); + if(ButtonPress == xev.type || (KeyPress == xev.type && XK_Escape == (XLookupKeysym((XKeyEvent *)&xev, 0)))) run = 0; + } + + + XCloseDisplay(dpy); + + return 0; +} /* main() */ + -- 2.11.4.GIT