ipkg: add a default postinst/prerm script
authorJohn Crispin <john@openwrt.org>
Thu, 11 Sep 2014 12:27:49 +0000 (12:27 +0000)
committerJohn Crispin <john@openwrt.org>
Thu, 11 Sep 2014 12:27:49 +0000 (12:27 +0000)
the postinst script enables/starts the init.d scripts upon package installation
and installs the users required by the package.

the prerm script stops and disables the init.d scripts.

Signed-off-by: John Crispin <blogic@openwrt.org>
SVN-Revision: 42470

include/package-ipkg.mk
package/Makefile
package/base-files/files/lib/functions.sh

index 1caeaa25b63bc4b1be94b0d465a59c2b85c8e2b5..494444613ae66079e883907bfa3520513827482c 100644 (file)
@@ -16,7 +16,7 @@ IPKG_STATE_DIR:=$(TARGET_DIR)/usr/lib/opkg
 define BuildIPKGVariable
 ifdef Package/$(1)/$(2)
   $(call shexport,Package/$(1)/$(2))
-  $(1)_COMMANDS += var2file "$(call shvar,Package/$(1)/$(2))" $(2);
+  $(1)_COMMANDS += var2file "$(call shvar,Package/$(1)/$(2))" $(2)$(3);
 endif
 endef
 
@@ -117,8 +117,8 @@ ifeq ($(DUMP),)
 
     $(eval $(call BuildIPKGVariable,$(1),conffiles))
     $(eval $(call BuildIPKGVariable,$(1),preinst))
-    $(eval $(call BuildIPKGVariable,$(1),postinst))
-    $(eval $(call BuildIPKGVariable,$(1),prerm))
+    $(eval $(call BuildIPKGVariable,$(1),postinst,-pkg))
+    $(eval $(call BuildIPKGVariable,$(1),prerm,-pkg))
     $(eval $(call BuildIPKGVariable,$(1),postrm))
 
     $(STAGING_DIR_ROOT)/stamp/.$(1)_installed: $(STAMP_BUILT)
@@ -174,6 +174,17 @@ ifeq ($(DUMP),)
                echo -n "Description: "; $(SH_FUNC) getvar $(call shvar,Package/$(1)/description) | sed -e 's,^[[:space:]]*, ,g'; \
        ) > $$(IDIR_$(1))/CONTROL/control
        chmod 644 $$(IDIR_$(1))/CONTROL/control
+       ( \
+               echo "#!/bin/sh"; \
+               echo ". \$$$${IPKG_INSTROOT}/lib/functions.sh"; \
+               echo "default_postinst \$$$$0 \$$$$@"; \
+       ) > $$(IDIR_$(1))/CONTROL/postinst
+       ( \
+               echo "#!/bin/sh"; \
+               echo ". \$$$${IPKG_INSTROOT}/lib/functions.sh"; \
+               echo "default_prerm \$$$$0 \$$$$@"; \
+       ) > $$(IDIR_$(1))/CONTROL/prerm
+       chmod 0755 $$(IDIR_$(1))/CONTROL/prerm
        $(SH_FUNC) (cd $$(IDIR_$(1))/CONTROL; \
                $($(1)_COMMANDS) \
        )
index 0cba87857646a56b4effbce32c762c51251f3ff1..a42b4b0dd01100981a8f627504a3e44e80c6d860 100644 (file)
@@ -116,7 +116,8 @@ $(curdir)/install: $(TMP_DIR)/.build
        @-find $(TARGET_DIR) -name CVS   | $(XARGS) rm -rf
        @-find $(TARGET_DIR) -name .svn  | $(XARGS) rm -rf
        @-find $(TARGET_DIR) -name '.#*' | $(XARGS) rm -f
-       rm -f $(TARGET_DIR)/usr/lib/opkg/info/*.postinst
+       rm -f $(TARGET_DIR)/usr/lib/opkg/info/*.postinst*
+       rm -f $(TARGET_DIR)/usr/lib/opkg/info/*.prerm*
        $(if $(CONFIG_CLEAN_IPKG),rm -rf $(TARGET_DIR)/usr/lib/opkg)
        $(call mklibs)
 
index 0d4b2a33dbbf4e18acd474ae2adaad0ea3c1c4a0..67f4a046e595c0a0a862cf739c521e6a2afd73a1 100755 (executable)
@@ -160,6 +160,50 @@ insert_modules() {
        }
 }
 
+default_prerm() {
+       local name
+       name=$(echo $(basename $1) | cut -d. -f1)
+       [ -f /usr/lib/opkg/info/${name}.prerm-pkg ] && . /usr/lib/opkg/info/${name}.prerm-pkg
+       for i in `cat /usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do
+               $i disable
+               $i stop
+       done
+}
+
+default_postinst() {
+       local name rusers
+       name=$(echo $(basename $1) | cut -d. -f1)
+       [ -f ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.postinst-pkg ] && . ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.postinst-pkg
+       rusers=$(grep "Require-User:" ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.control)
+       [ -n "$rusers" ] && {
+               local user group
+               for a in $(echo $rusers | sed "s/Require-User://g"); do
+                       user=""
+                       group=""
+                       for b in $(echo $a | sed "s/:/ /g"); do
+                               [ -z "$user" ] && {
+                                       user=$b
+                                       continue
+                               }
+                               [ -z "$group" ] && {
+                                       group=$b
+                                       group_add_next $b
+                                       gid=$?
+                                       user_exists $user || user_add $user "" $gid
+                                       continue
+                               }
+                               group_add_next $b
+                               group_add_user $b $user
+                       done
+               done
+       }
+       [ -n "${IPKG_INSTROOT}" -o "$PKG_UPGRADE" = "1" ] || for i in `cat /usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do
+               $i enable
+               $i start
+       done
+       return 0
+}
+
 include() {
        local file
 
@@ -199,14 +243,45 @@ group_exists() {
        grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
 }
 
+group_add_next() {
+       local gid gids
+       gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
+       [ -n "$gid" ] && return $gid
+       gids=$(cat ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
+       gid=100
+       while [ -n "$(echo $gids | grep $gid)" ] ; do
+               gid=$((gid + 1))
+       done
+       group_add $1 $gid
+       return $gid
+}
+
+group_add_user() {
+       local grp delim=","
+       grp=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group)
+       [ -z "$(echo $grp | cut -d: -f4 | grep $2)" ] || return
+       [ -n "$(echo $grp | grep ":$")" ] && delim=""
+       [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
+       sed -i "s/$grp/$grp$delim$2/g" ${IPKG_INSTROOT}/etc/group
+       [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
+}
+
 user_add() {
        local name="${1}"
        local uid="${2}"
-       local gid="${3:-$2}"
+       local gid="${3}"
        local desc="${4:-$1}"
        local home="${5:-/var/run/$1}"
        local shell="${6:-/bin/false}"
        local rc
+       [ -z "$uid" ] && {
+               uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3)
+               uid=100
+               while [ -n "$(echo $uids | grep $uid)" ] ; do
+                       uid=$((uid + 1))
+               done
+       }
+       [ -z "$gid" ] && gid=$uid
        [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
        [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
        echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd