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