Mercurial > trustbridge
annotate common/linuxlockfile.c @ 1104:a7a72353d6d2
(issue111) Wording and German translation.
author | Emanuel Schuetze <emanuel@intevation.de> |
---|---|
date | Mon, 15 Sep 2014 14:08:40 +0200 |
parents | 20ca94680003 |
children | a974b61a5cce |
rev | line source |
---|---|
782
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
1 #ifndef WIN32 |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
2 |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
3 #include <fcntl.h> |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
4 #include <stdio.h> |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
5 #include <sys/types.h> |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
6 #include <unistd.h> |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
7 |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
8 #include "logging.h" |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
9 |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
10 int |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
11 open_lockfile(char *path) |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
12 { |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
13 int fd; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
14 char pidstr[20]; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
15 size_t pidstrlen; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
16 struct flock lk; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
17 fd = open(path, O_RDWR | O_CREAT, 0600); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
18 if (fd != -1) |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
19 { |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
20 /* Get an exclusive lock on the whole file. */ |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
21 lk.l_type = F_WRLCK; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
22 lk.l_whence = SEEK_SET; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
23 lk.l_start = 0; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
24 lk.l_len = 0; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
25 if (fcntl(fd, F_SETLK, &lk) != -1) |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
26 { |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
27 /* FIXME: For extra security we should check if there is |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
28 already a pid in the file. If so we should check in |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
29 /proc/$PID if there is still a process of the same name |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
30 as ours running... */ |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
31 ftruncate(fd, 0); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
32 pidstrlen = (size_t)snprintf(pidstr, sizeof(pidstr), "%d", getpid()); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
33 write(fd, pidstr, pidstrlen); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
34 } |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
35 else |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
36 { |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
37 /* Lock can not be acquired. Bail out... */ |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
38 close(fd); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
39 DEBUGPRINTF("Could not get an exclusive lock on %s.\n", path); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
40 return -1; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
41 } |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
42 } |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
43 else |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
44 { |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
45 DEBUGPRINTF("Failed to open lock file: %s.\n", path); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
46 } |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
47 return fd; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
48 } |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
49 |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
50 void |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
51 close_lockfile(int fd) |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
52 { |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
53 struct flock lk; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
54 |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
55 /* Delete the PID from file. */ |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
56 /* We do this instead of trying to unlink the file. */ |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
57 ftruncate(fd, 0); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
58 |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
59 /* Remove lock from the file. */ |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
60 lk.l_type = F_UNLCK; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
61 lk.l_whence = SEEK_SET; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
62 lk.l_start = 0; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
63 lk.l_len = 0; |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
64 if (fcntl(fd, F_SETLK, &lk) == -1) |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
65 { |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
66 /* Lock can not be removed?!? WTF? */ |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
67 DEBUGPRINTF("Could not remove lock from pid file. STRANGE!\n"); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
68 } |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
69 close(fd); |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
70 } |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
71 |
20ca94680003
Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
72 #endif /* Not WIN32 */ |