File: test-sysctl.c

package info (click to toggle)
ell 0.81-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,344 kB
  • sloc: ansic: 61,546; sh: 4,896; makefile: 574
file content (73 lines) | stat: -rw-r--r-- 1,590 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
/*
 * Embedded Linux library
 * Copyright (C) 2023  Cruise LLC
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#include <ell/ell.h>

static void test_sysctl_get_set(const void *data)
{
	FILE *f;
	uint32_t n;
	uint32_t expected;
	int r;

	/* Grab the setting via sysctl */
	f = popen("sysctl -n net.core.somaxconn", "r");
	assert(f);

	assert(fscanf(f, "%u", &expected) == 1);
	pclose(f);

	/* Now check against what we get */
	assert(l_sysctl_get_u32(&n, "/proc/sys/%s/%s/%s",
					"net", "core", "somaxconn") == 0);
	assert(n == expected);

	/*
	 * Attempt to change the setting, if we get an -EPERM, then we're
	 * most likely not running as root, so succeed silently.  Any other
	 * error is treated as a unit test failure
	 */

	r = l_sysctl_set_u32(5000, "/proc/sys/net/core/%s", "somaxconn");
	if (r == -EACCES)
		return;

	assert(!r);
	assert(!l_sysctl_get_u32(&n, "/proc/sys/net/core/somaxconn"));
	assert(n == 5000);

	/* Set it back to original */
	assert(!l_sysctl_set_u32(expected, "/proc/sys/net/core/somaxconn"));
}

int main(int argc, char *argv[])
{
	int ret;
	l_test_init(&argc, &argv);

	/*
	* test is failing if /proc/sys/net/core/somaxconn does not exist
	* this can happen when either net is not compiled, or running in
	* a namespace where sysfs is not mounted
	*/
	ret = access("/proc/sys/net/core/somaxconn", F_OK);
	if (!ret)
		l_test_add("sysctl/get_set", test_sysctl_get_set, NULL);


	return l_test_run();
}