summaryrefslogtreecommitdiff
path: root/libfshelp-tests/locks.c
blob: d9af78aec9bd1496c6d2f9005d663abfe1f3efb5 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* Test record locking.

   Copyright (C) 2001, 2018-2019 Free Software Foundation, Inc.

   Written by Neal H Walfield <neal@cs.uml.edu>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will 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.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.  */

#include "../libfshelp/fshelp.h"
#include "../libfshelp/rlock.h"
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <string.h>

#include "fs_U.h"

#ifndef PEROPENS
#define PEROPENS 10
#endif

struct rlock_box box;
struct rlock_peropen peropens[PEROPENS];
loff_t pointers[PEROPENS];
loff_t file_size;

struct command
{
  char *name;
  int (*func)(char *cmds);
  char *doc;
};

error_t cmd_help (char *);
error_t cmd_comment (char *);
error_t cmd_echo (char *);
error_t cmd_lock (char *);
error_t cmd_list (char *);
error_t cmd_seek (char *);
error_t cmd_exec (char *);

struct command commands [] =
  {
    { "help", cmd_help, "Print this screen" },
    { "#", cmd_comment, "Comment (Must _start_ the line)." },
    { "echo", cmd_echo, "Echo the line." },
    { "lock", cmd_lock,
      "po start length type\n"
      "\ttype = { F_UNLCK=0, F_RDLCK,=1, F_WRLCK=2 }" },
    { "list", cmd_list, "list all locks' status" },
    { "seek", cmd_seek, "PO1 ... Print the position of the given po.\n"
      "\tPO1=N ... Seek a given po." },
    { "exec", cmd_exec, "Execute a built in echoing the command."}
  };

error_t
cmd_help (char *args)
{
  int i;
  printf ("Commands:\n");
  for (i = 0; i < sizeof (commands) / sizeof (struct command); i ++)
    printf ("%s\t%s\n", commands[i].name, commands[i].doc);
  return 0;
}

error_t
cmd_comment (char *args)
{
  return 0;
}

error_t
cmd_echo (char *args)
{
  printf ("%s", args);
  return 0;
}

error_t
cmd_lock (char *args)
{
  int po, type;
  loff_t start, len;
  struct flock64 lock;
  mach_port_t rendezvous = MACH_PORT_NULL;
  error_t err;

  if (4 != sscanf (args, "%d %ld %ld %d", &po, (long*)&start, (long*)&len, &type))
    {
      printf ("Syntax error.\n");
      return 0;
    }

  lock.l_type = type;
  lock.l_whence = SEEK_CUR;
  lock.l_start = (long)start;
  lock.l_len = (long)len;

  if (po < 0 || po >= PEROPENS)
    {
      printf ("Unknown peropen: %d.\n", po);
      return 0;
    }

  switch (type)
    {
    case 0: lock.l_type = F_UNLCK; break;
    case 1: lock.l_type = F_RDLCK; break;
    case 2: lock.l_type = F_WRLCK; break;
    default: printf ("Unknown type.\n"); return 0;
    }

  err= fshelp_rlock_tweak (&box, NULL, &peropens[po], O_RDWR,
			   file_size, pointers[po], F_SETLK64,
			   &lock, rendezvous);
  if (! err)
    {
      char buf[10];
      sprintf (buf, "%d\n", po);
      cmd_list (buf);
    }
  return err;
}

error_t
cmd_list (char *args)
{
  char *end;

  void dump (int i)
    {
      struct rlock_list *l;

      printf ("%3d:", i);
      for (l = *peropens[i].locks; l; l = l->po.next)
        {
	  printf ("\tStart = %4ld; Length = %4ld; Type = ", (long)l->start, (long)l->len);
	  switch (l->type)
	    {
	    case F_RDLCK: printf ("F_RDLCK"); break;
	    case F_WRLCK: printf ("F_WRLCK"); break;
	    case F_UNLCK: printf ("F_UNLCK"); break;
	    default: printf ("UNKNOWN"); break;
	    }
	  printf ("\n");
	}

      if (*peropens[i].locks == NULL)
	printf ("\n");
    }

  while (*args == ' ')
    args ++;

  if (*args == '\n' || *args == '\0')
    {
      int i;

      for (i = 0; i < PEROPENS; i ++)
	dump (i);
      return 0;
    }

  while (1)
    {
      long int p = strtoll (args, &end, 0);
      if (end == args)
        {
	  printf ("Syntax error.\n");
	  return 0;
	}

      if (p < 0 || p > PEROPENS)
	printf ("%3ld:\tOut of range.", p);
      else
	dump (p);

      while (*end == ' ')
	end ++;

      if (*end == '\n' || *end == '\0')
	return 0;
      args = end;
    }
}

error_t
cmd_seek (char *args)
{
  char *end;
  int p;

  while (*args == ' ')
    args ++;

  if (*args == '\n' || *args == '\0')
    {
      int i;
      for (i = 0; i < PEROPENS; i ++)
        printf ("%3d: %ld\n", i, (long)pointers[i]);
      return 0;
    }

  while (1)
    {
      int set = 0;
      long seek_to = 0;

      p = strtol (args, &end, 0);
      if (end == args)
        {
	  printf ("Syntax error.\n");
	  return 0;
	}

      if (*end == '=')
        {
	  set = 1;
	  args = end + 1;
	  seek_to = strtol (args, &end, 0);
	  if (end == args)
	    {
	      printf ("Syntax error.\n");
	      return 0;
	    }
	}

      if (p < 0 || p > PEROPENS)
	printf ("%3d: unknown peropen\n", p);
      else
        {
          printf ("%3d: %ld", p, (long)pointers[p]);
	  if (set)
	    printf (" => %ld\n", (long)(pointers[p] = seek_to));
	  else
	    printf ("\n");
	}

      while (*end == ' ')
	end ++;
      if (*end == '\0' || *end == '\n')
        return 0;
      args = end;
    }
}

error_t
interpret (char *buffer)
{
  int i;

  while (*buffer == ' ')
    buffer ++;

  if (*buffer == '\n')
    return 0;

  for (i = 0; i < sizeof (commands) / sizeof (struct command); i ++)
  if (strncmp (commands[i].name, buffer, strlen (commands[i].name)) == 0)
    {
      error_t err;
      err = commands[i].func (buffer + strlen (commands[i].name) + 1);
      if (err)
	printf ("%s\n", strerror (err));
      return err;
    }

  printf ("Unknown command.\n");
  return 0;
}

error_t
cmd_exec (char *arg)
{
  printf ("%s", arg);
  interpret (arg);
  return 0;
}

int main (int argc, char *argv[])
{
  int i;

  if (argc != 1)
    {
      printf ("Usage: %s\n"
	      "\tType `help' at the prompt.\n"
	      "\tUsed to test the record locking functions in libfshelp\n",
	      argv[0]);
      return 1;
    }

  fshelp_rlock_init (&box);
  for (i = 0; i < PEROPENS; i ++)
    fshelp_rlock_po_init (&peropens[i]);

  while (! feof (stdin))
    {
      char b[1024];

      printf ("> ");
      fflush (stdout);

      if (! fgets (b, sizeof (b), stdin))
	{
	  if (feof (stdin))
	    break;
	  else
	    continue;
	}

      interpret (b);
    }

  printf ("\n");
  return 0;
}