Omit CVS Id. Update copyright year.
[idzebra-moved-to-github.git] / util / tstlockscope.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** \file tstlockscope.c
21     \brief tests scope of fcntl locks.. Either "process"  or "thread"
22 */
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <pthread.h>
29 #include <yaz/log.h>
30 #include <yaz/test.h>
31 #if YAZ_POSIX_THREADS
32 #include <fcntl.h>
33 #endif
34
35 int fd;
36
37 const char *scope = "unknown";
38
39 static int file_lock(int fd, int type, int cmd)
40 {
41     struct flock area;
42     area.l_type = type;
43     area.l_whence = SEEK_SET;
44     area.l_len = area.l_start = 0L;
45     
46     return fcntl(fd, cmd, &area);
47 }
48
49 void *run_func(void *arg)
50 {
51     if (file_lock(fd, F_WRLCK, F_SETLK) == -1)
52         scope = "thread";
53     else
54         scope = "process";
55     return 0;
56 }
57
58 void tst(void)
59 {
60     pthread_t child_thread;
61     int r;
62     fd = open("my.LCK", (O_CREAT|O_RDWR), 0666);
63
64     YAZ_CHECK(fd != -1);
65     if (fd == -1)
66     {
67         yaz_log(YLOG_FATAL|YLOG_ERRNO, "open");
68         return;
69     }
70
71     r = file_lock(fd, F_WRLCK, F_SETLKW);
72     YAZ_CHECK(r != -1);
73     if (r == -1)
74     {
75         yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcnt");
76         return;
77     }
78
79 #if YAZ_POSIX_THREADS
80     pthread_create(&child_thread, 0 /* attr */, run_func, 0);
81     pthread_join(child_thread, 0);
82 #endif
83     yaz_log(YLOG_LOG, "fcntl lock scope: %s", scope);
84 }
85
86 int main(int argc, char **argv)
87 {
88     YAZ_CHECK_INIT(argc, argv);
89     YAZ_CHECK_LOG();
90     tst();
91     YAZ_CHECK_TERM;
92     return 0;
93 }
94
95 /*
96  * Local variables:
97  * c-basic-offset: 4
98  * indent-tabs-mode: nil
99  * End:
100  * vim: shiftwidth=4 tabstop=8 expandtab
101  */
102