-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctrl.c
More file actions
108 lines (99 loc) · 2.29 KB
/
ctrl.c
File metadata and controls
108 lines (99 loc) · 2.29 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
#include "ctrl.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
static int OPEN_FLAGS = O_PATH | O_CLOEXEC | O_NOFOLLOW;
static int
set_feature(hbsdctrl_ctx_t*, const char*, const char*, int);
int
enable_feature(hbsdctrl_ctx_t* ctx, const char* name, const char* path)
{
return set_feature(ctx, name, path, 1);
}
int
disable_feature(hbsdctrl_ctx_t* ctx, const char* name, const char* path)
{
return set_feature(ctx, name, path, 2);
}
int
sysdef_feature(hbsdctrl_ctx_t* ctx, const char* name, const char* path)
{
return set_feature(ctx, name, path, -1);
}
int
feature_status(hbsdctrl_ctx_t* ctx,
const char* name,
const char* path,
char** status)
{
hbsdctrl_feature_t* feature;
hbsdctrl_feature_state_t state;
int res = 0, fd = -1;
errno = 0;
if (ctx == NULL) {
res = -1;
goto done;
}
if ((fd = open(path, OPEN_FLAGS)) == -1) {
res = -1;
goto done;
}
if ((feature = hbsdctrl_ctx_find_feature_by_name(ctx, name)) == NULL) {
res = -1;
goto done;
}
if (feature->hf_get(ctx, feature, &fd, &state) == RES_FAIL) {
res = -1;
goto done;
} else {
errno = 0;
}
*status = (char*)hbsdctrl_feature_state_to_string(&state);
goto done;
done:
if (fd != -1)
close(fd);
if (errno != 0)
return errno;
return res;
}
static int
set_feature(hbsdctrl_ctx_t* ctx, const char* name, const char* path, int state)
{
hbsdctrl_feature_t* feature;
int res = 0, fd = -1;
errno = 0;
if (ctx == NULL) {
res = -1;
goto done;
}
if ((feature = hbsdctrl_ctx_find_feature_by_name(ctx, name)) == NULL) {
res = -1;
goto done;
}
if ((fd = open(path, OPEN_FLAGS)) == -1) {
res = -1;
goto done;
}
if (state == -1) {
if (feature->hf_unapply(ctx, feature, &fd, NULL) == RES_FAIL) {
res = -1;
goto done;
} else {
errno = 0;
}
} else {
if (feature->hf_apply(ctx, feature, &fd, &state) == RES_FAIL) {
res = -1;
goto done;
}
}
goto done;
done:
if (fd != -1)
close(fd);
if (errno != 0)
return errno;
return res;
}