tags -1 patch On Mon, 2022-06-27 at 22:02 +0200, Marc Haber wrote: > > /run/adduser please, and probably a simple flock()? lock files in a > shell script are a minefield for code running as root, I hope it is > easier in perl.
Good call. I've attached a minimal POC, which works in manual testing, but automated testing is tricky and there may be edge cases. (This test is blocking; NB (erroring out) might be easier to test, but which is the better UX?) mb
diff --git a/AdduserCommon.pm b/AdduserCommon.pm index ffb8bac..2330d3a 100644 --- a/AdduserCommon.pm +++ b/AdduserCommon.pm @@ -12,11 +12,14 @@ use File::Basename; +use Fcntl qw(:flock SEEK_END); use constant PROGNAME => basename($0); use vars qw(@EXPORT $VAR1); +my $lockfile; + @EXPORT = ( 'dief', 'get_group_members', @@ -280,6 +283,17 @@ sub preseed_config { } } +BEGIN { + open($lockfile, '>>', '/run/adduser') or dief "could not open lock file %s!\n", '/run/adduser'; + flock($lockfile, LOCK_EX) or dief "could not lock file %s!\n", '/run/adduser'; + seek($lockfile, 0, SEEK_END) or dief "could not seek - %s!\n", '/run/adduser'; +} + +END { + flock($lockfile, LOCK_UN) or dief "could not unlock file %s - %s!\n", '/run/adduser', "$!"; + close($lockfile) or dief "could not close lock file %s - %s!\n", '/run/adduser', "$!"; +} + # Local Variables: # mode:cperl # End: