]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.sysctl
Use autotools.
[people/ms/network.git] / src / functions / functions.sysctl
CommitLineData
ef953be2 1#!/bin/bash
15a5f44d
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
ef953be2 5# Copyright (C) 2013 IPFire Network Development Team #
15a5f44d
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
ef953be2
MT
22SYSCTL_PATH="/proc/sys"
23
24function sysctl_key_to_path() {
25 local key="${1}"
26 assert isset key
27
28 print "${SYSCTL_PATH}/${key//.//}"
29
30 return ${EXIT_OK}
31}
32
33function sysctl_key_exists() {
34 local key="${1}"
35 assert isset key
36
37 local path="$(sysctl_key_to_path "${key}")"
38
39 [ -e "${path}" ] && return ${EXIT_OK}
40 return ${EXIT_ERROR}
41}
42
43function sysctl_get() {
44 local key="${1}"
45 assert isset key
46
47 fread "$(sysctl_key_to_path "${key}")" || return $?
48 return ${EXIT_OK}
49}
50
51function sysctl_set() {
52 local key="${1}"
53 assert isset key
54
55 local value="${2}"
56
57 fwrite "$(sysctl_key_to_path "${key}")" "${value}" || return $?
58 return ${EXIT_OK}
59}
60
61function sysctl_set_bool() {
62 local key="${1}"
63
64 local value="${2}"
65 if enabled value; then
66 value="1"
67 else
68 value="0"
69 fi
70
71 sysctl_set "${key}" "${value}"
72}
73
74function sysctl_set_recursively() {
75 local basekey="${1}"
76 assert isset basekey
77
78 local subkey="${2}"
79 assert isset subkey
80
81 local value="${3}"
82
83 local basepath="$(sysctl_key_to_path "${basekey}")"
84 local subpath="/${subkey//\.//}"
85
86 local path
87 for path in $(find "${basepath}" -type f); do
88 [[ ${path} =~ ${subpath}$ ]] || continue
89
90 fwrite "${path}" "${value}"
91 done
92
93 return ${EXIT_OK}
94}