aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2024-04-15 19:38:05 +0300
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2024-04-15 19:38:05 +0300
commit0be46c952901aaafe76e1fb5a8faecb38323d549 (patch)
tree9ec9e7a2c99bd5cd81e17f1578a7be725558f88e
parentInitial commit (diff)
downloadlinux-game-trainer-0be46c952901aaafe76e1fb5a8faecb38323d549.tar.gz
linux-game-trainer-0be46c952901aaafe76e1fb5a8faecb38323d549.zip
Attaching & Detaching from process
-rw-r--r--.gitignore3
-rw-r--r--Makefile13
-rw-r--r--src/main.c34
3 files changed, 49 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..bc22e3a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
1*~
2*.out
3*.o \ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..844cc7b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
1CC = gcc
2CFLAGS = -Wall -g
3OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
4OUT = hack.out
5
6$(OUT): $(OBJS)
7 $(CC) $(CFLAGS) -o $@ $(OBJS)
8
9%.o: %.c
10 $(CC) $(CFLAGS) -c $? -o $@
11
12clean:
13 rm -f src/*.o $(OUT)
diff --git a/src/main.c b/src/main.c
index b54dbf1..1d23902 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,6 +1,38 @@
1#include <stdlib.h>
1#include <stdio.h> 2#include <stdio.h>
3#include <sys/ptrace.h>
4#include <sys/wait.h>
5#include <unistd.h>
6
7#define ERROR(...) \
8 do { \
9 fprintf(stderr, __VA_ARGS__); \
10 exit(1); \
11 } while (0)
12
13#define LOG(...) \
14 fprintf(stderr, __VA_ARGS__)
2 15
3int main(int argc, char *argv[]) 16int main(int argc, char *argv[])
4{ 17{
5 18 if (argc < 2) {
19 ERROR("Usage: %s <tracee_pid>\n", argv[0]);
20 }
21
22 int pid;
23 if ((pid = atoi(argv[1])) == 0) {
24 ERROR("Invalid pid '%s'\n", argv[1]);
25 }
26
27 ptrace(PTRACE_ATTACH, pid, NULL, NULL);
28 waitpid(pid, NULL, __WALL);
29 LOG("Attached to process %d\n", pid);
30
31 /* Do stuff ... */
32 sleep(5);
33
34 ptrace(PTRACE_DETACH, pid, NULL, NULL);
35 LOG("Detached from process %d\n", pid);
36
37 return 0;
6} 38}