From: NeilBrown Date: Sun, 22 Apr 2012 04:23:15 +0000 (+1000) Subject: cycle_watchers: don't write the 'all done' signal until after the rename. X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=33c0bebbbf2defd40ac00941ec9c9bb4fe7ea25e;p=susman.git cycle_watchers: don't write the 'all done' signal until after the rename. When a suspend cycle is finished we move the new 'watching-next' into place and we write to the old one to make sure any process watching will notice something has happened. It is best to write to the file *After* the rename else a process that notices the file change might make wrong assumptions about the then-current 'watching' file. i.e. close a small and probably unimportant race. Signed-off-by: NeilBrown --- diff --git a/lsusd.c b/lsusd.c index dff24f4..c9eab88 100644 --- a/lsusd.c +++ b/lsusd.c @@ -58,14 +58,11 @@ static void cycle_watchers(void) fd = open("/var/run/suspend/watching", O_RDWR|O_CREAT, 0640); if (fd < 0) return; - zero[0] = zero[1] = 0; - if (write(fd, zero, 2) != 2) { - close(fd); - return; - } - close(fd); rename("/var/run/suspend/watching-next", "/var/run/suspend/watching"); + zero[0] = zero[1] = 0; + write(fd, zero, 2); + close(fd); } static int read_wakeup_count()