;<134-TENEX>JOBZRO.MAC;34    15-Apr-80 12:26:35    EDIT BY LYNCH
; MADE THIS CODE RESIDENT BECAUSE IF IT IS ON THE DISK WHEN
; IT IS TO BE RUN AND THE DISK ARE SCREWEY WE NEVER FIND OUT.
;<134-TENEX>JOBZRO.MAC;33     2-Apr-80 07:54:43    EDIT BY LYNCH
; CHANGED PRIME TIME TO BE 6AM TO 6PM ON MONDAY-FRIDAY.
;<134-TENEX>JOBZRO.MAC;32    28-Mar-80 21:39:18    EDIT BY PETERS
; Removed BUGTAB from EXTERNAL status
;DSK:<134-TENEX>JOBZRO.MAC;28 13-Feb-80 18:00:39, Edit by FRENCH
;REMOVED EXTERNALS FOR DISK ERROR STUFF, CHKDRE AND CHKDIE REMOVED
;MOVED DISK ERROR TYPE CHECKING TO DSKPAK WHERE IT BELONGS
;<134-TENEX>JOBZRO.MAC;28    13-Oct-79 22:23:36    EDIT BY PETERS
;increase ddmp absolute timeout by two minutes
;<134-TENEX>JOBZRO.MAC;27     1-Oct-79 17:30:54    EDIT BY CLYDE
;removed drum error checking code
;<134-TENEX>JOBZRO.MAC;26    16-Sep-79 18:06:11    EDIT BY PETERS
;<134-TENEX>JOBZRO.MAC;25     8-Sep-78 16:54:30    EDIT BY PETERS
;<134-TENEX>JOBZRO.MAC;24     5-Sep-78 18:28:53    EDIT BY PETERS
;<134-TENEX>JOBZRO.MAC;23     6-Jul-78 19:20:34    EDIT BY PETERS
;<134-TENEX>JOBZRO.MAC;22    28-NOV-77 13:11:34    EDIT BY PETERS
;<134-TENEX>JOBZRO.MAC;21    29-Jun-77 20:51:08    TVEDIT'd by Geoff
; DDUMP (RUNDD5+2) runs on SPQ instead of HIQ. 
;<134-TENEX>JOBZRO.MAC;20    22-Mar-77 19:41:00    EDIT BY LYNCH
; TOOK OUT PDP-15 
;<134-TENEX>JOBZRO.MAC;19    14-SEP-76 20:59:07    EDIT BY GEOFF
; added some lower caseing.
;<134-TENEX>JOBZRO.MAC;16     4-MAY-76 15:39:43    EDIT BY UNTULIS
;ADDED NON PRIME TIME ACCUMULATION CODE
;<134-TENEX>JOBZRO.MAC;15    18-APR-76 11:55:33    EDIT BY UNTULIS
;<134-TENEX>JOBZRO.MAC;13    29-MAR-76 11:20:12    EDIT BY UNTULIS
;ADDED DEFAULT DUE FOR IMPHLT
;<134-TENEX>JOBZRO.MAC;12    25-FEB-76 09:44:11    EDIT BY UNTULIS
;<134-TENEX>JOBZRO.MAC;9    17-FEB-76 12:26:22    EDIT BY UNTULIS
;DELETED CALL TO TTHCK
;<134-TENEX>JOBZRO.MAC;8    12-FEB-76 17:28:17    EDIT BY UNTULIS
;ADDED GETPER CODE TO PRINT DISK AND DRUM ERRORS
;<134-TENEX>JOBZRO.MAC;7    11-FEB-76 15:10:00    EDIT BY UNTULIS
;PUT CHKDRM BACK IN
;<134-TENEX>JOBZRO.MAC;5    11-FEB-76 14:18:27    EDIT BY UNTULIS
;ADDED FIFTEEN EXTERNALS
;<134-TENEX>JOBZRO.MAC;3     9-FEB-76 13:33:13    EDIT BY UNTULIS
;ADDED SRI DRUM AND FIFTEEEN CODE
;<134-TENEX>JOBZRO.MAC;12    17-SEP-75 13:33:29    EDIT BY PLUMMER
; ALLOW FOR HSYST5 TO HAVE USER NUMBER IN LH
;<134-TENEX>JOBZRO.MAC;11    29-AUG-75 12:02:52    EDIT BY OPERATOR
;<134-TENEX>JOBZRO.MAC;10    28-AUG-75 13:34:23    EDIT BY ALLEN
;<134-TENEX>JOBZRO.MAC;7    27-AUG-75 11:57:12    EDIT BY ALLEN
;<134-TENEX>JOBZRO.MAC;6    27-AUG-75 11:55:05    EDIT BY ALLEN
; CORRECT MAINTENANCE OF PIE-SLICE GROUP CPU TIME
;<134-TENEX>JOBZRO.MAC;4    11-AUG-75 18:03:38    EDIT BY CLEMENTS
; Make system reset HSYS if one was pending before reload.
;<134-TENEX>JOBZRO.MAC;3     7-AUG-75 13:50:49    EDIT BY CLEMENTS
; Move CHKDRM error printer out into individual drum drivers
;  so it can print device dependent stuff.
; Change err msg from CHECKDSK to CKECKDISK at RUNDD1
;<134-TENEX>JOBZRO.MAC;2     3-JUN-75 15:53:55    EDIT BY ALLEN
; CHKR RUNS ON HIQ FOR PIE-SLICE SYSTEM
;<134-TENEX>JOBZRO.MAC;1    23-APR-75 13:04:12    EDIT BY CLEMENTS
;SPLITTING SWPMON.MAC INTO PIECES. THIS IS JOBZRO.MAC

	SEARCH PROLOG
	TITLE JOBZRO
;;;	SWAPCD

INTERNAL	RUNDD,HSYS4,CHKR,CVTAD,CHKRI,CHKH1,CHKBG1

EXTERNAL	LOGDES
EXTERNAL	CAPENB,CAPMSK,DISKP
EXTERNAL	ACCIFG,BHC,BLOCKW,BUGCHK,BUGHLT,BUGNTE
EXTERNAL	CHKBT,CHKTIM,CHKTM1,CRSTAD,CTIMS
EXTERNAL	DDMP,DDMPFK,DDTIME,DONSJ,DRMFRE,DRMIN0
EXTERNAL	EDISMS,ENTFLG
EXTERNAL	FACTSW,FORKX,HSYST1,HSYST2,HSYST3,HSYST4,HSYST5
EXTERNAL	IOIP,JB0FLG,JB0TT,JOBPT,JOBRT,MENTR,NXTDMP
EXTERNAL	OTYLIN,PRIMRY,R,SPC1,SPCSTJ,SPTC
EXTERNAL	SVNUM,SYSIFG,SYSRST,TNAMS1,TODCLK,TTFORK,XEFACT
DEFINE TMSG <	PUSHJ P,TMSGQ##>

; START OF JOB ZERO ONCE THE MONITOR INITIALIZATION HAS BEEN DONE,
; I.E. SWAPPABLE CODE IS IN AND DEVICES INITIALIZED.
; FIRE UP CHECKDSK, SYSJOB, THEN RUN CHKR BACKGROUND TASKS

RUNDD::	MOVSI 1,777
	ANDCAM 1,CAPENB		;FLUSH ANY SUPERIOR FORK CAPS
	SKIPN 1,CRSTAD		;DO WE HAVE A SAVED TIME FROM CRASH?
	JRST RUNDD3		;NO
	STAD			;YES, SET IT
	JFCL
RUNDD3:	MOVEI 1,101
	DOBE
	SETO 1,
	HRROI 2,[ASCIZ /
Tenex restarting, wait... /]
	MOVE 3,DBUGSW##		;IF USERS WILL GET A CHANCE AT IT,
	CAIGE 3,2		; ..
	TTMSG			;TELL THEM
	SKIPG DISKP
	JRST RUNDD2		;SKIP DISK STUFF IF NO DISK
	MOVEI 1,RUNDD1		;BREAK TO RUNDD1 IF ANYTHING GOES WRONG
	HRRM 1,MONBK
	CALL CHKBT		;CHECK CONSISTENCY OF DISK BIT TABLE
	IFDEF IMPCHN,<CALL IMPBEG##> ;START IMP ASYNCH FORK
	MOVE 2,DBUGSW
	CAIN 2,2		;IF REAL DEBUGGING,
	JRST RUNDD2		;SKIP DISK CHECKING
RUNCKD:	HRROI 1,[ASCIZ /
Run checkdisk? /]
	PSOUT
	PBIN
	TRZ 1,40
	CAIN 1,"N"
	 JRST RUNDD2
	CAIE 1,"Y"
	 JRST RUNCKD
	MOVSI 1,(1B2+1B17)
	HRROI 2,[ASCIZ /<SYSTEM>CHECKDISK.SAV/]
	GTJFN
	JRST RUNDD1
	PUSH P,1
	MOVSI 1,(1B1)		;XMIT CAPABILITIES
	CFORK			;CREATE A FORK FOR CHECKDSK
	BUG(HLT,<JOB 0 CFORK FAILED>)
	EXCH 1,0(P)
	HRL 1,0(P)
	GET
	MOVE 1,[CHKDSR]		;SAY CHECK DSK RUNNING
	IORM 1,FACTSW
	POP P,1
	SETZ 2,
	SFRKV			;START AT MAIN ENTRY
	WFORK
	KFORK
	MOVE 1,[CHKDSR]		;SAY CHKDSK COMPLETED
	ANDCAM 1,FACTSW
	JRST RUNDD2

RUNDD1:	HRROI 1,[ASCIZ / No checkdisk
/]
	PSOUT

RUNDD2:	MOVEI 1,RUNDD6		;BREAK TO RUNDD6 IF ANY SCREWUP
	HRRM 1,MONBK
	MOVEI 1,101
	DOBE			;WAIT FOR ANY JOB0 OUTPUT
	MOVE 1,[CHKDSE]		;DISK OK?
	TDNE 1,FACTSW
	JRST 	[HRROI 2,TNAMS1
		SETO 1,
		TTMSG
		JRST .+1]
	MOVE 1,JB0TT		;JOB 0 TTY
	CAIE 1,377777		;REAL TTY?
	JRST RUNDD4		;YES
	MOVSI 1,(1B0+1B17)	;NONE, SO OPEN FILE FOR MESSAGES
	HRROI 2,[ASCIZ /<SYSTEM>ERROR.REPORT/]
	GTJFN
	BUG(CHK,<CAN'T GTJFN ERROR REPORT FILE>)
	MOVE 2,[44B5+1B20]
	OPENF
	BUG(CHK,<CAN'T OPENF ERROR REPORT FILE>)
RUNDD4:	HRRM 1,PRIMRY		;USE IT FOR OUTPUT 
	MOVE 1,FORKX
	MOVEM 1,DDMPFK		; RECORD FORKX OF DDMP FORK
	MOVEI 1,[SIXBIT '$RUNNING DDMP$$/']
	TMSG			;ON TTY 0
	MOVSI 1,(740B8)
	MOVEM 1,LOGBUF
	HRROI 1,LOGBUF
	EFACT			;RESTART ENTRY IN FACT FILE
	 JSP 2,XEFACT		;CHECK EFACT FAILURE (ALWAYS SKIPS)
	  SETOM ACCIFG		; IF SUCCESS, ASSUME ACCOUNTING GOOD
	AOS SYSIFG		;MAKE SYSTEM AVAILABLE
	CALL DONSJ		;START NEXT SPECIAL JOB
	MOVSI 1,(1B1)		;XMIT CAPABILITIES
	CFORK			;CREATE A FORK FOR SYSJOB
	BUG(HLT,<JOB 0 CFORK FAILED>)
	PUSH P,1
	MOVSI 1,(1B2+1B17)
	HRROI 2,[ASCIZ /<SYSTEM>SYSJOB.SAV/]
	GTJFN
	JRST [	POP P,1
		KFORK
		MOVEI 1,[SIXBIT '$NO SYSJOB$/']
		TMSG
		JRST RUNDD5]
	HRL 1,0(P)
	GET
	POP P,1
	SETZ 2,
	SFRKV			;START AT MAIN ENTRY, LET RUN FOREVER

RUNDD5:
RUNDD6:
IFDEF CHAOS,<
	CALL CHAINI##
>
	MOVEI 2,CTYLIN
	CALL SPCSTJ		; START UP FIRST AUTO JOB
IFE PIESLC,<
	MOVEI 1,103
	MOVEM 1,JOBBIT		;SET PRIORITY FOR CHKR
> ; END NON-PIE-SLICE CONDITIONAL
IFN PIESLC,<CALL SETspq##>	; [SRI] 
	MOVEI 1,CHKRI
	HRRM 1,MONBK		;SET DISPATCH FOR INTERRUPTS
	MOVE 1,FORKX		;SAVE FORK INDEX FOR DEBUGGING
	MOVEM 1,CHKFRK		; ..
	JRST CHKR		;GO DO BACKGROUND

LS CHKFRK,1			;FORKX OF THE DDMP FORK

;FORK STARTED BY CHKR AT ARMAGEDDON TIME.
;COMMENCE SHUTDOWN. PREVENT NEW JOBS, LOGOUT EXISTING JOBS, HALT.

HSYS4:	MOVSI 1,UMODF		;FAKE UP SLOW JSYS ENTRY
	MOVEM 1,FPC		; ..
	JSYS MENTR		; ..
	IFDEF IMPCHN,<
	MOVE 2,HSYST4
	MOVE 1,HSYST5
	ANDI 1,17		;EXTRACT GOING DOWN REASON
	SKIPN	1		;SKIP IF DUE IS SET
	MOVEI	1,7		;IF NOT SET, ASSUME SOFTWARE
	CALL IMPHLT##>
	MOVEI 6,377777
	MOVEM 6,LOGDES	;TURN OFF LOGOUT JOB MESSAGES
	MOVE 6,[XWD -NJOBS+1,1]	;START WITH JOB 1
	MOVEI 5,^D60000		; INITIAL TIME-OUT INTERVAL
	SKIPGE JOBRT(6)		;LOOK FOR EXISTING JOB
HSYS41:	AOBJN 6,.-1
	JUMPGE 6,HSYS7		;JUMP IF DONE
	HLRZ 1,JOBPT(6)		;WHAT TTY IS THE JOB ON?
	CAME 1,OTYLIN		;ONE OF THE PRIVILEGED ONES?
	CAIN 1,CTYLIN		; ..
	JRST HSYS41		;YES. LET IT STAY ON
	CAMN 1,ENTFLG		;SHOULD WE LET THIS ONE CONTINUE
	SKIPG ENTFLG		;IS TTY# .GT. 0
	CAIA			;NO
	JRST HSYS41		;YES, LET THIS ONE CONTINUE
	MOVEI 1,(6)		;NO, LOG OUT THIS JOB NUMBER
	LGOUT			; ..
	  JFCL
	ADDI 5,^D10000		; AUGMENT TIME-OUT BY 10 SEC PER JOB
	JRST HSYS41

HSYS7:	ADD 5,TODCLK		; TIME TO STOP WAITING FOR LOGOUTS
HSYS8:	CAMG 5,TODCLK		;WAITED MAX TIME?
	JRST HSYS9		;YES, PROCEED WITH SHUTDOWN
	MOVE 6,[XWD -NJOBS+1,1]	;START WITH JOB 1
	SKIPGE JOBRT(6)		;SEE IF ANY JOBS STILL EXIST
HSYS81:	AOBJN 6,.-1
	JUMPGE 6,HSYS9		;NONE FOUND, PROCEED
	HLRZ 1,JOBPT(6)		;TTY NUMBER OF THIS JOB
	CAME 1,OTYLIN		;PRIVILEGED?
	CAIN 1,CTYLIN		; ..
	JRST HSYS81		;YES, IGNORE
	CAMN 1,ENTFLG		;SHOULD WE LET THIS ONE CONTINUE
	SKIPG ENTFLG		;IS TTY# .GT. 0
	CAIA			;NO
	JRST HSYS81		;YES, LET THIS ONE CONTINUE
	MOVEI 1,^D1000		;NO, WAIT 1 SEC., THEN CHECK AGAIN
	DISMS
	JRST HSYS8

HSYS9:	SETZM NXTDMP		;INITIATE DDMP
	SETZM DDTIME
	AOS JB0FLG
	MOVEI 5,^D30		;MAX TIME FOR JOB 0
HSYS91:	MOVEI 1,^D1000		;WAIT 1 SEC
	DISMS
	SKIPL NXTDMP
	SOJG 5,HSYS91		;NO, WAIT MORE
	JUMPLE 5,HSYS9		;IF TIMED OUT, TRY AGAIN
	SKIPE IOIP		;WRITES IN PROGRESS?
	JRST .-1		;YES, WAIT FOR COMPLETION
	MOVEI 1,CTYLIN		;ANNOUNCE SHUTDOWN.
	HRROI 2,[ASCIZ /Shutdown complete.
/]
	TTMSG
	MOVEI 1,^D15000		;FINALLY FOR TTY OUTPUT TO QUIT,
	DISMS			; WAIT JUST A BIT MORE
	MOVE 1,OTYLIN		;NOW HALT OR HALTF DEPENDING ON
	MOVEI 2,CTYLIN
	SKIPGE TTFORK(1)	;WHETHER A JOB IS STILL ON CTY/OTY
	SKIPL TTFORK(2)		; ..
	HALTF			;SOMEONE STILL ON. KEEP RUNNING.
	JRST [	PIOFF 610000	;CLEAR EVERYTHING
		CONO APR,200000	;IOB RESET
		SETOM ENTFLG	; IN CASE CONTINUED
		JRST 4,SYSRST]

;ROUTINE TOSEND GOING DOWN MESSAGE TO ALL LINES

THSYS:	PUSH P,1		;1 CONTAINS NUMBER OF MINUTES
	SETO 1,
	HRROI 2,[SYSNAM(<ASCIZ /
*** >,< going down in />)]
	TTMSG
	MOVE 2,0(P)		;GET NUMBER OF MINUTES AGAIN
	CAIN 2,1		;THE 'ONE' CASE?
	JRST [	HRROI 2,[ASCIZ /one minute!! ***
/]
		JRST HSYS51]	;YES, SPECIAL MESSAGE
	MOVEI 1,1(P)		;USE STACK TO HOLD STRING
	ADD P,BHC+2
	HRLI 1,440700
	PUSH P,1
	MOVEI 3,^D10
	NOUT
	JFCL
	POP P,2			;POINTER TO STRING
	SETO 1,
	TTMSG			;PRINT NUMBER
	SUB P,BHC+2		;FLUSH TEMPS FROM STACK
	HRROI 2,[ASCIZ / minutes. ***
/]
HSYS51:	SETO 1,
	TTMSG
	POP P,1			;RETURN NUMBER OF MINUTES
	RET

;PERIODIC (10 SEC) CHECK OF THINGS

CHKR:	SETZM JB0FLG		;CLEAR REQUEST FLAG
	MOVE 1,TODCLK
	ADDI 1,^D120000		; IN 2 MINUTES GET HI-Q
	MOVEM 1,CHKTM1
	ADDI 1,^D240000		; 4 AFTER THAT CALL US HUNG
	MOVEM 1,CHKTIM		;LEAVE FOR SCHED TO CHECK
	SKIPE BUGHLT		;BUGHLTS LATELY?
	CALL CHKHLT		;REPORT
	SKIPE BUGCHK		;BUGCHK?
	CALL CHKBUG		;YES
	SKIPE BUGNTE		;ANY BUGNTES?
	CALL CHKNTE		;YES
	CALL CHKDMS		;CHECK DRUM SPACE
	CALL CHKSPT		;CHECK SPT SPACE
	CALL CHKDSK##		;LOG DISK ERRORS IF ANY
	CALL RESLCK##		;LOCK OR UNLOCK RESIDENT FREE SPACE
IFDEF IMPCHN,<
	CALL CHKNET##		;YES, LOG IT
>
	CALL DDMP		;DISK BACKUP
	SKIPN HSYSIN		;HAS <SYSTEM>HSYS.TIMES BEEN CHECKED?
	CALL CHSYSI		;NO, GO DO IT NOW.
	SKIPE HSYST1		;SYSTEM SHUTTING DOWN?
	CALL CHKHSY		;YES. CHECK HSYS TIMERS.
	call tymjb0##		;go do tymnet checks

IFN PIESLC,<
	CALL CHKPIE		;CHECK IF PIE-SLICE CPU TIMES NEED UPDATE
> ; END PIE-SLICE SCHED CONDITIONAL

	MOVE 2,TODCLK		;SETUP DISMISS CONDITION FOR
	ANDI 2,377777		;NOW PLUS 30 SEC
	MOVSI 1,^D30000(2)
	HRRI 1,JB0TST		;OR JB0FLG
	JSYS EDISMS
	JRST CHKR

;;;	RESCD

JB0TST:	MOVE 3,4		;SAVE RETURN
	JSP 4,BLOCKW		;TEST CLOCK
	SKIPE JB0FLG		;OR FLAG
	JRST 1(3)		;RUN
	JRST 0(3)

;;;	SWAPCD

CHKH1:	MOVEI 1,101
	MOVEI 3,^D8
	NOUT
	JFCL
	MOVEI 2," "
	BOUT
CHKR1::	GTAD			;CURRENT TIME AND DATE
	JUMPL 1,CHKR2		;IF NOT SET
	MOVE 2,1
	MOVEI 1,101
	SETZ 3,
	ODTIM			;INCLUDE TAD WITH MESSAGE
CHKR2:	HRROI 1,[ASCIZ /
/]
	PSOUT
	RET

CHKHLT:	HRROI 1,[ASCIZ /
*****BUGHLT AT /]
	PSOUT
	HRRZ 2,BUGHLT
	SETZM BUGHLT
	SOJA	2,CHKBG1

CHKNTE:	HRROI 1,[ASCIZ /
***** BUGNOTE AT /]
	PSOUT
	HRRZ 2,BUGNTE
	SETZM BUGNTE
	SOJA 2,CHKBG1

CHKBUG:	HRROI 1,[ASCIZ /
*****BUGCHK AT /]
	PSOUT
	HRRZ 2,BUGCHK
	SUBI 2,1		;TO POINT AT JSR
	SETZM BUGCHK
CHKBG1:	PUSH P,2		;SAVE ADDRESS
	CALL CHKH1		;PRINT IT
	SKIPE NBUGTP		;HAVE BUG TABLE NOW?
	JRST BUGLK5		;YES
	MOVE 1,SVNUM		; VERSION NUMBER OF THIS SYSTEM
	HRLI 1,(1B2+1B17)	; OLD FILE & SHORT FORM STRING
	HRROI 2,[SYSNAM(<ASCIZ \<SYSTEM>BUGTABLE.>,<\>)]
	GTJFN
	JRST BUGLK0
	MOVE 2,[44B5+1B19]
	OPENF
	JRST BUGLK0
	BIN			;FIRST WORD IS (NEG) COUNT OF PTRS
	MOVEM 2,NBUGTP
	MOVE 7,[XWD -SBUGTF,BUGTAB]
BUGLK3:	BIN
	JUMPN 2,BUGLK2
	GTSTS
	TLNE 2,(1B8)		;EOF?
	JRST BUGLK4		;YES
	SETZ 2,
BUGLK2:	MOVEM 2,0(7)
	AOBJN 7,BUGLK3
BUGLK4:	CLOSF
	JFCL
BUGLK5:	POP P,1			;BUG ADDRESS
	HRLZ 7,NBUGTP
BUGLK1:	MOVE 6,BUGTAB(7)
	HLRZ 2,6
	CAME 1,2
	AOBJN 7,BUGLK1
	JUMPGE 7,R		;FAILED TO FIND ADDRESS, PRINT NOTHING
	HRROI 1,[ASCIZ / - /]
	PSOUT
	HRROI 1,BUGTAB(6)	;STRING STORAGE RELATIVE TO START OF TABLE
	PSOUT
	JRST CHKR2		;PRINT EOL AND RETURN

BUGLK0:	HRROI 1,[ASCIZ @ - (FAILED TO GTJFN/OPEN BUGTABLE FILE)
@]
	PSOUT
	POP P,1
	RET

LS NBUGTP,1		;NEG COUNT OF PTRS IN BUGTABLE

;CHECK IF SYSTEM SHUTDOWN MESSAGE NEEDS TO BE TYPED.
;CALLED ONLY FROM JOB 0, CHKR ROUTINE.

CHKHSY:	GTAD			; GET NOW
	CALL CVTAD		; LINEARIZE
	SKIPE HSYST3		; ANY MSGS TYPED YET?
	JRST CHKHS2		;YES.
	MOVE 2,HSYST2		; GET SHUTDOWN TIME
	CAIG 2,^D<2*60*60>(1)	; MORE THAN TWO HOURS AWAY?
	 SKIPA 2,1		; NO, USE NOW
	  SUBI 2,^D<60*60>	; YES, USE 1 HOUR BEFORE
	MOVEM 2,HSYST3		; ELSE USE NOW
CHKHS2:	CAML 1,HSYST2		; SHUT DOWN TIME YET?
	 JRST CHKHS4		; YES, GO DO IT
	CAMGE 1,HSYST3		;UP TO NOTIFICATION TIME?
	JRST CHKHS3		;NO. DO NOTHING.
	SUB 1,HSYST2		;YES. COMPUTE TIME TILL SHUTDOWN.
	MOVNS 1
	ADDI 1,^D30		; ROUND TO NEAREST MINUTE
	IDIVI 1,^D60
	CALL THSYS		;NOT TIME YET. SAY HOW LONG TO ALL TTY'S
	MOVE 3,[POINT 9,CTIMS]	;FIND WHEN NEXT NOTIFY SHOULD BE
	ILDB 2,3		;LOOK THRU LIST OF TIMES
	CAMG 1,2		;SHORTER TIME?
	JUMPG 2,.-2		;NO. LOOK ON. (JUMPG IN CASE IMPOS ERR)
	IMULI 2,^D60		;BACK TO SECONDS
	SUB 2,HSYST2		; ..
	MOVNM 2,HSYST3		;STORE FOR NEXT TYPEOUT TEST
CHKHS3:	RET			;DONE FOR THIS TIME.

CHKHS4:	SKIPG ENTFLG		; SKIP IF SPECIFIC TTY TO STAY ACTIVE
	SETZM ENTFLG		;THE TIME HAS COME. START SHUTDOWN.
	SETZM HSYST1		;CLEAR FLAGS IN CASE RESTARTED,
	SETZM HSYST2		; AND TO PREVENT MULTIPLE HSYS4 FORKS
	SETZM HSYST3		; ..
	MOVSI 1,(1B1)		;CREATE A FORK WITH CAPABILITIES
	CFORK			; ..
	  BUG (HLT,<JOB 0 CFORK FAILED - HSYS>)	;OH WELL, TIME TO QUIT..
	MOVEI 2,HSYS4		;WHERE TO START INFERIOR, EXEC MODE.
	MSFRK			;START IT.
	RET			;AND THATS ALL CHKR DOES ABOUT IT.

; CONVERT GTAD FORMAT TO LINEAR TIME AND DATE

CVTAD:	PUSH P,1
	HRRZS 0(P)
	HLRZS 1
	IMULI 1,^D<60*60*24>
	ADD 1,0(P)
	SUB P,BHC+1
	RET

;Routine to get back HSYS times from before system restart

LS HSYSIN,1		;Flag nonzero when this has been done already

CHSYSI:	GTAD			;HAS SYSTEM GOT A TAD YET?
	JUMPL 1,R		;NO IF NEG. DO IT LATER.
	MOVE 4,1		;HOLD THE CURRENT TIME
	SETOM HSYSIN		;FLAG DOING THIS NOW, DO ONLY ONCE.
	MOVSI A,100001		;SEE IF FILE EXISTS
	HRROI B,[ASCIZ /<SYSTEM>HSYS.TIMES;1/]
	GTJFN
	  RET			;NO FILE. ALL THROUGH.
	MOVE C,A		;SAVE THE JFN
	MOVE B,[440000,,200000]	;OPEN FOR READ
	OPENF
	  JRST [MOVE A,C
		RLJFN
		  JFCL
		RET]
	BIN			;GET SHUTDOWN TIME
	PUSH P,B
	BIN			;GET TIME BACK UP
	PUSH P,B
	BIN			;AND REASON
	MOVE C,B		;TO RIGHT AC'S
	CLOSF
	  JFCL
	POP P,B
	POP P,A
	CAMLE 1,4		;PAST THIS TAD ALREADY?
	HSYS			;NO, SET THE SHUTDOWN
	  JFCL
	RET



CHKDMS:	MOVE 1,DRMFRE
	CAMLE 1,DRMIN0		;DRUM SPACE LOW?
	RET			;NO
	HRROI 1,[ASCIZ /
*****DRUM SPACE LOW, /]
	PSOUT
	MOVE 2,DRMFRE
	JRST CHKH1		;PRINT FREE COUNT

CHKSPT:	MOVE 1,SPTC
	CAMGE 1,SPC1		;SPT LOW?
	RET			;NO
	HRROI 1,[ASCIZ /
*****SPT SPACE LOW, /]
	PSOUT
	MOVN 2,[NOFN-SSPT]
	SUB 2,SPTC		;SPACE LEFT
	JRST CHKH1

;ANY INTERRUPT WILL EVENTUALLY TRAP TO HERE

CHKRI:	MOVE P,UPP
	ADD P,BHC+2		;RESET STACK
	SETZM SLOWF
	SETOM INTDF
	SETZM PSIBW
	HRROI 1,[ASCIZ /
*****JOB 0 CRASH, CONTINUING
/]
	PSOUT
	JRST CHKR

IFDEF DRMCHN,<
LS CHKDMC,1
>

IFN PIESLC,<
; ROUTINE TO PERIODICALLY UPDATE PIE-SLICE GROUP CPU TIMES
NGS PRIMET		;=0 NOW IS PRIME TIME,NEGATIVE SAYS NON-PRIME TIME
CHKPIE:	MOVE 1,TODCLK		;TIME TO DO CHECK?
	CAMGE 1,PIETIM
	 RET			;NOT TIME YET
	ADD 1,[^D<15*60*1000>]	;YES, DO IT AGAIN 15 MINUTES FROM NOW
	MOVEM 1,PIETIM
	GTAD
	JUMPL A,[	SETZM PIETIM	; KEEP CALLING THIS ROUTINE UNTIL TIME IS SET.
			RET]
	SETO B,
	SETZ D,
	ODCNV
	HRRZ C,C
	SETO A,		; SET FLAG FOR NON PRIME TIME
	CAIL C,5	; IS IT A WEEKEND?
	 JRST CHKPI1	; YES
	HRRZ D,D
	CAIL D,^D6*^D3600
	 CAIL D,^D18*^D3600
	  CAIA
	SETZ A,		; IT IS PRIME TIME
CHKPI1:	MOVEM A,PRIMET	; SET FLAG 

	MOVSI 1,-NJOBS		;PREPARE TO LOOP THRU ALL JOBS
	SKIPL JOBRT##(1)	;DON'T CALL IF JOB NOT THERE
	 CALL UPDPIE
	AOBJN 1,.-2
	RET

;ROUTINE TO UPDATE CPUSED FOR GROUP OF WHICH JOB IN AC1 IS A MEMBER
UPDPIE:: SKIPN PIEFLG##		;PIE-SLICE DATA FILE MAPPED?
	 RET			;NO, FORGET IT
	NOINT
	LOCK GRPLOK##


	SKIP JOBORT(1)		;TOUCH PRIOR TO GOING NOSKED
	NOSKED
	MOVE 3,JOBRT(1)		;GET JOB'S CURRENT RUNTIME
	JUMPL 3,[OKSKED		;JOB NOT THERE, RETURN
		JRST UPDPI1]
	SUBM 3,JOBORT(1)	;COMPUTE TIME SINCE LAST UPDATE
	EXCH 3,JOBORT(1)
	MOVE 2,PIEGRP##(1)	;AND GET HIS GROUP
	OKSKED

	JUMPGE 3,UPDPI2		;MAKE SURE NO SCREW-UP
	BUG (NTE,<UPDPIE: NEGATIVE INCREMENTAL RUNTIME>)
	JRST UPDPI1


UPDPI2:	IMULI 2,NWDGRP		;GET OFFSET INTO DATA FILE
	SKIPE PRIMET	;IF WE ARE IN PRIME TIME, FORGET OFFSET
	HRRZI 2,4(2)	;POINT TO NON-PRIME TIME SLOT. 4 SHOULD BE
			; <CPUNPT-CPUSED>
	ADDM 3,CPUSED##(2)	;AND ACCUMULATE

UPDPI1:	UNLOCK GRPLOK
	OKINT
	RET

;STORAGE FOR ABOVE ROUTINES
NGS JOBORT,NJOBS
NGS PIETIM,1

> ;END OF PIE-SLICE SCHED CONDITIONAL

	END ; OF JOBZRO.MAC

