Skip to content

Instantly share code, notes, and snippets.

@Low-power
Created April 22, 2020 09:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Low-power/90190a0d28f7ff685e9773c0879666ca to your computer and use it in GitHub Desktop.
Save Low-power/90190a0d28f7ff685e9773c0879666ca to your computer and use it in GitHub Desktop.
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/libc.so.1"
#ifndef __SVR4
#warning "You may need to define macro LIBC_PATH"
#endif
#endif
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) {
perror("dlopen");
abort();
}
*(void **)&f = dlsym(libc, "tcsetattr");
if(!f) {
perror("dlsym: tcsetattr");
abort();
}
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