Skip to content

Instantly share code, notes, and snippets.

@baruch
baruch / defer.h
Created May 27, 2018 19:09
Defer cleanup for C (gcc and llvm)
#define DEFER_MERGE(a,b) a##b
#define DEFER_VARNAME(a) DEFER_MERGE(defer_scopevar_, a)
#define DEFER_FUNCNAME(a) DEFER_MERGE(defer_scopefunc_, a)
#define DEFER(BLOCK) void DEFER_FUNCNAME(__LINE__)(int *a) BLOCK; __attribute__((cleanup(DEFER_FUNCNAME(__LINE__)))) int DEFER_VARNAME(__LINE__)
// Usage:
/*
void dosomething()
{
char* data = malloc(100);
@baruch
baruch / scsi_sense.stp
Last active April 21, 2018 20:12
Get SCSI Sense buffers in Linux with systemtap
probe module("scsi_mod").function("scsi_command_normalize_sense") {
printf("CDB:");
for (i = 0; i < $cmd->cmd_len; i++) {
printf(" %02X", $cmd->cmnd[i]);
}
printf(" | SENSE:");
for (i = 0; i < 32; i++) {
printf(" %02X", $cmd->sense_buffer[i]);
}
printf("\n");
@baruch
baruch / pi-nrf24le1.sh
Last active April 28, 2022 12:11
Setup Raspberry Pi for nRF24LE1 development
# Install sdcc to build nRF24LE1 code
# Install python-dev and python-setuptools to build python code for hacking
# Install minicom for UART communication
# Install screen to avoid network disconnect interruptions
sudo apt-get update && sudo apt-get install sdcc python-dev python-setuptools minicom screen
sudo apt-get dist-upgrade
# Install bcm2835 library
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.38.tar.gz && tar xvf bcm2835/bcm2835-1.38.tar.gz && cd bcm2835-1.38 && ./configure && make && sudo make install
cd ~
@baruch
baruch / gist:106a3da12187b284830e
Created July 20, 2014 06:12
Optimal tar compression
find $d -type f | rev | sort | rev | tar -c -T /dev/stdin -f - | pv -pterb -B 10m -s $(du -ms $d | cut -f1)m -W -i 10 | bzip2 -6 > $d.tar.bz2;
@baruch
baruch / scsi_sense.stp
Created May 15, 2014 19:08
Get full SCSI Sense out of any command failure
probe module("scsi_mod").function("scsi_command_normalize_sense") {
printf("CDB:");
for (i = 0; i < $cmd->cmd_len; i++) {
printf(" %02X", $cmd->cmnd[i]);
}
printf(" | SENSE:");
for (i = 0; i < 32; i++) {
printf(" %02X", $cmd->sense_buffer[i]);
}
printf("\n");
@baruch
baruch / gist:8584442
Created January 23, 2014 18:47
ReQL: For a field get in one query the count, min, max and sample entries
r.db('mydb').table('disk').withFields('glist').groupedMapReduce(
function(x) { return x },
function(x) { return 1 },
function(x,y) { return 1 }
)
.map(function (x) { return x('group')('glist') })
.reduce(function(x,y) { return
{
count: x('count').add(1),
val: x('val').union([y]).sample(10),
@baruch
baruch / libev_async_dns.c
Created May 5, 2013 19:02
Asynchronous dns resolving with libev and the gethostbyname_r function.
#include <ev.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <netdb.h>
#include <memory.h>
typedef struct net_client_t {
ev_signal resolv_watcher;
char hostname[64];