jaromail

a commandline tool to easily and privately handle your e-mail
git clone git://parazyd.org/jaromail.git
Log | Files | Refs | Submodules | README

commit 9c7e6394673e768554502eb3ac0f2bd9152a90b1
parent bd25a12d126f38d82fe9b7d4ba6882da65edbe32
Author: Jaromil <jaromil@dyne.org>
Date:   Wed, 19 Sep 2012 07:33:37 -0500

fixes to new locking system

Diffstat:
Msrc/jaro | 16+++++++++-------
Msrc/zlibs/email | 15++++++---------
Msrc/zlibs/helpers | 40++++++++++++++++++++++++++++++++--------
Msrc/zlibs/locking | 33+++++++++++++++++++++++++--------
4 files changed, 72 insertions(+), 32 deletions(-)

diff --git a/src/jaro b/src/jaro @@ -229,22 +229,24 @@ cleanexit() { # then locks, with a warning for l in `find -O3 $TMPDIR/ -maxdepth 1 -type f -name '*.lock'`; do lname=`basename ${(s:.lock:)l}` + func "cleaning lock for $lname" # skip if in course of unlink - parallel operation, see unlink() { test -r ${TMPDIR}/${lname}.unlink } && { continue } - pid="$lname.pid" - if [ -r ${pid} ]; then - error "forced removal of lock created by pid $pid: $lname" - rm -f ${TMPDIR}/${pid} + pidfile="${TMPDIR}/$lname.pid" + if [ -r ${pidfile} ]; then + pid=`cat $pidfile` + error "forced removal of lock left by pid $pid: $lname" + rm -f ${pidfile} else - error "forced removal of lock created by unknown pid: $lname" + error "forced removal of lock left by unknown pid: $lname" fi - rm -f ${TMPDIR}/${lock} + rm -f ${TMPDIR}/${lname}.lock # remove the actual file if [ -r ${TMPDIR}/$lname ]; then ${=rm} ${TMPDIR}/$lname - else error "stale lock: locked file not found"; fi + else act "empty lock: file was already removed"; fi done diff --git a/src/zlibs/email b/src/zlibs/email @@ -24,7 +24,7 @@ compose() { # no args, open in compose mode { test -z $PARAM } && { ${=mutt} -F $MUTTDIR/rc ""; return $? } - + ${=mutt} -F $MUTTDIR/rc "${PARAM[@]}" return $? } @@ -86,7 +86,7 @@ fetchall() { fetch() { { test "$account" = "" } && { fetchall; return $? } - + read_account ${account_type} ${account} { test $? != 0 } && { error "Error on account entry: $account_type $account" @@ -103,11 +103,8 @@ fetch() { notice "Examining mailbox for $name <$email> on $host" - # check if its online - nc -w 8 -z ${host} ${port} > /dev/null - { test $? = 0 } || { - error "server is off-line or unreachable, skipped" - return 1 } + is_online ${host} ${port} + { test $? = 0 } || { return 1 } ask_password $login $host @@ -238,7 +235,7 @@ send() { unset password all; return 1 } - tmp=$TMPDIR/$host.send.$RANDOM + tmp=$TMPDIR/msmtp.$host.$datestamp.$RANDOM newlock $tmp cat <<EOF > $tmp account default @@ -268,8 +265,8 @@ EOF act "Mail sent succesfully" # whitelist those to whom we send mails cat ${mail} | $WORKDIR/bin/jaro -q learn recipient - ${=rm} ${mail} unlink ${smtp} + ${=rm} ${mail} fi done unlink $tmp diff --git a/src/zlibs/helpers b/src/zlibs/helpers @@ -139,15 +139,39 @@ open_file() { ######################### ## check if we are online is_online() { - ping -c3 -n 8.8.8.8 2>/dev/null - { test $? = 0 } || { - error "Internet seems unreachable" - { test "$FORCE" = "1" } || { - act "Network connection is checked with a ping to 8.8.8.8" - act "if your network doesn't allows it to pass, use -f to force." - error "Operation aborted." - exit 1 } + { test "$FORCE" = "1" } && { + act "Internet check skipped (--force in use)" + return 0 } + _res=1 + _host=${1:-8.8.8.8} + _port=${2:-} + + _mode=inet # or host + { test "$port" = "" } || { _mode=host } + + case $_mode in + inet) + ping -c1 -n 8.8.8.8 2>&1 > /dev/null + { test $? = 0 } || { + error "Internet seems unreachable" + act "Network connection is checked with a ping to 8.8.8.8" + act "if your network doesn't allows it to pass, use -f to force." + error "Operation aborted." + exit 1 + } + ;; + host) + nc -w 16 -z ${_host} ${_port} > /dev/null + { test $? = 0 } || { + error "Host unreachable: $_host" + act "Network connection is checked with 16s timeout" + act "if you want to bypass this check, use -f to force." + error "Operation aborted." + return 1 + } + ;; + esac } diff --git a/src/zlibs/locking b/src/zlibs/locking @@ -27,7 +27,10 @@ lock() { 1) error "Cannot lock non existing file: $1" return 1 ;; 3) error "Locked file in use: $1" + + pidcheck ${1} return 3 ;; + 5) error "Impossible to lock: $1" return 5 ;; # success @@ -44,14 +47,29 @@ newlock() { # create locked } pidcheck() { # check if lock belongs to us - lockpid="`cat ${1}.pid`" - { test -r "${1}.pid" } && { + if [ -r "${1}.pid" ]; then + lockpid="`cat ${1}.pid`" { test "$$" != "$lockpid" } && { - error "Unlock attempt by different PID on `basename $1`" - act "created by $lockpid now we ($$) try to unlock" - act "skipped removal: $1" - return 1 } - } + error "Unlock attempt by multiple processes on `basename $1`" + { test "$FORCE" = "1" } || { + _prun=1 + while [ "$prun" = "1" ]; do + ps ax | grep "^${lockpid}" > /dev/null + if [ test $? = 0 ]; then + act "Owner (pid ${_pid}) still running, waiting release..." + else + act "Owner (pid ${_pid}) not running, taking over the lock" + rm -f ${1}.pid; echo "$$" > ${1}.pid + _prun=0 + fi + done + } + act "left behind by $lockpid - we ($$) will unlock it" + } + else + act "left behind by unknown owner, we'll take it over" + rm -f ${1}.pid; echo "$$" > ${1}.pid + fi return 0 } @@ -85,4 +103,3 @@ unlink() { # delete a file that we are locking ) &! return 0 } -