-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibstfu.c
More file actions
94 lines (78 loc) · 1.91 KB
/
libstfu.c
File metadata and controls
94 lines (78 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <fcntl.h>
const char* SELF_EXE = "/proc/self/exe";
const char* ENV_NAME = "STFU_EXE";
// In large parts inspired by https://stackoverflow.com/a/24582217/2058753
char * getSelfExecutableName() {
size_t linkname_size = 1024;
ssize_t r;
char * linkname;
// Try reading the process name into a buffer of increasing size
while(1) {
linkname = malloc(linkname_size + 1);
if (linkname == NULL) {
fprintf(stderr, "Insufficient memory\n");
exit(EXIT_FAILURE);
}
r = readlink(SELF_EXE, linkname, linkname_size);
if(r < 0) {
perror("readlink");
exit(EXIT_FAILURE);
}
if (r < linkname_size) {
break;
}
free(linkname);
linkname_size *= 2;
}
char *tmp;
tmp = realloc(linkname, r + 1);
if (tmp) {
linkname = tmp;
linkname_size = r + 1;
}
linkname[r] = '\0';
return linkname;
}
void suppress() {
// Open /dev/null
int devNull = open("/dev/null", O_RDWR);
if(devNull < 0) {
perror("Failed to open /dev/null");
return;
}
// Redirect stdin, stdout and stderr to /dev/null
for (int i = 1; i < 3; ++i) {
dup2(devNull, i);
}
}
__attribute__((constructor)) static void initialize() {
// Get the name of the executable that is starting
char * linkname = getSelfExecutableName();
char * executableName = basename(linkname);
// Get the name of the executable we want to silence
char * suppressed = getenv(ENV_NAME);
if(strcmp(executableName, suppressed) == 0) {
suppress();
} else {
char * suppressedListBuf = malloc(strlen(suppressed) + 1);
strcpy(suppressedListBuf, suppressed);
char * token = strtok(suppressedListBuf, ":");
if(strcmp(executableName, token) == 0) {
suppress();
}
while(token = strtok(NULL, ":")) {
if(strcmp(executableName, token) == 0) {
suppress();
break;
}
}
}
free(linkname);
}