Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2#include <stdio.h>
3#include <errno.h>
4#include <string.h>
5
6#include "sysctl_helpers.h"
7#include "test_progs.h"
8
9int sysctl_set(const char *sysctl_path, char *old_val, const char *new_val)
10{
11 int ret = 0;
12 FILE *fp;
13
14 fp = fopen(sysctl_path, "r+");
15 if (!fp)
16 return -errno;
17 if (old_val && fscanf(fp, "%s", old_val) <= 0) {
18 ret = -ENOENT;
19 } else if (!old_val || strcmp(old_val, new_val) != 0) {
20 fseek(fp, 0, SEEK_SET);
21 if (fprintf(fp, "%s", new_val) < 0)
22 ret = -errno;
23 }
24 fclose(fp);
25
26 return ret;
27}
28
29int sysctl_set_or_fail(const char *sysctl_path, char *old_val, const char *new_val)
30{
31 int err;
32
33 err = sysctl_set(sysctl_path, old_val, new_val);
34 if (err)
35 PRINT_FAIL("failed to set %s to %s: %s\n", sysctl_path, new_val, strerror(-err));
36 return err;
37}