File: sysctl.c

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (102 lines) | stat: -rw-r--r-- 1,514 bytes parent folder | download | duplicates (2)
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
#include <fcntl.h>

#include "zdtmtst.h"
#include "sysctl.h"

int sysctl_read_str(const char *name, char *data, size_t size)
{
	int fd, ret;

	fd = open(name, O_RDONLY);
	if (fd < 0) {
		pr_perror("Can't open %s", name);
		return -1;
	}

	ret = read(fd, data, size - 1);
	if (ret < 0) {
		pr_perror("Can't read %s", name);
		close(fd);
		return -1;
	}
	data[ret] = '\0';
	close(fd);

	return 0;
}

int sysctl_write_str(const char *name, char *data)
{
	int fd, ret;

	fd = open(name, O_WRONLY);
	if (fd < 0) {
		pr_perror("Can't open %s", name);
		return -1;
	}

	ret = write(fd, data, strlen(data));
	if (ret < 0) {
		pr_perror("Can't write %s into %s", data, name);
		close(fd);
		return -1;
	}
	close(fd);

	return 0;
}

int sysctl_read_int(const char *name, int *data)
{
	int fd;
	int ret;
	char buf[16];

	fd = open(name, O_RDONLY);
	if (fd < 0) {
		pr_perror("Can't open %s", name);
		return fd;
	}

	ret = read(fd, buf, sizeof(buf) - 1);
	if (ret < 0) {
		pr_perror("Can't read %s", name);
		ret = -errno;
		goto err;
	}

	buf[ret] = '\0';

	*data = (int)strtoul(buf, NULL, 10);
	ret = 0;
err:
	close(fd);
	return ret;
}

int sysctl_write_int(const char *name, int val)
{
	int fd;
	int ret;
	char buf[16];

	fd = open(name, O_WRONLY);
	if (fd < 0) {
		pr_perror("Can't open %s", name);
		return fd;
	}

	sprintf(buf, "%d\n", val);

	ret = write(fd, buf, strlen(buf));
	if (ret < 0) {
		pr_perror("Can't write %d into %s", val, name);
		ret = -errno;
		goto err;
	}

	ret = 0;
err:
	close(fd);
	return ret;
}