Commit | Line | Data |
---|---|---|
698b1561 VB |
1 | --- /dev/null 2016-12-11 08:33:21.825618476 +0530 |
2 | +++ ./cups/lockf.cpp 2016-12-11 09:44:20.050966252 +0530 | |
3 | @@ -0,0 +1,89 @@ | |
4 | +/* | |
5 | + * Copyright (C) 2016 The Android Open Source Project | |
6 | + * All rights reserved. | |
7 | + * | |
8 | + * Redistribution and use in source and binary forms, with or without | |
9 | + * modification, are permitted provided that the following conditions | |
10 | + * are met: | |
11 | + * * Redistributions of source code must retain the above copyright | |
12 | + * notice, this list of conditions and the following disclaimer. | |
13 | + * * Redistributions in binary form must reproduce the above copyright | |
14 | + * notice, this list of conditions and the following disclaimer in | |
15 | + * the documentation and/or other materials provided with the | |
16 | + * distribution. | |
17 | + * | |
18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
21 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
22 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
23 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
24 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
25 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
26 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
27 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
28 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | + * SUCH DAMAGE. | |
30 | + */ | |
31 | +#include <unistd.h> | |
32 | +#include <errno.h> | |
33 | +#include <fcntl.h> | |
34 | +#include <string.h> | |
35 | + | |
36 | +#ifndef _BITS_LOCKF_H_ | |
37 | +#define _BITS_LOCKF_H_ | |
38 | + | |
39 | +#include <sys/cdefs.h> | |
40 | + | |
41 | +#define F_ULOCK 0 | |
42 | +#define F_LOCK 1 | |
43 | +#define F_TLOCK 2 | |
44 | +#define F_TEST 3 | |
45 | + | |
46 | +__BEGIN_DECLS | |
47 | + | |
48 | +#if defined(__USE_FILE_OFFSET64) | |
49 | +int lockf(int, int, off_t) __RENAME(lockf64); | |
50 | +#else | |
51 | +int lockf(int, int, off_t); | |
52 | +#endif | |
53 | +int lockf64(int, int, off64_t); | |
54 | + | |
55 | +__END_DECLS | |
56 | + | |
57 | +#endif | |
58 | + | |
59 | + | |
60 | +int lockf64(int fd, int cmd, off64_t length) { | |
61 | + // Translate POSIX lockf into fcntl. | |
62 | + struct flock fl; | |
63 | + memset(&fl, 0, sizeof(fl)); | |
64 | + fl.l_whence = SEEK_CUR; | |
65 | + fl.l_start = 0; | |
66 | + fl.l_len = length; | |
67 | + if (cmd == F_ULOCK) { | |
68 | + fl.l_type = F_UNLCK; | |
69 | + cmd = F_SETLK64; | |
70 | + return fcntl(fd, F_SETLK64, &fl); | |
71 | + } | |
72 | + if (cmd == F_LOCK) { | |
73 | + fl.l_type = F_WRLCK; | |
74 | + return fcntl(fd, F_SETLKW64, &fl); | |
75 | + } | |
76 | + if (cmd == F_TLOCK) { | |
77 | + fl.l_type = F_WRLCK; | |
78 | + return fcntl(fd, F_SETLK64, &fl); | |
79 | + } | |
80 | + if (cmd == F_TEST) { | |
81 | + fl.l_type = F_RDLCK; | |
82 | + if (fcntl(fd, F_GETLK64, &fl) == -1) return -1; | |
83 | + if (fl.l_type == F_UNLCK || fl.l_pid == getpid()) return 0; | |
84 | + errno = EACCES; | |
85 | + return -1; | |
86 | + } | |
87 | + errno = EINVAL; | |
88 | + return -1; | |
89 | +} | |
90 | +int lockf(int fd, int cmd, off_t length) { | |
91 | + return lockf64(fd, cmd, length); | |
92 | +} |