diff options
-rw-r--r-- | src/session.sh | 140 |
1 files changed, 70 insertions, 70 deletions
diff --git a/src/session.sh b/src/session.sh index bb6347b..3dbf875 100644 --- a/src/session.sh +++ b/src/session.sh @@ -26,6 +26,54 @@ session_mountdir= session_atexit= session_sigs= +session_handle_sig() +{ + local sig="${1}" + shift 1 + + session_end + + exit $((128 + ${sig})) +} + +session_set_sigs() +{ + local i= + local sig= + + # We need the signal *number* in the signal handler. The only portable + # and easy way to get the number of a named signal is to search for it + # as in the following loop hack. + i=0 + session_sigs='' + while [ ${i} -lt 127 ]; do + i=$((${i} + 1)) + sig="$(kill -l ${i} 2>/dev/null)" || continue + case "${sig}" in + 'HUP' | 'INT' | 'QUIT' | 'ABRT' | 'ALRM' | 'TERM') + session_sigs="${session_sigs} ${i}" + trap "session_handle_sig ${i}" ${i} + ;; + esac + done +} + +session_mount() +{ + local fs= + local dir= + local fstype= + local options= + + while read fs dir fstype options; do + [ "x${dir}" = 'x' ] && continue + mount -t "${fstype}" -o "${options}" -- "${fs}" \ + "${session_root}/${dir}" + done <<-EOF + $(profile_get_fstab "${session_arch}" "${session_plat}") + EOF +} + session_begin() { local root="${1}" @@ -85,6 +133,28 @@ session_begin() return 0 } +session_umount() +{ + local fs= + local dir= + local fstype= + local options= + + while read fs dir fstype options; do + [ "x${dir}" = 'x' ] && continue + # umount sometimes complains that the /dev file system is busy. + # Here's a kludge to try to handle that. We better make sure + # bind mounts get unmounted; otherwise, `rm -Rf ${root}` can be + # painful. + while ! umount -- "${session_root}/${dir}"; do + sleep 1 + done + done <<-EOF + $(profile_get_fstab "${session_arch}" "${session_plat}" | \ + sed -n '1!G;h;$p') + EOF +} + session_end() { trap : ${session_sigs} @@ -147,73 +217,3 @@ session_exec() return ${?} } - -session_mount() -{ - local fs= - local dir= - local fstype= - local options= - - while read fs dir fstype options; do - [ "x${dir}" = 'x' ] && continue - mount -t "${fstype}" -o "${options}" -- "${fs}" \ - "${session_root}/${dir}" - done <<-EOF - $(profile_get_fstab "${session_arch}" "${session_plat}") - EOF -} - -session_umount() -{ - local fs= - local dir= - local fstype= - local options= - - while read fs dir fstype options; do - [ "x${dir}" = 'x' ] && continue - # umount sometimes complains that the /dev file system is busy. - # Here's a kludge to try to handle that. We better make sure - # bind mounts get unmounted; otherwise, `rm -Rf ${root}` can be - # painful. - while ! umount -- "${session_root}/${dir}"; do - sleep 1 - done - done <<-EOF - $(profile_get_fstab "${session_arch}" "${session_plat}" | \ - sed -n '1!G;h;$p') - EOF -} - -session_set_sigs() -{ - local i= - local sig= - - # We need the signal *number* in the signal handler. The only portable - # and easy way to get the number of a named signal is to search for it - # as in the following loop hack. - i=0 - session_sigs='' - while [ ${i} -lt 127 ]; do - i=$((${i} + 1)) - sig="$(kill -l ${i} 2>/dev/null)" || continue - case "${sig}" in - 'HUP' | 'INT' | 'QUIT' | 'ABRT' | 'ALRM' | 'TERM') - session_sigs="${session_sigs} ${i}" - trap "session_handle_sig ${i}" ${i} - ;; - esac - done -} - -session_handle_sig() -{ - local sig="${1}" - shift 1 - - session_end - - exit $((128 + ${sig})) -} |