snappy-app-dev 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/sh
  2. # udev callout to allow a snap to access a device node
  3. set -e
  4. # debugging
  5. #exec >>/tmp/snap-device-helper.log
  6. #exec 2>&1
  7. #set -x
  8. # end debugging
  9. ACTION="$1"
  10. APPNAME="$2"
  11. DEVPATH="$3"
  12. MAJMIN="$4"
  13. [ -n "$APPNAME" ] || { echo "no app name given" >&2; exit 1; }
  14. [ -n "$DEVPATH" ] || { echo "no devpath given" >&2; exit 1; }
  15. [ -n "$MAJMIN" ] || { echo "no major/minor given" >&2; exit 0; }
  16. NOSNAP="${APPNAME#snap_}"
  17. [ "$NOSNAP" != "$APPNAME" ] || { echo "malformed appname $APPNAME" >&2; exit 1; }
  18. # FIXME: this will break for instances that are called "hook" :(
  19. # Handle hooks first, the the nosnap part looks like this:
  20. # - "$snap_hook_$hookname"
  21. # - "$snap_$instance_hook_$hookname
  22. # we need to make sure we change this to:
  23. # - "$snap_hook.$hookname"
  24. # - "$snap_$instance_hook.$hookname"
  25. if [ -z "${NOSNAP##*_hook_hook_*}" ]; then
  26. # $instance is 'hook'; $snap_hook_hook.$hookname -> $snap_hook_hook.$hookname
  27. NOSNAP="${NOSNAP%_hook_*}_hook.${NOSNAP#*_hook_hook_}"
  28. elif [ -z "${NOSNAP##*_hook_*}" ]; then
  29. # $snap_$instance_hook_$hookname -> $snap_$instance_hook.$hookname
  30. NOSNAP="${NOSNAP%_hook_*}_hook.${NOSNAP#*_hook_}"
  31. fi
  32. # Now deal with app/instance untangling
  33. if [ "${NOSNAP#*_*_}" = "${NOSNAP}" ]; then
  34. # snap_<snap>_<app> -> snap.<snap>.<app>
  35. SNAPAPP="snap.${NOSNAP%_*}.${NOSNAP#*_}"
  36. else
  37. # snap_<snap>_<instance>_<app> -> snap.<snap>_<instance>.<app>
  38. SNAPAPP="snap.${NOSNAP%_*}.${NOSNAP#*_*_}"
  39. fi
  40. DEVICES_CGROUP=${DEVICES_CGROUP:="/sys/fs/cgroup/devices"}
  41. app_dev_cgroup="$DEVICES_CGROUP/$SNAPAPP"
  42. # The cgroup is only present after snap start so ignore any cgroup changes
  43. # (eg, 'add' on boot, hotplug, hotunplug) when the cgroup doesn't exist
  44. # yet. LP: #1762182.
  45. if [ ! -e "$app_dev_cgroup" ]; then
  46. exit 0
  47. fi
  48. # check if it's a block or char dev
  49. if [ "${DEVPATH#*/block/}" != "$DEVPATH" ]; then
  50. type="b"
  51. else
  52. type="c"
  53. fi
  54. acl="$type $MAJMIN rwm"
  55. case "$ACTION" in
  56. add|change)
  57. echo "$acl" > "$app_dev_cgroup/devices.allow"
  58. ;;
  59. remove)
  60. echo "$acl" > "$app_dev_cgroup/devices.deny"
  61. ;;
  62. *)
  63. echo "ERROR: unknown action $ACTION" >&2
  64. exit 1 ;;
  65. esac