pidof misses a pid sometimes

Denys Vlasenko vda.linux at googlemail.com
Mon Dec 26 20:18:19 UTC 2011


On Mon, Dec 26, 2011 at 3:32 PM, Bastian Bittorf <bittorf at bluebottle.com> wrote:
> dear embedded warriors,
>
> we use "BusyBox v1.19.3 (2011-11-24 08:39:59 CET)"
> on ar71xx and brcm47xx (mipsel) on openWRT.
>
> It took us some days to trac down a problem with
> pidof. We have some checker scripts, which regulary
> watch for tasks, if they are running or not using 'pidof'.
> Sometimes we catch an error but in fact everything is ok,
> but pidof returns an empty list.
>
> The best thing: it's reproduceable (attached and inline):
>
> #!/bin/sh
>
> mywget()
> {
>        local url="http://127.0.0.1/favicon.ico"
>        local pidnum pidbackground mycmdline pidbackground pidlist
>
>        ( wget -qO - "$url" 2>/tmp/error_wget.$$.txt || echo "$?"
>>/tmp/error_wget.$$.rc ) &
>        pidbackground="$!"
>        pidlist="$( pidof wget )"
>...

Why do you think pidof process will be spawned AFTER wget process?
Subshell created by ( ... ) runs in parallel with the main shell.
We are in the era of quad CPUs (and even bigger ones) widely available.
Thus, pidof can easily outrun subshell, and look at process table
before wget appears there.

I instrumented find_pid_by_name() to print some values,
let your script run and when it detected failure, I see this:

...
...
...
pidof: pid:9071 no match: comm:'sh' argv0:'sh' exe:'/bin/bash'
pidof: pid:9072 no match: comm:'busybox' argv0:'./busybox'
exe:'/root/srcdevel/bbox/fix/busybox.t2/busybox'
pidof: pid:9073 no match: comm:'sh' argv0:'sh' exe:'/bin/bash'
pidof: pid:24014 no match: comm:'top' argv0:'top'
exe:'/app/busybox-1.19.2/busybox'
found a pid, which pidof did'nt see: 9073 -> wget

In other words, when pidof was looking at process 9073, it was still a
shell process -
IOW, the syscall to execute wget did not happen yet.

-- 
vda


More information about the busybox mailing list