blob: 644fa9454ff690f6ba838abf273e764b297d09cf [file] [log] [blame]
Dmitry V. Levincc1e1492015-10-09 00:48:19 +00001/*
2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_CONFIG_H
29# include "config.h"
30#endif
31
32#include <assert.h>
33#include <errno.h>
34#include <stdio.h>
35#include <stdint.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <sys/syscall.h>
39#include "flock.h"
40
41#define FILE_LEN 4096
42
43static void
44test_flock(int nr, const char *name)
45{
46 struct_kernel_flock fl = {
47 .l_type = F_RDLCK,
48 .l_start = (off_t) 0xdefaced1facefeed,
49 .l_len = (off_t) 0xdefaced2cafef00d
50 };
51 int rc;
52
53 syscall(nr, -1, F_SETLK, &fl);
54 printf("%s(-1, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET"
55 ", l_start=%jd, l_len=%jd}) = -1 %s\n",
56 name, (intmax_t) fl.l_start, (intmax_t) fl.l_len,
57 errno == EBADF ? "EBADF (Bad file descriptor)" :
58 "EINVAL (Invalid argument)");
59
60 fl.l_start = 0;
61 fl.l_len = FILE_LEN;
62
63 rc = syscall(nr, 0, F_SETLK, &fl);
64 printf("%s(0, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET"
65 ", l_start=0, l_len=%d}) = %s\n",
66 name, FILE_LEN, rc ? "-1 EINVAL (Invalid argument)" : "0");
67
68 if (rc)
69 return;
70
71 syscall(nr, 0, F_GETLK, &fl);
72 printf("%s(0, F_GETLK, {l_type=F_UNLCK, l_whence=SEEK_SET"
73 ", l_start=0, l_len=%d, l_pid=0}) = 0\n", name, FILE_LEN);
74
75 syscall(nr, 0, F_SETLK, &fl);
76 printf("%s(0, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET"
77 ", l_start=0, l_len=%d}) = 0\n", name, FILE_LEN);
78
79}
80
81static void
82test_flock64(int nr, const char *name)
83{
84 struct_kernel_flock64 fl = {
85 .l_type = F_RDLCK,
86 .l_start = 0xdefaced1facefeed,
87 .l_len = 0xdefaced2cafef00d
88 };
89 int rc;
90
91 syscall(nr, -1, F_SETLK64, &fl);
92 printf("%s(-1, F_SETLK64, {l_type=F_RDLCK, l_whence=SEEK_SET"
93 ", l_start=%jd, l_len=%jd}) = -1 %s\n",
94 name, (intmax_t) fl.l_start, (intmax_t) fl.l_len,
95 errno == EBADF ? "EBADF (Bad file descriptor)" :
96 "EINVAL (Invalid argument)");
97
98 fl.l_start = 0;
99 fl.l_len = FILE_LEN;
100
101 rc = syscall(nr, 0, F_SETLK64, &fl);
102 printf("%s(0, F_SETLK64, {l_type=F_RDLCK, l_whence=SEEK_SET"
103 ", l_start=0, l_len=%d}) = %s\n",
104 name, FILE_LEN, rc ? "-1 EINVAL (Invalid argument)" : "0");
105
106 if (rc)
107 return;
108
109 syscall(nr, 0, F_GETLK64, &fl);
110 printf("%s(0, F_GETLK64, {l_type=F_UNLCK, l_whence=SEEK_SET"
111 ", l_start=0, l_len=%d, l_pid=0}) = 0\n", name, FILE_LEN);
112
113 syscall(nr, 0, F_SETLK64, &fl);
114 printf("%s(0, F_SETLK64, {l_type=F_UNLCK, l_whence=SEEK_SET"
115 ", l_start=0, l_len=%d}) = 0\n", name, FILE_LEN);
116}
117
118int
119main(void)
120{
121 char fname[] = "struct_flock_XXXXXX";
122
123 (void) close(0);
124 assert(mkstemp(fname) == 0);
125 assert(unlink(fname) == 0);
126 assert(ftruncate(0, FILE_LEN) == 0);
127
128#ifdef __NR_fcntl
129 test_flock(__NR_fcntl, "fcntl");
130 test_flock64(__NR_fcntl, "fcntl");
131#endif
132#ifdef __NR_fcntl64
133 test_flock(__NR_fcntl64, "fcntl64");
134 test_flock64(__NR_fcntl64, "fcntl64");
135#endif
136
137 puts("+++ exited with 0 +++");
138 return 0;
139}