Skip to content

Commit 26bf7c6

Browse files
committed
Initial commit
0 parents  commit 26bf7c6

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed

Diff for: LICENSE

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright 2020 Rao Zvorovski
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10+
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Diff for: Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
all: test/a test/b test/c libstfu.so
2+
3+
test/a: test/a.c
4+
gcc -o test/a test/a.c
5+
test/b: test/b.c
6+
gcc -o test/b test/b.c
7+
test/c: test/c.c
8+
gcc -o test/c test/c.c
9+
10+
libstfu.so: libstfu.c
11+
gcc -shared -fPIC -o libstfu.so libstfu.c
12+
13+
.PHONY: clean
14+
clean:
15+
rm test/a test/b test/c libstfu.so

Diff for: README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# libstfu
2+
3+
This is a small LD\_PRELOAD library that will direct stdin, stdout and stderr to /dev/null for specified executables by name.
4+
5+
## Usage
6+
7+
Example usage (assuming libstfu.so is in LD\_LIBRARY\_PATH, otherwise must give a path to libstfu.so):
8+
```
9+
STFU_EXE=someloudprogram LD_PRELOAD=libstfu.so someprogramthatcallssomeloudprogram
10+
```
11+
12+
Multiple targets can be specified by separating with colons:
13+
```
14+
STFU_EXE=someloudprogram:anotherloudprogram LD_PRELOAD=libstfu.so someprogramthatcallsthem
15+
```
16+
17+
## Contributing
18+
Pull requests are welcome.
19+
20+
## License
21+
Libstfu is licensed under the 3-Clause BSD License.

Diff for: libstfu.c

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

Diff for: test/a.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
puts("a");
5+
}

Diff for: test/b.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
puts("b");
5+
}

Diff for: test/c.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main() {
5+
puts("c");
6+
system("test/a");
7+
system("test/b");
8+
}

0 commit comments

Comments
 (0)