Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 87340 | Differences between
and this patch

Collapse All | Expand All

(-)keychain-orig (-13 / +21 lines)
Lines 322-377 Link Here
322
        tl_faking=true
322
        tl_faking=true
323
        tl_start=0
323
        tl_start=0
324
        tl_end=`expr $lockwait \* 10`
324
        tl_end=`expr $lockwait \* 10`
325
        tl_current=0
325
        tl_current=0
326
    fi
326
    fi
327
327
328
    # Try to lock for $lockwait seconds
328
    # Try to lock for $lockwait seconds
329
    while [ $lockwait -eq 0 -o $tl_current -lt $tl_end ]; do
329
    while [ $lockwait -eq 0 -o $tl_current -lt $tl_end ]; do
330
        if tl_error=`ln -s $$ "$lockf" 2>&1`; then
330
        if tl_error=`(umask 0377; echo $$ > "$lockf") 2>&1`; then
331
            havelock=true
331
            havelock=true
332
            return 0
332
            return 0
333
        fi
333
        fi
334
334
335
        # advance our timer
335
        # advance our timer
336
        if [ $lockwait -gt 0 ]; then
336
        if [ $lockwait -gt 0 ]; then
337
            if $tl_faking; then
337
            if $tl_faking; then
338
                tl_current=`expr $tl_current + 1`
338
                tl_current=`expr $tl_current + 1`
339
            else
339
            else
340
                tl_current=`now $tl_current`
340
                tl_current=`now $tl_current`
341
            fi
341
            fi
342
        fi
342
        fi
343
343
344
        # check for old-style lock; unlikely
344
        # check for old-style lock; unlikely
345
        if [ -f "$lockf" ]; then
345
        if [ -h "$lockf" ]; then
346
            error "please remove old-style lock: $lockf"
346
            error "please remove old-style lock: $lockf"
347
            return 1
347
            return 1
348
        fi
348
        fi
349
349
350
        # read the lock
350
        # read the lock
351
        tl_pid=`readlink "$lockf" 2>/dev/null`
351
        tl_pid=`cat "$lockf" 2>/dev/null`
352
        if [ -z "$tl_pid" ]; then 
353
            tl_pid=`ls -l "$lockf" 2>/dev/null | awk '{print $NF}'`
354
        fi
355
        if [ -z "$tl_pid" ]; then
352
        if [ -z "$tl_pid" ]; then
356
            # lock seems to have disappeared, try again
353
            # lock seems to have disappeared, try again
357
            continue
354
            continue
358
        fi
355
        fi
359
356
360
        # test for a stale lock
357
        # test for a stale lock
361
        kill -0 "$tl_pid" 2>/dev/null
358
        kill -0 "$tl_pid" 2>/dev/null
362
        if [ $? != 0 ]; then
359
        if [ $? != 0 ]; then
363
            # Avoid a race condition; another keychain might have started at
360
            # Avoid a race condition; another keychain might have started at
364
            # this point.  If the pid is the same as the last time we
361
            # this point.  If the pid is the same as the last time we checked,
365
            # checked, then go ahead and remove the stale lock.  Otherwise
362
            # try to remove the stale lock.  Otherwise remember the pid and try
366
            # remember the pid and try again.
363
            # again.
367
            if [ "$tl_pid" = "$tl_oldpid" ]; then
364
            if [ "$tl_pid" = "$tl_oldpid" ]; then
368
                warn "removing stale lock for pid $tl_pid"
365
                warn "found stale lock for pid $tl_pid"
369
                rm -f "$lockf"
366
                if (umask 0377; echo $$ > "$lockr") 2>&1; then
367
                    tl_pid=`cat "$lockf" 2>/dev/null`
368
                    if [ -n "$tl_pid" ]; then
369
                        kill -0 "$tl_pid" 2>/dev/null
370
                        if [ $? != 0 ]; then
371
                            warn "removing stale lock for pid $tl_pid"
372
                            rm -f "$lockf"
373
                        fi
374
                    fi
375
                    rm -f "$lockr"
376
                fi
370
            else
377
            else
371
                tl_oldpid="$tl_pid"
378
                tl_oldpid="$tl_pid"
372
            fi
379
            fi
373
            # try try again
380
            # try try again
374
            continue
381
            continue
375
        fi
382
        fi
376
383
377
        # sleep for a bit to wait for the keychain process holding the lock
384
        # sleep for a bit to wait for the keychain process holding the lock
Lines 387-403 Link Here
387
    [ -n "$tl_pid" ] || unset tl_pid    # ${var+...} relies on set vs. unset
394
    [ -n "$tl_pid" ] || unset tl_pid    # ${var+...} relies on set vs. unset
388
    error "failed to get the lock${tl_pid+, held by pid $tl_pid}: $tl_error"
395
    error "failed to get the lock${tl_pid+, held by pid $tl_pid}: $tl_error"
389
    return 1
396
    return 1
390
}
397
}
391
398
392
# synopsis: droplock
399
# synopsis: droplock
393
# Drops the lock if we're holding it.
400
# Drops the lock if we're holding it.
394
droplock() {
401
droplock() {
395
    [ -n "$lockf" ] && rm -f "$lockf"
402
    $havelock && [ -n "$lockf" ] && rm -f "$lockf"
396
}
403
}
397
404
398
# synopsis: findpids [prog]
405
# synopsis: findpids [prog]
399
# Returns a space-separated list of agent pids.
406
# Returns a space-separated list of agent pids.
400
# prog can be ssh or gpg, defaults to ssh.  Note that if another prog is ever
407
# prog can be ssh or gpg, defaults to ssh.  Note that if another prog is ever
401
# added, need to pay attention to the length for Solaris compatibility.
408
# added, need to pay attention to the length for Solaris compatibility.
402
findpids() {
409
findpids() {
403
    fp_prog=${1-ssh}
410
    fp_prog=${1-ssh}
Lines 1198-1219 Link Here
1198
1205
1199
# Set filenames *after* parsing command-line options to allow 
1206
# Set filenames *after* parsing command-line options to allow 
1200
# modification of $keydir and/or $hostopt
1207
# modification of $keydir and/or $hostopt
1201
#
1208
#
1202
# pidf holds the specific name of the keychain .ssh-agent-myhostname file.
1209
# pidf holds the specific name of the keychain .ssh-agent-myhostname file.
1203
# We use the new hostname extension for NFS compatibility. cshpidf is the
1210
# We use the new hostname extension for NFS compatibility. cshpidf is the
1204
# .ssh-agent file with csh-compatible syntax. lockf is the lockfile, used
1211
# .ssh-agent file with csh-compatible syntax. lockf is the lockfile, used
1205
# to serialize the execution of multiple ssh-agent processes started 
1212
# to serialize the execution of multiple ssh-agent processes started 
1206
# simultaneously
1213
# simultaneously. lockr is the lockfile, used to remove a stale lock.
1207
[ -z "$hostopt" ] && hostopt="${HOSTNAME}"
1214
[ -z "$hostopt" ] && hostopt="${HOSTNAME}"
1208
[ -z "$hostopt" ] && hostopt=`uname -n 2>/dev/null || echo unknown`
1215
[ -z "$hostopt" ] && hostopt=`uname -n 2>/dev/null || echo unknown`
1209
pidf="${keydir}/${hostopt}-sh"
1216
pidf="${keydir}/${hostopt}-sh"
1210
cshpidf="${keydir}/${hostopt}-csh"
1217
cshpidf="${keydir}/${hostopt}-csh"
1211
lockf="${keydir}/${hostopt}-lock"
1218
lockf="${keydir}/${hostopt}-lock"
1219
lockr="${keydir}/${hostopt}-repair"
1212
1220
1213
# Don't use color if there's no terminal on stdout
1221
# Don't use color if there's no terminal on stdout
1214
if [ -n "$OFF" ]; then
1222
if [ -n "$OFF" ]; then
1215
    tty <&1 >/dev/null 2>&1 || unset BLUE CYAN GREEN OFF RED
1223
    tty <&1 >/dev/null 2>&1 || unset BLUE CYAN GREEN OFF RED
1216
fi
1224
fi
1217
1225
1218
# versinfo uses qprint, which honors --quiet
1226
# versinfo uses qprint, which honors --quiet
1219
versinfo
1227
versinfo

Return to bug 87340