From:	MERC::"uunet!CRVAX.SRI.COM!RELAY-INFO-VAX" 25-JUL-1992 11:13:39.72
To:	info-vax@kl.sri.com
CC:	
Subj:	Re: How to deallocate a device ALLOCATE'd by someone else?

In article <01GMR6JQHG4200008B@VAXF.COLORADO.EDU>, DWING@UH01.Colorado.EDU (Dan Wing) writes:
>Roy Nakatsuka, roy@umbra.gsfc.nasa.gov, wrote:
>
>>Does anyone know of a way in VMS to deallocate a device ALLOCATE'd by another
>>user or process?  It seems that no matter what privileges you have or who you
>>are, even SYSTEM is not allowed to do this.
>
>If the PID that has the device allocated is no longer on the system, you've
>encountered a VMS bug; contact your local office for a program that will
>free the device (also available on DSNlink).
>
>If the PID is currently on the system, a REPLY/USER to remind them usually
>works, and STOP/ID will do it, too.

OK, here's a program that's intended to change the ownership of an allocated
device to the master pid of the current job.  I make absolutely no guarantees
about this program, since I didn't write it and haven't tested it extensively. 
If you get it to work, you can run this program, then deallocate the device
from the process with the master pid of your job.  USE AT YOUR OWN RISK!
AND DO NOT USE ANY SIMPLE VARIANT OF THIS PROGRAM TO CHANGE OWNERSHIP OF A
MOUNTED DEVICE!

	.TITLE	TEC
	.IDENT	/V01.0/
;
;  DEFINE NECESSARY SYMBOLS
;
	$DDBDEF
	$UCBDEF
	$FABDEF
	$RABDEF
	$JPIDEF
	$IPLDEF
	$DCDEF
;
;  $GETJPI FIELD DEFINITIONS
;
JPIBLK:	.WORD	4			;BUFFER LENGTH
	.WORD	JPI$_PID		;INFORMATION TO RETURN CODE
	.ADDRESS	USRPID		;ADDRESS OF USRPID BUFFER
	.ADDRESS	USRPIDL		;ADDRESS OF RETURNED LENGTH
	.WORD	4			;BUFFER LENGTH
	.WORD	JPI$_MASTER_PID		;INFORMATION TO RETURN CODE
	.ADDRESS	MSTPID		;ADDRESS OF MSTPID BUFFER
	.ADDRESS	MSTPIDL		;ADDRESS OF RETURNED LENGTH
	.LONG	0			;END OF LIST
USRPID:	.BLKL	1			;PID BUFFER
USRPIDL:.BLKL	1			;LENGTH OF USER PID
MSTPID:	.BLKL	1
MSTPIDL:.BLKL	1
IOSB:	.BLKQ	1			;IO STATUS BLOCK
LPAG:	.LONG	JPIBLK,ENDSTUFF		;INADR FOR $LCKPAG AND $ULKPAG
;
;  LOCK PROGRAM IN MEMORY SO A PAGEFAULT WON'T CRASH THE SYSTEM WHILE
;  EXECUTING IN KERNEL MODE
;
	.ENTRY	TEC,0
	$LCKPAG_S	INADR=LPAG		;LOCK PROGRAM IN MEMORY
;
;  GET CURRENT PID AND MASTER PID
;
	$GETJPI_S	ITMLST=JPIBLK,IOSB=IOSB	;GET USRPID FOR CURRENT PROCESS
	BLBS	R0,24$			;BRANCH IF NO ERROR
	BRW	ERROR			;BRANCH IF ERROR
;
;  GO STUFF IN CORRECT PID
;     
24$:	
	$CMKRNL_S STUFF			;GO STUFF IN CORRECT PID
					;   MUST BE DONE IN EXEC MODE
	BLBC	R0,ERROR		;IF ERROR OCCURRED BRANCH
	MOVL	#RMS$_SUC,R4		;INDICATE SUCCESS
	BRW	FINISH			;GET OUT GRACEFULLY
;
;  ERROR PROCESSING
;
;    ON UNIDENTIFIED ERROR BRANCH HERE AND EXIT WITH STATUS    
;
ERROR:	
	MOVL	R0,R4			;SAVE COMPLETION CODE
FINISH:	$ULKPAG_S	INADR=LPAG	;UNLOCK MEMORY PAGES
	$EXIT_S	R4			;EXIT WITH STATUS
;
;  FIND JIB AND STUFF PROJECT INTO JIB$T_ACCOUNT
;
	.ENTRY	STUFF,0			;ENTRY POINT FOR EXEC ROUTINE
	SETIPL	#IPL$_SYNCH		;LOCK OUT INTERRUPTS
	MOVL	USRPID,R0		;SET UP TO CONVERT EPID TO IPID
	JSB	EXE$EPID_TO_IPID	;CONVERT USRPID
	MOVL	R0,USRPID		;SAVE USER IPID
	MOVL	MSTPID,R0		;SET UP TO CONVERT EPID TO IPID
	JSB	EXE$EPID_TO_IPID	;CONVERT MSTPID
	MOVL	R0,MSTPID		;SAVE MASTER PID
	MOVL	IOC$GL_DEVLIST,R6	;GET THE ADDRESS OF THE FIRST DDB
	BRW 	CHUNIT			;IF EQUAL, GO CHECK UNIT
NEXTDEV:
	CMPL	#0,DDB$L_LINK(R6)	;ANYMORE DEVICES?
	BEQL	ALLDONE			;IF EQL, NO, GET OUT
	MOVL	DDB$L_LINK(R6),R6	;ADDRESS OF NEXT DDB TO R6
CHUNIT:
	MOVL	DDB$L_UCB(R6),R7	;ADDRESS OF FIRST UCB  TO R7
	TSTL	R7
	BEQL	NEXTDEV
NEXTUNIT:
	CMPB	#DC$_TAPE,UCB$B_DEVCLASS(R7)	;IS THIS A TAPE DEVICE
	BEQL	WHOOWN			;YES, GO CHECK IT OUT
	CMPB	#DC$_DISK,UCB$B_DEVCLASS(R7)	;IS THIS A DISK DEVICE
	BNEQ	NEXTDEV			;NO, GO GET NEXT DEVICE
WHOOWN:	CMPL	MSTPID,UCB$L_PID(R7)	;DOES THIS DEVICE BELONG TO MASTER
	BEQL	INSERT			;YES, GO CHANGE OWNER TO USRPID
MOREUNIT:
	CMPL	#0,UCB$L_LINK(R7)	;NO, ANYMORE UCB'S IN THIS CHAIN
	BEQL	NEXTDEV			;IF EQL, NO MORE UCBS, GOTO NEXTDEV
	MOVL	UCB$L_LINK(R7),R7	;GET NEXT UCB IN CHAIN
	BRB	NEXTUNIT		;TRY NEXT UNIT
INSERT:
	MOVL	USRPID,UCB$L_PID(R7)	;PUT IN CORRECT PID
	BRW	MOREUNIT		;GET NEXT UNIT OF THIS DEVICE
ALLDONE:
	MOVL	#RMS$_SUC,R0		;INDICATE SUCCESS
	SETIPL	#0			;RESET IPL TO USER, ALLOW INTERRUPTS
ENDSTUFF:
	RET
	.END	TEC
--------------------------------------------------------------------------------
Carl J Lydick | INTERnet: CARL@SOL1.GPS.CALTECH.EDU | NSI/HEPnet: SOL1::CARL

Disclaimer:  Hey, I understand VAXen and VMS.  That's what I get paid for.  My
understanding of astronomy is purely at the amateur level (or below).  So
unless what I'm saying is directly related to VAX/VMS, don't hold me or my
organization responsible for it.  If it IS related to VAX/VMS, you can try to
hold me responsible for it, but my organization had nothing to do with it.

