Skip to content

Instantly share code, notes, and snippets.

@ZeWaren
Created August 5, 2015 06:53
Show Gist options
  • Save ZeWaren/fd1a86642cf04e216449 to your computer and use it in GitHub Desktop.
Save ZeWaren/fd1a86642cf04e216449 to your computer and use it in GitHub Desktop.
FreeBSD kernel module that unlock an unlinked fifo's locked threads
#include <sys/types.h>
#include <sys/module.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/capability.h>
int link_vnode_at(struct thread *td, struct vnode *vp, char *path);
void mysql_fifo_test_main_thread(void *p);
static struct mtx main_thread_mutex;
static struct thread *main_thread;
void mysql_fifo_test_main_thread(void *p) {
struct vop_vector *f;
vop_open_t *fifo_open;
vop_close_t *fifo_close;
mtx_lock(&main_thread_mutex);
printf("MySQL FIFO Test In da thread.\n");
/* Get a pointer to the original open args */
struct vop_open_args *ap;
ap = (struct vop_open_args *) 0xfffffe0095f17678;
/* Get pointers to the fifo ops functions */
f = &fifo_specops;
fifo_open = f->vop_open;
fifo_close = f->vop_close;
/* Call fifo_open */
struct file some_file;
struct vop_open_args ap_open;
ap_open.a_vp = ap->a_vp;
ap_open.a_mode = FWRITE;
ap_open.a_cred = NULL; //unused in fifo_open
ap_open.a_fp = &some_file;
ap_open.a_td = main_thread;
fifo_open(&ap_open);
/* Call fifo_close */
struct vop_close_args ap_close;
ap_close.a_vp = ap->a_vp;
ap_close.a_fflag = FWRITE;
ap_close.a_cred = NULL; //unused in fifo_close
ap_close.a_td = main_thread;
fifo_close(&ap_close);
mtx_unlock(&main_thread_mutex);
kthread_exit();
}
static void
mysql_fifo_test_load() {
mtx_init(&main_thread_mutex, "mysql_fifo_test_main_thread_mutex", NULL, MTX_DEF);
mtx_lock(&main_thread_mutex);
kthread_add(mysql_fifo_test_main_thread, NULL, NULL, &main_thread, 0, 0, "mysql_fifo_test_main_thread");
mtx_unlock(&main_thread_mutex);
}
static void
mysql_fifo_test_unload() {
mtx_lock(&main_thread_mutex);
mtx_destroy(&main_thread_mutex);
}
static int
mysql_fifo_test_loader(struct module *m, int what, void *arg)
{
int err = 0;
switch (what) {
case MOD_LOAD:
mysql_fifo_test_load();
uprintf("MySQL FIFO Test loaded.\n");
break;
case MOD_UNLOAD:
mysql_fifo_test_unload();
uprintf("MySQL FIFO Test unloaded.\n");
break;
default:
err = EOPNOTSUPP;
break;
}
return(err);
}
static moduledata_t mysql_fifo_test_mod = {
"mysql_fifo_test",
mysql_fifo_test_loader,
NULL
};
DECLARE_MODULE(mysql_fifo_test, mysql_fifo_test_mod, SI_SUB_KLD, SI_ORDER_ANY);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment