1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* Copyright (c) 2010 David Teigland
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License V2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/poll.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "libdlm.h"
static int ast_called = 0;
static int quit;
static int do_release(void)
{
static dlm_lshandle_t *dh;
dh = dlm_create_lockspace("default", 0600);
if (!dh) {
printf("dlm_lock create lockspace error\n");
return -1;
}
dlm_release_lockspace("default", dh, 1);
return 0;
}
void sigterm_handler(int sig)
{
quit = 1;
}
static void bast_fn(void *arg)
{
printf("dlm_lock bast\n");
}
static void ast_fn(void *arg)
{
printf("dlm_lock ast\n");
ast_called = 1;
}
static int poll_for_ast(void)
{
struct pollfd pfd;
pfd.fd = dlm_get_fd();
pfd.events = POLLIN;
while (!ast_called) {
if (poll(&pfd, 1, 0) < 0) {
perror("poll");
return -1;
}
dlm_dispatch(pfd.fd);
}
ast_called = 0;
return 0;
}
int do_lock(char *name, struct dlm_lksb *lksb)
{
int len = strlen(name);
int rv;
printf("dlm_lock request NL ...\n");
fflush(stdout);
rv = dlm_lock(LKM_NLMODE, lksb, 0, name, len, 0,
ast_fn, lksb, bast_fn, NULL);
if (rv < 0) {
printf("dlm_lock request NL done error %d %d", rv, errno);
return -1;
}
poll_for_ast();
printf("dlm_lock request NL done status %d lkid %x\n",
lksb->sb_status, lksb->sb_lkid);
printf("dlm_lock convert EX ...\n");
fflush(stdout);
rv = dlm_lock(LKM_EXMODE, lksb, LKF_CONVERT, name, len, 0,
ast_fn, lksb, bast_fn, NULL);
if (rv < 0) {
printf("dlm_lock convert EX done error %d %d", rv, errno);
return -1;
}
poll_for_ast();
printf("dlm_lock convert EX done status %d\n", lksb->sb_status);
fflush(stdout);
return 0;
}
int main(int argc, char *argv[])
{
struct dlm_lksb lksb;
char name[32];
int i, count;
signal(SIGTERM, sigterm_handler);
printf("dlm_lock pid %d\n", getpid());
if (argc > 1 && !strcmp(argv[1], "release")) {
do_release();
return 0;
}
if (argc < 2)
count = 1;
else
count = atoi(argv[1]);
for (i = 0; i < count; i++) {
memset(name, 0, sizeof(name));
sprintf(name, "dlm_lock%d", i);
printf("dlm_lock resource %s\n", name);
/* too lazy to properly use separate lksb for each */
memset(&lksb, 0, sizeof(lksb));
do_lock(name, &lksb);
}
while (!quit)
pause();
return 0;
}
|