Annotation of freem/src/events.c, revision 1.4
1.1 snw 1: /*
1.4 ! snw 2: * $Id: events.c,v 1.3 2025/03/09 19:14:24 snw Exp $
1.1 snw 3: * event framework
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.4 ! snw 26: * $Log: events.c,v $
! 27: * Revision 1.3 2025/03/09 19:14:24 snw
! 28: * First phase of REUSE compliance and header reformat
! 29: *
1.3 snw 30: *
31: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
32: * SPDX-License-Identifier: AGPL-3.0-or-later
1.1 snw 33: **/
34:
35: #include <stdlib.h>
36: #include <string.h>
37:
38: #include "mpsdef.h"
39: #include "events.h"
40: #include "freem.h"
41: #include "mref.h"
42:
43:
44: short evt_async_enabled = FALSE;
45: short evt_async_restore = FALSE;
46: short evt_async_initial = FALSE;
47:
48: short evt_queue_rear = -1;
49: short evt_queue_front = -1;
50:
51: evt_entry *evt_queue[EVT_QLENGTH];
52: short evt_status[EVT_MAX];
53: int evt_blocks[EVT_MAX];
54: int evt_depth;
55:
56: void evt_init (void)
57: {
58: register int i;
59:
60: for(i = 0; i < EVT_MAX; i++) {
61: evt_status[i] = EVT_S_DISABLED;
62: evt_blocks[i] = 0;
63: }
64:
65: evt_async_enabled = FALSE;
66: evt_depth = 0;
67: }
68:
69: int evt_ablock (short evt_class)
70: {
71: register int i;
72: int unblocked_evt_count = 0;
73:
74: evt_blocks[evt_class]++;
75:
76: for (i = 0; i < EVT_MAX; i++) {
77: if (evt_blocks[i] == 0) unblocked_evt_count++;
78: }
79:
80: if (unblocked_evt_count == 0) evt_async_enabled = FALSE;
81:
82: return evt_blocks[evt_class];
83: }
84:
85: int evt_aunblock (short evt_class)
86: {
87: register int i;
88: int unblocked_evt_count = 0;
89:
90: if (evt_blocks[evt_class]) evt_blocks[evt_class]--;
91:
92: for (i = 0; i < EVT_MAX; i++) {
93: if (evt_blocks[i] == 0) unblocked_evt_count++;
94: }
95:
96: if (unblocked_evt_count) evt_async_enabled = TRUE;
97:
98: return evt_blocks[i - 1];
99: }
100:
101: int evt_enqueue (char *event_id, short evt_class, short interrupt)
102: {
103: evt_entry *e = malloc (sizeof (evt_entry));
104: NULLPTRCHK(e,"evt_enqueue");
105:
106: if (!evt_registered (event_id, evt_class)) {
107: free (e);
108: return -1;
109: }
110:
111: strncpy (e->event_id, event_id, 256);
112: e->evt_class = evt_class;
113:
114: if (evt_queue_rear == EVT_QLENGTH - 1) {
115: /* queue overflow */
116: free (e);
117: return -1;
118: }
119:
120: if (evt_queue_front == -1) evt_queue_front = 0;
121:
122: evt_queue[++evt_queue_rear] = e;
123:
124: if (interrupt && (evt_blocks[evt_class] == 0)) {
125: merr_raise (ASYNC);
126: }
127:
128: return evt_queue_rear;
129: }
130:
131: evt_entry *evt_dequeue (void)
132: {
133: evt_entry *e;
134:
135: if (evt_queue_front == -1 || evt_queue_front > evt_queue_rear) {
136: /* queue underflow */
137: return NULL;
138: }
139:
140: e = evt_queue[evt_queue_front++];
141:
142: return e;
143: }
144:
145:
146: char *evt_class_name (evt_entry *e) {
147:
148: char *evt_name = malloc (15 * sizeof (char));
149: NULLPTRCHK(evt_name,"evt_class_name");
150:
151: switch (e->evt_class) {
152:
153: case EVT_CLS_COMM:
154: strcpy (evt_name, "COMM");
155: break;
156:
157: case EVT_CLS_HALT:
158: strcpy (evt_name, "HALT");
159: break;
160:
161: case EVT_CLS_IPC:
162: strcpy (evt_name, "IPC");
163: break;
164:
165: case EVT_CLS_INTERRUPT:
166: strcpy (evt_name, "INTERRUPT");
167: break;
168:
169: case EVT_CLS_POWER:
170: strcpy (evt_name, "POWER");
171: break;
172:
173: case EVT_CLS_TIMER:
174: strcpy (evt_name, "TIMER");
175: break;
176:
177: case EVT_CLS_USER:
178: strcpy (evt_name, "USER");
179: break;
180:
181: case EVT_CLS_WAPI:
182: strcpy (evt_name, "WAPI");
183: break;
184:
185: case EVT_CLS_TRIGGER:
186: strcpy (evt_name, "TRIGGER");
187: break;
188:
189: }
190:
191: return evt_name;
192:
193: }
194:
195: char *evt_class_name_c (int c) {
196:
197: char *evt_name = malloc (15 * sizeof (char));
198: NULLPTRCHK(evt_name,"evt_class_name_c");
199:
200: switch (c) {
201:
202: case EVT_CLS_COMM:
203: strcpy (evt_name, "COMM");
204: break;
205:
206: case EVT_CLS_HALT:
207: strcpy (evt_name, "HALT");
208: break;
209:
210: case EVT_CLS_IPC:
211: strcpy (evt_name, "IPC");
212: break;
213:
214: case EVT_CLS_INTERRUPT:
215: strcpy (evt_name, "INTERRUPT");
216: break;
217:
218: case EVT_CLS_POWER:
219: strcpy (evt_name, "POWER");
220: break;
221:
222: case EVT_CLS_TIMER:
223: strcpy (evt_name, "TIMER");
224: break;
225:
226: case EVT_CLS_USER:
227: strcpy (evt_name, "USER");
228: break;
229:
230: case EVT_CLS_WAPI:
231: strcpy (evt_name, "WAPI");
232: break;
233:
234: case EVT_CLS_TRIGGER:
235: strcpy (evt_name, "TRIGGER");
236: break;
237:
238: }
239:
240: return evt_name;
241:
242: }
243:
244: int evt_get_handlers (char *buf) {
245:
246: char *t_key;
247: char *class_name;
248: char *t_buf;
249: freem_ref_t *r;
250: freem_ref_t *rs;
251: char pid_s[10];
252: int ct;
253: evt_entry *e;
254:
1.4 ! snw 255: class_name = malloc (STRLEN * sizeof (char));
1.1 snw 256: NULLPTRCHK(class_name,"evt_get_handlers");
257:
258: t_buf = malloc (STRLEN * sizeof (char));
259: NULLPTRCHK(t_buf,"evt_get_handlers");
260:
261: r = malloc (sizeof (freem_ref_t));
262: NULLPTRCHK(r,"evt_get_handlers");
263:
264: rs = malloc (sizeof (freem_ref_t));
265: NULLPTRCHK(rs,"evt_get_handlers");
266:
267: ct = 0;
268: t_buf[0] = '\201';
269:
270: mref_init (r, MREF_RT_SSVN, "^$JOB");
271: mref_init (rs, MREF_RT_SSVN, "^$SYSTEM");
272:
273: snprintf (pid_s, 9, "%d", pid);
274:
275:
276: while ((e = evt_dequeue ()) != NULL) {
277:
278: if (evt_status[e->evt_class] > EVT_S_DISABLED) {
279: class_name = evt_class_name (e);
280:
281: mref_set_subscript (r, 0, pid_s);
282: mref_set_subscript (r, 1, "EVENT");
283: mref_set_subscript (r, 2, class_name);
284: mref_set_subscript (r, 3, e->event_id);
285:
286: t_key = mref_to_internal (r);
287: ssvn (get_sym, t_key, t_buf);
288: free (t_key);
289: stcnv_m2c (t_buf);
290:
291: if (strlen (t_buf) > 0) {
292:
293: ct++;
294:
295: strcat (buf, t_buf);
296: strcat (buf, ",");
297: }
298:
299: mref_set_subscript (rs, 0, "EVENT");
300: mref_set_subscript (rs, 1, class_name);
301: mref_set_subscript (rs, 2, e->event_id);
302:
303: t_key = mref_to_internal (rs);
304: ssvn (get_sym, t_key, t_buf);
305: free (t_key);
306: stcnv_m2c (t_buf);
307:
308: if (strlen (t_buf) > 0) {
309:
310: ct++;
311:
312: strcat (buf, t_buf);
313: strcat (buf, ",");
314: }
315:
316: }
317:
318: free (e);
319:
320: }
321:
322: if (ct) {
323: buf[strlen (buf) - 1] = NUL;
324: }
325: else {
326: buf[0] = NUL;
327: }
328:
329: free (r);
330: free (rs);
331: free (class_name);
332: free (t_buf);
333:
334: return ct;
335: }
336:
337: short evt_registered (char *event_id, short evt_class)
338: {
339: char *t_key;
340: char *class_name;
341: char *t_buf;
342: freem_ref_t *r;
343: freem_ref_t *rs;
344: char pid_s[10];
345: int ct;
346:
1.4 ! snw 347: t_key = malloc (STRLEN * sizeof (char));
1.1 snw 348: NULLPTRCHK(t_key,"evt_registered");
349:
1.4 ! snw 350: class_name = malloc (STRLEN * sizeof (char));
1.1 snw 351: NULLPTRCHK(class_name,"evt_registered");
352:
353: t_buf = malloc (STRLEN * sizeof (char));
354: NULLPTRCHK(t_buf,"evt_registered");
355:
356:
357: r = malloc (sizeof (freem_ref_t));
358: NULLPTRCHK(r,"evt_registered");
359:
360: rs = malloc (sizeof (freem_ref_t));
361: NULLPTRCHK(rs,"evt_registered");
362:
363: ct = 0;
364: t_buf[0] = '\201';
365:
366: mref_init (r, MREF_RT_SSVN, "^$JOB");
367:
368: snprintf (pid_s, 9, "%d", pid);
369:
370: class_name = evt_class_name_c (evt_class);
371:
372: mref_set_subscript (r, 0, pid_s);
373: mref_set_subscript (r, 1, "EVENT");
374: mref_set_subscript (r, 2, class_name);
375: mref_set_subscript (r, 3, event_id);
376:
377: t_key = mref_to_internal (r);
378:
379: ssvn (get_sym, t_key, t_buf);
380: stcnv_m2c (t_buf);
381:
382: if (strlen (t_buf) > 0) ct++;
383:
384: mref_init (rs, MREF_RT_SSVN, "^$SYSTEM");
385:
386: mref_set_subscript (rs, 0, "EVENT");
387: mref_set_subscript (rs, 1, class_name);
388: mref_set_subscript (rs, 2, event_id);
389:
390: t_key = mref_to_internal (rs);
391:
392: ssvn (get_sym, t_key, t_buf);
393: stcnv_m2c (t_buf);
394:
395: if (strlen (t_buf) > 0) ct++;
396:
397: free (r);
398: free (rs);
399: free (t_key);
400: free (class_name);
401: free (t_buf);
402:
403: return (ct > 0) ? TRUE : FALSE;
404: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>