From 716ca530e1c4515d8683c9d5be3d56b301758b66 Mon Sep 17 00:00:00 2001 From: James <> Date: Wed, 4 Nov 2015 11:49:21 +0000 Subject: trunk-47381 --- package/system/procd/Makefile | 146 ++++++++ package/system/procd/files/hotplug-preinit.json | 20 ++ package/system/procd/files/hotplug.json | 86 +++++ package/system/procd/files/nand-preinit.sh | 21 ++ package/system/procd/files/nand.sh | 361 ++++++++++++++++++++ package/system/procd/files/procd.sh | 430 ++++++++++++++++++++++++ package/system/procd/files/reload_config | 15 + 7 files changed, 1079 insertions(+) create mode 100644 package/system/procd/Makefile create mode 100644 package/system/procd/files/hotplug-preinit.json create mode 100644 package/system/procd/files/hotplug.json create mode 100644 package/system/procd/files/nand-preinit.sh create mode 100644 package/system/procd/files/nand.sh create mode 100644 package/system/procd/files/procd.sh create mode 100644 package/system/procd/files/reload_config (limited to 'package/system/procd') diff --git a/package/system/procd/Makefile b/package/system/procd/Makefile new file mode 100644 index 0000000..d70cfd7 --- /dev/null +++ b/package/system/procd/Makefile @@ -0,0 +1,146 @@ +# +# Copyright (C) 2014-2015 OpenWrt.org +# +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=procd +PKG_VERSION:=2015-10-26 + +PKG_RELEASE=$(PKG_SOURCE_VERSION) + +PKG_SOURCE_PROTO:=git +PKG_SOURCE_URL:=git://nbd.name/luci2/procd.git +PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION) +PKG_SOURCE_VERSION:=d5fddd91b966424bb63e943e789704d52382cc18 +PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz +CMAKE_INSTALL:=1 + +PKG_LICENSE:=GPL-2.0 +PKG_LICENSE_FILES:= + +PKG_MAINTAINER:=John Crispin + +PKG_CONFIG_DEPENDS:= CONFIG_KERNEL_SECCOMP CONFIG_NAND_SUPPORT CONFIG_PROCD_SHOW_BOOT CONFIG_PROCD_ZRAM_TMPFS \ + CONFIG_KERNEL_NAMESPACES CONFIG_PACKAGE_procd-ujail CONFIG_PACKAGE_procd-seccomp + +include $(INCLUDE_DIR)/package.mk +include $(INCLUDE_DIR)/cmake.mk + +TARGET_LDFLAGS += $(if $(CONFIG_USE_GLIBC),-lrt) + +define Package/procd + SECTION:=base + CATEGORY:=Base system + DEPENDS:=+ubusd +ubus +libjson-script +ubox +USE_GLIBC:librt +libubox +libubus +NAND_SUPPORT:procd-nand + TITLE:=OpenWrt system process manager +endef + +define Package/procd-ujail + SECTION:=base + CATEGORY:=Base system + DEPENDS:=@KERNEL_NAMESPACES +@KERNEL_UTS_NS +@KERNEL_IPC_NS +@KERNEL_PID_NS +libubox +libblobmsg-json + TITLE:=OpenWrt process jail helper +endef + +define Package/procd-seccomp + SECTION:=base + CATEGORY:=Base system + DEPENDS:=@arm||@armeb||@mips||@mipsel||@i386||@x86_64 @!TARGET_uml @KERNEL_SECCOMP +libubox +libblobmsg-json + TITLE:=OpenWrt process seccomp helper + utrace +endef + +define Package/procd-nand + SECTION:=utils + CATEGORY:=Utilities + DEPENDS:=@NAND_SUPPORT +ubi-utils + TITLE:=OpenWrt sysupgrade nand helper +endef + +define Package/procd-nand-firstboot + SECTION:=utils + CATEGORY:=Utilities + DEPENDS:=procd-nand + TITLE:=OpenWrt firstboot nand helper +endef + +define Package/procd/config +menu "Configuration" + depends on PACKAGE_procd + +config PROCD_SHOW_BOOT + bool + default n + prompt "Print the shutdown to the console as well as logging it to syslog" + +config PROCD_ZRAM_TMPFS + bool + default n + prompt "Mount /tmp using zram." +endmenu +endef + + +ifeq ($(CONFIG_NAND_SUPPORT),y) + CMAKE_OPTIONS += -DBUILD_UPGRADED=1 +endif + +ifeq ($(CONFIG_PROCD_SHOW_BOOT),y) + CMAKE_OPTIONS += -DSHOW_BOOT_ON_CONSOLE=1 +endif + +ifeq ($(CONFIG_PROCD_ZRAM_TMPFS),y) + CMAKE_OPTIONS += -DZRAM_TMPFS=1 +endif + +ifdef CONFIG_PACKAGE_procd-ujail + CMAKE_OPTIONS += -DJAIL_SUPPORT=1 +endif + +ifdef CONFIG_PACKAGE_procd-seccomp + CMAKE_OPTIONS += -DSECCOMP_SUPPORT=1 -DUTRACE_SUPPORT=1 +endif + +define Package/procd/install + $(INSTALL_DIR) $(1)/sbin $(1)/etc $(1)/lib/functions + + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/{init,procd,askfirst,udevtrigger} $(1)/sbin/ + $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/lib/libsetlbf.so $(1)/lib + $(INSTALL_BIN) ./files/reload_config $(1)/sbin/ + $(INSTALL_DATA) ./files/hotplug*.json $(1)/etc/ + $(INSTALL_DATA) ./files/procd.sh $(1)/lib/functions/ +endef + +define Package/procd-ujail/install + $(INSTALL_DIR) $(1)/sbin + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/ujail $(1)/sbin/ +endef + +define Package/procd-seccomp/install + $(INSTALL_DIR) $(1)/sbin $(1)/lib + $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/lib/libpreload-seccomp.so $(1)/lib + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/utrace $(1)/sbin/ + $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/lib/libpreload-trace.so $(1)/lib +endef + +define Package/procd-nand/install + $(INSTALL_DIR) $(1)/sbin $(1)/lib/upgrade + + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upgraded $(1)/sbin/ + $(INSTALL_DATA) ./files/nand.sh $(1)/lib/upgrade/ +endef + +define Package/procd-nand-firstboot/install + $(INSTALL_DIR) $(1)/lib/preinit + + $(INSTALL_DATA) ./files/nand-preinit.sh $(1)/lib/preinit/60-nand-firstboot.sh +endef + +$(eval $(call BuildPackage,procd)) +$(eval $(call BuildPackage,procd-ujail)) +$(eval $(call BuildPackage,procd-seccomp)) +$(eval $(call BuildPackage,procd-nand)) +$(eval $(call BuildPackage,procd-nand-firstboot)) diff --git a/package/system/procd/files/hotplug-preinit.json b/package/system/procd/files/hotplug-preinit.json new file mode 100644 index 0000000..614b104 --- /dev/null +++ b/package/system/procd/files/hotplug-preinit.json @@ -0,0 +1,20 @@ +[ + [ "case", "ACTION", { + "add": [ + [ "if", + [ "has", "FIRMWARE" ], + [ + [ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ], + [ "load-firmware", "/lib/firmware" ], + [ "return" ] + ] + ], + ], + }, ], + [ "if", + [ "and", + [ "eq", "SUBSYSTEM", "button" ], + ], + [ "exec", "/etc/rc.button/failsafe" ] + ], +] diff --git a/package/system/procd/files/hotplug.json b/package/system/procd/files/hotplug.json new file mode 100644 index 0000000..27b4836 --- /dev/null +++ b/package/system/procd/files/hotplug.json @@ -0,0 +1,86 @@ +[ + [ "case", "ACTION", { + "add": [ + [ "if", + [ "and", + [ "has", "MAJOR" ], + [ "has", "MINOR" ], + ], + [ + [ "if", + [ "or", + [ "eq", "DEVNAME", + [ "null", "full", "ptmx", "zero" ], + ], + [ "regex", "DEVNAME", + [ "^gpio", "^hvc" ], + ], + ], + [ + [ "makedev", "/dev/%DEVNAME%", "0666" ], + [ "return" ], + ] + ], + [ "if", + [ "or", + [ "eq", "DEVNAME", "mapper/control" ], + [ "regex", "DEVPATH", "^ppp" ], + ], + [ + [ "makedev", "/dev/%DEVNAME%", "0600" ], + [ "return" ], + ], + ], + [ "if", + [ "has", "DEVNAME" ], + [ "makedev", "/dev/%DEVNAME%", "0644" ], + ], + ], + ], + [ "if", + [ "has", "FIRMWARE" ], + [ + [ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ], + [ "load-firmware", "/lib/firmware" ], + [ "return" ] + ] + ], + ], + "remove" : [ + [ "if", + [ "and", + [ "has", "DEVNAME" ], + [ "has", "MAJOR" ], + [ "has", "MINOR" ], + ], + [ "rm", "/dev/%DEVNAME%" ] + ] + ] + } ], + [ "if", + [ "eq", "SUBSYSTEM", "platform" ], + [ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ] + ], + [ "if", + [ "and", + [ "has", "BUTTON" ], + [ "eq", "SUBSYSTEM", "button" ], + ], + [ "button", "/etc/rc.button/%BUTTON%" ] + ], + [ "if", + [ "eq", "SUBSYSTEM", + [ "net", "input", "usb", "usbmisc", "ieee1394", "block", "atm", "zaptel", "tty", "button" ] + ], + [ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ] + ], + [ "if", + [ "and", + [ "eq", "SUBSYSTEM", "usb-serial" ], + [ "regex", "DEVNAME", + [ "^ttyUSB", "^ttyACM" ] + ], + ], + [ "exec", "/sbin/hotplug-call", "tty" ] + ], +] diff --git a/package/system/procd/files/nand-preinit.sh b/package/system/procd/files/nand-preinit.sh new file mode 100644 index 0000000..cf59624 --- /dev/null +++ b/package/system/procd/files/nand-preinit.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# Copyright (C) 2014 OpenWrt.org + +nand_takeover() { + . /lib/upgrade/nand.sh + mtd=$(find_mtd_index "$CI_UBIPART") + esize=$(cat /proc/mtd | grep mtd$mtd |cut -d" " -f 3) + [ -z "$esize" ] && return 1 + esize=$(printf "%d" 0x$esize) + for a in `seq 0 64`; do + mtd -o $((a * esize)) -l 400 dump /dev/mtd$mtd > /tmp/takeover.hdr + MAGIC=$(dd if=/tmp/takeover.hdr bs=1 skip=261 count=5 2> /dev/null) + SIZE=$(printf "%d" 0x$(dd if=/tmp/takeover.hdr bs=4 count=1 2> /dev/null | hexdump -v -n 4 -e '1/1 "%02x"')) + [ "$MAGIC" = "ustar" ] && { + mtd -o $((a * esize)) -l $((SIZE + 4)) dump /dev/mtd$mtd | dd bs=1 skip=4 of=/tmp/sysupgrade.tar + nand_do_upgrade_stage2 /tmp/sysupgrade.tar + } + done +} + +boot_hook_add initramfs nand_takeover diff --git a/package/system/procd/files/nand.sh b/package/system/procd/files/nand.sh new file mode 100644 index 0000000..3b1c749 --- /dev/null +++ b/package/system/procd/files/nand.sh @@ -0,0 +1,361 @@ +#!/bin/sh +# Copyright (C) 2014 OpenWrt.org +# + +. /lib/functions.sh + +# 'kernel' partition on NAND contains the kernel +CI_KERNPART="kernel" + +# 'ubi' partition on NAND contains UBI +CI_UBIPART="ubi" + +ubi_mknod() { + local dir="$1" + local dev="/dev/$(basename $dir)" + + [ -e "$dev" ] && return 0 + + local devid="$(cat $dir/dev)" + local major="${devid%%:*}" + local minor="${devid##*:}" + mknod "$dev" c $major $minor +} + +nand_find_volume() { + local ubidevdir ubivoldir + ubidevdir="/sys/devices/virtual/ubi/$1" + [ ! -d "$ubidevdir" ] && return 1 + for ubivoldir in $ubidevdir/${1}_*; do + [ ! -d "$ubivoldir" ] && continue + if [ "$( cat $ubivoldir/name )" = "$2" ]; then + basename $ubivoldir + ubi_mknod "$ubivoldir" + return 0 + fi + done +} + +nand_find_ubi() { + local ubidevdir ubidev mtdnum + mtdnum="$( find_mtd_index $1 )" + [ ! "$mtdnum" ] && return 1 + for ubidevdir in /sys/devices/virtual/ubi/ubi*; do + [ ! -d "$ubidevdir" ] && continue + cmtdnum="$( cat $ubidevdir/mtd_num )" + [ ! "$mtdnum" ] && continue + if [ "$mtdnum" = "$cmtdnum" ]; then + ubidev=$( basename $ubidevdir ) + ubi_mknod "$ubidevdir" + echo $ubidev + return 0 + fi + done +} + +nand_get_magic_long() { + dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"' +} + +get_magic_long_tar() { + ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null +} + +identify_magic() { + local magic=$1 + case "$magic" in + "55424923") + echo "ubi" + ;; + "31181006") + echo "ubifs" + ;; + "68737173") + echo "squashfs" + ;; + "d00dfeed") + echo "fit" + ;; + "4349"*) + echo "combined" + ;; + *) + echo "unknown $magic" + ;; + esac +} + + +identify() { + identify_magic $(nand_get_magic_long "$1" "${2:-0}") +} + +identify_tar() { + identify_magic $(get_magic_long_tar "$1" "$2") +} + +nand_restore_config() { + sync + local ubidev=$( nand_find_ubi $CI_UBIPART ) + local ubivol="$( nand_find_volume $ubidev rootfs_data )" + [ ! "$ubivol" ] && + ubivol="$( nand_find_volume $ubidev rootfs )" + mkdir /tmp/new_root + if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then + echo "mounting ubifs $ubivol failed" + rmdir /tmp/new_root + return 1 + fi + mv "$1" "/tmp/new_root/sysupgrade.tgz" + umount /tmp/new_root + sync + rmdir /tmp/new_root +} + +nand_upgrade_prepare_ubi() { + local rootfs_length="$1" + local rootfs_type="$2" + local has_kernel="${3:-0}" + local has_env="${4:-0}" + + local mtdnum="$( find_mtd_index "$CI_UBIPART" )" + if [ ! "$mtdnum" ]; then + echo "cannot find ubi mtd partition $CI_UBIPART" + return 1 + fi + + local ubidev="$( nand_find_ubi "$CI_UBIPART" )" + if [ ! "$ubidev" ]; then + ubiattach -m "$mtdnum" + sync + ubidev="$( nand_find_ubi "$CI_UBIPART" )" + fi + + if [ ! "$ubidev" ]; then + ubiformat /dev/mtd$mtdnum -y + ubiattach -m "$mtdnum" + sync + ubidev="$( nand_find_ubi "$CI_UBIPART" )" + [ "$has_env" -gt 0 ] && { + ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB + ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB + } + fi + + local kern_ubivol="$( nand_find_volume $ubidev kernel )" + local root_ubivol="$( nand_find_volume $ubidev rootfs )" + local data_ubivol="$( nand_find_volume $ubidev rootfs_data )" + + # remove ubiblock device of rootfs + local root_ubiblk="ubiblock${root_ubivol:3}" + if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then + echo "removing $root_ubiblk" + if ! ubiblock -r /dev/$root_ubivol; then + echo "cannot remove $root_ubiblk" + return 1; + fi + fi + + # kill volumes + [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true + [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true + [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true + + # update kernel + if [ "$has_kernel" = "1" ]; then + if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then + echo "cannot create kernel volume" + return 1; + fi + fi + + # update rootfs + local root_size_param + if [ "$rootfs_type" = "ubifs" ]; then + root_size_param="-m" + else + root_size_param="-s $rootfs_length" + fi + if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then + echo "cannot create rootfs volume" + return 1; + fi + + # create rootfs_data for non-ubifs rootfs + if [ "$rootfs_type" != "ubifs" ]; then + if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then + echo "cannot initialize rootfs_data volume" + return 1 + fi + fi + sync + return 0 +} + +nand_do_upgrade_success() { + local conf_tar="/tmp/sysupgrade.tgz" + + sync + [ -f "$conf_tar" ] && nand_restore_config "$conf_tar" + echo "sysupgrade successful" + reboot -f +} + +# Flash the UBI image to MTD partition +nand_upgrade_ubinized() { + local ubi_file="$1" + local mtdnum="$(find_mtd_index "$CI_UBIPART")" + + [ ! "$mtdnum" ] && { + CI_UBIPART="rootfs" + mtdnum="$(find_mtd_index "$CI_UBIPART")" + } + + if [ ! "$mtdnum" ]; then + echo "cannot find mtd device $CI_UBIPART" + reboot -f + fi + + local mtddev="/dev/mtd${mtdnum}" + ubidetach -p "${mtddev}" || true + sync + ubiformat "${mtddev}" -y -f "${ubi_file}" + ubiattach -p "${mtddev}" + nand_do_upgrade_success +} + +# Write the UBIFS image to UBI volume +nand_upgrade_ubifs() { + local rootfs_length=`(cat $1 | wc -c) 2> /dev/null` + + nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0" + + local ubidev="$( nand_find_ubi "$CI_UBIPART" )" + local root_ubivol="$(nand_find_volume $ubidev rootfs)" + ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1 + + nand_do_upgrade_success +} + +nand_upgrade_tar() { + local tar_file="$1" + local board_name="$(cat /tmp/sysinfo/board_name)" + local kernel_mtd="$(find_mtd_index $CI_KERNPART)" + + local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null` + local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null` + + local rootfs_type="$(identify_tar "$tar_file" sysupgrade-$board_name/root)" + + local has_kernel=1 + local has_env=0 + + [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && { + tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - $CI_KERNPART + } + [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0 + + nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env" + + local ubidev="$( nand_find_ubi "$CI_UBIPART" )" + [ "$has_kernel" = "1" ] && { + local kern_ubivol="$(nand_find_volume $ubidev kernel)" + tar xf $tar_file sysupgrade-$board_name/kernel -O | \ + ubiupdatevol /dev/$kern_ubivol -s $kernel_length - + } + + local root_ubivol="$(nand_find_volume $ubidev rootfs)" + tar xf $tar_file sysupgrade-$board_name/root -O | \ + ubiupdatevol /dev/$root_ubivol -s $rootfs_length - + + nand_do_upgrade_success +} + +# Recognize type of passed file and start the upgrade process +nand_do_upgrade_stage2() { + local file_type=$(identify $1) + + [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs" + + case "$file_type" in + "ubi") nand_upgrade_ubinized $1;; + "ubifs") nand_upgrade_ubifs $1;; + *) nand_upgrade_tar $1;; + esac +} + +nand_upgrade_stage2() { + [ $1 = "nand" ] && { + [ -f "$2" ] && { + touch /tmp/sysupgrade + + killall -9 telnetd + killall -9 dropbear + killall -9 ash + + kill_remaining TERM + sleep 3 + kill_remaining KILL + + sleep 1 + + if [ -n "$(rootfs_type)" ]; then + v "Switching to ramdisk..." + run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2" + else + nand_do_upgrade_stage2 $2 + fi + return 0 + } + echo "Nand upgrade failed" + exit 1 + } +} + +nand_upgrade_stage1() { + [ -f /tmp/sysupgrade-nand-path ] && { + path="$(cat /tmp/sysupgrade-nand-path)" + [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] && + rm $CONF_TAR + + ubus call system nandupgrade "{\"path\": \"$path\" }" + exit 0 + } +} +append sysupgrade_pre_upgrade nand_upgrade_stage1 + +# Check if passed file is a valid one for NAND sysupgrade. Currently it accepts +# 3 types of files: +# 1) UBI - should contain an ubinized image, header is checked for the proper +# MAGIC +# 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume, +# header is checked for the proper MAGIC +# 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty +# "CONTROL" file (at this point its content isn't verified) +# +# You usually want to call this function in platform_check_image. +# +# $(1): board name, used in case of passing TAR file +# $(2): file to be checked +nand_do_platform_check() { + local board_name="$1" + local tar_file="$2" + local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null` + local file_type="$(identify $2)" + + [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && { + echo "Invalid sysupgrade file." + return 1 + } + + return 0 +} + +# Start NAND upgrade process +# +# $(1): file to be used for upgrade +nand_do_upgrade() { + echo -n $1 > /tmp/sysupgrade-nand-path + cp /sbin/upgraded /tmp/ + nand_upgrade_stage1 +} diff --git a/package/system/procd/files/procd.sh b/package/system/procd/files/procd.sh new file mode 100644 index 0000000..e83e75c --- /dev/null +++ b/package/system/procd/files/procd.sh @@ -0,0 +1,430 @@ +# procd API: +# +# procd_open_service(name, [script]): +# Initialize a new procd command message containing a service with one or more instances +# +# procd_close_service() +# Send the command message for the service +# +# procd_open_instance([name]): +# Add an instance to the service described by the previous procd_open_service call +# +# procd_set_param(type, [value...]) +# Available types: +# command: command line (array). +# respawn info: array with 3 values $fail_threshold $restart_timeout $max_fail +# env: environment variable (passed to the process) +# data: arbitrary name/value pairs for detecting config changes (table) +# file: configuration files (array) +# netdev: bound network device (detects ifindex changes) +# limits: resource limits (passed to the process) +# user info: array with 1 values $username +# +# No space separation is done for arrays/tables - use one function argument per command line argument +# +# procd_close_instance(): +# Complete the instance being prepared +# +# procd_kill(service, [instance]): +# Kill a service instance (or all instances) +# + +. $IPKG_INSTROOT/usr/share/libubox/jshn.sh + +_PROCD_SERVICE= + +_procd_call() { + local old_cb + + json_set_namespace procd old_cb + "$@" + json_set_namespace $old_cb +} + +_procd_wrapper() { + while [ -n "$1" ]; do + eval "$1() { _procd_call _$1 \"\$@\"; }" + shift + done +} + +_procd_ubus_call() { + local cmd="$1" + + [ -n "$PROCD_DEBUG" ] && json_dump >&2 + ubus call service "$cmd" "$(json_dump)" + json_cleanup +} + +_procd_open_service() { + local name="$1" + local script="$2" + + _PROCD_SERVICE="$name" + _PROCD_INSTANCE_SEQ=0 + + json_init + json_add_string name "$name" + [ -n "$script" ] && json_add_string script "$script" + json_add_object instances +} + +_procd_close_service() { + json_close_object + service_triggers + _procd_ubus_call set +} + +_procd_add_array_data() { + while [ "$#" -gt 0 ]; do + json_add_string "" "$1" + shift + done +} + +_procd_add_array() { + json_add_array "$1" + shift + _procd_add_array_data "$@" + json_close_array +} + +_procd_add_table_data() { + while [ -n "$1" ]; do + local var="${1%%=*}" + local val="${1#*=}" + [ "$1" = "$val" ] && val= + json_add_string "$var" "$val" + shift + done +} + +_procd_add_table() { + json_add_object "$1" + shift + _procd_add_table_data "$@" + json_close_object +} + +_procd_open_instance() { + local name="$1"; shift + + _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))" + name="${name:-instance$_PROCD_INSTANCE_SEQ}" + json_add_object "$name" + [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1" +} + +_procd_open_trigger() { + json_add_array "triggers" +} + +_procd_open_validate() { + json_add_array "validate" +} + +_procd_add_jail() { + json_add_object "jail" + json_add_string name "$1" + json_add_string root "/tmp/.jail/$1" + + shift + + for a in $@; do + case $a in + log) json_add_boolean "log" "1";; + ubus) json_add_boolean "ubus" "1";; + procfs) json_add_boolean "procfs" "1";; + sysfs) json_add_boolean "sysfs" "1";; + esac + done + json_add_object "mount" + json_close_object + json_close_object +} + +_procd_add_jail_mount() { + local _json_no_warning=1 + + json_select "jail" + [ $? = 0 ] || return + json_select "mount" + [ $? = 0 ] || { + json_select .. + return + } + for a in $@; do + json_add_string "$a" "0" + done + json_select .. + json_select .. +} + +_procd_add_jail_mount_rw() { + local _json_no_warning=1 + + json_select "jail" + [ $? = 0 ] || return + json_select "mount" + [ $? = 0 ] || { + json_select .. + return + } + for a in $@; do + json_add_string "$a" "1" + done + json_select .. + json_select .. +} + +_procd_set_param() { + local type="$1"; shift + + case "$type" in + env|data|limits) + _procd_add_table "$type" "$@" + ;; + command|netdev|file|respawn|watch) + _procd_add_array "$type" "$@" + ;; + error) + json_add_array "$type" + json_add_string "" "$@" + json_close_array + ;; + nice) + json_add_int "$type" "$1" + ;; + user|seccomp) + json_add_string "$type" "$1" + ;; + stdout|stderr) + json_add_boolean "$type" "$1" + ;; + esac +} + +_procd_add_interface_trigger() { + json_add_array + _procd_add_array_data "$1" + shift + + json_add_array + _procd_add_array_data "if" + + json_add_array + _procd_add_array_data "eq" "interface" "$1" + shift + json_close_array + + json_add_array + _procd_add_array_data "run_script" "$@" + json_close_array + + json_close_array + json_close_array +} + +_procd_add_reload_interface_trigger() { + local script=$(readlink "$initscript") + local name=$(basename ${script:-$initscript}) + + _procd_open_trigger + _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload + _procd_close_trigger +} + +_procd_add_config_trigger() { + json_add_array + _procd_add_array_data "$1" + shift + + json_add_array + _procd_add_array_data "if" + + json_add_array + _procd_add_array_data "eq" "package" "$1" + shift + json_close_array + + json_add_array + _procd_add_array_data "run_script" "$@" + json_close_array + + json_close_array + + json_close_array +} + +_procd_add_raw_trigger() { + json_add_array + _procd_add_array_data "$1" + shift + local timeout=$1 + shift + + json_add_array + json_add_array + _procd_add_array_data "run_script" "$@" + json_close_array + json_close_array + + json_add_int "" "$timeout" + + json_close_array +} + +_procd_add_reload_trigger() { + local script=$(readlink "$initscript") + local name=$(basename ${script:-$initscript}) + local file + + _procd_open_trigger + for file in "$@"; do + _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload + done + _procd_close_trigger +} + +_procd_add_validation() { + _procd_open_validate + $@ + _procd_close_validate +} + +_procd_append_param() { + local type="$1"; shift + local _json_no_warning=1 + + json_select "$type" + [ $? = 0 ] || { + _procd_set_param "$type" "$@" + return + } + case "$type" in + env|data|limits) + _procd_add_table_data "$@" + ;; + command|netdev|file|respawn|watch) + _procd_add_array_data "$@" + ;; + error) + json_add_string "" "$@" + ;; + esac + json_select .. +} + +_procd_close_instance() { + json_close_object +} + +_procd_close_trigger() { + json_close_array +} + +_procd_close_validate() { + json_close_array +} + +_procd_add_instance() { + _procd_open_instance + _procd_set_param command "$@" + _procd_close_instance +} + +_procd_kill() { + local service="$1" + local instance="$2" + + json_init + [ -n "$service" ] && json_add_string name "$service" + [ -n "$instance" ] && json_add_string instance "$instance" + _procd_ubus_call delete +} + +procd_open_data() { + local name="$1" + json_set_namespace procd __procd_old_cb + json_add_object data +} + +procd_close_data() { + json_close_object + json_set_namespace $__procd_old_cb +} + +_procd_set_config_changed() { + local package="$1" + + json_init + json_add_string type config.change + json_add_object data + json_add_string package "$package" + json_close_object + + ubus call service event "$(json_dump)" +} + +procd_add_mdns_service() { + local service proto port + service=$1; shift + proto=$1; shift + port=$1; shift + json_add_object "${service}_$port" + json_add_string "service" "_$service._$proto.local" + json_add_int port "$port" + [ -n "$1" ] && { + json_add_array txt + for txt in $@; do json_add_string "" $txt; done + json_select .. + } + json_select .. +} + +procd_add_mdns() { + procd_open_data + json_add_object "mdns" + procd_add_mdns_service $@ + json_close_object + procd_close_data +} + +uci_validate_section() +{ + local _package="$1" + local _type="$2" + local _name="$3" + local _result + local _error + shift; shift; shift + _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null` + _error=$? + eval "$_result" + [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null` + return $_error +} + +_procd_wrapper \ + procd_open_service \ + procd_close_service \ + procd_add_instance \ + procd_add_raw_trigger \ + procd_add_config_trigger \ + procd_add_interface_trigger \ + procd_add_reload_trigger \ + procd_add_reload_interface_trigger \ + procd_open_trigger \ + procd_close_trigger \ + procd_open_instance \ + procd_close_instance \ + procd_open_validate \ + procd_close_validate \ + procd_add_jail \ + procd_add_jail_mount \ + procd_add_jail_mount_rw \ + procd_set_param \ + procd_append_param \ + procd_add_validation \ + procd_set_config_changed \ + procd_kill diff --git a/package/system/procd/files/reload_config b/package/system/procd/files/reload_config new file mode 100644 index 0000000..8d1cdb1 --- /dev/null +++ b/package/system/procd/files/reload_config @@ -0,0 +1,15 @@ +#!/bin/sh +rm -rf /var/run/config.check +mkdir -p /var/run/config.check +for config in /etc/config/*; do + file=${config##*/} + uci show "${file##*/}" > /var/run/config.check/$file +done +MD5FILE=/var/run/config.md5 +[ -f $MD5FILE ] && { + for c in `md5sum -c $MD5FILE 2>/dev/null| grep FAILED | cut -d: -f1`; do + ubus call service event "{ \"type\": \"config.change\", \"data\": { \"package\": \"$(basename $c)\" }}" + done +} +md5sum /var/run/config.check/* > $MD5FILE +rm -rf /var/run/config.check -- cgit v1.2.3