Skip to content

Commit

Permalink
Add signal handing to interposer script
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-u committed Nov 15, 2022
1 parent cd8f0c7 commit 023b3b5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
58 changes: 30 additions & 28 deletions bin/shortcut/sc_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "sc_lib.h"
#include <dlfcn.h>
#include <stdio.h>
#include <signal.h>

// TODO: Maybe move this to makefile -I?
#include "../../../Symlib/include/LINF/sym_all.h"
Expand All @@ -19,34 +20,31 @@ extern char **environ;
// Function that prints string in red
void print_red(const char *str) { printf("\033[1;31m%s\033[0m", str); }

#if 0
void print_sc_envt_vars(){
// Produce array of environment variables
for (size_t i = 0; environ[i] != NULL; i++) {
// ELEVATE
if (strncmp(environ[i], "ELEVATE", 7) == 0) {
// Print the environment variable, skipping the first 8 chars and removing the last 2 chars
printf("%s\n", environ[i]);
// printf("%s\n", environ[i] + 8);
// If the environment variable suffix is 'read=1' set the read fn
// if(strncmp(environ[i] + 8, "read=1", 6) == 0){
// printf("Need to elevate %s\n", environ[i] + 8);
// //rd_ctrl.do_shortcut = true;

// }
}
// LOWER
if (strncmp(environ[i], "LOWER", 5) == 0) {
printf("%s\n", environ[i]);
}
// SHORTCUT
if (strncmp(environ[i], "SHORTCUT", 8) == 0) {
printf("%s\n", environ[i]);
}
}
// Signal handler for SIGUSR1
void sigusr1_handler(int signum) {
printf("Received SIGUSR1\n");
printf("lowering now\n");
sym_lower();
}
// Signal handler for SIGUSR2
void sigusr2_handler(int signum) {
printf("Received SIGUSR2\n");
printf("elevating now\n");
sym_elevate();
}

// Signal handler for SIGSYS
void sigsys_handler(int signum) {
printf("Received SIGSYS\n");

// Print if shortcut is on or off
if (master_toggle_shortcut) {
printf("Shortcut is on, turning off\n");
} else {
printf("Shortcut is off, turning on\n");
}
master_toggle_shortcut = !master_toggle_shortcut;
}
#endif

// Function that initializes a fn_ctrl struct
void init_fn_ctrl(struct fn_ctrl *ctrl) {
Expand All @@ -73,6 +71,11 @@ void __attribute__((constructor)) init(void) {
// for debugging
// print_sc_envt_vars();

// Register signal handlers
signal(SIGUSR1, sigusr1_handler);
signal(SIGUSR2, sigusr2_handler);
signal(SIGSYS, sigsys_handler);

// If environment variable 'BEGIN_ELE=1' is set, elevate
if (envt_var_exists("BEGIN_ELE=1")) {
printf("Elevating\n");
Expand Down Expand Up @@ -272,5 +275,4 @@ void egress_work(struct fn_ctrl *ctrl) {

// This macro allocates a "real_" fn ptr, a "ksys_" fn ptr, and a fn_ctrl struct
// Then it implements the relevant interposer fn.
MAKE_STRUCTS_AND_FN(write, ssize_t, int, fd, const void *, buf, size_t, count)
MAKE_STRUCTS_AND_FN(read, ssize_t, int, fd, void *, buf, size_t, count)
MAKE_STRUCTS_AND_FN(write, ssize_t, int, fd, const void *, buf, size_t, count)
6 changes: 5 additions & 1 deletion bin/shortcut/sc_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct fn_ctrl {
// If real_write is null, get it from dlsym
// Place to do one time work
// Configs ctrl struct with envt variables, gets ptrs to real and shortcut

int hacky_ctr = 0;
#define MAKE_INTERPOSE_FN(fn_name, ret_t, t_1, arg_1, t_2, arg_2, t_3, arg_3 ) \
ret_t fn_name ( t_1 arg_1, t_2 arg_2, t_3 arg_3 ) { \
if (! real_##fn_name ) { \
Expand All @@ -43,7 +43,11 @@ struct fn_ctrl {
ingress_work(&fn_name##_ctrl); \
int ret; \
if (fn_name##_ctrl.do_shortcut ^ master_toggle_shortcut) { \
if( (hacky_ctr++ % 20) == 0) { \
ret = real_##fn_name( arg_1, arg_2, arg_3 ); \
} else { \
ret = ksys_##fn_name( arg_1, arg_2, arg_3 ); \
} \
} else { \
ret = real_##fn_name( arg_1, arg_2, arg_3 ); \
} \
Expand Down

0 comments on commit 023b3b5

Please sign in to comment.