Browse Source

Here comes the new UCI. Enjoy :)

SVN-Revision: 10367
Felix Fietkau 18 years ago
parent
commit
0cf1a58282

+ 22 - 36
package/base-files/files/etc/functions.sh

@@ -10,6 +10,7 @@ N="
 
 _C=0
 NO_EXPORT=1
+LOAD_STATE=1
 
 hotplug_dev() {
 	env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
@@ -23,12 +24,21 @@ append() {
 	eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
 }
 
+config_load() {
+	[ -n "$IPKG_INSTROOT" ] && return 0
+	uci_load "$@"
+}
+
 reset_cb() {
 	config_cb() { return 0; }
 	option_cb() { return 0; }
 }
 reset_cb
 
+package() {
+	return 0
+}
+
 config () {
 	local cfgtype="$1"
 	local name="$2"
@@ -84,37 +94,6 @@ config_clear() {
 	done
 }
 
-config_load() {
-	local cfg
-	local uci
-	local PACKAGE="$1"
-
-	case "$PACKAGE" in
-		/*)	cfg="$PACKAGE"
-			uci=""
-		;;
-		*)	cfg="$UCI_ROOT/etc/config/$PACKAGE"
-			uci="/tmp/.uci/${PACKAGE}"
-		;;
-	esac
-
-	[ -e "$cfg" ] || cfg=""
-	[ -e "$uci" ] || uci=""
-
-	# no config
-	[ -z "$cfg" -a -z "$uci" ] && return 1
-
-	_C=0
-	export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
-	export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
-	export ${NO_EXPORT:+-n} CONFIG_SECTION=
-
-	${cfg:+. "$cfg"}
-	${uci:+. "$uci"}
-	
-	${CONFIG_SECTION:+config_cb}
-}
-
 config_get() {
 	case "$3" in
 		"") eval "echo \"\${CONFIG_${1}_${2}}\"";;
@@ -218,8 +197,15 @@ jffs2_mark_erase() {
 	echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
 }
 
-uci_set_default() {
-	local PACKAGE="$1"
-	[ -e "/etc/config/$1" ] && return 0
-	cat > "/etc/config/$1"
-}
+uci_apply_defaults() {(
+	cd /etc/uci-defaults || return 0
+	files="$(ls)"
+	[ -z "$files" ] && return 0
+	mkdir -p /tmp/.uci
+	for file in $files; do
+		( . "./$(basename $file)" ) && rm -f "$file"
+	done
+	uci commit
+)}
+
+[ -z "$IPKG_INSTROOT" ] && . /lib/config/uci.sh

+ 1 - 1
package/base-files/files/etc/hotplug.d/iface/00-netstate

@@ -1,3 +1,3 @@
 [ ifup = "$ACTION" ] && {
-	uci set "/var/state/network.$INTERFACE.up=1"
+	uci_set_state network "$INTERFACE" up 1 
 }

+ 0 - 1
package/base-files/files/etc/hotplug.d/iface/10-routes

@@ -63,7 +63,6 @@ case "$ACTION" in
 	ifup)
 		include /lib/network
 		scan_interfaces
-		. /var/state/network
 		config_foreach "add_route" route
 		config_foreach "add_route6" route6
 	;;

+ 2 - 0
package/base-files/files/etc/init.d/boot

@@ -36,6 +36,8 @@ start() {
 	mkdir -p /var/log
 	mkdir -p /var/lock
 	mkdir -p /var/state
+	mkdir -p /tmp/.uci
+	chown 0700 /tmp/.uci
 	touch /var/log/wtmp
 	touch /var/log/lastlog
 	ln -sf /tmp/resolv.conf.auto /tmp/resolv.conf

+ 1 - 1
package/base-files/files/lib/network/config.sh

@@ -140,7 +140,7 @@ setup_interface() {
 	config_get macaddr "$config" macaddr
 	grep "$iface:" /proc/net/dev > /dev/null && \
 		$DEBUG ifconfig "$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} up
-	uci set "/var/state/network.$config.ifname=$iface"
+	uci_set_state network "$config" ifname "$iface"
 
 	pidfile="/var/run/$iface.pid"
 	case "$proto" in

+ 1 - 5
package/base-files/files/sbin/ifdown

@@ -11,12 +11,8 @@
 	exit
 }
 
-config_load /var/state/network
-
 # remove the interface's network state
-FILE=/var/state/network.$$
-grep -v "^config_set '$1' " /var/state/network > "$FILE"
-mv "$FILE" /var/state/network
+uci_revert_state network "$1"
 
 include /lib/network
 scan_interfaces

+ 4 - 7
package/base-files/files/sbin/wifi

@@ -75,22 +75,19 @@ start_net() {(
 set_wifi_up() {
 	local cfg="$1"
 	local ifname="$2"
-	uci set "/var/state/wireless.${cfg}.up=1"
-	uci set "/var/state/wireless.${cfg}.ifname=$ifname"
+	uci_set_state wireless "$cfg" up 1
+	uci_set_state wireless "$cfg" ifname "$ifname"
 }
 
 set_wifi_down() {
 	local cfg="$1"
 	local vifs vif vifstr
-	[ -f /var/state/wireless ] || return
 
+	uci_revert_state wireless "$cfg"
 	config_get vifs "$cfg" vifs
 	for vif in $vifs; do
-		append vifstr "$vif" "|"
+		uci_revert_state wireless "$vif"
 	done
-	FILE="/var/state/wireless.$$"
-	grep -vE "^config_set '($vifstr)' " /var/state/wireless > "$FILE"
-	mv "$FILE" /var/state/wireless
 }
 
 scan_wifi() {

+ 5 - 6
package/base-files/files/usr/share/udhcpc/default.script

@@ -7,7 +7,6 @@ RESOLV_CONF="/tmp/resolv.conf.auto"
 
 hotplug_event() {
 	scan_interfaces
-	config_load /var/state/network
 	for ifc in $interfaces; do
 		config_get ifname $ifc ifname
 		[ "$ifname" = "$interface" ] || continue
@@ -15,11 +14,11 @@ hotplug_event() {
 		config_get proto $ifc proto
 		[ "$proto" = "dhcp" ] || continue
 		[ ifup = "$1" ] && {
-			uci set "/var/state/network.$ifc.ipaddr=$ip"
-			uci set "/var/state/network.$ifc.netmask=${subnet:-255.255.255.0}"
-			uci set "/var/state/network.$ifc.dnsdomain=$domain"
-			uci set "/var/state/network.$ifc.dns=$dns"
-			uci set "/var/state/network.$ifc.gateway=$router"
+			uci_set_state network "$ifc" ipaddr "$ip"
+			uci_set_state network "$ifc" netmask "${subnet:-255.255.255.0}"
+			uci_set_state network "$ifc" dnsdomain "$domain"
+			uci_set_state network "$ifc" dns "$dns"
+			uci_set_state network "$ifc" gateway "$router"
 		}
 		env -i ACTION="$1" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
 	done

+ 0 - 1
package/dnsmasq/files/dnsmasq.init

@@ -231,7 +231,6 @@ dhcp_option_add() {
 start() {
 	include /lib/network
 	scan_interfaces
-	config_load /var/state/network
 	config_load dhcp
 
 	args=""

+ 0 - 1
package/iptables/files/firewall.init

@@ -6,7 +6,6 @@ START=45
 start() {
 	include /lib/network
 	scan_interfaces
-	config_load /var/state/network
 	
 	config_get WAN wan ifname
 	config_get WANDEV wan device

+ 1 - 3
package/ppp/files/etc/ppp/ip-down

@@ -10,9 +10,7 @@ export PPP_IFACE PPP_TTY PPP_SPEED PPP_LOCAL PPP_REMOTE PPP_IPPARAM
 	env -i ACTION="ifdown" INTERFACE="$PPP_IPPARAM" DEVICE="$PPP_IFACE" PROTO=ppp /sbin/hotplug-call "iface"
 
 	# remove the interface's network state
-	FILE=/var/state/network.$$
-	grep -v "^config_set '$PPP_IPPARAM' " /var/state/network > "$FILE"
-	mv "$FILE" /var/state/network
+	uci_revert_state network "$PPP_IPPARAM"
 }
 
 [ -d /etc/ppp/ip-down.d ] && {

+ 2 - 2
package/ppp/files/etc/ppp/ip-up

@@ -8,8 +8,8 @@ PPP_IPPARAM="$6"
 export PPP_IFACE PPP_TTY PPP_SPEED PPP_LOCAL PPP_REMOTE PPP_IPPARAM
 [ -z "$PPP_IPPARAM" ] || env -i ACTION="ifup" INTERFACE="$PPP_IPPARAM" DEVICE="$PPP_IFACE" PROTO=ppp /sbin/hotplug-call "iface"
 [ -z "$PPP_IPPARAM" -o -z "$PPP_LOCAL" ] || {
-	uci set "/var/state/network.$PPP_IPPARAM.ipaddr=$PPP_LOCAL"
-	uci set "/var/state/network.$PPP_IPPARAM.gateway=$PPP_REMOTE"
+	uci_set_state network "$PPP_IPPARAM" ipaddr "$PPP_LOCAL"
+	uci_set_state network "$PPP_IPPARAM" gateway "$PPP_REMOTE"
 }
 
 

+ 1 - 1
package/pptp/files/pptp.sh

@@ -23,7 +23,7 @@ setup_interface_pptp() {
 	# make sure the network state references the correct ifname
 	scan_ppp "$config"
 	config_get ifname "$config" ifname
-	uci set "/var/state/network.$config.ifname=$ifname"
+	uci_set_state network "$config" ifname "$ifname"
 
 	config_get mtu "$cfg" mtu
 	config_get server "$cfg" server

+ 0 - 1
package/qos-scripts/files/usr/lib/qos/generate.sh

@@ -17,7 +17,6 @@ add_insmod() {
 		reset_cb
 		include /lib/network
 		scan_interfaces
-		config_load /var/state/network
 		config_get "$1" ifname
 	)}
 } || {

+ 75 - 0
package/uci/Makefile

@@ -0,0 +1,75 @@
+# 
+# Copyright (C) 2008 OpenWrt.org
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+# $Id$
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=uci
+PKG_VERSION:=0.1
+PKG_RELEASE:=1
+
+PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
+PKG_SOURCE_URL:=http://downloads.openwrt.org/sources
+PKG_MD5SUM:=f6340dce09f5f1552c4e03be98e64265
+
+include $(INCLUDE_DIR)/package.mk
+
+# set to 1 to enable debugging
+DEBUG=
+
+define Package/libuci
+  SECTION:=libs
+  CATEGORY:=Libraries
+  DEPENDS:=+libuci
+  TITLE:=C library for the Unified Configuration Interface (UCI)
+endef
+
+define Package/uci
+  SECTION:=base
+  CATEGORY:=Base system
+  DEPENDS:=+libuci
+  TITLE:=Utility for the Unified Configuration Interface (UCI)
+endef
+
+define Package/uci-sh
+  SECTION:=base
+  CATEGORY:=Base system
+  DEPENDS:=@!PACKAGE_uci
+  TITLE:=Old shell/awk implementation of UCI
+endef
+
+define Build/Configure
+endef
+
+define Build/Compile
+	$(MAKE) -C $(PKG_BUILD_DIR) \
+		$(TARGET_CONFIGURE_OPTS) \
+		COPTS="$(TARGET_CFLAGS)" \
+		DEBUG="$(DEBUG)" \
+		VERSION="$(PKG_VERSION)" \
+		OS="Linux"
+endef
+
+define Package/libuci/install
+	$(INSTALL_DIR) $(1)/lib
+	$(CP) $(PKG_BUILD_DIR)/libuci.so* $(1)/lib/
+endef
+
+define Package/uci/install
+	$(INSTALL_DIR) $(1)/sbin
+	$(INSTALL_BIN) $(PKG_BUILD_DIR)/uci $(1)/sbin/
+	$(CP) ./files/uci/* $(1)/
+endef
+
+define Package/uci-sh/install
+	$(INSTALL_DIR) $(1)
+	$(CP) ./files/uci-sh/* $(1)/
+endef
+
+$(eval $(call BuildPackage,uci))
+$(eval $(call BuildPackage,libuci))
+$(eval $(call BuildPackage,uci-sh))

+ 0 - 0
package/base-files/files/bin/uci → package/uci/files/uci-sh/bin/uci


+ 9 - 9
package/base-files/files/lib/config/uci.awk → package/uci/files/uci-sh/lib/config/uci.awk

@@ -80,7 +80,7 @@ function uci_update_config(cfg, update,  \
 		i2 = 1
 		delete l
 		rest = line
-        	while (length(rest)) {
+		while (length(rest)) {
 			if (match(rest, /[ \t\"]+/)) {
 				if (RSTART>1) {
 					l[i2] = substr(rest,1,RSTART-1)
@@ -102,7 +102,7 @@ function uci_update_config(cfg, update,  \
 			}
 		}
 		line = lines[n]
-		
+
 		# when a command wants to set a config value for the current
 		# section and a blank line is encountered before an option with
 		# the same name, insert it here to maintain some coherency between
@@ -128,17 +128,17 @@ function uci_update_config(cfg, update,  \
 					flag=1
 					gsub("^" section ".", "", update)
 					cfg = cfg uci_cmd2option(update) "\n"
-					
+
 					update = "-" section "." update
-				} 
+				}
 				if (flag!=0) cfg = cfg "\n"
 			}
-			
+
 			remove = ""
 			section = l[3]
 			if (!length(section)) {
 				section = "cfg" scnt
-			}	
+			}
 			scnt++
 			if (update == "-" section) {
 				remove = "section"
@@ -147,7 +147,7 @@ function uci_update_config(cfg, update,  \
 				update = ""
 			} else if (update ~ "^&" section "=") {
 				gsub("^&" section "=", "", update)
-				line = uci_cmd2config(l[2],update) 
+				line = uci_cmd2config(l[2],update)
 				update = ""
 			}
 		}
@@ -163,7 +163,7 @@ function uci_update_config(cfg, update,  \
 		}
 		if (remove == "") cfg = cfg line "\n"
 	}
-	
+
 	# any new options for the last section??
 	if (section != "") {
 		if (update ~ "^" section "\\.") {
@@ -171,7 +171,7 @@ function uci_update_config(cfg, update,  \
 			cfg = cfg uci_cmd2option(update) "\n"
 
 			update = "-" section "." update
-		} 
+		}
 	}
 
 	if (update ~ "^@") {

+ 52 - 12
package/base-files/files/lib/config/uci.sh → package/uci/files/uci-sh/lib/config/uci.sh

@@ -18,20 +18,41 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
-uci_load() {
-	config_load "$1"
+uci_set_default() {
+	local PACKAGE="$1"
+	[ -e "/etc/config/$1" ] && return 0
+	cat > "/etc/config/$1"
 }
 
-uci_apply_defaults() {(
-	cd /etc/uci-defaults || return 0
-	files="$(ls)"
-	[ -z "$files" ] && return 0
-	mkdir -p /tmp/.uci
-	for file in $files; do
-		( . "./$(basename $file)" ) && rm -f "$file"
-	done
-	uci commit
-)}
+uci_load() {
+	local cfg
+	local uci
+
+	_C=0
+	export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
+	export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
+	export ${NO_EXPORT:+-n} CONFIG_SECTION=
+
+	case "$PACKAGE" in
+		/*) cfg="$PACKAGE";;
+		 *) 
+		 	cfg="$UCI_ROOT/etc/config/$PACKAGE"
+			uci="$UCI_ROOT/tmp/.uci/$PACKAGE"
+			state="$UCI_ROOT/var/state/$PACKAGE"
+		;;
+	esac
+
+	# no config?
+	[ -z "$cfg" -o \! -f "$cfg" ] && return 1
+	. "$cfg"
+
+	${CONFIG_SECTION:+config_cb}
+
+	[ -z "$uci" -o \! -f "$uci" ] || . "$uci"
+	[ -z "$LOAD_STATE" -z "$state" -o \! -f "$state" ] || . "$state"
+
+	return 0
+}
 
 uci_call_awk() {
 	local CMD="$*"
@@ -70,6 +91,25 @@ uci_add_update() {
 	echo "$UPDATE" >> "$UCIFILE"
 }
 
+uci_revert_state() {
+	local PACKAGE="$1"
+	local CONFIG="$2"
+	FILE="/var/state/$PACKAGE.$$"
+	grep -v "^config_set '$CONFIG' " "/var/state/$PAKAGE" > "$FILE"
+	mv "$FILE" "/var/state/$PACKAGE"
+}
+
+uci_set_state() {
+	local PACKAGE="$1"
+	local CONFIG="$2"
+	local OPTION="$3"
+	local VALUE="$4"
+
+	[ -z "$VALUE" ] && return 1
+	uci_set "/var/state/$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
+}
+
+
 uci_set() {
 	local PACKAGE="$1"
 	local CONFIG="$2"

+ 93 - 0
package/uci/files/uci/lib/config/uci.sh

@@ -0,0 +1,93 @@
+#!/bin/sh
+# Shell script compatibility wrappers for /sbin/uci
+#
+# Copyright (C) 2008  Felix Fietkau <[email protected]>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+uci_load() {
+	local PACKAGE="$1"
+
+	_C=0
+	export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
+	export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
+	export ${NO_EXPORT:+-n} CONFIG_SECTION=
+
+	eval "$(/sbin/uci ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE")"
+
+	${CONFIG_SECTION:+config_cb}
+}
+
+uci_set_default() {
+	local PACKAGE="$1"
+	/sbin/uci -q show "$1" > /dev/null && return 0
+	/sbin/uci import "$1"
+}
+
+uci_revert_state() {
+	local PACKAGE="$1"
+	local CONFIG="$2"
+	local OPTION="$3"
+
+	/bin/uci -P /var/state revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
+}
+
+uci_set_state() {
+	local PACKAGE="$1"
+	local CONFIG="$2"
+	local OPTION="$3"
+	local VALUE="$4"
+
+	[ -z "$VALUE" ] && return 0
+	/sbin/uci -P /var/state set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
+}
+
+uci_set() {
+	local PACKAGE="$1"
+	local CONFIG="$2"
+	local OPTION="$3"
+	local VALUE="$4"
+
+	/sbin/uci set "$PACKAGE.$CONFIG.$OPTION=$TYPE"
+}
+
+uci_add() {
+	local PACKAGE="$1"
+	local TYPE="$2"
+	local CONFIG="$3"
+
+	/sbin/uci set "$PACKAGE.$CONFIG=$TYPE"
+}
+
+uci_rename() {
+	local PACKAGE="$1"
+	local CONFIG="$2"
+	local VALUE="$3"
+
+	/sbin/uci rename "$PACKAGE.$CONFIG=$VALUE"
+}
+
+uci_remove() {
+	local PACKAGE="$1"
+	local CONFIG="$2"
+	local OPTION="$3"
+
+	/sbin/uci del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
+}
+
+uci_commit() {
+	local PACKAGE="$1"
+	/sbin/uci commit $PACKAGE
+}