Skip to content
Snippets Groups Projects
Select Git revision
  • 708410d2fdb0b0c828866c41d6ba384ac75a3d68
  • master default
  • jedian1806
  • jedian
  • Strozzi
5 results

infiniteLoop.s

Blame
  • Forked from Roberto Hexsel / cMIPS
    Source project has a limited visibility.
    integrity-checker.sh 5.05 KiB
    #!/bin/bash
    # Copyright (C) 2004-2010 Centro de Computacao Cientifica e Software Livre
    # Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
    #
    # This file is part of collect-agent
    #
    # collect-agent is free software; you can redistribute it and/or
    # modify it under the terms of the GNU General Public License
    # as published by the Free Software Foundation; either version 2
    # of the License, or (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    # USA.
    
    
    PREFIX="$(dirname $0)"
    PREFIXESC="$(echo ${PREFIX} | sed "s/\./\\\./g")"
    
    MD5DIR="${PREFIX}/.md5sum"
    MD5DIRESC="$(echo ${MD5DIR} | sed "s/\./\\\./g")"
    
    BACKUPNAME=".backup"
    BACKUPDIR="${PREFIX}/${BACKUPNAME}"
    
    
    # Try to restore corrupted file
    restore_backup(){
    
        # If the corrupted file is a backup file
        # will try to restore from the original file
        if echo "${FILEREAL}" |grep -q "${BACKUPNAME}"; then
            FILE="$(echo ${FILE} |sed "s#${BACKUPNAME}##g")"
            FILEREAL="$(echo ${FILEREAL} |sed "s#${BACKUPNAME}##g")"
    
            echo "$FILE $FILEREAL"
            (md5sum "${PREFIX}/${FILEREAL}" |cut -d' ' -f1 | \
             diff - "${FILE}" &> /dev/null) || return 1
            mkdir -p "$(dirname "${BACKUPDIR}/${FILEREAL}")"
            cp -a "${PREFIX}/${FILEREAL}" "${BACKUPDIR}/${FILEREAL}"
            return $?
        fi
    
        # This is a original file, so will check if there is some backup for it
        if test -e "${BACKUPDIR}/${FILEREAL}"; then
            (md5sum "${BACKUPDIR}/${FILEREAL}" |cut -d' ' -f1 | \
             diff - "${MD5DIR}/.backup/${FILEREAL}" &> /dev/null) || return 1
            cp -a "${BACKUPDIR}/${FILEREAL}" "${PREFIX}/${FILEREAL}"
            return $?
        fi
    
        return 1
    }
    
    # Will check one file integrity
    # If this file is corrupted, tries to restore it from a backup
    check_file_integrity(){
        FILE="$*"
        FILEREAL="$(echo ${FILE} |sed "s#^${MD5DIRESC}##g")"
    
        test -f "${PREFIX}/${FILEREAL}" || restore_backup || return 1
        (md5sum "${PREFIX}/${FILEREAL}" |cut -d' ' -f1 | \
         diff - "${FILE}" &> /dev/null) || restore_backup || return 1
    }
    
    
    # Will check if all necessary files to run the agent are ok
    # If we can't restore some file, this update can't happen
    try_force_update(){
        DEPENDENTFILES="agent.sh collect.conf client/run.sh client/bin/client
                        load-config.sh client/common.sh client/conf/version
                        collect.conf"
    
        for FILE in ${DEPENDENTFILES}; do
            check_file_integrity "${MD5DIR}/${FILE}" || return 1
        done
    
        # Just to make sure the agent will auto update
        echo "0.0.0" > "${PREFIX}/client/conf/version"
    
        # Luck for us, all needed files are ok so we can call the agent
        # Source the agent, it will be auto updated
        source "${PREFIX}/agent.sh" || return 1
    
        return 0
    }
    
    # Will try to auto update in a simpler way
    # If everything goes wrong, this is our last hope
    update_myself(){
        UPDATETRIES=3
        UPDATETIMEOUT=100
        UPDATESERVER="http://seed.c3sl.ufpr.br/pacotes/download"
        TMPDIR="${PREFIX}/tmp"
    
        # The proxy config itself
        if ! check_file_integrity "${MD5DIR}/client/conf/proxy"; then
            PROXY=""
            puid=""
            ppasswd=""
        else
            PROXYCONF="${PREFIX}/client/conf/proxy"
            phost=$(grep "^phost=" ${PROXYCONF} | cut -f2 -d=)
            pport=$(grep "^pport=" ${PROXYCONF} | cut -f2 -d=)
            puid=$(grep "^puid=" ${PROXYCONF} | cut -f2 -d=)
            ppasswd=$(grep "^ppasswd=" ${PROXYCONF} | cut -f2 -d=)
    
            if test -z "$phost"; then
                PROXY=""
            else
                PROXY="http://${phost}:${pport}"
            fi
        fi
    
        if which wget &> /dev/null; then
            WGETBIN="$(which wget)"
        else
            WGETBIN="${PREFIX}/bin/wget"
        fi
    
        http_proxy="${PROXY}" ${WGETBIN} --tries=${UPDATETRIES} \
                                         --timeout=${UPDATETIMEOUT} \
                                         --proxy-user="${puid}" \
                                         --proxy-password="${ppasswd}" \
                                         "${UPDATESERVER}/SEED2-update.run" \
                                         -O "${TMPDIR}/SEED2-update.run" \
                                         &> /dev/null || return 1
    
        # Package downloaded, now try to update
        bash "${TMPDIR}/SEED2-update.run" || return 1
        rm -f "${TMPDIR}/SEED2-update.run" &> /dev/null
    
        return 0
    }
    
    
    # First check if some update went wrong
    if test -e "${PREFIX}/updating" || ! test -d "${MD5DIR}"; then
    
        try_force_update || update_myself || exit 1
        rm -f "${PREFIX}/updating" 2> /dev/null
        exit 0
    
    fi
    
    
    
    # Checking all files MD5
    for FILE in $(find "${MD5DIR}" -type f); do
    
        check_file_integrity ${FILE} ||
            ((try_force_update || update_myself) && exit 0) ||
            exit 1
    
    done
    
    exit 0