Пример #1
0
/*{
** Name: CS_map_sys_segment()	- Map the system control segment to this process
**
** Description:
**	Maps the system control block to this process.  This must happen before
**	any other action can be taken on shared memory (with the exception of
**	CS_create_sys_segment()).
**
**	Upon successful exectution of this routine CS may manipulate the system
**	control data structure CS_SMCNTRL (taking care to use proper semaphore
**	techniques which take into account that the data structure is shared
**	across processes).
**
**	This call is internal to CS is meant only to be called by CS, and may
**	only exist on unix systems supporting shared memory.
**
** Inputs:
**	none.
**
** Outputs:
**      cssm_segment			ptr to the CS_SMCNTRL data structure.
**      err_code			system dependent error information.
**
**	Returns:
**	    E_DB_OK
**
**	Exceptions:
**	    none
**
** Side Effects:
**	    none
**
** History:
**      08-Sep-88 (mmm)
**          First Version
**	12-jun-89 (rogerk)
**	    Added allocated_pages argument to MEget_pages calls.
**	    Changed shared memory key from a LOCATIONN pointer to a character
**	    string.
**      26-aug-89 (rexl)
**          Added calls to protect page allocator.
**	18-oct-1993 (bryanp)
**	    Issue trace messages in failure cases. Simply returning FAIL doesnt
**		help the diagnosis process very much, while the trace messages
**		can help (if the caller has done a TRset_file by this point).
**	14-Oct-1998 (jenjo02)
**	    CS_map_sys_segment(): Don't initialize css_spinlock. That was done 
**	    when the shared segment was created. Reinitializing it may 
**	    effectively destroy a holding process's lock!
**	21-May-2004 (wanfr01)
**	    INGSRV2835, Bug 112371
**	    As per the Jon Jensen's last update, removed the invalid initialize
**	    of css_spinlock.
**	5-Apr-2006 (kschendel)
**	    Use generated css version instead of constant.
** 12-aug-2008 (stephenb)
**		previous change is too strict if programs not shipped with Ingres
**		try to connect to the system segment because the checked version
**		goes right down to the build number. allow some latitude using
**		II_SYSSEG_VERS. If this is set to the current major Ingres 
**		version we will allow the connection. This is an interim 
**		solution to Golden Gate's issue.
*/
STATUS
CS_map_sys_segment(CL_ERR_DESC *err_code)
{
    STATUS	status = OK;
    PTR		address;
    SIZE_TYPE	alloc_pages;

    /* map segment into first available address and then initialize it */
    /* Use "sysseg.mem" as shared memory key. */
    address = 0;
    status = MEget_pages(ME_SSHARED_MASK, 0, "sysseg.mem", &address,
                         &alloc_pages, err_code);

    if (status)
    {
#ifdef xDEBUG
	TRdisplay("CS_map_sys_segment: unable to attach to sysseg.mem (%x)\n",
			status);
#endif
	/* Unable to attach allocated shared memory segment. */
	/* status = FAIL; This isn't a useful error return code! */
    }
    else
    {
    	char	*segvers;
    	i4		segint;
    	
    	Cs_sm_cb = (CS_SMCNTRL *) address;
    	/* 
    	** check if segment version has been provided, if so we'll
    	** deem it O.K. if it matches the current major version
    	*/
    	NMgtAt("II_SYSSEG_VERS", &segvers);
    	if (segvers != (char *)NULL && *segvers != EOS && 
    			CVal(segvers, &segint) == OK && segint == GV_MAJOR)
    		status = OK;
    	else
    	{
    		i4 id = make_css_version();
	
			if (Cs_sm_cb->css_version != id)
			{
			    TRdisplay(
		"CS_map_sys_segment: sysseg.mem is version %x (%d). We need version %x (%d)\n",
				Cs_sm_cb->css_version, Cs_sm_cb->css_version,
				id, id);
			    SETCLERR(err_code, 0, 0);
			    status = FAIL;
			}
    	}
    }

    return(status);
}
Пример #2
0
/*{
** Name: CS_map_serv_segment() - Map the server segment.
**
** Description:
**      Maps the server control block to this process.
**
**      Upon successful exectution of this routine the process may access
**      memory shared among server and slave processes.
**
**      This call is internal to CS is meant only to be called by CS, and may
**      only exist on unix systems supporting shared memory.
**
** Inputs:
**      serv_seg_num                    id of server segment.
**
** Outputs:
**      address                         on success, set to point to segment
**      err_code                        system dependent error information.
**
**      Returns:
**          E_DB_OK
**
**      Exceptions:
**          none
**
** Side Effects:
**          none
**
** History:
**      08-sep-88 (mmm)
**          First Version
**      12-jun-89 (rogerk)
**          Added allocated_pages argument to MEget_pages calls.
**          Changed shared memory key to a character string rather than a
**          LOCATION pointer.
**      26-aug-89 (rexl)
**          Added calls to protect page allocator.
*/
STATUS
CS_map_serv_segment(u_i4 serv_seg_num, PTR *address, CL_ERR_DESC *err_code)
{
    STATUS      status = OK;
    char        segname[48];
    SIZE_TYPE   alloc_pages;

    if (!address)
    {
        SETCLERR(err_code, 0, 0);
        status = FAIL;
    }
    else
    {
# ifdef SERV_MAP_ADDR
        *address = (PTR) SERV_MAP_ADDR;
# else
        *address = (PTR) 0;
# endif /* SERV_MAP_ADDR */

        Cs_svinfo = &Cs_sm_cb->css_servinfo[serv_seg_num];
        STcopy("server.", segname);
        CVna((i4)serv_seg_num, segname+STlength(segname));

# ifdef SERV_MAP_ADDR
        status = MEget_pages(ME_SSHARED_MASK|ME_ADDR_SPEC, 0, segname,
                             address, &alloc_pages, err_code);
# else
        status = MEget_pages(ME_SSHARED_MASK, 0, segname, address,
                             &alloc_pages, err_code);
# endif /* SERV_MAP_ADDR */

        if (status)
        {
            /* Unable to attach allocated shared memory segment. */
            status = FAIL;
        }
    }

    return(status);
}
Пример #3
0
/*{
** Name: CS_map_sys_segment()   - Map the system control segment to this process
**
** Description:
**      Maps the system control block to this process.  This must happen before
**      any other action can be taken on shared memory (with the exception of
**      CS_create_sys_segment()).
**
**      Upon successful exectution of this routine CS may manipulate the system
**      control data structure CS_SMCNTRL (taking care to use proper semaphore
**      techniques which take into account that the data structure is shared
**      across processes).
**
**      This call is internal to CS is meant only to be called by CS, and may
**      only exist on unix systems supporting shared memory.
**
** Inputs:
**      none.
**
** Outputs:
**      cssm_segment                    ptr to the CS_SMCNTRL data structure.
**      err_code                        system dependent error information.
**
**      Returns:
**          E_DB_OK
**
**      Exceptions:
**          none
**
** Side Effects:
**          none
**
** History:
**      08-Sep-88 (mmm)
**          First Version
**      12-jun-89 (rogerk)
**          Added allocated_pages argument to MEget_pages calls.
**          Changed shared memory key from a LOCATIONN pointer to a character
**          string.
**      26-aug-89 (rexl)
**          Added calls to protect page allocator.
**      18-oct-1993 (bryanp)
**          Issue trace messages in failure cases. Simply returning FAIL doesnt
**              help the diagnosis process very much, while the trace messages
**              can help (if the caller has done a TRset_file by this point).
**      14-Oct-1998 (jenjo02)
**          CS_map_sys_segment(): Don't initialize css_spinlock. That was done
**          when the shared segment was created. Reinitializing it may
**          effectively destroy a holding process's lock!
**      21-May-2004 (wanfr01)
**          INGSRV2835, Bug 112371
**          As per the Jon Jensen's last update, removed the invalid initialize
**          of css_spinlock.
**      5-Apr-2006 (kschendel)
**          Use generated css version instead of constant.
*/
STATUS
CS_map_sys_segment(CL_ERR_DESC *err_code)
{
    STATUS      status = OK;
    PTR         address;
    SIZE_TYPE   alloc_pages;

    /* map segment into first available address and then initialize it */
    /* Use "sysseg.mem" as shared memory key. */
    address = 0;
    status = MEget_pages(ME_SSHARED_MASK, 0, "sysseg.mem", &address,
                         &alloc_pages, err_code);

    if (status)
    {
#ifdef xDEBUG
        TRdisplay("CS_map_sys_segment: unable to attach to sysseg.mem (%x)\n",
                        status);
#endif
        /* Unable to attach allocated shared memory segment. */
        /* status = FAIL; This isn't a useful error return code! */
    }
    else
    {
        i4 id = make_css_version();

        Cs_sm_cb = (CS_SMCNTRL *) address;

        if (Cs_sm_cb->css_version != id)
        {
            TRdisplay(
"CS_map_sys_segment: sysseg.mem is version %x (%d). We need version %x (%d)\n",
                Cs_sm_cb->css_version, Cs_sm_cb->css_version,
                id, id);
            SETCLERR(err_code, 0, 0);
            status = FAIL;
        }
    }

    return(status);
}
Пример #4
0
/*{
** Name: CS_alloc_serv_segment()        - Create shared memory for a server
**
** Description:
**      Allocate a shared memory segment of "size" bytes.  All data placed
**      in this segment should be position independent.
**
**      Upon success this function will create the shared memory segment with
**      the given id and size.  To actually access the shared memory segment
**      the user must make a CS_map_server_segment() call.  It is expected
**      that this single request will fulfill all shared memory needs particular
**      to a single server.  It may be difficult or impossible to extend the
**      shared memory resources of some machines to provide for more than 3
**      different shared memory segments mapped to a single process (the
**      control segment, the locking/logging segment, and the server segment).
**
**      It is expected that CS will call CS_alloc_serv_segment() and
**      CS_map_serv_segment() once at server initialization.
**      Slaves will only call the map function.
**
**      This call is internal to CS is meant only to be called by CS, and may
**      only exist on unix systems supporting shared memory.
**
** Inputs:
**      size                            size in bytes of shared mem segment.
**
** Outputs:
**      id                              on success set to id of segment.
**      err_code                        system dependent error code.
**
**      Returns:
**          E_DB_OK
**
**      Exceptions:
**          none
**
** Side Effects:
**          none
**
** History:
**      08-sep-88 (mmm)
**          First Version
**      12-jun-89 (rogerk)
**          Changed arguments to MEget_pages, MEdetach calls.  They now use
**          character name keys rather than a LOCATION pointer.  Changed
**          ME_loctokey call to ME_getkey.
**      26-aug-89 (rexl)
**          Added calls to protect page allocator.
**      3-jul-1992 (bryanp)
**          Explicitly ACLR the TAS objects when allocating server segment.
**      07-nov-1996 (canor01)
**          Explicitly initialize the csi_spinlock object.
*/
STATUS
CS_alloc_serv_segment(SIZE_TYPE size, u_i4 *server_seg_num,
	PTR *address, CL_ERR_DESC *err_code)
{
    STATUS              status = OK;
    CS_SERV_INFO        *serv_info;
    PTR                 dummy;
    i4                  i;
    SIZE_TYPE           alloc_pages;
    char                segname[48];

    status = CS_find_server_slot(&i);

    serv_info = Cs_sm_cb->css_servinfo;

    if (!address)
    {
        address = &dummy;
    }

    if (status)
    {
        SETCLERR(err_code, 0, 0);    /* since find_server_slot doesn't set */
    }
    else
    {
        /* now allocate and initialize the server segment */

        STcopy("server.", segname);
        CVna(i, segname+STlength(segname));

        size = (size + ME_MPAGESIZE - 1) & ~ (ME_MPAGESIZE - 1);
# ifdef SERV_MAP_ADDR
        *address = (PTR) SERV_MAP_ADDR;
        status = MEget_pages(ME_MZERO_MASK|ME_SSHARED_MASK|ME_CREATE_MASK|
                        ME_ADDR_SPEC, size/ME_MPAGESIZE, segname, address,
                        &alloc_pages, err_code);
# else
        status = MEget_pages(ME_MZERO_MASK|ME_SSHARED_MASK|ME_CREATE_MASK,
                             size/ME_MPAGESIZE, segname, address,
                             &alloc_pages, err_code);
# endif

        if (status)
        {
            /* Unable to create lg/lk shared memory segment */
            status = FAIL;
        }
        else
        {
            /* initialize it's description in the master control block */

            Cs_svinfo->csi_serv_desc.cssm_size = size;
            Cs_svinfo->csi_serv_desc.cssm_id = ME_getkey(segname);

            *server_seg_num = i;

            /*
            ** Explicitly clear these TAS objects, for machines such as the HP
            ** where MEfill'ing the memory with zeros does not initialize the
            ** TAS objects to "clear" state.
            */
            CS_ACLR(&Cs_svinfo->csi_nullev);
            CS_ACLR(&Cs_svinfo->csi_events_outstanding);
            CS_ACLR(&Cs_svinfo->csi_wakeme);
            CS_SPININIT(&Cs_svinfo->csi_spinlock);
            for (i = 0;
                i < (sizeof(Cs_svinfo->csi_subwake) /
                        sizeof(Cs_svinfo->csi_subwake[0]));
                i++)
            {
                CS_ACLR(&Cs_svinfo->csi_subwake[i]);
            }

            if (address == &dummy)
                MEdetach(segname, address, err_code); /* and free pages */

            /* FIX ME - bunch of other stuff */
        }
    }

    return(status);
}
Пример #5
0
/*{
** Name: CS_create_sys_segment()        - Create system shared mem segment.
**
** Description:
**      Create system control block.  Only caller should be the system
**      initialization program.  Other users of the system shared memory
**      segment should only map an already created shared memory segment.
**
**      This procedure creates a shared memory segment.  It then maps it into
**      the current process and initializes it.  It then unmaps it from the
**      current process (so that the CS_map_sys_segment() can be used without
**      any special casing).
**
**      Currently the already created shared memory segment is destroyed
**      before the new one is created.  We may add a destroy procedure
**      once the system installation program is fleshed out.
**
** Inputs:
**      num_of_servers                  number of servers this installation
**                                      should support.
**      num_of_wakeups                  number of wakeupblocks this installation
**                                      should support.
** Outputs:
**      err_code                        system dependent error.
**
**      Returns:
**          OK                          successfully created the segment.
**          FAIL                        failure to create shared mem segment.
**
**      Exceptions:
**          none
**
** Side Effects:
**          none
**
** History:
**      01-jan-88 (mmm)
**          Created.
**      21-jan-89 (mikem)
**          set correct value in the CS_SMCNTRL for the LG/LK shared memory id.
**          also add support for dynamic LGLK segment.
**      12-jun-89 (rogerk)
**          Integrated Terminator changes.  Changed args to MEget_pages calls.
**          Changed shared memory key to character string, not LOCATION pointer.
**          Changed ME_loctokey call to ME_getkey.
**      21-Jul-89 (anton)
**          Don't need a sys V semaphore per server any more.
**      26-aug-89 (rexl)
**          Added calls to protect page allocator.
**      28-Dec-89 (fredp)
**          Add TRdisplay error message on failure of semget.
**      16-feb-90 (greg)
**          NMgtAt is a VOID.
**      16-mar-90 (fredp)
**          Added support for system-wide II_LG_MAP_ADDR variable. If it
**          is set, it contains the hex address of where the lock segment
**          should be mapped into memory (instead of the default LG_MAP_ADDR).
**          If the system segment attach fails on Sun3, retry with a 1MB
**          lower attach address since the start of the user stack dropped
**          1MB from that of SunOS 4.0.x.
**      3-jul-1992 (bryanp)
**          We don't need to call LG/LK initialize anymore.
**          Added num_of_wakeups argument and used it to set css_wakeups_off.
**      26-jul-1993 (bryanp)
**          Removed the 5 extra semaphores that no-one needed nor used. This
**              leaves a few unused variables in various control blocks, which
**              eventually I'll get around to removing, as well. For now,
**              removing the unneeded semaphores reduces the system resource
**              usage of Ingres, which is a Good Thing.
**      31-jan-94 (mikem)
**          bug #58298
**          Changed CS_handle_wakeup_events() to maintain a high-water mark
**          for events in use to limit the scanning necessary to find
**          cross process events outstanding.  Previous to this each call to
**          the routine would scan the entire array, which in the default
**          configuration was 4k elements long.  Changed CS_create_sys_segment()
**          to initalize the new fields.
**      20-apr-94 (mikem)
**          Bug #57043
**          Added a call to CS_clockinit() to CS_create_sys_segment().  This
**          call is responsible for initializing new control information stored
**          in the shared memory system segment which is used to manipulate
**          the pseudo-quantum clock.  This clock is used to maintain both
**          quantum and timeout queues in CS.  See csclock.c for more
**          information.
**      19-sep-2002 (devjo01)
**          Allocate shared memory from local RAD if Running NUMA.
**      5-Apr-2006 (kschendel)
**          Use generated css version instead of constant.
*/
STATUS
CS_create_sys_segment(i4 num_of_servers, i4 num_of_wakeups, CL_ERR_DESC *err_code)
{
    SIZE_TYPE           size;
    STATUS              status;
    PTR                 address;
    SIZE_TYPE           alloc_pages;
    char                *string;
    char                *addr_string;
    STATUS              cv_status = OK;
    i4                  meflags;

    /* the system shared memory control block looks as follows:
    **
    **  ------------------------------
    **  |     control block          |
    **  |-----------------------------
    **  |     array of server info   |
    **  |            ...             |
    **  |            ...             |
    **  |-----------------------------
    **  |     array of wakeup blocks |
    **  |            ...             |
    **  |            ...             |
    **  ------------------------------
    */
    size = sizeof(CS_SMCNTRL) + ((num_of_servers + 1) * sizeof(CS_SERV_INFO));
    size += (num_of_wakeups * sizeof(CS_SM_WAKEUP_CB));
    /* round to ME_MPAGESIZE */
    size = (size + ME_MPAGESIZE - 1) & ~(ME_MPAGESIZE - 1);

    /*
    ** Create shared segment for sysseg.
    ** Use SYSSEG.MEM as shared memory key.
    */
    meflags = ME_MZERO_MASK|ME_SSHARED_MASK|ME_CREATE_MASK|ME_NOTPERM_MASK;
    address = (PTR) 0;

    if (CXnuma_user_rad())
        meflags |= ME_LOCAL_RAD;

    status = MEget_pages(meflags, size/ME_MPAGESIZE, "sysseg.mem", &address,
                &alloc_pages, err_code);

    /* FIX ME - initialize all the server control blocks */

    if (!status)
    {

        /* initialize the shared memory data base */
        Cs_sm_cb = (CS_SMCNTRL *) address;

        Cs_sm_cb->css_css_desc.cssm_addr = (PTR) 0;
        Cs_sm_cb->css_css_desc.cssm_size = size;
#ifdef __vms
        /* there's no such thing as a shared mem id in VMS */ 
        Cs_sm_cb->css_css_desc.cssm_id = 0;
#else
        Cs_sm_cb->css_css_desc.cssm_id = ME_getkey("sysseg.mem");
#endif
        Cs_sm_cb->css_numservers = num_of_servers;
        Cs_sm_cb->css_wakeup.css_numwakeups = num_of_wakeups;
        Cs_sm_cb->css_wakeup.css_minfree = 0;
        Cs_sm_cb->css_wakeup.css_maxused = 0;
        CS_clockinit(Cs_sm_cb);
        Cs_sm_cb->css_version = make_css_version();

        CS_SPININIT(&Cs_sm_cb->css_spinlock);

        /* now allocate cross process semaphores */
# ifdef xCL_075_SYS_V_IPC_EXISTS
        Cs_sm_cb->css_semid = semget(Cs_sm_cb->css_css_desc.cssm_id,
                                     CSCP_NUM, 0600|IPC_CREAT);
        if (Cs_sm_cb->css_semid < 0)
        {
            TRdisplay("semget of %d semaphores failed\n", CSCP_NUM);
            TRdisplay("errno = %d\n",errno);
            SETCLERR(err_code, 0, 0);
            status = FAIL;
        }
# endif
    }

    if (!status)
    {
        Cs_sm_cb->css_wakeups_off =
            (char *)&Cs_sm_cb->css_servinfo[num_of_servers] - (char *)Cs_sm_cb;

        /* initialize the server-server event structures */
        Cs_sm_cb->css_events_off = Cs_sm_cb->css_wakeups_off +
                                    (num_of_wakeups * sizeof(CS_SM_WAKEUP_CB));

        Cs_sm_cb->css_events_end = Cs_sm_cb->css_css_desc.cssm_size;
        Cs_sm_cb->css_nsem = 0;
        Cs_sm_cb->css_usem = 0;


    }

        /* FIX ME - initialize all the server control blocks */

#ifdef xCL_NEED_SEM_CLEANUP
        /* Init Shared Memory mutex chain mutex */
    if (!status)
    {
        CS_cp_synch_init(&Cs_sm_cb->css_shm_mutex, &status);
        }
#endif

    return(status);
}
Пример #6
0
PTR
MEreqmem(
	u_i2	tag,
	SIZE_TYPE size,
	bool	zero,
	STATUS	*status)
{
    PTR	block=NULL;
    register ME_NODE *node;		/* the node to return */
    register ME_NODE *start;		/* for searching free list */
    register ME_NODE *this;		/* block to get node from */
    register ME_NODE *frag;		/* fragment block */
    
    ME_NODE 	*tmp;			/* not register for MEadd */
    SIZE_TYPE	nsize;			/* nsize node to obtain */
    SIZE_TYPE	fsize;			/* size of 'this' fragment block */
    SIZE_TYPE	newstuff;		/* size to add to process, or  0 */
    SIZE_TYPE	prev_actual;		/* rescan free list? */
    SIZE_TYPE	alloc_pages;
    CL_ERR_DESC	err_code;

    STATUS	MEstatus = OK;
    
    i_meuser += size;
    
    if (!size)
	MEstatus = ME_NO_ALLOC;
    
    if( !MEsetup )
        MEinitLists();
    
    /*
    **	Try to do the allocation.
    */
    if( MEstatus == OK )
    {
	nsize = SIZE_ROUND( size );
	/*
	** Get memory with malloc().
	*/
	if( MEadvice == ME_USER_ALLOC )
	{
	    if( (node = (ME_NODE *)malloc( nsize )) == NULL )
	    	MEstatus = ME_GONE;
	}
	/*
	** Get block from private free list.
	*/
	else
	{
# ifdef OS_THREADS_USED
	    CS_synch_lock( &MEfreelist_mutex );
# endif /* OS_THREADS_USED */

	    /*
	    **  Look on free list for 1st block big enough
	    **  to hold request.  This linear search can be slow.
	    */
	    start = (ME_NODE *)&MEfreelist;
	    this = MEfreelist.MEfirst;
	    while ( this != NULL && this != start && this->MEsize < nsize )
		this = this->MEnext;
	    
	    if( this == NULL )
	        MEstatus = ME_CORRUPTED;
	    
	    /*
	    ** At this point, we are in one of three states:
	    ** 1)  Corrupted memory; MEstatus != OK
	    ** 2)  this is good node, this != start
	    ** 3)  No good node; this == start;
	    */
	    if ( MEstatus == OK )
	    {
		/*
		** If nothing on free list is big enough
		** get one or more standard blocks from system,
		** take what is needed and add remainder
		** to free list.
		*/
		if (this != start)
		{
		    /* take right off the free list */
		    newstuff = 0;
		}
		else	/* this == start */
		{
		    /*
		     * Expand the free list by calling getpages
		     * newstuff holds the number of pages needed
		     */
		    newstuff = (nsize + ME_MPAGESIZE-1)/ME_MPAGESIZE;
		    /* if first time allocation, get enough for MO overhead */
		    if ( (prev_actual = i_meactual) == (SIZE_TYPE) 0 )
			newstuff += 4;
# ifdef OS_THREADS_USED
	            CS_synch_unlock( &MEfreelist_mutex );
# endif /* OS_THREADS_USED */
		    MEstatus = MEget_pages(ME_SPARSE_MASK, newstuff, NULL, 
			(PTR *)&tmp, &alloc_pages, &err_code);
# ifdef OS_THREADS_USED
	            CS_synch_lock( &MEfreelist_mutex );
# endif /* OS_THREADS_USED */
		    if (MEstatus == OK)
		    {
			/* now we need to find where to put this new memory
			   on the sorted free list - we search in reverse */
			tmp->MEsize = newstuff * ME_MPAGESIZE;
			this = MEfreelist.MElast;
			while (start != this && this != NULL &&
			       this > tmp)
			    this = this->MEprev;
			if (this != start && NEXT_NODE(this) == tmp)
			{
			    this->MEsize += tmp->MEsize;
			}
			else
			{
			    (void)QUinsert( (QUEUE *) tmp, (QUEUE *)this );
			    this = tmp;
			}
			if (this->MEnext != start &&
			    NEXT_NODE(this) == this->MEnext)
			{
			    this->MEsize += this->MEnext->MEsize;
			    (void)QUremove( (QUEUE *) this->MEnext);
			}
			/*
			** While the free list mutex was released, another
			** thread may have freed up a big enough piece of
			** memory for our needs, or may have extended the
			** free list.
			** If that's the case, research the free list;
			** we'll find either a right-sized node or 
			** the new memory we just added to the free list.
			*/
			if ( prev_actual != i_meactual )
			{
			    this = MEfreelist.MEfirst;
			    while ( this != NULL && this != start && this->MEsize < nsize )
				this = this->MEnext;
		
			    if( this == NULL )
				MEstatus = ME_CORRUPTED;
			}
		    }
		    else
			if (MEstatus == ME_OUT_OF_MEM)
			    MEstatus = ME_GONE;
		}

		/*
		** At this point, we can be in two states.
		** 1)  Corrupted memory, MEstatus != OK
		** 2)  'this' is an OK node from the free list.
		*/
		
		if ( MEstatus == OK )
		{
		    node = this;
		    
		    /*
		    ** if this is correct size or would
		    **   leave useless block in chain
		    **	just move block to allocated list
		    ** else
		    **	grab what is needed from 'this'
		    **	  block and then update 'this'
		    */
		    
		    fsize = node->MEsize - nsize;
		    if ( fsize <= sizeof(ME_NODE) )
		    {
			(void)QUremove( (QUEUE *) node );
			
			/* fudge size in node to eat leftover amount. */
			fsize = 0;
			nsize = node->MEsize;
		    }
		    else	/* make fragment block */
		    {
			/*
			** Make a leftover block after the
			** allocated space in node, in 'this'
			*/
			frag = (ME_NODE *)((char *) node + nsize );
			frag->MEsize = fsize;
			frag->MEtag = 0;
			
			/* remove node, add fragment to free list */
			(void)QUremove( (QUEUE *) node );
			MEstatus = MEfadd( frag, FALSE );
			
		    }  /* fragment left over */
		    /* Increment meactual while mutex held */
		    i_meactual += nsize;
		}  /* Got a node */
	    }  /* free list search OK */
# ifdef OS_THREADS_USED
	    CS_synch_unlock( &MEfreelist_mutex );
# endif /* OS_THREADS_USED */
	}  /* ME_USER_ALLOC */
	
	/*
	** At this point we are in one of two states:
	** 1.  Corrupted, MEstatus != OK.
	** 2.  Have a 'node' to use, from freelist or malloc.
	**     The freelist is consistant, but the allocated list is
	**     not setup for the node. "nsize" is the actual size of "node".
	*/
	
	if( MEstatus == OK )
	{
	    /* splice into allocated object queue */
	    if (0 == tag)
	    {
# ifdef OS_THREADS_USED
	    	CS_synch_lock( &MElist_mutex );
# endif /* OS_THREADS_USED */
	    	(void)QUinsert( (QUEUE *) node, (QUEUE *) MElist.MElast );
# ifdef OS_THREADS_USED
		CS_synch_unlock( &MElist_mutex );
# endif /* OS_THREADS_USED */
	    }
	    else
	    {
		IIME_atAddTag(tag, node);
	    }
	    /* Set values in block to be returned */
	    node->MEtag = tag;
	    node->MEsize = nsize;
	    node->MEaskedfor = size;
	    
	    /* Fill in the returned pointer */
	    block = (PTR)((char *)node + sizeof(ME_NODE));
	    
	    if (zero)
		MEfill( (nsize - sizeof(ME_NODE)), 0, block);
	}  /* got node OK */
    }
    if (status != NULL)
	*status = MEstatus;
    if (MEstatus != OK)
	return((PTR)NULL);
    else
	return(block);
}
Пример #7
0
/*
** Name: PCexec_suid - Execute a command as the ingres user.
**
** Description:
**	This procedure works with the Ingres service to run the given
**	command as the ingres user. It mimicks the "setuid" bit in UNIX.
**
** Inputs:
**	cmdbuf - command to execute as the ingres user
**
** Outputs:
**	none
**
** Returns:
**	OK
**	FAIL
**
** Side Effects:
**	none
**
** History:
**	08-jan-1998 (somsa01)
**	    Created.
**	19-feb-1998 (somsa01)
**	    We need to pass to the service the current working directory
**	    as well.  (Bug #89006)
**	25-feb-1998 (somsa01)
**	    We now have an input file for the process' stdin which
**	    runs through the OpenIngres service.
**	19-jun-1998 (somsa01)
**	    Use SYSTEM_PRODUCT_NAME for the name of the service.
**	10-jul-1998 (kitch01)
**		Bug 91362. If user is 'system' run through OpenIngres service
**		despite having access to server shared memory 'system' does not
**		have required privilege to access semaphores/mutexes.
**	11-jun-1999 (somsa01)
**	    If the command is a startup command, then it is always run through
**	    the Ingres service.
**	03-nov-1999 (somsa01)
**	    A failure from ControlService() should be treated as a severe
**	    error which should not let us continue.
** 	22-jan-2000 (somsa01)
**	    Return the exit code of the spawned process. Also, if the
**	    files exist, truncate them. The service name is now keyed off
**	    of II_INSTALLATION.
**	05-jun-2000 (somsa01)
**	    The Ingres installation may be started as the SYSTEM account,
**	    in which the 'ingres' user will not automatically have access
**	    to the shared memory segments. Therefore, even if the real
**	    user is 'ingres', check to see if he has access.
**	24-oct-2000 (somsa01)
**	    Removed the check on shared memory access. Access to the shared
**	    memory segment does not necessarily mean that the user running
**	    the process does not need to run the specified process as the
**	    Ingres owner. Also, generalized the check of the user with
**	    IDname_service().
**	18-dec-2000 (somsa01)
**	    Modified the cases to run the command "as is" without the Ingres
**	    service.
**	20-mar-2002 (somsa01)
**	    If all is well, return the exit code of the child process that
**	    was executed.
**	29-mar-2002 (somsa01)
**	    Properly return the child process exit code.
**	11-apr-2003 (somsa01)
**	    While waiting for "pending" to not be set, give some CPU back
**	    to the OS.
**      29-Jul-2005 (drivi01)
**	    Allow user to run the command if he/she owns a shared
**	    segment and ingres is not running as a service.
**	06-Dec-2006 (drivi01)
**	    Adding support for Vista, Vista requires "Global\" prefix for
**	    shared objects as well.  Replacing calls to GVosvers with 
**	    GVshobj which returns the prefix to shared objects.
**	    Added PCadjust_SeDebugPrivilege to allow quering of 
**	    System processes.
**	25-Jul-2007 (drivi01)
**		On Vista, PCexec_suid is unable to use SE_DEBUG Privilege
**	    to query process status and retireve its exit code.
**		The routine for monitoring a process and retrieving 
**	    its exit code has been moved to Ingres Service.
**	05-Nov-2009 (wanfr01) b122847
**	    Don't do a PCsleep unless you are waiting for more input
*/
STATUS
PCexec_suid(char *cmdbuf)
{
    EX_CONTEXT		context;
    SERVICE_STATUS	ssServiceStatus;
    LPSERVICE_STATUS	lpssServiceStatus = &ssServiceStatus;
    struct SETUID	setuid;
    DWORD		ProcID;
    HANDLE		SaveStdout;
    SECURITY_ATTRIBUTES	sa;
    CHAR		szRealUserID[25] = "";
    CHAR		*pszRealUserID = szRealUserID;
    CHAR		szServiceUserID[25] = "";
    CHAR		*pszServiceUserID = szServiceUserID;
    DWORD		BytesWritten, BytesRead = 0;
    CHAR		*inst_id;
    CHAR		SetuidShmName[64];
    CHAR		*temp_loc;
    CHAR		InBuf[256], OutBuf[256];
    static CHAR		SetuidPipeName[32];
    CL_ERR_DESC		err_code;
    CHAR		ServiceName[255];
    DWORD		ExitCode = 0;
    CHAR		tchII_INSTALLATION[3];
    BOOL		SetuidDbCmd = FALSE, ServiceCommand = FALSE;
    int			i, cmdlen;
    char		*ObjectPrefix;
	u_i4		drType;
    SC_HANDLE		schSCManager, OpIngSvcHandle;
    BOOL		bServiceStarted = FALSE;

    if (EXdeclare(ex_handler, &context) != OK)
    {
	EXdelete();
	PCexit(FAIL);
    }
    NMgtAt("II_INSTALLATION", &inst_id);
    STcopy(inst_id, tchII_INSTALLATION);

    /*
    ** See if this is a command that MUST be run through the Ingres
    ** service.
    */
    cmdlen = (i4)STlength(cmdbuf);
    for (i = 0; ServiceCommands[i] ; i++)
    {
	if (STbcompare( cmdbuf, cmdlen, ServiceCommands[i],
			(i4)STlength(ServiceCommands[i]), FALSE ) == 0)
	{
	    ServiceCommand = TRUE;
	    break;
	}
    }

    /*
    ** If the user is the same as the user who started the Ingres
    ** service, just spawn the command.
    */
    if (!ServiceCommand)
    {
	IDname(&pszRealUserID);
	if (!IDname_service(&pszServiceUserID) &&
	    STcompare(pszServiceUserID, pszRealUserID) == 0 && 
		PCisAdmin())
	{
	    /*
	    ** Attempt to just execute the command.
	    */
	    return( PCcmdline( (LOCATION *) NULL, cmdbuf, PC_WAIT,
		    (LOCATION *) NULL, &err_code) );
	}
	else
	{
	    /*
	    ** If current user is not the same as service user and ingres is not
	    ** running as a service, check if shared memory segment is owned
	    ** by current user, if user has access to shared segment allow him
	    ** to run the command.
	    */
	    PTR	shmem;
	    SIZE_TYPE	allocated_pages=0;
	    STATUS status;

	    if((status = MEget_pages(ME_MSHARED_MASK, 1, "lglkdata.mem",
	                 &shmem, &allocated_pages, &err_code)) == OK)
	    {
	        STprintf(ServiceName, "%s_Database_%s", SYSTEM_SERVICE_NAME, 
		tchII_INSTALLATION);
	        if ((schSCManager = OpenSCManager(NULL, NULL, 
					SC_MANAGER_CONNECT)) != NULL)
	        {
		    if ((OpIngSvcHandle = OpenService(schSCManager, ServiceName,
					SERVICE_QUERY_STATUS)) != NULL)
		    {
		       if (QueryServiceStatus(OpIngSvcHandle,lpssServiceStatus))
		       {
		    	if (ssServiceStatus.dwCurrentState != SERVICE_STOPPED)
			     bServiceStarted = TRUE;
		        }
		    }
	        }	
	        if (!bServiceStarted)
		    return(PCcmdline( (LOCATION *) NULL, cmdbuf, PC_WAIT,
			(LOCATION *) NULL, &err_code) );
	    }

	}

	/*
	** See if this command is an Ingres command which needs to interact
	** with at least one database.
	*/
	for (i = 0; validSetuidDbCmds[i] ; i++)
	{
	    if (STbcompare( cmdbuf, cmdlen, validSetuidDbCmds[i],
			    (i4)STlength(validSetuidDbCmds[i]), FALSE ) == 0)
	    {
		SetuidDbCmd = TRUE;
		break;
	    }
	}

	/*
	** If the user has access to the Ingres shared memory segment,
	** just spawn the command provided that it is not in the
	** validSetuidDbCmds list.
	*/
	if (!SetuidDbCmd)
	{
	    PTR	shmem;
	    SIZE_TYPE	allocated_pages=0;
	    STATUS	status;

	    if (((status = MEget_pages(ME_MSHARED_MASK, 1, "lglkdata.mem",
		&shmem,
		&allocated_pages, &err_code)) == OK) ||
		(status == ME_NO_SUCH_SEGMENT))
	    {
		if (status != ME_NO_SUCH_SEGMENT)
		    MEfree_pages(shmem, allocated_pages, &err_code);
		
		return( PCcmdline( (LOCATION *) NULL, cmdbuf, PC_WAIT,
			(LOCATION *) NULL, &err_code) );
	    }
	}
    }

    /*
    ** We must run the command through the Ingres service.
    */

    if ( STstrindex(cmdbuf, "-silent", 0, FALSE ) )
	SilentMode = TRUE;

    iimksec(&sa);


	GVshobj(&ObjectPrefix);
	STprintf(SetuidShmName, "%s%sSetuidShm", ObjectPrefix, tchII_INSTALLATION);

    if ( (SetuidShmHandle = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE,
					    FALSE,
					    SetuidShmName)) == NULL )
    {
	error_exit(GetLastError());
	return(FAIL);
    }
    if ( (SetuidShmPtr = MapViewOfFile(SetuidShmHandle,
				       FILE_MAP_WRITE | FILE_MAP_READ,
				       0,
				       0,
				       sizeof(struct SETUID_SHM))) == NULL )
    {
	error_exit(GetLastError());
	return(FAIL);
    }

    /* Set up the information to send to the service. */
    STcopy(cmdbuf, setuid.cmdline);
    GetCurrentDirectory(sizeof(setuid.WorkingDirectory),
			setuid.WorkingDirectory);
	NMgtAt("II_TEMPORARY", &temp_loc);
	drType = GetDriveType(NULL);
	if (drType == DRIVE_REMOTE)
	{
		STcopy(temp_loc, setuid.WorkingDirectory);
	}
    SaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    CVla(GetCurrentProcessId(), setuid.ClientProcID);
    STprintf(SetuidPipeName, "\\\\.\\PIPE\\INGRES\\%s\\SETUID", inst_id);

    /* Set up the stdout file for the command. */
    STprintf(OutfileName, "%s\\%sstdout.tmp", temp_loc, setuid.ClientProcID);
    if ( (OutFile = CreateFile(OutfileName,
			       GENERIC_READ | GENERIC_WRITE,
			       FILE_SHARE_READ | FILE_SHARE_WRITE,
			       &sa,
			       CREATE_ALWAYS,
			       FILE_ATTRIBUTE_NORMAL,
			       NULL)) == INVALID_HANDLE_VALUE )
    {
	error_exit(GetLastError());
	return(FAIL);
    }

    /* Set up the stdin file for the command. */
    STprintf(InfileName, "%s\\%sstdin.tmp", temp_loc, setuid.ClientProcID);
    if ( (InFile = CreateFile(InfileName,
			      GENERIC_READ | GENERIC_WRITE,
			      FILE_SHARE_READ | FILE_SHARE_WRITE,
			      &sa,
			      CREATE_ALWAYS,
			      FILE_FLAG_WRITE_THROUGH,
			      NULL)) == INVALID_HANDLE_VALUE )
    {
	error_exit(GetLastError());
	return(FAIL);
    }

    /* Wait until the service is ready to process our request. */
    while (SetuidShmPtr->pending == TRUE)
	PCsleep(100);
    SetuidShmPtr->pending = TRUE;

    /* Trigger the "setuid" event of the service. */
    if ( (schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT)) == NULL)
    {
	error_exit(GetLastError());
	return(FAIL);
    }

    STprintf(ServiceName, "%s_Database_%s", SYSTEM_SERVICE_NAME,
	     tchII_INSTALLATION );
	OpIngSvcHandle = OpenService(schSCManager, ServiceName,
						SERVICE_USER_DEFINED_CONTROL);
	if (OpIngSvcHandle == NULL)
	{
		STprintf(ServiceName, "%s_DBATools_%s", SYSTEM_SERVICE_NAME,
	     tchII_INSTALLATION );
		OpIngSvcHandle = OpenService(schSCManager, ServiceName,
						SERVICE_USER_DEFINED_CONTROL);
	}
    if ( OpIngSvcHandle == NULL)
    {
	error_exit(GetLastError());
	return(FAIL);
    }
    if (!ControlService(OpIngSvcHandle, RUN_COMMAND_AS_INGRES,
			lpssServiceStatus))
    {
	error_exit(GetLastError());
	CloseServiceHandle(schSCManager);
	return(FAIL);
    }

    WaitNamedPipe(SetuidPipeName, NMPWAIT_WAIT_FOREVER);

    /* Send the information to the service. */
    if ( (Setuid_Handle = CreateFile(SetuidPipeName,
				     GENERIC_READ|GENERIC_WRITE,
				     FILE_SHARE_READ|FILE_SHARE_WRITE,
				     &sa,
				     OPEN_EXISTING,
				     FILE_ATTRIBUTE_NORMAL,
				     NULL)) == INVALID_HANDLE_VALUE )
    {
	error_exit(GetLastError());
	return(FAIL);
    }
    if (!WriteFile(Setuid_Handle, &setuid, sizeof(struct SETUID),
		   &BytesWritten, NULL))
    {
	error_exit(GetLastError());
	return(FAIL);
    }

    /*
    ** Retrieve information back from the service, and then
    ** disconnect from the pipe.
    */
    if (!ReadFile(Setuid_Handle, &setuid, sizeof(struct SETUID),
		  &BytesRead, NULL))
    {
	error_exit(GetLastError());
	return(FAIL);
    }

    ProcID = setuid.CreatedProcID;
    SetuidShmPtr->pending = FALSE;

    UnmapViewOfFile(SetuidShmPtr);
    SetuidShmPtr = NULL;
    CloseHandle(SetuidShmHandle);

    if ( (ProcID != -1) && (ProcID != -2) )
    {
	/*
	** Wait for the "spawned" process to exit, reading its output
	** from the stdout file.
	*/
	for (;;)
	{
	    if ( ((!ReadFile(OutFile, OutBuf, sizeof(OutBuf), &BytesRead, NULL)
		  || (BytesRead == 0)) && setuid.ExitCode != STILL_ACTIVE ))
		break;

	    if ( BytesRead &&
		 (!WriteFile(SaveStdout, OutBuf, BytesRead, &BytesWritten,
		  NULL)) && setuid.ExitCode != STILL_ACTIVE)
		break;
	    else if (BytesRead < sizeof(OutBuf))
		PCsleep(200);

	    /*
	    ** Currently, the only DBA program which can require
	    ** user input is verifydb. Therefore, when it spits out
	    ** the appropriate messages asking for user input, get
	    ** it from the end user and pass it along to the spawned
	    ** process.
	    */
	    if ( (STrstrindex(OutBuf, "S_DU04FF_CONTINUE_PROMPT", 0, FALSE)
			!= NULL) ||
		 (STrstrindex(OutBuf, "S_DU0300_PROMPT", 0, FALSE) != NULL) )
	    {
		SIflush(stdout);
		MEfill(sizeof(OutBuf), ' ', &OutBuf);
		MEfill(sizeof(InBuf), ' ', &InBuf);
		SIgetrec(InBuf, 255, 0);
		WriteFile(InFile, InBuf, sizeof(OutBuf), &BytesWritten, NULL);
	    }
	}

	ExitCode = setuid.ExitCode;
	CloseHandle(Setuid_Handle);
	CloseHandle(InFile);
	DeleteFile(InfileName);
	CloseHandle(OutFile);
	DeleteFile(OutfileName);
	CloseServiceHandle(OpIngSvcHandle);
	CloseServiceHandle(schSCManager);
        return(ExitCode);
    }
    else
    {
	error_exit(GetLastError());
	return(FAIL);
    }
}
Пример #8
0
/*{
** Name:	RSstats_init - initialize monitor statistics
**
** Description:
**	Initializes the Replicator Server statistics memory segment.
**
** Inputs:
**	none
**
** Outputs:
**	none
**
** Returns:
**	OK	Function completed normally.
*/
STATUS
RSstats_init()
{
	STATUS		status;
	char		*val;
	i4		num_targs;
	SIZE_TYPE	pages_alloc;
	u_i4	stats_size;
	RS_TARGET_STATS	*targ;
	RS_TARGET_STATS	*targ_end;
	RS_TABLE_STATS	*tbl;
	RS_CONN		*conn;
	RS_TBLDESC	*tbl_info;
	CL_SYS_ERR	sys_err;
	char		server_num[4];

	PMsetDefault(1, PMhost());
	STprintf(server_num, ERx("%d"), (i4)RSserver_no);
	PMsetDefault(3, server_num);
	status = PMget(ERx("II.$.REPSERV.$.SHARED_STATISTICS"), &val);
	if (status == OK && STbcompare(val, 0, ERx("ON"), 0, TRUE) == 0)
		shared_stats = TRUE;
	num_targs = RSnum_conns - TARG_BASE;
	stats_size = sizeof(RS_MONITOR) +
		sizeof(RS_MONITOR) % sizeof(ALIGN_RESTRICT);
	targ_size = sizeof(RS_TARGET_STATS) +
		sizeof(RS_TARGET_STATS) % sizeof(ALIGN_RESTRICT);
	if (shared_stats)
		tbl_size = sizeof(RS_TABLE_STATS) + sizeof(RS_TABLE_STATS) %
			sizeof(ALIGN_RESTRICT);
	else
		tbl_size = 0;
	num_pages = (stats_size + num_targs * (targ_size + RSrdf_svcb.num_tbls *
		tbl_size)) / ME_MPAGESIZE + 1;
	if (shared_stats)
	{
		STprintf(sm_key, ERx("%s.%03d"), RS_STATS_FILE, RSserver_no);
		status = MEget_pages(ME_SSHARED_MASK | ME_CREATE_MASK |
			ME_MZERO_MASK | ME_NOTPERM_MASK, num_pages, sm_key,
			(PTR *)&mon_stats, &pages_alloc, &sys_err);
		if (status == ME_ALREADY_EXISTS)
		{
			status = MEsmdestroy(sm_key, &sys_err);
			if (status == OK)
				status = MEget_pages(ME_SSHARED_MASK |
					ME_CREATE_MASK | ME_MZERO_MASK |
					ME_NOTPERM_MASK, num_pages, sm_key,
					(PTR *)&mon_stats, &pages_alloc,
					&sys_err);
		}
	}
	else
	{
		mon_stats = (RS_MONITOR *)MEreqmem(0, num_pages * ME_MPAGESIZE,
			TRUE, &status);
	}
	if (status != OK)
		return (status);
	mon_stats->server_no = RSserver_no;
	mon_stats->local_db_no = RSlocal_conn.db_no;
	PCpid(&mon_stats->pid);
	mon_stats->startup_time = TMsecs();
	mon_stats->num_targets = num_targs;
	mon_stats->num_tables = RSrdf_svcb.num_tbls;
	STcopy(RSlocal_conn.vnode_name, mon_stats->vnode_name);
	STcopy(RSlocal_conn.db_name, mon_stats->db_name);
	mon_stats->target_stats = (RS_TARGET_STATS *)((PTR)mon_stats +
		stats_size);
	targ_end = (RS_TARGET_STATS *)((PTR)mon_stats->target_stats +
		mon_stats->num_targets * (targ_size + tbl_size *
		mon_stats->num_tables));
	for (conn = &RSconns[TARG_BASE], targ = mon_stats->target_stats;
		targ < targ_end; ++conn, targ = (RS_TARGET_STATS *)((PTR)targ +
		targ_size + tbl_size * mon_stats->num_tables))
	{
		targ->db_no = conn->db_no;
		STcopy(conn->vnode_name, targ->vnode_name);
		STcopy(conn->db_name, targ->db_name);
		if (shared_stats)
		{
			targ->table_stats = (RS_TABLE_STATS *)((PTR)targ +
				targ_size);
			for (tbl_info = RSrdf_svcb.tbl_info,
				tbl = targ->table_stats;
				tbl < targ->table_stats + mon_stats->num_tables;
				++tbl, ++tbl_info)
			{
				tbl->table_no = tbl_info->table_no;
				STcopy(tbl_info->table_owner, tbl->table_owner);
				STcopy(tbl_info->table_name, tbl->table_name);
			}
		}
	}

	return (OK);
}
Пример #9
0
/*{
** Name: LGK_initialize()	-  initialize the lg/lk shared mem segment.
**
** Description:
**	This routine is called by the LGinitialize or LKinitialize routine.  IT
**	assumes that a previous caller has allocated the shared memory segment.
**
**	If it discovers that the shared memory segment has not yet been
**	initialized, it calls the LG and LK initialize-memory routines to do so.
**
** Inputs:
**	flag		- bit mask of:
**			  LOCK_LGK_MEMORY to lock the shared data segment
**			  LGK_IS_CSP if process is CSP process this node.
**
** Outputs:
**	sys_err		- place for system-specific error information.
**
**	Returns:
**	    OK	- success
**	    !OK - failure (CS*() routine failure, segment not mapped, ...)
**	
**  History:
**	Summer, 1992 (bryanp)
**	    Working on the new portable logging and locking system.
**	19-oct-1992 (bryanp)
**	    Check memory version number when attaching.
**	22-oct-1992 (bryanp)
**	    Change LGLKDATA.MEM to lglkdata.mem.
**	23-Oct-1992 (daveb)
**	    name the semaphore too.
**	13-feb-1993 (keving)
**	    Remove support for II_LGK_MEMORY_SIZE. If II_LG_MEMSIZE
**	    is not set then calculate memory size from PM values. 
**	24-may-1993 (bryanp)
**	    If the shared memory is the wrong version, don't install the
**	    at_exit handlers (the rundown routines won't be able to interpret
**	    the memory properly).
**	26-jul-1993 (jnash)
**	    Add 'flag' param lock the LGK data segment.
**	20-sep-1993 (bryanp)
**	    In addition to calling PCatexit, call (on VMS) sys$dclexh, since
**		there are some situations (image death and image rundown without
**		process rundown) which are caught neither by PCatexit (since
**		PCexit isn't run) nor by check-dead threads (since process
**		rundown never happened). This fixes a hole where an access-
**		violating ckpdb or auditdb command never got cleaned up.
**	31-jan-1994 (bryanp)
**	    Back out a few "features" which are proving countereffective:
**	    1) Don't bother checking mem_creator_pid to see if the previous
**		creator of the shared memory has died. This was an attempt to
**		gracefully re-use sticky shared memory following a system crash,
**		but it is suspected as being the culprit in a series of system
**		failures by re-initializing the shared memory at inopportune
**		times.
**	    2) Don't complain if the shared memory already exists but is of a
**		different size than you expected. Just go ahead and try to use
**		it anyway.
**	21-feb-1994 (bryanp)
**	    Reverse item (1) of the above 31-jan-1994 change and re-enable the
**		graceful re-use of shared memory. People weren't happy with
**		having to run ipcclean and csinstall all the time.
**	23-may-1994 (bryanp)
**	    On VMS, disable ^Y for LG/LK-aware processes. We don't want to allow
**		^Y because you might interrupt the process right in the middle
**		of an LG or LK operation, while holding the shared memory
**		semaphore, and this would then wedge the whole installation.
**          
**      17-May-1994 (daveb) 59127
**          Attach lgk_mem semaphore if we're attaching to the segment.
**      30-jan-1995 (lawst01) bug 61984
**          Use memory needed calculation from the 'lgk_calculate_size'
**          function to determine the size of the shared memory pool for
**          locking and locking. If the II_LG_MEMSIZE variable is specified
**          with a value larger than needed use the supplied value. If
**          lgk_calculate_size is unable to calculate a size then use the
**          magic number of 400000.  In addition issue a warning message
**          and continue executing in the event the number of pages
**          allocated is less than the number requested. 
**	24-apr-1997 (nanpr01)
**	    Reinstate Bryanp's change. In the process of fixing bug 61984
**	    by Steve Lawrence and subsequent undo of Steve's fix by Nick
**	    Ireland on 25-jun-96 (nick) caused the if 0 code removed.
**	    Part of the Steve's change was not reinstated such as not returning
**	    the status and exit and continue.
**	    1. Don't complain if the shared memory already exists but is of a
**	    different size than you expected. Just go ahead and try to use
**	    it.
**     18-aug-1998 (hweho01)
**          Reclaim the kernel resource if LG/LK shared memory segment is  
**          reinitialized. If the shared segment is re-used (the previous creator 
**          of the shared segment has died), the cross-process semaphores get 
**          initialized more than once at the same locations. That cause the
**          kernel resource leaks on DG/UX (OS release 4.11MU04). To fix the 
**          problem, CS_cp_sem_cleanup() is called to destroy all the 
**          semaphores before LG/LK shraed segment get recreated. 
**          CS_cp_sem_cleanup() is made dependent on xCL_NEED_SEM_CLEANUP and
**          OS_THREADS_USED, it returns immediately for most platforms.  
**	27-Mar-2000 (jenjo02)
**	    Added test for crossed thread types, refuse connection
**	    to LGK memory with E_DMA811_LGK_MT_MISMATCH.
**	18-apr-2001 (devjo01)
**	    s103715 (Portable cluster support)
**	    - Add CX mem requirement calculations.
**	    - Add LGK_IS_CSP flag to indicate that LGK memory is being
**	      initialized for a CSP process.
**	    - Add basic CX initialization.
**      19-sep-2002 (devjo01)
**          If running NUMA clustered allocate memory out of local RAD.
**	30-Apr-2003 (jenjo02)
**	    Rearchitected to silence long-tolerated race conditions.
**	    BUG 110121.
**	27-feb-2004 (devjo01)
**	    Rework allocation of CX shared memory to be compatible
**	    with race condition fix introduced for bug 110121.
**	29-Dec-2008 (jonj)
**	    If lgk_calculate_size() returns FAIL, the total memory
**	    needed exceeds MAX_SIZE_TYPE and we can't continue, but
**	    tell what we can about the needs of the various bits of
**	    memory before quitting.
**	06-Aug-2009 (wanfr01)
**	    Bug 122418 - Return E_DMA812 if LOCK_LGK_MUST_ATTACH is
**	    is passed in and memory segment does not exist
**      20-Nov-2009 (maspa05) bug 122642
**          In order to synchronize creation of UUIDs across servers added
**          a semaphore and a 'last time' variable into LGK memory. 
**      14-Dec-2009 (maspa05) bug 122642
**          #ifdef out the above change for Windows. The rest of the change
**          does not apply to Windows so the variables aren't defined.
*/
STATUS
LGK_initialize(
i4	  	flag,
CL_ERR_DESC	*sys_err,
char		*lgk_info)
{
    PTR		ptr;
    SIZE_TYPE	memleft;
    SIZE_TYPE	size;
    STATUS	ret_val;
    STATUS	mem_exists;
    char	mem_name[15];
    SIZE_TYPE	allocated_pages;
    i4		me_flags;
    i4		me_locked_flag;
    SIZE_TYPE	memory_needed;
    char	*nm_string;
    SIZE_TYPE	pages;
    LGK_MEM	*lgk_mem;
    i4		err_code;
    SIZE_TYPE   min_memory;
    i4		retries;
    i4		i;
    i4		attached;
    PID		*my_pid_slot;
    i4		clustered;
    u_i4	nodes;
    SIZE_TYPE	cxmemreq;
    PTR		pcxmem;
    LGLK_INFO	lgkcount;
    char	instid[4];

    CL_CLEAR_ERR(sys_err);

    /*
    ** if LGK_base is set then this routine has already been called.  It is
    ** set up so that both LGiniitalize and LKinitialize calls it, but only
    ** the first call does anything.
    */

    if (LGK_base.lgk_mem_ptr)
	return(OK);

    PCpid( &LGK_my_pid );

    memory_needed = 0;
    NMgtAt("II_LG_MEMSIZE", &nm_string);
    if (nm_string && *nm_string)
#if defined(LP64)
	if (CVal8(nm_string, (long*)&memory_needed))
#else
	if (CVal(nm_string, (i4 *)&memory_needed))
#endif /* LP64 */
	    memory_needed = 0;

    /* Always calculate memory needed from PM resource settings  */
    /* and compare with supplied value, if supplied value is less */
    /* than minimum then use minimum                             */

    min_memory = 0;
    if ( OK == lgk_get_counts(&lgkcount, FALSE))
    {
	if ( lgk_calculate_size(FALSE, &lgkcount, &min_memory) )
	{
	    /*
	    ** Memory exceeds MAX_SIZE_TYPE, can't continue.
	    ** 
	    ** Do calculation again, this time with "wordy"
	    ** so user can see allocation bits, then quit.
	    */
	    lgk_calculate_size(TRUE, &lgkcount, &min_memory);
	    return (E_DMA802_LGKINIT_ERROR); 
	}
    }
    if (min_memory)
       memory_needed = (memory_needed < min_memory) ? min_memory
                                                    : memory_needed;
    else
       memory_needed = (memory_needed < 400000 ) ? 400000 
                                                 : memory_needed;

    clustered = (i4)CXcluster_enabled();
    cxmemreq = 0;
    if ( clustered )
    {

	if ( OK != CXcluster_nodes( &nodes, NULL ) )
	    nodes = 0;
	cxmemreq = CXshm_required( 0, nodes, lgkcount.lgk_max_xacts,
		    lgkcount.lgk_max_locks, lgkcount.lgk_max_resources );
	if ( MAX_SIZE_TYPE - memory_needed < cxmemreq )
	{
	    /*
	    ** Memory exceeds MAX_SIZE_TYPE, can't continue.
	    ** 
	    ** Do calculation again, this time with "wordy"
	    ** so user can see allocation bits, then quit.
	    */
	    SIprintf("Total LG/LK/CX allocation exceeds max of %lu bytes by %lu\n"
	    	     "Adjust logging/locking configuration values and try again\n",
		         MAX_SIZE_TYPE, cxmemreq - (MAX_SIZE_TYPE - memory_needed));
	    lgk_calculate_size(TRUE, &lgkcount, &min_memory);
	    return (E_DMA802_LGKINIT_ERROR); 
	}
	memory_needed += cxmemreq;
    }

    if ( memory_needed < MAX_SIZE_TYPE - ME_MPAGESIZE )
	pages = (memory_needed + ME_MPAGESIZE - 1) / ME_MPAGESIZE;
    else
        pages = memory_needed / ME_MPAGESIZE;

    /*
    ** Lock the LGK segment if requested to do so
    */
    if (flag & LOCK_LGK_MEMORY)
	me_locked_flag = ME_LOCKED_MASK;
    else
	me_locked_flag = 0;

    me_flags = (me_locked_flag | ME_MSHARED_MASK | ME_IO_MASK | 
		ME_CREATE_MASK | ME_NOTPERM_MASK | ME_MZERO_MASK);
    if (CXnuma_user_rad())
        me_flags |= ME_LOCAL_RAD;

    STcopy("lglkdata.mem", mem_name);

    /*
    ** In general, we just want to attach to the shared memory and detect if
    ** we are the first process to do so. However, there are ugly race
    ** conditions to consider, as well as complications because the shared
    ** memory may be left around following a system crash.
    **
    ** First we attempt to create the shared memory. Usually it already exists,
    ** so we check for and handle the case of "already exists".
    */

    /*
    ** (jenjo02)
    **
    ** Restructured to better handle all those ugly race conditions
    ** which are easily reproduced by running two scripts, one that
    ** continuously executes "lockstat" while the other is starting
    ** and stopping Ingres.
    **
    ** For example,
    **
    **		lockstat A	acquires and init's the memory
    **		RCP		attaches to "A" memory
    **		lockstat A	terminates normally
    **		lockstat B	attaches to "A" memory, sees that
    **				"A"s pid is no longer alive, and
    **				reinitializes the memory, much to
    **				the RCP's chagrin.
    ** or (more commonly)
    **
    **		lockstat A	acquires and begins to init the mem
    **		RCP		attaches to "A" memory which is
    **				still being zero-filled by lockstat,
    **				checks the version number (zero),
    **				and fails with a E_DMA434 mismatch.
    **
    ** The fix utilizes the mem_ext_sem to synchronize multiple
    ** processes; if the semaphore hasn't been initialized or
    ** if mem_version_no is zero, we'll wait one second and retry,
    ** up to 60 seconds before giving up. This gives the creating
    ** process time to complete initialization of the memory.
    **
    ** Up to LGK_MAX_PIDS are allowed to attach to the shared
    ** memory. When a process attaches it sets its PID in the
    ** first vacant slot in lgk_mem->mem_pid[]; if there are
    ** no vacant slots, the attach is refused. When the process
    ** terminates normally by calling LGK_rundown(), it zeroes
    ** its PID slot.
    **
    ** When attaching to an existing segment, we check if  
    ** there are any live processes still using the memory;
    ** if so, we can't destroy it (no matter who created it).
    ** If there are no live processes attached to the memory,
    ** we destroy and reallocate it (based on current config.dat
    ** settings).
    */

    for ( retries = 0; ;retries++ )
    {
	LGK_base.lgk_mem_ptr = (PTR)NULL;
	
	/* Give up if unable to get memory in one minute */
#if defined(conf_CLUSTER_BUILD)
        if (retries > 1)
#else
	if ( retries )
#endif
	{
	    if ( retries < 60 )
		PCsleep(1000);
	    else
	    {
		/* Another process has it blocked way too long */
		uleFormat(NULL, E_DMA800_LGKINIT_GETMEM, (CL_ERR_DESC *)NULL,
				ULE_LOG, NULL, NULL, 0, NULL, &err_code, 0);
		/* Unable to attach allocated shared memory segment. */
		return (E_DMA802_LGKINIT_ERROR); 
	    }
	}

	ret_val = MEget_pages(me_flags,
				pages, mem_name, (PTR*)&lgk_mem,
				&allocated_pages, sys_err);

	if ( mem_exists = ret_val )
	{
	    if (ret_val == ME_ALREADY_EXISTS)
	    {
		ret_val = MEget_pages((me_locked_flag | 
				       ME_MSHARED_MASK | ME_IO_MASK),
				      pages, mem_name, (PTR*)&lgk_mem,
				      &allocated_pages, sys_err);
#if defined(conf_CLUSTER_BUILD)
                if (ret_val && !retries)
                    continue;  /* try one more time */
#endif
	    }
	    if (ret_val)
	    {
		uleFormat(NULL, ret_val, sys_err,
				ULE_LOG, NULL, NULL, 0, NULL, &err_code, 0);
		uleFormat(NULL, E_DMA800_LGKINIT_GETMEM, (CL_ERR_DESC *)NULL,
				ULE_LOG, NULL, NULL, 0, NULL, &err_code, 0);
		/* Unable to attach allocated shared memory segment. */
		return (E_DMA802_LGKINIT_ERROR); 
	    }
	}
	else if (flag & LOCK_LGK_MUST_ATTACH)
	{	
	    /* Do not use the shared segment you just allocated */
	    MEfree_pages((PTR)lgk_mem, allocated_pages, sys_err);
	    return (E_DMA812_LGK_NO_SEGMENT); 
	}

	size = allocated_pages * ME_MPAGESIZE;

	/* Expose this process to the memory */
	LGK_base.lgk_mem_ptr = (PTR)lgk_mem;

	if ( mem_exists )
	{
	    /*
	    ** Memory exists.
	    **
	    ** Try to acquire the semaphore. If it's
	    ** uninitialzed, retry from the top.
	    **
	    ** If the version is zero, then another
	    ** process is initializing the memory;
	    ** keep retrying until the version is 
	    ** filled in.
	    **
	    */
	    if ( ret_val = CSp_semaphore(1, &lgk_mem->mem_ext_sem) )
	    {
		if ( ret_val != E_CS000A_NO_SEMAPHORE )
		{
		    uleFormat(NULL, ret_val, sys_err,
				ULE_LOG, NULL, NULL, 0, NULL, &err_code, 0);
		    ret_val = E_DMA802_LGKINIT_ERROR;
		    break;
		}
		continue;
	    }

	    /* Retry if still being init'd by another process */
	    if ( !lgk_mem->mem_version_no )
	    {
		CSv_semaphore(&lgk_mem->mem_ext_sem);
		continue;
	    }

	    /*
	    ** Check pids which appear to be attached to
	    ** the memory:
	    **
	    ** If any process is still alive, then we
	    ** assume the memory is consistent and use it.
	    **
	    ** If a process is now dead, it terminated
	    ** without going through LGK_rundown
	    ** to zero its PID slot, zero it now.
	    **
	    ** If there are no live PIDs attached to 
	    ** the memory, we destroy and recreate it.
	    */
	    my_pid_slot = (PID*)NULL;
	    attached = 0;

	    for ( i = 0; i < LGK_MAX_PIDS; i++ )
	    {
		if ( lgk_mem->mem_pid[i] && 
		     PCis_alive(lgk_mem->mem_pid[i]) )
		{
		    attached++;
		}
		else
		{
		    /* Vacate the slot */
		    if (lgk_mem->mem_pid[i])
		    {
			uleFormat(NULL, E_DMA499_DEAD_PROCESS_INFO, (CL_ERR_DESC *)NULL,
				ULE_LOG, NULL, NULL, 0, NULL, &err_code, 2,
				0, lgk_mem->mem_pid[i],
				0, lgk_mem->mem_info[i].info_txt);
		    }
		    lgk_mem->mem_pid[i] = (PID)0;
		    lgk_mem->mem_info[i].info_txt[0] = EOS;

		    /* Use first vacant slot for this process */
		    if ( !my_pid_slot )
		    {
			my_pid_slot = &lgk_mem->mem_pid[i];
			LGK_base.lgk_pid_slot = i;
		    }
		}
		/* Quit when both questions answered */
		if ( attached && my_pid_slot )
		    break;
	    }

	    /* If no living pids attached, destroy/reallocate */
	    if ( !attached )
	    {
		CSv_semaphore(&lgk_mem->mem_ext_sem);
		if ( LGK_destroy(allocated_pages, sys_err) )
		{
		    ret_val = E_DMA802_LGKINIT_ERROR;
		    break;
		}
		continue;
	    }

	    /* All attached pids alive? */
	    if ( !my_pid_slot )
	    {
		/* ... then there's no room for this process */
		uleFormat(NULL, E_DMA80A_LGK_ATTACH_LIMIT, (CL_ERR_DESC *)NULL,
		    ULE_LOG, NULL, NULL, 0, NULL, &err_code, 1,
		    0, attached);
	        ret_val = E_DMA802_LGKINIT_ERROR;
	    }
	    else if (lgk_mem->mem_version_no != LGK_MEM_VERSION_CURRENT)
	    {
		uleFormat(NULL, E_DMA434_LGK_VERSION_MISMATCH, (CL_ERR_DESC *)NULL,
		    ULE_LOG, NULL, NULL, 0, NULL, &err_code, 2,
		    0, lgk_mem->mem_version_no, 0, LGK_MEM_VERSION_CURRENT);
		ret_val = E_DMA435_WRONG_LGKMEM_VERSION;
	    }
	    /*
	    ** Don't allow mixed connections of MT/non-MT processes.
	    ** Among other things, the mutexing mechanisms are 
	    ** incompatible!
	    */
	    else if ( (CS_is_mt() && (lgk_mem->mem_status & LGK_IS_MT) == 0) ||
		     (!CS_is_mt() &&  lgk_mem->mem_status & LGK_IS_MT) )
	    {
		uleFormat(NULL, E_DMA811_LGK_MT_MISMATCH, (CL_ERR_DESC *)NULL,
		    ULE_LOG, NULL, NULL, 0, NULL, &err_code, 2,
		    0, (lgk_mem->mem_status & LGK_IS_MT) ? "OS"
							 : "INTERNAL",
		    0, (CS_is_mt()) ? "OS"
				    : "INTERNAL");
		ret_val = E_DMA802_LGKINIT_ERROR;
	    }
	    else
	    {
		/*
		** CX memory (if any) will lie immediately past LGK header.
		*/
		pcxmem = (PTR)(lgk_mem + 1);
		pcxmem = (PTR)ME_ALIGN_MACRO(pcxmem, sizeof(ALIGN_RESTRICT));

		LGK_base.lgk_lkd_ptr = (char *)LGK_base.lgk_mem_ptr +
					lgk_mem->mem_lkd;
		LGK_base.lgk_lgd_ptr = (char *)LGK_base.lgk_mem_ptr +
					lgk_mem->mem_lgd;
		
		/* Stuff our pid in first vacant slot */
		*my_pid_slot = LGK_my_pid;
		STlcopy(lgk_info, lgk_mem->mem_info[i].info_txt, LGK_INFO_SIZE-1);
	    }

#if defined(VMS) || defined(UNIX)
	    /* set up pointers to reference the uuid mutex and last time
	     * variable */

	    if (!ID_uuid_sem_ptr)
           	ID_uuid_sem_ptr=&lgk_mem->id_uuid_sem;

	    if (!ID_uuid_last_time_ptr)
                ID_uuid_last_time_ptr=&lgk_mem->uuid_last_time;

	    if (!ID_uuid_last_cnt_ptr)
                ID_uuid_last_cnt_ptr=&lgk_mem->uuid_last_cnt;
#endif

	    CSv_semaphore(&lgk_mem->mem_ext_sem);
	}
	else
	{

	    /* Memory did not exist */
	    /* Zero the version to keep other processes out */
	    lgk_mem->mem_version_no = 0;

#if defined(VMS) || defined(UNIX)
	    /* set up the uuid mutex and last time pointers to
	     * reference the objects in shared memory */

	    {
	        STATUS id_stat;

	        ID_uuid_sem_ptr=&lgk_mem->id_uuid_sem;
                ID_uuid_last_time_ptr=&lgk_mem->uuid_last_time;
                ID_uuid_last_cnt_ptr=&lgk_mem->uuid_last_cnt;
	        *ID_uuid_last_cnt_ptr=0;
	        ID_UUID_SEM_INIT(ID_uuid_sem_ptr,CS_SEM_MULTI,"uuid sem",
				&id_stat);
	    }
#endif

	    /* ... then initialize the mutex */
	    CSw_semaphore(&lgk_mem->mem_ext_sem, CS_SEM_MULTI,
	    			    "LGK mem ext sem" );

	    /* Record if memory created for MT or not */
	    if ( CS_is_mt() )
		lgk_mem->mem_status = LGK_IS_MT;

	    /*
	    ** memory is as follows:
	    **
	    **	-----------------------------------------------------------|
	    **	| LGK_MEM struct (keep track of this mem)	           |
	    **	|							   |
	    **	-----------------------------------------------------------|
	    **	| If a clustered installation memory reserved for CX       |
	    **	|							   |
	    **	------------------------------------------------------------
	    **	| LKD - database of info for lk system			   |
	    **	|							   |
	    **	------------------------------------------------------------
	    **	| LGD - database of info for lg system			   |
	    **	|							   |
	    **	------------------------------------------------------------
	    **	| memory manipulated by LGKm_* routines for structures used |
	    **	| by both the lk and lg systems.			   |
	    **	|							   |
	    **	------------------------------------------------------------
	    */

	    /* put the LGK_MEM struct at head of segment leaving ptr pointing 
	    ** at next aligned piece of memory
	    */

	    /*
	    ** CX memory (if any) will lie immediately past LGK header.
	    */
	    pcxmem = (PTR)(lgk_mem + 1);
	    pcxmem = (PTR)ME_ALIGN_MACRO(pcxmem, sizeof(ALIGN_RESTRICT));

	    LGK_base.lgk_lkd_ptr = pcxmem + cxmemreq;
	    LGK_base.lgk_lkd_ptr = (PTR) ME_ALIGN_MACRO(LGK_base.lgk_lkd_ptr,
						sizeof(ALIGN_RESTRICT));
	    lgk_mem->mem_lkd = (i4)((char *)LGK_base.lgk_lkd_ptr -
					 (char *)LGK_base.lgk_mem_ptr);

	    LGK_base.lgk_lgd_ptr = (PTR) ((char *) LGK_base.lgk_lkd_ptr +
					    sizeof(LKD));
	    LGK_base.lgk_lgd_ptr = (PTR) ME_ALIGN_MACRO(LGK_base.lgk_lgd_ptr,
						sizeof(ALIGN_RESTRICT));
	    lgk_mem->mem_lgd = (i4)((char *)LGK_base.lgk_lgd_ptr -
					 (char *)LGK_base.lgk_mem_ptr);

	    /* now initialize the rest of memory for allocation */

	    /* how much memory is left? */

	    ptr = ((char *)LGK_base.lgk_lgd_ptr + sizeof(LGD));
	    memleft = size - (((char *) ptr) - ((char *) LGK_base.lgk_mem_ptr));

	    if ( (ret_val = lgkm_initialize_mem(memleft, ptr)) == OK &&
		 (ret_val = LG_meminit(sys_err)) == OK &&
		 (ret_val = LK_meminit(sys_err)) == OK )
	    {
		/* Clear array of attached pids and pid info */
		for ( i = 0; i < LGK_MAX_PIDS; i++ )
		{
		    lgk_mem->mem_pid[i] = (PID)0;
		    lgk_mem->mem_info[i].info_txt[0] = EOS;
		}

		/* Set the creator pid */
		LGK_base.lgk_pid_slot = 0;
		lgk_mem->mem_creator_pid = LGK_my_pid;

		/* Set the version, releasing other processes */
		lgk_mem->mem_version_no = LGK_MEM_VERSION_CURRENT;
	    }
	    else
	    {
		uleFormat(NULL, ret_val, (CL_ERR_DESC *)NULL,
			    ULE_LOG, NULL, NULL, 0, NULL, &err_code, 0);
		ret_val = E_DMA802_LGKINIT_ERROR;

		/* Destroy the shared memory */
		LGK_destroy(allocated_pages, sys_err);
	    }
	}

	if ( ret_val == OK )
	{
	    PCatexit(LGK_rundown);

	    if ( clustered )
	    {
		/*
		** Perform preliminary cluster connection and CX memory init.
		*/

		/* Get installation code */
		NMgtAt("II_INSTALLATION", &nm_string);
		if ( nm_string )
		{
		    instid[0] = *(nm_string);
		    instid[1] = *(nm_string+1);
		}
		else
		{
		    instid[0] = 'A';
		    instid[1] = 'A';
		}
		instid[2] = '\0';
		ret_val = CXinitialize( instid, pcxmem, flag & LGK_IS_CSP );
		if ( ret_val )
		{
		    /* Report error returned from CX */
		    uleFormat(NULL, ret_val, (CL_ERR_DESC *)NULL,
			ULE_LOG, NULL, NULL, 0, NULL, &err_code, 0 );
		    break;
		}
	    }

#ifdef VMS
	    {
	    static $EXHDEF	    exit_block;
	    i4			ctrl_y_mask = 0x02000000;

	    /*
	    ** On VMS, programs like the dmfjsp and logstat run as images in
	    ** the shell process. That is, the system doesn't start and stop
	    ** a process for each invocation of the program, it just starts
	    ** and stops an image in the same process. This means that if
	    ** the program should die, the image may be rundown but the process
	    ** will remain, which means that the check-dead threads of other
	    ** processes in the installation will not feel that they need to
	    ** rundown this process, since it's still alive.
	    **
	    ** By declaring an exit handler, which will get a chance to run
	    ** even if PCexit isn't called, we improve our chances of getting
	    ** to perform rundown processing if we should die unexpectedly.
	    **
	    ** Furthermore, we ask DCL to disable its ^Y processing, which
	    ** lessens the chance that the user will interrupt us while we
	    ** are holding the semaphore.
	    */
	    exit_block.exh$g_func = LGK_rundown;
	    exit_block.exh$l_argcount = 1;
	    exit_block.exh$gl_value = &exit_block.exh$l_status;

	    if (sys$dclexh(&exit_block) != SS$_NORMAL)
		ret_val = FAIL;

	    lib$disable_ctrl(&ctrl_y_mask, 0);
	    }
#endif
	}
	break;
    }

    if ( ret_val )
	LGK_base.lgk_mem_ptr = NULL;

    return(ret_val);
}