]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/opie/libopie/unlock.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / opie / libopie / unlock.c
1 /* unlock.c: The opieunlock() library function.
2
3 %%% portions-copyright-cmetz-96
4 Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights
5 Reserved. The Inner Net License Version 2 applies to these portions of
6 the software.
7 You should have received a copy of the license with this software. If
8 you didn't get a copy, you may request one from <license@inner.net>.
9
10 Portions of this software are Copyright 1995 by Randall Atkinson and Dan
11 McDonald, All Rights Reserved. All Rights under this copyright are assigned
12 to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
13 License Agreement applies to this software.
14
15         History:
16
17         Modified by cmetz for OPIE 2.31. Bug fix.
18         Modified by cmetz for OPIE 2.3. Do refcounts whether or not
19             we actually lock. Fixed USER_LOCKING=0 case.
20         Modified by cmetz for OPIE 2.22. Added reference count support.
21             Changed lock filename/refcount symbol names to better indicate
22             that they're not user serviceable.
23         Modified by cmetz for OPIE 2.2. Use FUNCTION declaration.
24             Check for read() == -1. ifdef around unistd.h.
25         Created at NRL for OPIE 2.2 from opiesubr2.c
26 */
27 #include "opie_cfg.h"
28 #include <string.h>
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif /* HAVE_UNISTD_H */
32 #include <fcntl.h>
33 #include "opie.h"
34
35 extern int __opie_lockrefcount;
36 #if USER_LOCKING
37 extern char *__opie_lockfilename;
38 #endif /* USER_LOCKING */
39
40 /* 
41   Just remove the lock, right?
42   Well, not exactly -- we need to make sure it's ours. 
43 */
44 int opieunlock FUNCTION_NOARGS
45 {
46 #if USER_LOCKING
47   int fh, rval = -1, pid, t, i;
48   char buffer[128], *c, *c2;
49
50   if (--__opie_lockrefcount > 0)
51     return 0;
52
53   if (!__opie_lockfilename)
54     return -1;
55
56   if (!(fh = open(__opie_lockfilename, O_RDWR, 0600)))
57     goto unlockret;
58
59   if ((i = read(fh, buffer, sizeof(buffer))) < 0)
60     goto unlockret;
61
62   buffer[sizeof(buffer) - 1] = 0;
63   buffer[i - 1] = 0;
64
65   if (!(c = strchr(buffer, '\n')))
66     goto unlockret;
67
68   *(c++) = 0;
69
70   if (!(c2 = strchr(c, '\n')))
71     goto unlockret;
72
73   *(c2++) = 0;
74
75   if (!(pid = atoi(buffer)))
76     goto unlockret;
77
78   if (!(t = atoi(c)))
79     goto unlockret;
80
81   if ((pid != getpid()) && (time(0) <= OPIE_LOCK_TIMEOUT + t) && (!kill(pid, 0))) { 
82     rval = 1;
83     goto unlockret1;
84   }
85
86   rval = 0;
87
88 unlockret:
89   unlink(__opie_lockfilename);
90
91 unlockret1:
92   if (fh)
93     close(fh);
94   free(__opie_lockfilename);
95   __opie_lockfilename = NULL;
96   return rval;
97 #else /* USER_LOCKING */
98   if (__opie_lockrefcount-- > 0)
99     return 0;
100
101   return -1;
102 #endif /* USER_LOCKING */
103 }