[go: up one dir, main page]

Menu

[0383ad]: / src / locks.c  Maximize  Restore  History

Download this file

312 lines (290 with data), 7.7 kB

  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
/***
* $Source: /cvsroot-fuse/gump/FreeM/src/locks.c,v $
* $Revision: 1.7 $ $Date: 2000/02/22 17:48:24 $
*/
/* *
* * *
* * *
* ***************
* * * * *
* * MUMPS *
* * * * *
* ***************
* * *
* * *
* *
*
* Shalom ha-Ashkenaz, 1998/06/18 CE
*
* program to display the mumps 'locktab'
*
*/
#include "mpsdef0.h"
#ifndef FREEBSD
#define EOF -1
#endif
#include <stdio.h>
#ifdef SYSFIVE
#include <fcntl.h>
#endif /* SYSFIVE */
/* needed if byte data are to be interpreted as unsigned integer */
#define UNSIGN(A) ((A)&0377)
void unlock ();
void zname ();
short int znamenumeric ();
/* 01/18/99 rlf Apparently, tell disappeared with libc-6 */
#ifdef LINUX_GLIBC
long int
tell (int fd)
{
return lseek (fd, 0, SEEK_CUR);
}
#endif /* LINUX_GLIBC */
void
main (argc, argv)
int argc; /* arguments count */
char **argv; /* arguments string */
{
static char locktab[80] = "/usr/tmp/locktab"; /* file with LOCKs */
static long rempid = 0L; /* remove entry with rem PID */
short ltab; /* file descr. for locktab */
int pid;
short type;
char ch;
char line[300],
varnam[300];
int i,
j,
n;
if (argc > 1) {
j = 0;
while (--argc > 0) {
j++;
if (argv[j][0] == '-') {
if (rempid) {
fprintf (stderr, "usage is: %s [-pid] [lockfile]\n", *argv);
exit (0);
}
n = 0;
rempid = 0L;
while ((ch = argv[j][++n])) {
if (ch < '0' || ch > '9') {
fprintf (stderr, "usage is: %s [-pid] [lockfile]\n", *argv);
exit (0);
}
rempid = rempid * 10 + ch - '0';
}
continue;
}
strcpy (locktab, *(argv + j));
}
}
if (rempid)
unlock (locktab, rempid);
while ((ltab = open (locktab, 0)) == -1) {
printf ("cannot open '%s'\n", locktab);
exit (0);
}
lseek (ltab, 0L, 0);
for (;;)
{
read (ltab, line, 3);
pid = UNSIGN (line[0]) * 256 + UNSIGN (line[1]);
if (pid == 0)
break;
type = line[2];
i = 0;
do {
read (ltab, &ch, 1);
if (ch == EOF)
goto done;
varnam[i++] = ch;
} while (ch != EOL);
zname (line, varnam);
printf ("%d\t%s %s\n", pid, type == 'D' ? "ZA" : "L ", line);
}
done:;
close (ltab);
exit (0);
}
void
unlock (locktab, pid) /* unLOCK all entries of pid */
char *locktab; /* locktable */
long pid; /* process ID */
{
short ltab; /* file descr. for locktab */
int cpid;
char entry[256];
long int r_pos; /* position to read */
long int w_pos; /* position to write */
int i,
j;
/* open locktab, quit if nothing to be done */
if ((ltab = open (locktab, 2)) == -1)
return;
/* request exclusive access to locktab (read & write) */
locking (ltab, 1, 0L);
/* free all of your own locks; we do it by copying the whole stuff */
/* within 'locktab' omitting the old entries under our PID */
j = 0; /* count your old entries */
lseek (ltab, 0L, 0);
w_pos = 0L;
for (;;)
{
read (ltab, entry, 2);
cpid = UNSIGN (entry[0]) * 256 + UNSIGN (entry[1]);
if (cpid == 0) {
lseek (ltab, w_pos, 0);
write (ltab, entry, 2);
break;
}
i = 1;
do {
read (ltab, &entry[++i], 1);
} while (entry[i] != EOL);
i++;
if (cpid != pid) {
if (j) {
r_pos = tell (ltab);
lseek (ltab, w_pos, 0);
write (ltab, entry, (unsigned) i);
lseek (ltab, r_pos, 0);
}
w_pos += i;
} else
j++;
}
locking (ltab, 0, 0L);
close (ltab);
return;
} /* end lock() */
#ifdef SYSFIVE
/******************************************************************************/
/* under XENIX the system has an intrinsic function 'locking'. */
/* under UNIX System V it must be done with fcntl. */
/* the difference is, that the XENIX locking blocks all other accesses */
/* whereas UNIX System V gives only protection if all users of a file */
/* use 'locking'. */
/* since 'fcntl' is not properly documented, the outcome in any cases of */
/* errors is uncertain. */
int
locking (fd, action, count)
int fd; /* file descriptor of file to be locked */
int action; /* action to be performed */
long count; /* area to be locked */
{
struct flock lock;
lock.l_whence = 1; /* lock from current position */
lock.l_start = 0;
lock.l_len = count;
lock.l_pid = getpid ();
switch (action) {
case 0: /* LK_UNLK free previously locked area */
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLK, &lock);
break;
case 1: /* LK_LOCK lock against others reading and writing my data */
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);
break;
case 2: /* LK_NBLCK lock against others reading and writing my data, don't block */
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLK, &lock);
break;
case 3: /* LK_RLCK lock against others writing my data */
lock.l_type = F_RDLCK;
fcntl (fd, F_SETLKW, &lock);
break;
case 4: /* LK_NBRLCK lock against others writing my data, don't block */
lock.l_type = F_RDLCK;
lock.l_pid = getpid ();
fcntl (fd, F_SETLK, &lock);
}
return 0; /* action properly performed */
} /* end locking() */
/******************************************************************************/
#endif /* SYSFIVE */
/******************************************************************************/
void
zname (a, b)
char *a,
*b;
{
int i,
j,
f,
n;
i = 0;
j = 0;
f = FALSE; /* we are in name section (vs.subscr.) */
n = FALSE; /* part is numeric (vs.alphabetic) */
while ((a[i] = b[j++]) != EOL) {
if (a[i] == '"')
a[++i] = '"';
if (a[i] == DELIM) {
if (f) {
if (n == FALSE)
a[i++] = '"';
a[i] = ',';
if ((n = znamenumeric (&b[j])) == FALSE)
a[++i] = '"';
} else {
a[i] = '(';
f = TRUE;
if ((n = znamenumeric (&b[j])) == FALSE)
a[++i] = '"';
}
}
if (++i >= 255) {
a[255] = EOL;
}
}
if (f) {
if (n == FALSE)
a[i++] = '"';
a[i++] = ')';
a[i] = EOL;
}
a[i] = 0;
return;
} /* end zname() */
/******************************************************************************/
short int
znamenumeric (str)
char *str; /** boolean function that tests
* whether str is a canonical
* numeric */
{
register ptr = 0,
ch;
register point;
if (str[0] == '-')
ptr = 1;
if (str[ptr] == EOL)
return FALSE;
if (str[ptr] == DELIM)
return FALSE;
if (str[ptr] == '0')
return str[1] == EOL || str[1] == DELIM; /* leading zero */
point = FALSE;
while ((ch = str[ptr++]) != EOL && ch != DELIM) {
if (ch > '9')
return FALSE;
if (ch < '0') {
if (ch != '.')
return FALSE;
if (point)
return FALSE; /* multiple points */
point = TRUE;
}
}
if (point) {
if ((ch = str[ptr - 2]) == '0')
return FALSE; /* trailing zero */
if (ch == '.')
return FALSE; /* trailing point */
}
return TRUE;
} /* end of znamenumeric() */
/******************************************************************************/
/* End of $Source: /cvsroot-fuse/gump/FreeM/src/locks.c,v $ */