-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
242 lines (228 loc) · 5.76 KB
/
main.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <magic.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>
#include "debug.h"
/*
* Ugly, passing over the head of things.
*/
static long pid_max = -1;
static magic_t magic_cookie;
static struct dirent *parent = NULL;
static int proc_filter(const struct dirent *dirent)
{
char buf[256], path[256] = "/proc/", *endptr = NULL;
ssize_t read_ret;
long result;
int fd;
result = strtol(dirent->d_name, &endptr, 10);
if (endptr == dirent->d_name || result < 0 || result > pid_max)
return 0;
strcat(path, dirent->d_name);
strcat(path, "/cmdline");
if ((fd = open(path, O_RDONLY)) < 0) {
dprintf("Open failed for %s.\n", path);
return 0;
}
read_ret = read(fd, buf, sizeof(buf));
/* Very bad. read_ret == 0 needs some kind of retry. */
if (read_ret < 0) {
dprintf("Hard read failure for %s\n", path);
return 0;
} else if (!read_ret) {
/* It's oddly typical to have /proc/$PID/cmdline empty. */
if (0)
dprintf("Transient read failure or empty cmdline for %s\n", path);
return 0;
}
if (!strstr(buf, "plugin"))
return 0;
return 1;
}
static int fd_filter(const struct dirent *dirent)
{
char path[256] = "/proc/", *endptr = NULL;
const char *magic;
long result;
result = strtol(dirent->d_name, &endptr, 10);
if (endptr == dirent->d_name || result < 0 || result > INT_MAX)
return 0;
strcat(path, parent->d_name);
strcat(path, "/fd/");
strcat(path, dirent->d_name);
if (!(magic = magic_file(magic_cookie, path))) {
dprintf("Unable to determine magic on %s\n", path);
return 0;
}
return !strcmp(magic, "ISO Media, MPEG v4 system, version 2");
}
int get_pid_max(void)
{
/* vastly in excess of what's known to reside in-kernel */
char buf[256], *endptr = NULL;
ssize_t read_ret;
int fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
if (fd < 0) {
dprintf("open() failed.\n");
return -1;
}
read_ret = read(fd, buf, sizeof(buf));
if (read_ret < 0) {
dprintf("Hard read() failure.\n");
return -1;
} else if (!read_ret && errno != EAGAIN) {
dprintf("Transient read() failure.\n");
return -1;
} else if (read_ret >= (signed)sizeof(buf)) {
dprintf("Integer too high of precision for buffer.\n");
return -1;
}
pid_max = strtol(buf, &endptr, 10);
if (endptr == buf || pid_max <= 0 || errno) {
dprintf("Malformatted numeral, endptr - buf = %zd.\n",
endptr - buf);
dprintf("read_ret = %zd\n", read_ret);
dprintf("stat_buf.st_size = %ld\n", stat_buf.st_size);
dprintf("buf = \"%s\"\n", buf);
pid_max = -1;
return -1;
}
return 0;
}
static int copy_data(const char *src, const char *dst)
{
int in_fd, out_fd, size_same = 0, ret = EX_OK;
char *tmp, *buf = NULL;
size_t buf_size = 0, size_diff, left;
struct stat in_stat, out_stat;
if ((in_fd = open(src, O_RDONLY)) < 0) {
ret = EX_UNAVAILABLE;
goto out;
}
if ((out_fd = open(dst, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP)) < 0) {
ret = EX_UNAVAILABLE;
goto out;
}
while (size_same <= 2) {
struct timespec req = { .tv_sec = 1, .tv_nsec = 0 };
if (fstat(out_fd, &out_stat))
return EX_OSERR;
if (fstat(in_fd, &in_stat))
return EX_OSERR;
if (in_stat.st_size == out_stat.st_size) {
size_same++;
goto sleep;
}
size_same = 0;
if (out_stat.st_size > in_stat.st_size) {
sleep:
nanosleep(&req, NULL);
continue;
}
if (lseek(in_fd, out_stat.st_size, SEEK_SET) != out_stat.st_size) {
ret = EX_OSERR;
goto out_free;
}
if (lseek(out_fd, out_stat.st_size, SEEK_SET) != out_stat.st_size) {
ret = EX_OSERR;
goto out_free;
}
size_diff = in_stat.st_size - out_stat.st_size;
if (buf_size < size_diff) {
if (!(tmp = realloc(buf, size_diff))) {
ret = EX_OSERR;
goto out_free;
}
buf = tmp;
buf_size = size_diff;
}
left = size_diff;
while (left > 0) {
ssize_t read_ret, write_ret;
size_t write_left;
read_ret = read(in_fd, buf, left);
if (read_ret < 0) {
ret = EX_OSERR;
goto out_free;
} else if (!read_ret) {
if (errno && errno != EAGAIN) {
ret = EX_OSERR;
goto out_free;
}
goto sleep;
}
write_left = read_ret;
while (write_left > 0) {
write_ret = write(out_fd, buf, write_left);
if (write_ret < 0) {
ret = EX_OSERR;
goto out_free;
} else if (!write_ret) {
if (errno && errno != EAGAIN) {
ret = EX_OSERR;
goto out_free;
}
goto sleep;
}
write_left -= write_ret;
}
left -= read_ret;
}
}
out_free:
free(buf);
out:
return ret;
}
int main(int argc, char *argv[])
{
int i, nr_dirs, ret = EX_OK;
struct dirent **proclist = NULL;
if (argc != 2)
return EX_USAGE;
if (get_pid_max())
return EX_OSERR;
if (!(magic_cookie = magic_open(MAGIC_SYMLINK)))
return EX_OSFILE;
if (magic_load(magic_cookie, "/usr/share/misc/magic.mgc:/etc/magic"))
return EX_OSFILE;
if ((nr_dirs = scandir("/proc/", &proclist, proc_filter, NULL)) < 0)
return EX_OSFILE;
dprintf("nr_dirs = %d\n", nr_dirs);
if (nr_dirs > 1)
dprintf("Non-unique match for plugin process!\n");
for (i = 0; i < nr_dirs; ++i) {
struct dirent **fdlist = NULL;
char path[256] = "/proc/";
int nr_fds, j;
parent = proclist[i];
dprintf("proclist[%d] = %s\n", i, proclist[i]->d_name);
strcat(path, proclist[i]->d_name);
strcat(path, "/fd");
if ((nr_fds = scandir(path, &fdlist, fd_filter, NULL)) < 0) {
dprintf("scandir failure on %s\n", path);
return EX_OSFILE;
}
for (j = 0; j < nr_fds; ++j)
dprintf("%s->fd[%d] = %s\n", proclist[i]->d_name,
j, fdlist[j]->d_name);
strcat(path, "/");
strcat(path, fdlist[0]->d_name);
if (nr_fds == 1)
ret = copy_data(path, argv[1]);
else {
dprintf("Non-unique match for MPEG fd!\n");
return EX_OSFILE;
}
}
return ret;
}