1 /* unlock.c: The opieunlock() library function.
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
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>.
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.
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
31 #endif /* HAVE_UNISTD_H */
35 extern int __opie_lockrefcount;
37 extern char *__opie_lockfilename;
38 #endif /* USER_LOCKING */
41 Just remove the lock, right?
42 Well, not exactly -- we need to make sure it's ours.
44 int opieunlock FUNCTION_NOARGS
47 int fh, rval = -1, pid, t, i;
48 char buffer[128], *c, *c2;
50 if (--__opie_lockrefcount > 0)
53 if (!__opie_lockfilename)
56 if (!(fh = open(__opie_lockfilename, O_RDWR, 0600)))
59 if ((i = read(fh, buffer, sizeof(buffer))) < 0)
62 buffer[sizeof(buffer) - 1] = 0;
65 if (!(c = strchr(buffer, '\n')))
70 if (!(c2 = strchr(c, '\n')))
75 if (!(pid = atoi(buffer)))
81 if ((pid != getpid()) && (time(0) <= OPIE_LOCK_TIMEOUT + t) && (!kill(pid, 0))) {
89 unlink(__opie_lockfilename);
94 free(__opie_lockfilename);
95 __opie_lockfilename = NULL;
97 #else /* USER_LOCKING */
98 if (__opie_lockrefcount-- > 0)
102 #endif /* USER_LOCKING */