|
1 | 1 | #include <stdarg.h>
|
2 | 2 | #include <stdio.h>
|
| 3 | +#include <string.h> |
3 | 4 | #include <lkl_host.h>
|
4 | 5 |
|
5 | 6 | static const char * const lkl_err_strings[] = {
|
@@ -205,3 +206,58 @@ void lkl_bug(const char *fmt, ...)
|
205 | 206 |
|
206 | 207 | lkl_host_ops.panic();
|
207 | 208 | }
|
| 209 | + |
| 210 | +int lkl_sysctl(const char *path, const char *value) |
| 211 | +{ |
| 212 | + int ret; |
| 213 | + int fd; |
| 214 | + char *delim, *p; |
| 215 | + char full_path[256]; |
| 216 | + |
| 217 | + lkl_mount_fs("proc"); |
| 218 | + |
| 219 | + snprintf(full_path, sizeof(full_path), "/proc/sys/%s", path); |
| 220 | + p = full_path; |
| 221 | + while ((delim = strstr(p, "."))) { |
| 222 | + *delim = '/'; |
| 223 | + p = delim + 1; |
| 224 | + } |
| 225 | + |
| 226 | + fd = lkl_sys_open(full_path, LKL_O_WRONLY | LKL_O_CREAT, 0); |
| 227 | + if (fd < 0) { |
| 228 | + lkl_printf("lkl_sys_open %s: %s\n", |
| 229 | + full_path, lkl_strerror(fd)); |
| 230 | + return -1; |
| 231 | + } |
| 232 | + ret = lkl_sys_write(fd, value, strlen(value)); |
| 233 | + if (ret < 0) { |
| 234 | + lkl_printf("lkl_sys_write %s: %s\n", |
| 235 | + full_path, lkl_strerror(fd)); |
| 236 | + } |
| 237 | + |
| 238 | + lkl_sys_close(fd); |
| 239 | + |
| 240 | + return 0; |
| 241 | +} |
| 242 | + |
| 243 | +/* Configure sysctl parameters as the form of "key=value;key=value;..." */ |
| 244 | +void lkl_sysctl_parse_write(const char *sysctls) |
| 245 | +{ |
| 246 | + char *saveptr = NULL, *token = NULL; |
| 247 | + char *key = NULL, *value = NULL; |
| 248 | + char strings[256]; |
| 249 | + int ret = 0; |
| 250 | + |
| 251 | + strcpy(strings, sysctls); |
| 252 | + for (token = strtok_r(strings, ";", &saveptr); token; |
| 253 | + token = strtok_r(NULL, ";", &saveptr)) { |
| 254 | + key = strtok(token, "="); |
| 255 | + value = strtok(NULL, "="); |
| 256 | + ret = lkl_sysctl(key, value); |
| 257 | + if (ret) { |
| 258 | + lkl_printf("Failed to configure sysctl entries: %s\n", |
| 259 | + lkl_strerror(ret)); |
| 260 | + return; |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments