1 /* dotlock.c - dotfile locking
2 * Copyright (C) 1998, 2000, 2001, 2003, 2004,
3 * 2005, 2006 Free Software Foundation, Inc.
5 * This file is part of JNLIB.
7 * JNLIB is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 3 of
10 * the License, or (at your option) any later version.
12 * JNLIB is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #ifndef HAVE_DOSISH_SYSTEM
29 #include <sys/utsname.h>
31 #include <sys/types.h>
37 #include "libjnlib-config.h"
38 #include "stringhelp.h"
41 #if !defined(DIRSEP_C) && !defined(EXTSEP_C) \
42 && !defined(DIRSEP_S) && !defined(EXTSEP_S)
43 #ifdef HAVE_DOSISH_SYSTEM
59 struct dotlock_handle
*next
;
60 char *tname
; /* Name of lockfile template. */
61 size_t nodename_off
; /* Offset in TNAME of the nodename part. */
62 size_t nodename_len
; /* Length of the nodename part. */
63 char *lockname
; /* Name of the real lockfile. */
64 int locked
; /* Lock status. */
65 int disable
; /* When true, locking is disabled. */
69 static volatile DOTLOCK all_lockfiles
;
70 static int never_lock
;
72 static int read_lockfile (DOTLOCK h
, int *same_node
);
81 * Create a lockfile with the given name and return an object of
82 * type DOTLOCK which may be used later to actually do the lock.
83 * A cleanup routine gets installed to cleanup left over locks
84 * or other files used together with the lock mechanism.
85 * Although the function is called dotlock, this does not necessarily
86 * mean that real lockfiles are used - the function may decide to
87 * use fcntl locking. Calling the function with NULL only install
88 * the atexit handler and maybe used to assure that the cleanup
89 * is called after all other atexit handlers.
91 * Notes: This function creates a lock file in the same directory
92 * as file_to_lock with the name "file_to_lock.lock"
93 * A temporary file ".#lk.<hostname>.pid[.threadid] is used.
94 * This function does nothing for Windoze.
97 create_dotlock( const char *file_to_lock
)
99 static int initialized
;
103 const char *nodename
;
106 #ifndef HAVE_DOSISH_SYSTEM
107 struct utsname utsbuf
;
112 atexit( dotlock_remove_lockfiles
);
116 return NULL
; /* Only initialization was requested. */
118 h
= jnlib_xcalloc ( 1, sizeof *h
);
123 /* fixme: aquire mutex on all_lockfiles */
125 h
->next
= all_lockfiles
;
130 #ifndef HAVE_DOSISH_SYSTEM
131 sprintf (pidstr
, "%10d\n", (int)getpid() );
132 /* fixme: add the hostname to the second line (FQDN or IP addr?) */
134 /* Create a temporary file. */
135 if ( uname ( &utsbuf
) )
136 nodename
= "unknown";
138 nodename
= utsbuf
.nodename
;
142 char *iter
= (char *) nodename
;
143 for (; iter
[0]; iter
++)
147 #endif /* __riscos__ */
149 if ( !(dirpart
= strrchr ( file_to_lock
, DIRSEP_C
)) )
156 dirpartlen
= dirpart
- file_to_lock
;
157 dirpart
= file_to_lock
;
161 /* fixme: aquire mutex on all_lockfiles */
163 h
->next
= all_lockfiles
;
166 h
->tname
= jnlib_xmalloc ( dirpartlen
+ 6+30+ strlen(nodename
) + 11 );
167 h
->nodename_len
= strlen (nodename
);
169 sprintf (h
->tname
, "%.*s/.#lk%p.", dirpartlen
, dirpart
, h
);
170 h
->nodename_off
= strlen (h
->tname
);
171 sprintf (h
->tname
+h
->nodename_off
, "%s.%d", nodename
, (int)getpid ());
172 #else /* __riscos__ */
173 sprintf (h
->tname
, "%.*s.lk%p/", dirpartlen
, dirpart
, h
);
174 h
->nodename_off
= strlen (h
->tname
);
175 sprintf (h
->tname
+h
->nodename_off
, "%s/%d", nodename
, (int)getpid () );
176 #endif /* __riscos__ */
181 fd
= open (h
->tname
, O_WRONLY
|O_CREAT
|O_EXCL
,
182 S_IRUSR
|S_IRGRP
|S_IROTH
|S_IWUSR
);
184 while (fd
== -1 && errno
== EINTR
);
188 all_lockfiles
= h
->next
;
189 log_error ( "failed to create temporary file `%s': %s\n",
190 h
->tname
, strerror(errno
));
191 jnlib_free(h
->tname
);
195 if ( write (fd
, pidstr
, 11 ) != 11 )
197 if ( write (fd
, nodename
, strlen (nodename
) ) != strlen (nodename
) )
199 if ( write (fd
, "\n", 1 ) != 1 )
207 #endif /* !HAVE_DOSISH_SYSTEM */
208 h
->lockname
= jnlib_xmalloc ( strlen (file_to_lock
) + 6 );
209 strcpy (stpcpy(h
->lockname
, file_to_lock
), EXTSEP_S
"lock");
212 all_lockfiles
= h
->next
;
214 /* fixme: release mutex */
216 log_error ( "error writing to `%s': %s\n", h
->tname
, strerror(errno
) );
219 jnlib_free(h
->tname
);
226 destroy_dotlock ( DOTLOCK h
)
228 #ifndef HAVE_DOSISH_SYSTEM
233 /* First remove the handle from our global list of all locks. */
234 for (hprev
=NULL
, htmp
=all_lockfiles
; htmp
; hprev
=htmp
, htmp
=htmp
->next
)
238 hprev
->next
= htmp
->next
;
240 all_lockfiles
= htmp
->next
;
245 /* Second destroy the lock. */
248 if (h
->locked
&& h
->lockname
)
249 unlink (h
->lockname
);
252 jnlib_free (h
->tname
);
253 jnlib_free (h
->lockname
);
257 #endif /*!HAVE_DOSISH_SYSTEM*/
263 maybe_deadlock( DOTLOCK h
)
267 for ( r
=all_lockfiles
; r
; r
= r
->next
)
269 if ( r
!= h
&& r
->locked
)
276 * Do a lock on H. A TIMEOUT of 0 returns immediately, -1 waits
277 * forever (hopefully not), other values are reserved (should then be
278 * timeouts in milliseconds). Returns: 0 on success
281 make_dotlock( DOTLOCK h
, long timeout
)
283 #ifdef HAVE_DOSISH_SYSTEM
287 const char *maybe_dead
="";
292 return 0; /* Locks are completely disabled. Return success. */
297 log_debug("oops, `%s' is already locked\n", h
->lockname
);
298 #endif /* !__riscos__ */
305 if ( !link(h
->tname
, h
->lockname
) )
307 /* fixme: better use stat to check the link count */
311 if ( errno
!= EEXIST
)
313 log_error( "lock not made: link() failed: %s\n", strerror(errno
) );
316 #else /* __riscos__ */
317 if ( !renamefile(h
->tname
, h
->lockname
) )
322 if ( errno
!= EEXIST
)
324 log_error( "lock not made: rename() failed: %s\n", strerror(errno
) );
327 #endif /* __riscos__ */
329 if ( (pid
= read_lockfile (h
, &same_node
)) == -1 )
331 if ( errno
!= ENOENT
)
333 log_info ("cannot read lockfile\n");
336 log_info( "lockfile disappeared\n");
339 else if ( pid
== getpid() && same_node
)
341 log_info( "Oops: lock already held by us\n");
345 else if ( same_node
&& kill (pid
, 0) && errno
== ESRCH
)
348 log_info ("removing stale lockfile (created by %d)", pid
);
349 unlink (h
->lockname
);
351 #else /* __riscos__ */
352 /* Under RISCOS we are *pretty* sure that the other task
353 is dead and therefore we remove the stale lock file. */
354 maybe_dead
= " - probably dead - removing lock";
356 #endif /* __riscos__ */
361 /* Wait until lock has been released. */
364 log_info ("waiting for lock (held by %d%s) %s...\n",
365 pid
, maybe_dead
, maybe_deadlock(h
)? "(deadlock?) ":"");
368 /* We can't use sleep, cause signals may be blocked. */
369 tv
.tv_sec
= 1 + backoff
;
371 select(0, NULL
, NULL
, NULL
, &tv
);
379 #endif /* !HAVE_DOSISH_SYSTEM */
385 * Returns: 0 := success
388 release_dotlock( DOTLOCK h
)
390 #ifdef HAVE_DOSISH_SYSTEM
395 /* To avoid atexit race conditions we first check whether there are
396 any locks left. It might happen that another atexit handler
397 tries to release the lock while the atexit handler of this module
398 already ran and thus H is undefined. */
407 log_debug("oops, `%s' is not locked\n", h
->lockname
);
411 pid
= read_lockfile (h
, &same_node
);
414 log_error( "release_dotlock: lockfile error\n");
417 if ( pid
!= getpid() || !same_node
)
419 log_error( "release_dotlock: not our lock (pid=%d)\n", pid
);
423 if ( unlink( h
->lockname
) )
425 log_error( "release_dotlock: error removing lockfile `%s'",
429 #else /* __riscos__ */
430 if ( renamefile(h
->lockname
, h
->tname
) )
432 log_error( "release_dotlock: error renaming lockfile `%s' to `%s'",
433 h
->lockname
, h
->tname
);
436 #endif /* __riscos__ */
437 /* fixme: check that the link count is now 1 */
440 #endif /* !HAVE_DOSISH_SYSTEM */
445 Read the lock file and return the pid, returns -1 on error. True
446 will be stored at SAME_NODE if the lock file has been created on
450 read_lockfile (DOTLOCK h
, int *same_node
)
452 #ifdef HAVE_DOSISH_SYSTEM
455 char buffer_space
[10+1+70+1]; /* 70 is just an estimated value; node
456 name are usually shorter. */
463 expected_len
= 10 + 1 + h
->nodename_len
+ 1;
464 if ( expected_len
>= sizeof buffer_space
)
465 buffer
= jnlib_xmalloc (expected_len
);
467 buffer
= buffer_space
;
469 if ( (fd
= open (h
->lockname
, O_RDONLY
)) == -1 )
472 log_info ("error opening lockfile `%s': %s\n",
473 h
->lockname
, strerror(errno
) );
474 if (buffer
!= buffer_space
)
476 errno
= e
; /* Need to return ERRNO here. */
484 res
= read (fd
, p
, expected_len
- nread
);
485 if (res
== -1 && errno
== EINTR
)
489 log_info ("error reading lockfile `%s'", h
->lockname
);
491 if (buffer
!= buffer_space
)
493 errno
= 0; /* Do not return an inappropriate ERRNO. */
499 while (res
&& nread
!= expected_len
);
504 log_info ("invalid size of lockfile `%s'", h
->lockname
);
505 if (buffer
!= buffer_space
)
507 errno
= 0; /* Do not return an inappropriate ERRNO. */
511 if (buffer
[10] != '\n'
512 || (buffer
[10] = 0, pid
= atoi (buffer
)) == -1
515 #else /* __riscos__ */
516 || (!pid
&& riscos_getpid())
517 #endif /* __riscos__ */
520 log_error ("invalid pid %d in lockfile `%s'", pid
, h
->lockname
);
521 if (buffer
!= buffer_space
)
527 if (nread
== expected_len
528 && !memcmp (h
->tname
+h
->nodename_off
, buffer
+11, h
->nodename_len
)
529 && buffer
[11+h
->nodename_len
] == '\n')
532 if (buffer
!= buffer_space
)
540 dotlock_remove_lockfiles()
542 #ifndef HAVE_DOSISH_SYSTEM
546 all_lockfiles
= NULL
;