1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include "local_proto.h"
#include <grass/gis.h>
#include <grass/glocale.h>
/******************************************************************
*lock file pid
*
* this programs "locks" the file for process pid:
*
* 1. if file exists, the pid is read out of the file. if this
* process is still running, the file is considered locked.
* exit(2).
* 2. something weird happened. G_fatal_error() aka exit(1)
* 3. if file does not exist, or if file exists but process is not
* running (ie, lock was not removed), the file is locked for
* process pid by writing pid into the file.
* exit(0).
******************************************************************/
#include <errno.h>
extern int errno;
int main(int argc, char *argv[])
{
int pid;
int lockpid;
int lock;
int locked;
if (argc != 3 || sscanf(argv[2], "%d", &lockpid) != 1)
G_fatal_error(_("Usage: %s file pid"), argv[0]);
#define file argv[1]
#ifdef __MINGW32__
G_warning(_("Concurrent mapset locking is not supported on Windows"));
exit(0);
#else
locked = 0;
if ((lock = open(file, 0)) >= 0) { /* file exists */
G_sleep(1); /* allow time for file creator to write its pid */
if (read(lock, &pid, sizeof pid) == sizeof pid)
locked = find_process(pid);
close(lock);
}
if (locked)
exit(2);
if ((lock = creat(file, 0666)) < 0) {
perror(file);
G_fatal_error("%s: ", argv[0]);
}
if (write(lock, &lockpid, sizeof lockpid) != sizeof lockpid)
G_fatal_error(_("Unable to write lockfile %s (disk full? Permissions?)"),
file);
close(lock);
exit(0);
#endif
}
int find_process(int pid)
{
/* attempt to kill pid with NULL signal. if success, then
process pid is still running. otherwise, must check if
kill failed because no such process, or because user is
not owner of process
*/
#ifdef __MINGW32__
return 0;
#else
if (kill(pid, 0) == 0)
return 1;
return errno != ESRCH;
#endif
}
|