Skip to content

Instantly share code, notes, and snippets.

View larytet's full-sized avatar

Arkady Miasnikov larytet

View GitHub Profile
@larytet
larytet / gist:21fb14d5a71f2870d56d
Last active August 29, 2015 14:23
GreaseMonkey script for Yad2: Update the ads. If you have posted lot of ads this script is for you. Also opens all posted ads for review and editing.
// ==UserScript==
// @name Yad2 Ad update
// @namespace yad2
// @description Simulates click on "kaftor3"
// @include http://my.yad2.co.il/MyYad2/MyOrder/Yad2.php
// ==/UserScript==
// Use chromium-browser --enable-easy-off-store-extension-install to install offline
// in Chrome
/*
@larytet
larytet / test_aio.c
Last active October 12, 2019 02:39
Linux AIO example
/**
* Based on https://www.fsl.cs.sunysb.edu/~vass/linux-aio.txt
* Compile gcc -Wall test_aio.c -o test_aio
* Run rm -rf /tmp/testfile ;./iotest
*/
#define _GNU_SOURCE /* syscall() is not POSIX */
#include <stdio.h> /* for perror() */
#include <unistd.h> /* for syscall() */
@larytet
larytet / get_exe_name.c
Last active November 11, 2023 02:33
Get absolute path of the executable in the Linux kernel, context of do_exec() kprobe/SystemTap
static char* get_exe_name(int pid, char *buffer, int buffer_size)
{
char *result = NULL;
struct mm_struct* mm = current->mm;
if (mm)
{
int lock_result = down_read_trylock(&mm->mmap_sem);
if ((lock_result) && (mm->exe_file))
{
@larytet
larytet / define_symbol_usage.stp
Created June 16, 2017 20:03
Define symbol in STAP
@define cc1 %( "/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/cc1" %)
global insns
probe perf.hw.instructions.process(@cc1).counter("foo") {}
probe process(@cc1).function("*").return {
insns[execname(),tid()] = @perf("foo") // implicit max()
}
@larytet
larytet / get_inode.c
Last active December 10, 2021 02:39
Get inode and file given FD in the Linux kernel
static void get_inode(u64 fd, struct file **p_file, struct inode **p_inode)
{
struct file *file = NULL;
struct inode *f_inode = NULL;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(3,7,0))
file = fget_raw(fd); // do not forget to fput(file)
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) && (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,255))
{
struct files_struct *files = current->files;
if (files) // This is always the case, but I am not taking chances
@larytet
larytet / git_commit_sha.mk
Last active June 5, 2018 08:17
Makefile rule generating a C file with GIT commit
GIT_COMMIT_FILE=git_commit_filename.h
$(GIT_COMMIT_FILE): phony
$(eval GIT_COMMIT_SHA=$(shell git describe --abbrev=6 --always 2>/dev/null || echo 'Error'))
@echo SHA=$(GIT_COMMIT_SHA)
echo -n "static const char *GIT_COMMIT_SHA = \"$(GIT_COMMIT_SHA)\";" > $(GIT_COMMIT_FILE)
@larytet
larytet / lockfree_hashtable.c
Last active September 13, 2023 14:58
A simple hashtable for Linux kernel drivers
/**
* Lockfree is a set of lockfree containers for Linux and Linux kernel
* Copyright (C) <2017> Arkady Miasnikov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
@larytet
larytet / SystemTap_tests.sh
Last active July 7, 2017 16:43
Tight echo loop
#!/bin/bash
function echo_loop()
{
file=echo_file_`date +%s%N`
echo $file
echo > $file
counter=1
end=$((SECONDS+10))
while [ $SECONDS -lt $end ]; do
stap -D MAXSKIPPED=0 -D MAXTRYLOCK=1000000 -D TRYLOCKDELAY=1 -g -e 'global ar%; function w_ar() {ar[tid()]=tid();} %{long long counter;u8 shm[256];static void* w_shm(void);static void* w_shm() {memset(shm, 0, sizeof(shm));return shm;} %} probe syscall.close{w_ar();%{ {counter++;w_shm();} %}} probe syscall.close.return {w_ar();%{ {counter++;w_shm();} %}} probe syscall.open{w_ar();%{ {counter++;w_shm();} %}} probe syscall.open.return{w_ar();%{ {counter++;w_shm();} %}} probe syscall.dup2.return{w_ar();%{ {counter++;w_shm();} %}} probe syscall.dup2.return{w_ar();%{ {counter++;w_shm();} %}} probe syscall.read.return{w_ar();%{ {counter++;w_shm();} %}} probe syscall.read{w_ar();%{ {counter++;w_shm();} %}} probe end { %{ {printk("\n%lli\n", counter);} %}}'
stap -D MAXSKIPPED=0 -D MAXTRYLOCK=1000000 -D TRYLOCKDELAY=1 -g -e '%{long long counter;u8 shm[256];static void* w_shm(void);static void* w_shm() {memset(shm, 0, sizeof(shm));return shm;} %} probe syscall.close{%{ {counter++;w_shm();} %}} probe syscall.close
@larytet
larytet / exec.stp
Created July 7, 2017 03:17
SystemTap exec probes
probe kprocess.exec
{
%{HIT_MAP_INC(HIT_MAP_KPROCESS_EXEC)%}
tid = tid()
if (stringat(filename,0) == 0x22) // filename starts with a quotation mark
{
MAP_SYSCALL_EXEC_NAME[tid] = filename
MAP_SYSCALL_EXEC_ARGV[tid] = args
}
else // failed to recog the filename, trigger do_execve