xref: /illumos-gate/usr/src/lib/efcode/engine/alarm.c (revision 09e6639b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <unistd.h>
32 #include <fcode/private.h>
33 
34 /*
35  * 'user-abort' Fcode
36  */
37 void
user_abort(fcode_env_t * env)38 user_abort(fcode_env_t *env)
39 {
40 	forth_abort(env, "user-abort called");
41 }
42 
43 static fstack_t alarm_xt;
44 static fstack_t alarm_ms;
45 static fcode_env_t *alarm_env;
46 
47 static void
catch_alarm(int signo)48 catch_alarm(int signo)
49 {
50 	fcode_env_t *env = alarm_env;
51 
52 	if (env && alarm_xt && alarm_ms) {
53 		PUSH(DS, alarm_xt);
54 		execute(env);
55 		(void) signal(SIGALRM, catch_alarm);
56 		(void) alarm((alarm_ms + 999)/1000);
57 	}
58 }
59 
60 /*
61  * 'alarm' Fcode
62  */
63 void
do_alarm(fcode_env_t * env)64 do_alarm(fcode_env_t *env)
65 {
66 	fstack_t ms, xt;
67 
68 	CHECK_DEPTH(env, 2, "alarm");
69 	ms = POP(DS);
70 	xt = POP(DS);
71 	if (ms == 0) {
72 		(void) alarm(0);
73 		(void) signal(SIGALRM, SIG_DFL);
74 		alarm_xt = 0;
75 		alarm_ms = 0;
76 		alarm_env = 0;
77 	} else {
78 		(void) signal(SIGALRM, catch_alarm);
79 		alarm_xt = xt;
80 		alarm_ms = ms;
81 		alarm_env = env;
82 		(void) alarm((ms + 999)/1000);
83 	}
84 }
85 
86 #pragma init(_init)
87 
88 static void
_init(void)89 _init(void)
90 {
91 	fcode_env_t *env = initial_env;
92 
93 	ASSERT(env);
94 	NOTICE;
95 
96 	P1275(0x213, 0,		"alarm",		do_alarm);
97 
98 	P1275(0x219, 0,		"user-abort",		user_abort);
99 }
100