annotate common/linuxlockfile.c @ 1280:6e454e24b8af

MErged.
author Emanuel Schuetze <emanuel@intevation.de>
date Mon, 29 Sep 2014 11:43:23 +0200
parents 7ea3a4c0e2ae
children 8362e30f7b55
rev   line source
1160
a974b61a5cce (issue108) Add missing license headers
Andre Heinecke <andre.heinecke@intevation.de>
parents: 782
diff changeset
1 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
a974b61a5cce (issue108) Add missing license headers
Andre Heinecke <andre.heinecke@intevation.de>
parents: 782
diff changeset
2 * Software engineering by Intevation GmbH
a974b61a5cce (issue108) Add missing license headers
Andre Heinecke <andre.heinecke@intevation.de>
parents: 782
diff changeset
3 *
a974b61a5cce (issue108) Add missing license headers
Andre Heinecke <andre.heinecke@intevation.de>
parents: 782
diff changeset
4 * This file is Free Software under the GNU GPL (v>=2)
a974b61a5cce (issue108) Add missing license headers
Andre Heinecke <andre.heinecke@intevation.de>
parents: 782
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY!
a974b61a5cce (issue108) Add missing license headers
Andre Heinecke <andre.heinecke@intevation.de>
parents: 782
diff changeset
6 * See LICENSE.txt for details.
a974b61a5cce (issue108) Add missing license headers
Andre Heinecke <andre.heinecke@intevation.de>
parents: 782
diff changeset
7 */
a974b61a5cce (issue108) Add missing license headers
Andre Heinecke <andre.heinecke@intevation.de>
parents: 782
diff changeset
8
782
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
9 #ifndef WIN32
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
10
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
11 #include <fcntl.h>
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
12 #include <stdio.h>
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
13 #include <sys/types.h>
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
14 #include <unistd.h>
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
15
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
16 #include "logging.h"
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
17
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
18 int
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
19 open_lockfile(char *path)
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
20 {
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
21 int fd;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
22 char pidstr[20];
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
23 size_t pidstrlen;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
24 struct flock lk;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
25 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
26 if (fd != -1)
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
27 {
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
28 /* 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
29 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
30 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
31 lk.l_start = 0;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
32 lk.l_len = 0;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
33 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
34 {
1162
2a1206932f53 (issue107) Comment all TODO's and FIXME's with issues as documented in issue107
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1160
diff changeset
35 /* FIXME (issue139): For extra security we should check if there is
782
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
36 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
37 /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
38 as ours running... */
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
39 ftruncate(fd, 0);
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
40 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
41 write(fd, pidstr, pidstrlen);
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 /* 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
46 close(fd);
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
47 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
48 return -1;
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 }
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
51 else
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 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
54 }
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
55 return fd;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
56 }
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
57
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
58 void
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
59 close_lockfile(int fd)
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
60 {
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
61 struct flock lk;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
62
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
63 /* 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
64 /* 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
65 ftruncate(fd, 0);
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
66
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
67 /* 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
68 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
69 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
70 lk.l_start = 0;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
71 lk.l_len = 0;
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
72 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
73 {
1250
7ea3a4c0e2ae This should be serious software.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1162
diff changeset
74 /* Lock can not be removed */
7ea3a4c0e2ae This should be serious software.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1162
diff changeset
75 DEBUGPRINTF("Could not remove lock from pid file.\n");
782
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
76 }
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
77 close(fd);
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
78 }
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
79
20ca94680003 Implemented detection of running instance on linux using a lock file.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
80 #endif /* Not WIN32 */

http://wald.intevation.org/projects/trustbridge/