Create your own utility to lock what the Allocate Object (ALCOBJ) command does not allow you to lock.
The Allocate Object (ALCOBJ) command is used to acquire one or more lock states on each of a list of objects. The lock states are allocated to the requesting thread, its containing job (i.e., MI process), or a lock space (i.e., an MI transaction control structure (TCS) object). Generally, the ALCOBJ command and its partner, the Deallocate Object (DLCOBJ) command, can achieve tasks similar to the Lock Object (LOCK) and Unlock Object (UNLOCK) MI instructions. But not exactly.
The ALCOBJ command does not allow the user to lock specific object types—for example, the User Profile (*USRPRF) objects (with MI object type code hex 08), and it can't lock internal objects (MI objects that are not visible at the operation system level). ALCOBJ also adds many limitations on what kinds of lock states can be allocated on specific object types. For example, ALCOBJ forbids allocating an *EXCL lock state on a Library (*LIB) object or a Device Description (*DEVD) object. Also, lock states except *EXCL can't be allocated on a Subsystem Description (*SBSD) object via ALCOBJ. These restrictions are also true for the DLCOBJ command.
However, the ability to allocate or deallocate any kind of lock state on any kind of MI object is sometimes necessary for an IBM i developer or administrator. For example, allocating a *SHRRD lock on a *SBSD object can prevent accidental deletion of the *SBSD object. Actually, that's the method used by the system to protect the subsystem descriptions of active subsystems. (Try issuing a Work with Object Lock (WRKOBJLCK) command upon the *SBSD of an active subsystem.)
This TechTip shows you how to create your own utility to lock/unlock what the ALCOBJ/DCLOBJ commands do not allow.
The LCKLCK Program
The example program called LCKLCK implements the tasks mentioned above. The working mechanism of the LCKLCK program is quite simple—issuing the LOCK or the UNLOCK MI instruction on an MI object on behalf of its caller. The LCKLCK program accepts the following parameters:
- A system pointer to the target MI object to lock/unlock
- CHAR(1) requested lock state
- CHAR(1) flag that indicates whether a LOCK or UNLOCK MI instruction is to be issued on the target MI object
The source program (lcklck.rpgle) of LCKLCK is the following:
/** * @file lcklck.rpgle * * Lock/Unlock an MI object idendified by a system pointer. * @param [in] SYSPTR to the MI object * @param [in] Requested lock state * @param [in] Flag. 'U' = Unlock, anything else = Lock */
h dftactgrp(*no) actgrp('LCKLCK')
d lock_request ds qualified d num_requests... d 10i 0 inz(1) d offset_lock_state... d 5i 0 inz(32) d time_out 20u 0 d lock_opt d obj@ * d lock_state d * Lock request option related constants d LOCK_REQUEST_TYPE_IMMED... d c x'0000' d LOCK_REQUEST_TYPE_SYNC... d c x'4000' d LOCK_REQUEST_TYPE_ASYNC... d c x'8000' d LOCK_WAIT_4EVER... d c x'0200' d LOCK_SCOPED_TO_OBJ... d c x'0000' d LOCK_SCOPED_TO_THREAD... d c x'0080' d LOCK_SCOPE_OBJ_MI_PROCESS... d c x'0000' d LOCK_SCOPE_OBJ_TCS... d c x'0040' * The Lock Object (LOCK) MI instruction d lockobj pr extproc('_LOCK') d lock_request... d likeds(lock_request) * The Unlock Object (UNLOCK) MI instruction d unlockobj pr extproc('_UNLOCK') d unlock_request... d likeds(lock_request) * Prototype of the LCKLCK program d i_main pr extpgm('LCKLCK') d object * d lock_state d flag
d i_main pi d object@ * d lock_state d flag
/free lock_request.lock_opt = %bitor( LOCK_REQUEST_TYPE_SYNC : LOCK_WAIT_4EVER : LOCK_SCOPED_TO_OBJ : LOCK_SCOPE_OBJ_MI_PROCESS ); lock_request.obj@ = object@; lock_request.lock_state = lock_state; if flag = 'U'; unlockobj(lock_request); else; lockobj(lock_request); endif;
*inlr = *on; /end-free |
An OMI version of the LCKLCK program (lcklck.emi) is also available as follows:
/** * @file lcklck.emi * * Lock/Unlock an MI object idendified by a system pointer. * @param [in] SYSPTR to the MI object * @param [in] Requested lock state * @param [in] Flag. 'U' = Unlock, anything else = Lock */
dcl spcptr obj@@ parm ; dcl spcptr lck-sts@ parm ; dcl spcptr flag@ parm ; dcl ol pl-main ( obj@@, lck-sts@, flag@ ) parm ext ; entry i-main(pl-main) ext ;
dcl sysptr p-obj@ bas(obj@@) ; dcl dd p-lck-sts char(1) bas(lck-sts@) ; dcl dd flag char(1) bas(flag@) ;
dcl spcptr tmpl@ auto init(tmpl) ; dcl dd tmpl char(64) auto ; dcl dd num-requests bin(4) def(tmpl) pos(1) init(1) ; dcl dd off-lck-sts bin(2) def(tmpl) pos(5) init(32) ; dcl dd lck-opt char(2) def(tmpl) pos(15) init(x'4200') ; /* 01000010,00000000b */ /* Lock request type = '01'. Synchronous request- Wait until all locks can be granted. */ /* Time-out option = '1'. Wait indefinitely */ /* Template extension specified = '0'. No template extension */ /* Lock scope = '0'. Lock is scoped to the lock scope object type. */ /* Lock scope object type = '0'. Process containing the current thread. */ dcl sysptr obj@ def(tmpl) pos(17) ; dcl dd lck-sts char(1) def(tmpl) pos(33) ;
cpybwp obj@, p-obj@ ; cpybla lck-sts, p-lck-sts ; cmpbla(b) flag, 'U' / neq(=+3) ; unlock tmpl@ ; b =+2 ; : lock tmpl@ ; : rtx * ; pend ; |
Finally, to avoid the Object Domain or Hardware Storage Protection Violation (hex 4401) exception at security level 40 or above when locking/unlocking system domain objects, change the compiled LCKLCK program to system state by changing the program state field in the program header of LCKLCK from user state (hex 0001) to system state (hex 0080). At V5R4, the program state field is at offset hex
Experiment with the LCKLCK Program
Now let's do a couple of experiments with the LCKLCK program. As a lock-anything utility, you will surely find more uses of the LCKLCK program. The following tiny ILE C program (oo08t1.c) accepts the name of a library (i.e., a context object) and then calls the LCKLCK program to acquire an *EXCL (i.e., LENR) lock state on the library.
/** * @file oo08t1.c * * Test of the LCKLCK program. */
# include <stdlib.h> # include <string.h>
# pragma linkage(LCKLCK, OS) void LCKLCK(void **obj, char *lock_state, char *flag);
# pragma linkage(_RSLVSP2, builtin) void _RSLVSP2(void**, void*);
int main(int argc, char *argv[]) {
void *ctx = NULL; char lck_sts = 0x9; char flag = 'L'; char rt[34] = {0}; char *lib = NULL; if(argc < 2) { return -1; } lib = argv[1];
memcpy(rt, "\x04\x01", 2); memset(rt + 2, 0x40, 30); memcpy(rt + 2, lib, strlen(lib)); _RSLVSP2(&ctx, rt);
LCKLCK(&ctx, &lck_sts, &flag); return 0; } |
Compile the OO08T1 program and call it to lock the LIBA library in job JOBA. Then try to move an object (say, *DTAQ #
JOBA> CALL OO08T1 LIBA JOBB> MOVOBJ # |
Now the Work with Object Locks display of a WRKOBJLCK #
Opt Job User Lock Status Scope Thread JOBB LJL *SHRUPD WAIT *THREAD 00000966 JOBA LJL *EXCL HELD *JOB |
This tells us that the Move Object (MOVOBJ) command (or more exactly, the Command Processing Program (CPP) of MOVOBJ, QSYS/QLIMVOBJ) requests a *SHRUPD (i.e., LSUP) lock state synchronously before actually moving an object from its containing library to a new library.
Now let's do another experiment to protect a subsystem description object from being deleted by other jobs. The following ILE C program (oo08t2.c) allocates a *SHRRD (i.e., LSRD) lock state on a specified *SBSD.
/** * @file oo08t2.c * * Test of LCKLCK program. */
# include <stdlib.h> # include <string.h>
# pragma linkage(LCKLCK, OS) void LCKLCK(void **obj, char *lock_state, char *flag);
# pragma linkage(_RSLVSP2, builtin) void _RSLVSP2(void**, void*);
int main(int argc, char *argv[]) {
void *spc1909 = NULL; char lck_sts = 0x81; char flag = 'L'; char rt[34] = {0}; char *sbsd = NULL; if(argc < 2) { return -1; } sbsd = argv[1];
memcpy(rt, "\x19\x09", 2); memset(rt + 2, 0x40, 30); memcpy(rt + 2, sbsd, strlen(sbsd)); _RSLVSP2(&spc1909, rt);
LCKLCK(&spc1909, &lck_sts, &flag); return 0; } |
Compile the OO08T2 program and call it to allocate a *SHRRD (LSRD) lock state on a *SBSD (say, DEC) in job JOBA. Then try to delete DEC in job JOBB via a DLTSBSD command.
JOBA> CALL OO08T2 DEC JOBB> DLTSBSD DEC |
Now the Work with Object Locks display of a WRKOBJLCK DEC *SBSD command issued from JOBA might look like the following:
Opt Job User Lock Status Scope Thread JOBB LJL *EXCL WAIT *THREAD 00000966 JOBA LJL *SHRRD HELD *JOB |
This tells us that the DLTSBSD command (the CPP of which is QSYS/QLIDLOB) needs to acquire an *EXCL (i.e., LENR) lock state before destroying the *SBSD. And the *EXCL lock state requested by JOBB will not be granted due to the *SHRRD lock stated allocated on the *SBSD object by JOBA. The DLTSBD command issued from JOBB will fail after a wait time-out interval.
LATEST COMMENTS
MC Press Online