Annotation of freem/src/glocks.c, revision 1.3
1.1 snw 1: /*
1.3 ! snw 2: * $Id$
1.1 snw 3: * display and clear M locks
4: *
5: *
1.2 snw 6: * Author: Serena Willis <snw@coherent-logic.com>
1.1 snw 7: * Copyright (C) 1998 MUG Deutschland
1.3 ! snw 8: * Copyright (C) 2020, 2025 Coherent Logic Development LLC
1.1 snw 9: *
10: *
11: * This file is part of FreeM.
12: *
13: * FreeM is free software: you can redistribute it and/or modify
14: * it under the terms of the GNU Affero Public License as published by
15: * the Free Software Foundation, either version 3 of the License, or
16: * (at your option) any later version.
17: *
18: * FreeM is distributed in the hope that it will be useful,
19: * but WITHOUT ANY WARRANTY; without even the implied warranty of
20: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: * GNU Affero Public License for more details.
22: *
23: * You should have received a copy of the GNU Affero Public License
24: * along with FreeM. If not, see <https://www.gnu.org/licenses/>.
25: *
1.3 ! snw 26: * $Log$
! 27: *
! 28: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
! 29: * SPDX-License-Identifier: AGPL-3.0-or-later
1.1 snw 30: **/
31:
32: #include <stdlib.h>
33: #include <stddef.h>
34: #include "mpsdef0.h"
35: #include "errmsg.h"
36: #include <signal.h>
37: #include <setjmp.h>
38: #include <fcntl.h>
39: #include <unistd.h>
40: #include <sys/types.h>
41: #include <sys/wait.h>
42: #include <string.h>
43:
44: #ifndef FREEBSD
45: #define EOF -1
46: #endif
47:
48: #include <stdio.h>
49: #ifdef SYSFIVE
50: #include <fcntl.h>
51: #endif /* SYSFIVE */
52:
53: /* needed if byte data are to be interpreted as unsigned integer */
54: #define UNSIGN(A) ((A)&0377)
55: void unlock ();
56: void zname ();
57: short int znamenumeric ();
58: long int tell ();
59:
60: #ifndef SYSFIVE
61: #define FreeM_timezone -3600
62: #else
63:
64: #ifdef __CYGWIN__
65: #define FreeM_timezone _timezone
66: #else
67: extern long FreeM_timezone;
68: #endif /* __CYGWIN__ */
69:
70: #endif /* SYSFIVE */
71:
72:
73:
74: int
75: main (argc, argv)
76: int argc; /* arguments count */
77: char **argv; /* arguments string */
78:
79: {
80: static char locktab[80] = "/usr/tmp/locktab"; /* file with LOCKs */
81: static long rempid = 0L; /* remove entry with rem PID */
82: short ltab; /* file descr. for locktab */
83: int pid;
84: short type;
85: char ch;
86: char line[300], varnam[300];
87: int i, j, n;
88:
89: if (argc > 1) {
90: j = 0;
91: while (--argc > 0) {
92: j++;
93: if (argv[j][0] == '-') {
94: if (rempid) {
95: fprintf (stderr, "usage is: %s [-pid] [lockfile]\n", *argv);
96: exit (0);
97: }
98: n = 0;
99: rempid = 0L;
100: while ((ch = argv[j][++n])) {
101: if (ch < '0' || ch > '9') {
102: fprintf (stderr, "usage is: %s [-pid] [lockfile]\n", *argv);
103: exit (0);
104: }
105: rempid = rempid * 10 + ch - '0';
106: }
107: continue;
108: }
109: strcpy (locktab, *(argv + j));
110: }
111: }
112: if (rempid)
113: unlock (locktab, rempid);
114: while ((ltab = open (locktab, 0)) == -1) {
115: printf ("cannot open '%s'\n", locktab);
116: exit (0);
117: }
118:
119: lseek (ltab, 0L, 0);
120: for (;;)
121: {
122: read (ltab, line, 3);
123: pid = UNSIGN (line[0]) * 256 + UNSIGN (line[1]);
124: if (pid == 0)
125: break;
126: type = line[2];
127: i = 0;
128: do {
129: read (ltab, &ch, 1);
130: if (ch == EOF)
131: goto done;
132: varnam[i++] = ch;
133: } while (ch != EOL);
134: zname (line, varnam);
135: printf ("%d\t%s %s\n", pid, type == 'D' ? "ZA" : "L ", line);
136: }
137:
138: done:
139: close (ltab);
140: exit (0);
141: }
142:
143: void
144: unlock (locktab, pid) /* unLOCK all entries of pid */
145: char *locktab; /* locktable */
146: long pid; /* process ID */
147:
148: {
149: short ltab; /* file descr. for locktab */
150: int cpid;
151: char entry[256];
152: long int r_pos; /* position to read */
153: long int w_pos; /* position to write */
154: int i,
155: j;
156:
157: /* open locktab, quit if nothing to be done */
158: if ((ltab = open (locktab, 2)) == -1)
159: return;
160:
161: /* request exclusive access to locktab (read & write) */
162: locking (ltab, 1, 0L);
163:
164: /* free all of your own locks; we do it by copying the whole stuff */
165: /* within 'locktab' omitting the old entries under our PID */
166: j = 0; /* count your old entries */
167: lseek (ltab, 0L, 0);
168: w_pos = 0L;
169: for (;;)
170: {
171: read (ltab, entry, 2);
172: cpid = UNSIGN (entry[0]) * 256 + UNSIGN (entry[1]);
173: if (cpid == 0) {
174: lseek (ltab, w_pos, 0);
175: write (ltab, entry, 2);
176: break;
177: }
178: i = 1;
179: do {
180: read (ltab, &entry[++i], 1);
181: } while (entry[i] != EOL);
182: i++;
183: if (cpid != pid) {
184: if (j) {
185: r_pos = tell (ltab);
186: lseek (ltab, w_pos, 0);
187: write (ltab, entry, (unsigned) i);
188: lseek (ltab, r_pos, 0);
189: }
190: w_pos += i;
191: } else
192: j++;
193: }
194: locking (ltab, 0, 0L);
195: close (ltab);
196: return;
197: } /* end lock() */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>