-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.c
More file actions
136 lines (109 loc) · 3.16 KB
/
Process.c
File metadata and controls
136 lines (109 loc) · 3.16 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#define _GNU_SOURCE
#include "Process.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
static int is_pid_dir(const char *name) {
if (!name || *name == '\0') {
return 0;
}
for (const unsigned char *p = (const unsigned char *)name; *p != '\0'; p++) {
if (!isdigit(*p)) {
return 0;
}
}
return 1;
}
Process* process_create(const char *name) {
Process *proc = (Process *)malloc(sizeof(Process));
if (!proc) {
perror("Failed to allocate memory for Process");
return NULL;
}
strncpy(proc->name, name, sizeof(proc->name) - 1);
proc->name[sizeof(proc->name) - 1] = '\0';
proc->pid = -1;
DIR *dir;
struct dirent *ent;
char buffer[512];
if ((dir = opendir("/proc")) == NULL) {
perror("Failed to open /proc");
free(proc);
return NULL;
}
while ((ent = readdir(dir)) != NULL) {
if (!is_pid_dir(ent->d_name))
continue;
snprintf(buffer, sizeof(buffer), "/proc/%s/comm", ent->d_name);
FILE *fp = fopen(buffer, "r");
if (fp) {
if (fgets(buffer, sizeof(buffer), fp)) {
buffer[strcspn(buffer, "\n")] = '\0';
if (strcmp(buffer, name) == 0) {
char *end = NULL;
long pid = strtol(ent->d_name, &end, 10);
if (end && *end == '\0' && pid > 0 && pid <= INT_MAX) {
proc->pid = (pid_t)pid;
}
fclose(fp);
break;
}
}
fclose(fp);
}
}
closedir(dir);
if (proc->pid == -1) {
free(proc);
proc = NULL;
}
return proc;
}
void process_destroy(Process *proc) {
if (proc) {
free(proc);
}
}
pid_t process_get_pid(const Process *proc) {
return proc ? proc->pid : -1;
}
void process_set_pid(Process *proc, pid_t pid) {
if (proc != NULL) {
proc->pid = pid;
}
}
const char* process_get_name(const Process *proc) {
return proc ? proc->name : NULL;
}
void process_set_name(Process *proc, const char *name) {
if (proc && name) {
strncpy(proc->name, name, sizeof(proc->name) - 1);
proc->name[sizeof(proc->name) - 1] = '\0';
}
}
ssize_t process_read_memory(const Process *proc, unsigned long addr, void *buf, size_t size) {
if (!proc) return -1;
struct iovec local[1];
struct iovec remote[1];
local[0].iov_base = buf;
local[0].iov_len = size;
remote[0].iov_base = (void *)addr;
remote[0].iov_len = size;
ssize_t nread = process_vm_readv(proc->pid, local, 1, remote, 1, 0);
return nread;
}
ssize_t process_write_memory(const Process *proc, unsigned long addr, const void *buf, size_t size) {
if (!proc) return -1;
struct iovec local[1];
struct iovec remote[1];
local[0].iov_base = (void *)buf;
local[0].iov_len = size;
remote[0].iov_base = (void *)addr;
remote[0].iov_len = size;
ssize_t nwritten = process_vm_writev(proc->pid, local, 1, remote, 1, 0);
return nwritten;
}