-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathmisc.cpp
More file actions
195 lines (164 loc) · 4.66 KB
/
Copy pathmisc.cpp
File metadata and controls
195 lines (164 loc) · 4.66 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <sys/types.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <zconf.h>
#include <dirent.h>
#include <fcntl.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <sched.h>
#include <cctype>
#include "misc.h"
ssize_t fdgets(char *buf, const size_t size, int fd) {
ssize_t len = 0;
buf[0] = '\0';
while (len < size - 1) {
ssize_t ret = read(fd, buf + len, 1);
if (ret < 0)
return -1;
if (ret == 0)
break;
if (buf[len] == '\0' || buf[len++] == '\n') {
buf[len] = '\0';
break;
}
}
buf[len] = '\0';
buf[size - 1] = '\0';
return len;
}
int get_proc_name(int pid, char *name, size_t _size) {
int fd;
ssize_t __size;
char buf[1024];
snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
if (access(buf, R_OK) == -1 || (fd = open(buf, O_RDONLY)) == -1)
return 1;
if ((__size = fdgets(buf, sizeof(buf), fd)) == 0) {
snprintf(buf, sizeof(buf), "/proc/%d/comm", pid);
close(fd);
if (access(buf, R_OK) == -1 || (fd = open(buf, O_RDONLY)) == -1)
return 1;
__size = fdgets(buf, sizeof(buf), fd);
}
close(fd);
if (__size < _size) {
strncpy(name, buf, static_cast<size_t>(__size));
name[__size] = '\0';
} else {
strncpy(name, buf, _size);
name[_size] = '\0';
}
return 0;
}
int is_num(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; ++i)
if (s[i] < '0' || s[i] > '9')
return 0;
return 1;
}
int copyfileat(int src_path_fd, const char *src_path, int dst_path_fd, const char *dst_path) {
int src_fd;
int dst_fd;
struct stat stat_buf{};
int64_t size_remaining;
size_t count;
ssize_t result;
if ((src_fd = openat(src_path_fd, src_path, O_RDONLY)) == -1)
return -1;
if (fstat(src_fd, &stat_buf) == -1)
return -1;
dst_fd = openat(dst_path_fd, dst_path, O_WRONLY | O_CREAT | O_TRUNC, stat_buf.st_mode);
if (dst_fd == -1) {
close(src_fd);
return -1;
}
size_remaining = stat_buf.st_size;
for (;;) {
if (size_remaining > 0x7ffff000)
count = 0x7ffff000;
else
count = static_cast<size_t>(size_remaining);
result = sendfile(dst_fd, src_fd, nullptr, count);
if (result == -1) {
close(src_fd);
close(dst_fd);
unlink(dst_path);
return -1;
}
size_remaining -= result;
if (size_remaining == 0) {
close(src_fd);
close(dst_fd);
return 0;
}
}
}
int copyfile(const char *src_path, const char *dst_path) {
return copyfileat(0, src_path, 0, dst_path);
}
uintptr_t memsearch(const uintptr_t start, const uintptr_t end, const void *value, size_t size) {
uintptr_t _start = start;
while (true) {
if (_start + size >= end)
return 0;
if (memcmp((const void *) _start, value, size) == 0)
return _start;
_start += 1;
}
}
int switch_mnt_ns(int pid) {
char mnt[32];
snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);
if (access(mnt, R_OK) == -1) return -1;
int fd = open(mnt, O_RDONLY);
if (fd < 0) return -1;
int res = setns(fd, 0);
close(fd);
return res;
}
void foreach_proc(foreach_proc_function *func) {
DIR *dir;
struct dirent *entry;
if (!(dir = opendir("/proc")))
return;
while ((entry = readdir(dir))) {
if (entry->d_type != DT_DIR) continue;
if (!is_num(entry->d_name)) continue;
pid_t pid = atoi(entry->d_name);
func(pid);
}
closedir(dir);
}
char *trim(char *str) {
size_t len = 0;
char *frontp = str;
char *endp = nullptr;
if (str == nullptr) { return nullptr; }
if (str[0] == '\0') { return str; }
len = strlen(str);
endp = str + len;
/* Move the front and back pointers to address the first non-whitespace
* characters from each end.
*/
while (isspace((unsigned char) *frontp)) { ++frontp; }
if (endp != frontp) {
while (isspace((unsigned char) *(--endp)) && endp != frontp) {}
}
if (str + len - 1 != endp)
*(endp + 1) = '\0';
else if (frontp != str && endp == frontp)
*str = '\0';
/* Shift the string so that it starts at str so that if it's dynamically
* allocated, we can still free it on the returned pointer. Note the reuse
* of endp to mean the front of the string buffer now.
*/
endp = str;
if (frontp != str) {
while (*frontp) { *endp++ = *frontp++; }
*endp = '\0';
}
return str;
}