functions 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/bin/bash
  2. # Location of files which contains list of protected files
  3. PROTECTED_FILE_LIST='/etc/qubes/protected-files.d'
  4. qsvc() {
  5. # Returns whether a service is enabled.
  6. # Usage: qsvc <nameofservice>
  7. #
  8. # Must only be used after qubes-sysinit has started.
  9. # See qsvc_early for more information.
  10. local count=100
  11. while [ ! -e /var/run/qubes-service-environment ] ; do
  12. if [ "$count" = "0" ] ; then
  13. echo "qsvc: Warning: qubes-sysinit has not finished executing yet" >&2
  14. break
  15. fi
  16. sleep 0.1
  17. count=$(( count - 1 ))
  18. done
  19. [ -e /var/run/qubes-service/"$1" ]
  20. }
  21. under_systemd() {
  22. pidof systemd >/dev/null 2>&1
  23. }
  24. systemd_version_changed() {
  25. under_systemd || return
  26. systemd_pkg_version=`systemctl --version|head -n 1`
  27. if dmesg | grep -q "$systemd_pkg_version running in system mode."; then
  28. return 1
  29. fi
  30. return 0
  31. }
  32. possibly_run_save_script() {
  33. ENCODED_SCRIPT=$(qubesdb-read /qubes-save-script)
  34. if [ -z "$ENCODED_SCRIPT" ] ; then return ; fi
  35. tmpfile=$(mktemp /tmp/qubes-save-script.XXXXXXXXX)
  36. echo $ENCODED_SCRIPT|perl -e 'use MIME::Base64 qw(decode_base64); local($/) = undef;print decode_base64(<STDIN>)' >"$tmpfile"
  37. chmod 755 "$tmpfile"
  38. DISPLAY=:0 su - user -c "$tmpfile"
  39. ret=$?
  40. rm -f "$tmpfile"
  41. return $ret
  42. }
  43. have_qubesdb() {
  44. # Tests whether qubesdb-read exists and can be executed.
  45. type qubesdb-read >/dev/null 2>&1
  46. }
  47. have_qrexec_agent() {
  48. # Tests whether qrexec-agent exists and can be executed.
  49. PATH=/usr/lib/qubes type qrexec-agent >/dev/null 2>&1
  50. }
  51. qubes_vm_type() {
  52. qubesdb-read /qubes-vm-type
  53. }
  54. is_netvm() {
  55. [ "$(qubes_vm_type)" = "NetVM" ]
  56. }
  57. is_appvm() {
  58. [ "$(qubes_vm_type)" = "AppVM" ]
  59. }
  60. is_proxyvm() {
  61. [ "$(qubes_vm_type)" = "ProxyVM" ]
  62. }
  63. is_templatevm() {
  64. [ "$(qubes_vm_type)" = "TemplateVM" ]
  65. }
  66. is_dispvm() {
  67. [ "$(qubes_vm_type)" = "DisposableVM" ]
  68. }
  69. is_fully_persistent() {
  70. [ "$(qubesdb-read /qubes-vm-persistence)" = "full" ]
  71. }
  72. is_rwonly_persistent() {
  73. [ "$(qubesdb-read /qubes-vm-persistence)" = "rw-only" ]
  74. }
  75. is_updateable() {
  76. [ "$(qubesdb-read /qubes-vm-updateable)" = "True" ]
  77. }
  78. reload_random_seed() {
  79. qubesdb-read /qubes-random-seed | base64 -d > /dev/urandom
  80. qubesdb-rm /qubes-random-seed
  81. }
  82. is_protected_file() {
  83. grep -Fxrq --exclude='*.rpmsave' --exclude='*~' --exclude='*.rpmnew' --exclude='*.rpmold' -- "${1}" "$PROTECTED_FILE_LIST" 2>/dev/null
  84. }
  85. umount_retry() {
  86. local count=5
  87. while mountpoint -q "$1" ; do
  88. if umount "$1" ; then break ; fi
  89. echo "Something prevents unmounting $1:" >&2
  90. fuser -vmM "$1" >&2
  91. if [ "$count" = "0" ] ; then
  92. return 1
  93. fi
  94. sleep 5
  95. count=$(( count - 1 ))
  96. done
  97. return 0
  98. }