summaryrefslogtreecommitdiff
path: root/sysctl.c
blob: da1ad866a0edd569031b21295a691cfd7932e19e (plain)
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
/*
 * sysctl - sysctl set/get helpers
 * Subject to the GPL, version 2.
 */

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>

#include "built_in.h"
#include "sysctl.h"

int sysctl_set_int(const char *file, int value)
{
	char path[PATH_MAX];
	char str[64];
	ssize_t ret;
	int fd;

	strncpy(path, SYSCTL_PROC_PATH, PATH_MAX);
	strncat(path, file, PATH_MAX - sizeof(SYSCTL_PROC_PATH) - 1);

	fd = open(path, O_WRONLY);
	if (unlikely(fd < 0))
		return -1;

	ret = snprintf(str, 63, "%d", value);
	if (ret < 0) {
		close(fd);
		return -1;
	}

	ret = write(fd, str, strlen(str));

	close(fd);
	return ret <= 0 ? -1 : 0;
}

int sysctl_get_int(const char *file, int *value)
{
	char path[PATH_MAX];
	char str[64];
	ssize_t ret;
	int fd;

	strncpy(path, SYSCTL_PROC_PATH, PATH_MAX);
	strncat(path, file, PATH_MAX - sizeof(SYSCTL_PROC_PATH) - 1);

	fd = open(path, O_RDONLY);
	if (fd < 0)
		return -1;

	ret = read(fd, str, sizeof(str));
	if (ret > 0) {
		*value = atoi(str);
		ret = 0;
	} else {
		ret = -1;
	}

	close(fd);
	return ret;
}
lass='commit-msg'>Pull perf/core improvements and fixes from Arnaldo Carvalho de Melo: User visible changes: - Allow reading from a backward ring buffer (one setup via sys_perf_event_open() with perf_event_attr.write_backward = 1) (Wang Nan) Infrastructure changes: - Fix the build on Android NDK r12b (initially just for ARM), that is now port of my perf-build container collection and will get tested prior to sending patches upstream (Arnaldo Carvalho de Melo) - Add correct header for IPv6 definitions - Fix bitsperlong.h fallout (Arnaldo Carvalho de Melo, Peter Zijlstra) - Use base 0 (auto) in filename__read_ull(), so that we can handle hex values too (Jiri Olsa) Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'Documentation/devicetree')