Skip to content

Instantly share code, notes, and snippets.

@jserv
jserv / vwifi.c
Created August 6, 2021 11:58
virtual cfg80211 driver
#include <linux/module.h>
#include <linux/skbuff.h>
#include <net/cfg80211.h>
#include <linux/mutex.h>
#include <linux/workqueue.h>
#define WIPHY_NAME "owl" /* Our WireLess */
#define NDEV_NAME WIPHY_NAME "%d"
@jserv
jserv / simrupt.c
Created August 6, 2021 11:48
A device that simulates interrupts
/* simrupt: A device that simulates interrupts */
#include <linux/cdev.h>
#include <linux/circ_buf.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kfifo.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <inttypes.h>
#include <sys/time.h>
enum {
EV_READ = (1 << 0),
EV_WRITE = (1 << 1),
EV_TIMEOUT_ONESHOT = (1 << 2),
EV_TIMEOUT_PERIODIC = (1 << 3),
EV_SIGNAL = (1 << 4),
EV_CLOEXEC = (1 << 0),
/* Simple port forwarder */
#define _GNU_SOURCE 1
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <semaphore.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
static void test() { puts(__func__); }
int main() {
typedef void (*callback_t)(void);
callback_t cb = &test;
(*cb)();
}
@jserv
jserv / naive-sum.c
Last active September 1, 2019 06:48
#include <stdio.h>
#define NUM 10000
int main() {
float sum = 0.0f;
for (int i = 0; i < NUM; i++) sum += i + 1;
printf("Sum: %f\n", sum);
return 0;
}
@jserv
jserv / pyc.py
Last active March 13, 2021 11:27
import atexit
import ctypes
import os
import shlex
import sys
import tempfile
CMD_C_TO_SO = '{compiler} -shared -o {output} {input} {libraries}'
@jserv
jserv / exec_with_gdb.sh
Created April 26, 2017 19:37
GDB runner
# Execute an executable under gdb, printing a call stack if we get a crash.
gdb -return-child-result -quiet -batch \
-ex "set env MALLOC_CHECK_=3" \
-ex "set print thread-events off" \
-ex run \
-ex "thread apply all backtrace full" \
-ex "quit" \
$*
@jserv
jserv / string.c
Created October 8, 2013 15:46
The missing string.c
#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#define ALIGN (sizeof(size_t))
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
#define SS (sizeof(size_t))