-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabloid-user.c
54 lines (45 loc) · 1.11 KB
/
tabloid-user.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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define MY_MAGIC 'G'
#define CMD_SET_PID _IOW(MY_MAGIC, 0, int)
#define BUFFER_SIZE 0x1000
int main(int argc, char *argv[]) {
int fd = open("/proc/foo", O_RDONLY);
if (fd == -1) {
perror("open");
return EXIT_FAILURE;
}
if (argc == 2) {
int pid = atoi(argv[1]);
int res = ioctl(fd, CMD_SET_PID, &pid);
if (res == -1) {
perror("ioctl");
close(fd);
return EXIT_FAILURE;
}
} else if (argc > 2) {
fprintf(stderr, "Usage: %s [<pid>]\n", argv[0]);
close(fd);
return EXIT_FAILURE;
}
char buffer[BUFFER_SIZE];
ssize_t bytes_read;
while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {
write(1, buffer, bytes_read);
}
if (bytes_read == -1) {
perror("read");
close(fd);
return EXIT_FAILURE;
}
close(fd);
return EXIT_SUCCESS;
}