1

I ran into an issue yesterday with locking on Solaris and crontab editing that has me wondering what the best way to protect multiple concurrent edits of the same crontab are on Solaris. I've confirmed that the behaviour exists in Solaris through 11.2.10.5.0.

EDITOR=vi
OS=Solaris 10, 11
SHELL=bash
RBAC=pfexec

Now normally, vi uses .filename.swp files to prevent multiple concurrent edits of the same file, whether that be from multiple users or multiple invocations from the same user. However, "crontab -e" creates and passes a temp file with a name based on /tmp/crontabXXXXXX to $EDITOR, and multiple concurrent invocations of "crontab -e" will pass different temp files to $EDITOR, potentially allowing reversion of changes to the crontab when it is opened from two places at once, or when a suspended vi session gets killed due to the TTY timing out, as happened to me. There is additionally no warning from the $EDITOR on the second invocation of "crontab -e" as the file being edited is different.

How can I prevent this issue occurring? The use of a pseudo-random temp file name prevents the inbuilt locking in vi from working, so addressing that would seem to be the optimal route. Perhaps the issue is more fundamental and needs to be raised as an OS bug. The Solaris crontab man page doesn't leave much hope as it states "Simultaneous modifications of the same crontab file may lead to unexpected results.", but I am hoping that someone has an answer.

Ric F
  • 116
  • 4

1 Answers1

1

If this really is a concern you're going to have to write something yourself as, as the man page suggests it's not built in.

You could write a wrapper for crontab(1) that did the locking/lock checking before running crontab(1) itself.

In this answer I suggest a file locking method using mkdir.

This whole idea doesn't seem like a trivial undertaking though.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • Thanks Iain, this is the conclusion that I have come to as well. I may go the route of a wrapper script that does an export, edit, sanity check, import, and handles locking on its own. – Ric F Aug 14 '15 at 01:01