Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
LD_PRELOAD program used to prevent terminals from being write-protected by foreground process.
/* This file has no copyright assigned and is placed in the Public Domain.
* No warranty is given.
#include <termios.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef LIBC_PATH
// Use SysV path by default
#define LIBC_PATH "/usr/lib/"
#ifndef __SVR4
#warning "You may need to define macro LIBC_PATH"
static void *get_libc() {
static void *libc;
if(!libc) libc = dlopen(LIBC_PATH, RTLD_LAZY);
return libc;
static int (*get_libc_tcsetattr())(int, int, const struct termios *) {
static int (*f)(int, int, const struct termios *);
if(f) return f;
void *libc = get_libc();
if(!libc) {
*(void **)&f = dlsym(libc, "tcsetattr");
if(!f) {
perror("dlsym: tcsetattr");
return f;
int tcsetattr(int fd, int action, const struct termios *attr) {
if(attr->c_lflag & TOSTOP) {
struct termios new_attr = *attr;
new_attr.c_lflag &= ~TOSTOP;
return get_libc_tcsetattr()(fd, action, &new_attr);
return get_libc_tcsetattr()(fd, action, attr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment