Skip to content

Commit 84061bc

Browse files
committed
refactor cukinia_process around pidof
Several problems around cukinia_process lately. It would return false positives on more compact busybox configurations, and be extremely slow due to the scanning of all pids under /proc. The function is refactoring around pidof, considering it is fairly standard both in the Busybox and GNU worlds, and it provides a much more efficient process scanner.
1 parent c016120 commit 84061bc

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

cukinia

+17-14
Original file line numberDiff line numberDiff line change
@@ -447,29 +447,32 @@ _cukinia_process()
447447
local pname="$1"
448448
local puser="$2"
449449
local puser_uid
450+
local owner_uid
451+
local procdir
452+
local pid
450453

451454
_cukinia_prepare "Checking process \"$pname\" as ${puser:-any user}"
452455

453456
if [ -n "$puser" ]; then
454-
if ! puser_uid="$(id -u "$puser" 2>/dev/null)" ; then
455-
return 1
456-
fi
457+
# make sure $puser maps to a valid user
458+
puser_uid="$(id -u "$puser" 2>/dev/null)"
459+
[ -n "$puser_uid" ] || return 1
457460
fi
458461

459462
# scan /proc to get process name and optionally process owner
460-
for dir in /proc/[0-9]*/; do
461-
# check by process name
462-
local cmdline_nb="$(echo ${pname} | wc -w)"
463-
local cmdline="$(cat ${dir}/cmdline | tr "\000" " " | cut -d " " -f -${cmdline_nb})"
463+
for pid in $(pidof "$pname"); do
464+
procdir="/proc/$pid"
464465

465-
if [ "${cmdline}" != "${pname}" ]; then
466-
continue
467-
fi
466+
# skip kernel threads
467+
[ -x $procdir/exe ] || continue
468468

469-
# check optional process owner
470-
if [ -z "$puser" ] || [ "$(grep Uid "$dir/status" | cut -f 2)" = "$puser_uid" ]; then
471-
return 0
472-
fi
469+
# process found, no owner check
470+
[ -z "$puser" ] && return 0
471+
472+
473+
# check process owner
474+
owner_uid=$(awk '/^Uid:/ { print $2 }' < $procdir/status)
475+
[ "$owner_uid" = "$puser_uid" ] && return 0
473476
done
474477

475478
return 1

0 commit comments

Comments
 (0)