xref: /illumos-gate/usr/src/lib/efcode/engine/alarm.c (revision 09e6639b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <signal.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <fcode/private.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * 'user-abort' Fcode
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate void
user_abort(fcode_env_t * env)387c478bd9Sstevel@tonic-gate user_abort(fcode_env_t *env)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	forth_abort(env, "user-abort called");
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static fstack_t alarm_xt;
447c478bd9Sstevel@tonic-gate static fstack_t alarm_ms;
457c478bd9Sstevel@tonic-gate static fcode_env_t *alarm_env;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static void
catch_alarm(int signo)487c478bd9Sstevel@tonic-gate catch_alarm(int signo)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	fcode_env_t *env = alarm_env;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if (env && alarm_xt && alarm_ms) {
537c478bd9Sstevel@tonic-gate 		PUSH(DS, alarm_xt);
547c478bd9Sstevel@tonic-gate 		execute(env);
55*09e6639bSToomas Soome 		(void) signal(SIGALRM, catch_alarm);
56*09e6639bSToomas Soome 		(void) alarm((alarm_ms + 999)/1000);
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * 'alarm' Fcode
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate void
do_alarm(fcode_env_t * env)647c478bd9Sstevel@tonic-gate do_alarm(fcode_env_t *env)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	fstack_t ms, xt;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "alarm");
697c478bd9Sstevel@tonic-gate 	ms = POP(DS);
707c478bd9Sstevel@tonic-gate 	xt = POP(DS);
717c478bd9Sstevel@tonic-gate 	if (ms == 0) {
72*09e6639bSToomas Soome 		(void) alarm(0);
73*09e6639bSToomas Soome 		(void) signal(SIGALRM, SIG_DFL);
747c478bd9Sstevel@tonic-gate 		alarm_xt = 0;
757c478bd9Sstevel@tonic-gate 		alarm_ms = 0;
767c478bd9Sstevel@tonic-gate 		alarm_env = 0;
777c478bd9Sstevel@tonic-gate 	} else {
78*09e6639bSToomas Soome 		(void) signal(SIGALRM, catch_alarm);
797c478bd9Sstevel@tonic-gate 		alarm_xt = xt;
807c478bd9Sstevel@tonic-gate 		alarm_ms = ms;
817c478bd9Sstevel@tonic-gate 		alarm_env = env;
82*09e6639bSToomas Soome 		(void) alarm((ms + 999)/1000);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #pragma init(_init)
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void
_init(void)897c478bd9Sstevel@tonic-gate _init(void)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	ASSERT(env);
947c478bd9Sstevel@tonic-gate 	NOTICE;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	P1275(0x213, 0,		"alarm",		do_alarm);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	P1275(0x219, 0,		"user-abort",		user_abort);
997c478bd9Sstevel@tonic-gate }
100