Project

General

Profile

« Previous | Next » 

Revision aa4f498d

Added by Erik Fonnesbeck almost 15 years ago

Add sysctl functions that support getting/setting multiple values in a single call.

View differences:

etc/inc/util.inc
1073 1073
        return true;
1074 1074
}
1075 1075

  
1076
/*
1077
 * get_sysctl($names)
1078
 * Get values of sysctl OID's listed in $names (accepts an array or a single
1079
 * name) and return an array of key/value pairs set for those that exist
1080
 */
1081
function get_sysctl($names) {
1082
	if (empty($names))
1083
		return array();
1084

  
1085
	if (is_array($names)) {
1086
		$name_list = array();
1087
		foreach ($names as $name) {
1088
			$name_list[] = escapeshellarg($name);
1089
		}
1090
	} else
1091
		$name_list = array(escapeshellarg($names));
1092

  
1093
	exec("/sbin/sysctl -i " . implode(" ", $name_list), $output);
1094
	$values = array();
1095
	foreach ($output as $line) {
1096
		$line = explode(": ", $line, 2);
1097
		if (count($line) == 2)
1098
			$values[$line[0]] = $line[1];
1099
	}
1100

  
1101
	return $values;
1102
}
1103

  
1104
/*
1105
 * set_sysctl($value_list)
1106
 * Set sysctl OID's listed as key/value pairs and return
1107
 * an array with keys set for those that succeeded
1108
 */
1109
function set_sysctl($values) {
1110
	if (empty($values))
1111
		return array();
1112

  
1113
	$value_list = array();
1114
	foreach ($values as $key => $value) {
1115
		$value_list[] = escapeshellarg($key) . "=" . escapeshellarg($value);
1116
	}
1117

  
1118
	exec("/sbin/sysctl -i " . implode(" ", $value_list), $output, $success);
1119

  
1120
	/* Retry individually if failed (one or more read-only) */
1121
	if ($success <> 0 && count($value_list) > 1) {
1122
		foreach ($value_list as $value) {
1123
			exec("/sbin/sysctl -i " . $value, $output);
1124
		}
1125
	}
1126

  
1127
	$ret = array();
1128
	foreach ($output as $line) {
1129
		$line = explode(": ", $line, 2);
1130
		if (count($line) == 2)
1131
			$ret[$line[0]] = true;
1132
	}
1133

  
1134
	return $ret;
1135
}
1136

  
1076 1137
/*
1077 1138
 *     get_memory()
1078 1139
 *     returns an array listing the amount of

Also available in: Unified diff