Andrew Hewus Fresh <and...@afresh1.com> wrote: > I noticed this when testing how signal handling worked in fw_update, it > turns out that if you `pkill -KILL -f fw_update` it may leave behind a perl > process that is locking the package database. Instead of just waiting > to be killed, we can have that process check to see if its parent is > still around and exit if not. > > Is there a more appropriate solution to this? > What's the right way to notice your parent exited?
Your approach is racey. Instead, pass the soon-to-be-parent pid as an argument to the perl script. I don't know if there is a better solution to "detect if your parent exited". -Lucas diff /usr/src commit - e425abdca99af75b418563580e5a2e31165f6f10 path + /usr/src blob - 53efe8160238c66dc5628ea8d56e37e42988a9b4 file + usr.sbin/fw_update/fw_update.sh --- usr.sbin/fw_update/fw_update.sh +++ usr.sbin/fw_update/fw_update.sh @@ -213,7 +213,7 @@ lock_db() { [ -e /usr/bin/perl ] || return 0 set -o monitor - perl <<'EOL' |& + perl - "$$" <<'EOL' |& use v5.16; use warnings; no lib ('/usr/local/libdata/perl5/site_perl'); @@ -221,10 +221,15 @@ lock_db() { $|=1; + $0 = "fw_update: lock_db" lock_db(0); say $$; - sleep; + + # If our parent exits unexpectedly, + # ppid will become init and we should exit. + my $parent = shift; + sleep 1 while getppid == $parent; EOL set +o monitor