[Gllug] Single instance of a process - simple way

Henrik Bilar henrik at bilar.co.uk
Fri Jul 6 18:16:22 UTC 2007


> It's racy, though. Two processes trying to set the same lock at the
> same time could interfere with each other, such that one process could
> believe it has the lock, yet it contains the PID of the other processes.
> Try using mktemp(1) to make the temporary lock instead.

I don't understand how the setting of the lock is racy (it may be, but I
fail to see it). The link system call (which is used by ln) is atomic, and
we're using temporary unique file names to write the information to.


> ObRant: Don't shout with your variable names unless they're environment
> variables. Quote your variables when you deference them, particularly
> given that some of them have user supplied input.

I always use capitals for my variable names in shell scripts, as (my)
emacs colours my variable names then, and also my eyes have an easier time
marking MYVAR as a variable as opposed to myVar which almost looks like a
normal command. I only do this when shell scripting, for some reason
(possibly because shell code looks unclean at the best of times, so I need
the extra visual cues).

As for quoting variables, sure it would make the program more robust, but
then it's only a quick hack and someone attacking your locks will need to
have your user privs already (or be root) in which case it's game over
anyway. IMO it works well for e.g. serializing scripts (e.g. rsync's)


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list