~sourcemage/sorcery

e9a142540638bf09792acc31fd8d715e3b59d962 — Ismael Luceno 8 months ago 9f94574
Implement logging by reusing file descriptors

Avoid opening files, references to /proc/self/fd/ may cause permission
issues when dropping privileges.
M usr/sbin/alter => usr/sbin/alter +4 -6
@@ 43,19 43,17 @@ EOF
  [ -n "$COM_DATA" ] && echo "$COM_DATA" > "$C_LOG"
  message "${MESSAGE_COLOR}Altering...${DEFAULT_COLOR}"

  reserve_fds VOYEUR_STDOUT VOYEUR_STDERR
  if [ "$VOYEUR" == "on" -a -z "$SILENT" ]; then
    VOYEUR_STDOUT=/dev/stdout
    VOYEUR_STDERR=/dev/stderr
  else
    VOYEUR_STDOUT=/dev/null
    VOYEUR_STDERR=/dev/null
    eval "exec $((VOYEUR_STDOUT))>&1 $((VOYEUR_STDERR))>&2"
  fi

  invoke_installwatch
  (
    echo "$@"
    echo "$@" | bash
  ) 2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) > >(tee -a $C_LOG > $VOYEUR_STDOUT)
  ) 2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
  devoke_installwatch
  touch $IW_LOG $MD5_LOG $C_LOG_COMP
  create_install_log $IW_LOG $INST_LOG

M usr/sbin/delve => usr/sbin/delve +40 -42
@@ 102,11 102,16 @@ delve_log_helper() {
  CASTFS_DBGLOG=${CASTFS_DBGLOG:="/tmp/sorcery/delve/$SPELL.iwdbglog"}
  C_FIFO=${C_FIFO:="/dev/stdout"}
  IW_LOG=${IW_LOG:="/tmp/sorcery/delve/$SPELL.iw"}
  VOYEUR_STDOUT=${VOYEUR_STDOUT:="/dev/stdout"}
  VOYEUR_STDERR=${VOYEUR_STDERR:="/dev/stderr"}
  S_PWD=${S_PWD:="/tmp/sorcery/delve/$SPELL.pwd"}
  touch $C_LOG

  if [ -z "$VOYEUR_STDOUT" ]; then
     reserve_fds VOYEUR_STDOUT
     eval "exec $((VOYEUR_STDOUT))>&1"
  fi
  if [ -z "$VOYEUR_STDERR" ]; then
     reserve_fds VOYEUR_STDERR
     eval "exec $((VOYEUR_STDERR))>&2"
  fi
}

#---------------------------------------------------------------------------


@@ 151,22 156,20 @@ delve_build() {
  debug "delve" "Running $FUNCNAME"
  delve_is_valid_step BUILD 1 2 || return 1
  delve_log_helper
  (
    run_build
  ) 3> >(tee -a $C_LOG >> /dev/stdout) \
    2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) \
     > >(tee -a $C_LOG > $VOYEUR_STDOUT)
  ( run_build ) \
    3> >(tee -a "$C_LOG") \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
}

delve_pre_install() {
  debug "delve" "Running $FUNCNAME"
  delve_is_valid_step PRE_INSTALL 2 || return 1
  delve_log_helper
  (
    run_pre_install
  ) 3> >(tee -a $C_LOG >> /dev/stdout) \
    2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) \
     > >(tee -a $C_LOG > $VOYEUR_STDOUT)
  ( run_pre_install ) \
    3> >(tee -a "$C_LOG") \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
}

delve_staged_install_setup() {


@@ 197,11 200,11 @@ delve_install() {
    else
      $CHROOT_CMD delve $SPELL INSTALL
    fi
  ) 3> >(tee -a $C_LOG >> /dev/stdout) \
    2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) \
     > >(tee -a $C_LOG > $VOYEUR_STDOUT)
  ) 3> >(tee -a "$C_LOG") \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
  local rc=$?
  if [[ $CHROOT_CMD ]]
  if [[ -n "$CHROOT_CMD" && "$STAGED_INSTALL" = castfs ]]
  then
    devoke_stage_root
  fi &&


@@ 221,11 224,11 @@ delve_install_extras() {
    else
      $CHROOT_CMD delve $SPELL INSTALL_EXTRAS
    fi
  ) 3> >(tee -a $C_LOG >> /dev/stdout) \
    2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) \
     > >(tee -a $C_LOG > $VOYEUR_STDOUT)
  ) 3> >(tee -a "$C_LOG") \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
  local rc=$?
  if [[ $CHROOT_CMD ]]
  if [[ -n "$CHROOT_CMD" && "$STAGED_INSTALL" = castfs ]]
  then
    devoke_stage_root
  fi &&


@@ 242,33 245,30 @@ delve_post_build() {
    return 1
  }
  cd $SOURCE_DIRECTORY
  (
    run_post_build
  ) 3> >(tee -a $C_LOG >> /dev/stdout) \
    2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) \
     > >(tee -a $C_LOG > $VOYEUR_STDOUT)
  ( run_post_build ) \
    3> >(tee -a "$C_LOG") \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
}

delve_post_install() {
  debug "delve" "Running $FUNCNAME"
  delve_is_valid_step POST_INSTALL 1 2 || return 1
  delve_log_helper
  (
    run_post_install
  ) 3> >(tee -a $C_LOG >> /dev/stdout) \
    2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) \
     > >(tee -a $C_LOG > $VOYEUR_STDOUT)
  ( run_post_install ) \
    3> >(tee -a "$C_LOG") \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
}

delve_transfer() {
  debug "delve" "Running $FUNCNAME"
  delve_is_valid_step TRANSFER 2 || return 1
  delve_log_helper
  (
    run_transfer
  ) 3> >(tee -a $C_LOG >> /dev/stdout) \
    2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) \
     > >(tee -a $C_LOG > $VOYEUR_STDOUT)
  ( run_transfer ) \
    3> >(tee -a "$C_LOG") \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
}

delve_final() {


@@ 277,12 277,10 @@ delve_final() {
  delve_log_helper
  devoke_installwatch &&
  STAGED_INSTALL=off
  (
    run_final
  ) 3> >(tee -a $C_LOG >> /dev/stdout) \
    2> >(tee -a $C_LOG 1>&2 > $VOYEUR_STDERR) \
     > >(tee -a $C_LOG > $VOYEUR_STDOUT)

  ( run_final ) \
    3> >(tee -a "$C_LOG") \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
}

delve_triggers() {

M var/lib/sorcery/modules/build_api/api1 => var/lib/sorcery/modules/build_api/api1 +5 -12
@@ 30,19 30,13 @@ run_build_spell() {
  STAGED_INSTALL=off
  rm -f $C_LOG
  touch $C_LOG
  reserve_fds VOYEUR_STDOUT VOYEUR_STDERR
  if [[ $SCREEN_NAME ]] ; then
    screen_new_window "$SCREEN_NAME" $SCREEN_CAST_WIN "cast $SPELL" \
      tail -f -s 0.1 $C_LOG
    screen_switch_window "$SCREEN_NAME" $SCREEN_MAIN_WIN

    VOYEUR_STDOUT=/dev/null
    VOYEUR_STDERR=/dev/null
  elif [ "$VOYEUR" == "on" -a -z "$SILENT" ] ; then
    VOYEUR_STDOUT=/dev/stdout
    VOYEUR_STDERR=/dev/stderr
  else
    VOYEUR_STDOUT=/dev/null
    VOYEUR_STDERR=/dev/null
    eval "exec $((VOYEUR_STDOUT))>&1 $((VOYEUR_STDERR))>&2"
  fi
  # should OPTS get set here?



@@ 122,10 116,9 @@ run_build()  {
  message "$SPELL    $VERSION"

  run_config_loc
  (
    run_spell_file BUILD build
  ) 2> >(tee -a $C_LOG 1>&2 >> $VOYEUR_STDERR) \
     > >(tee -a $C_LOG >> $VOYEUR_STDOUT) # see bug 7201
  ( run_spell_file BUILD build ) \
    2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
     > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")

  if  [  "$?"  !=  0  ];  then
    message  "${PROBLEM_COLOR}"      \

M var/lib/sorcery/modules/build_api/api2 => var/lib/sorcery/modules/build_api/api2 +8 -16
@@ 36,24 36,16 @@ run_build_spell() {
  export VOYEUR_STDOUT VOYEUR_STDERR
  export __DELVE_IN_CAST=on

  rm  -f $CASTFS_DBGLOG
  touch $CASTFS_DBGLOG
  rm  -f $C_LOG
  touch $C_LOG
  >"$CASTFS_DBGLOG"
  >"$C_LOG"

  reserve_fds VOYEUR_STDOUT VOYEUR_STDERR
  if [[ $SCREEN_NAME ]] ; then
    screen_new_window "$SCREEN_NAME" $SCREEN_CAST_WIN "cast $SPELL" \
      tail -f -s 0.1 $C_LOG
      tail -f -s 0.1 "$C_LOG"
    screen_switch_window "$SCREEN_NAME" $SCREEN_MAIN_WIN

    VOYEUR_STDOUT=/dev/null
    VOYEUR_STDERR=/dev/null
  elif [ "$VOYEUR" == "on" -a -z "$SILENT" ] ; then
    VOYEUR_STDOUT=/dev/stdout
    VOYEUR_STDERR=/dev/stderr
  else
    VOYEUR_STDOUT=/dev/null
    VOYEUR_STDERR=/dev/null
    eval "exec $((VOYEUR_STDOUT))>&1 $((VOYEUR_STDERR))>&2"
  fi

  if [[ -z $STAGED_INSTALL ]]


@@ 92,9 84,9 @@ run_build_spell() {
      devoke_installwatch                   &&
      STAGED_INSTALL=off                    &&
      run_final | append_to_notice_log      || return 3
    ) 3> >(tee -a $C_LOG >> /dev/stdout) \
      2> >(tee -a $C_LOG 1>&2 >> $VOYEUR_STDERR) \
       > >(tee -a $C_LOG >> $VOYEUR_STDOUT) \
    ) 3> >(tee -a "$C_LOG") \
      2> >(tee -a "$C_LOG" >& "$VOYEUR_STDERR") \
       > >(tee -a "$C_LOG" >& "$VOYEUR_STDOUT")
  )
  rs=$?
  unset __DELVE_IN_CAST

M var/lib/sorcery/modules/libmisc => var/lib/sorcery/modules/libmisc +17 -0
@@ 1764,6 1764,23 @@ show_file() {
}

#---------------------------------------------------------------------
##
## Reserve file descriptors; connects them to /dev/null.
##
#---------------------------------------------------------------------
SORCERY_LAST_RESERVED_FD=2
reserve_fds() {
  while [ $# -gt 0 ]; do
    if ! : >& "$((SORCERY_LAST_RESERVED_FD))"; then
      eval "$1=$((SORCERY_LAST_RESERVED_FD)) &&
            exec $((SORCERY_LAST_RESERVED_FD))> /dev/null" &&
      shift
    fi 2>/dev/null
    let ++SORCERY_LAST_RESERVED_FD
  done
}

#---------------------------------------------------------------------
## @param title
## @Globals TERM
##