C H A P T E R  4

Building CMM Applications

For information about how to build applications that use the CMM API, see the following sections:


Installing Applications on a Cluster

If your application is to be run on a master-eligible node, install the application binary files on the node.

If your application is to be run on a diskless node, install the binaries on the master node in:

/export/root/diskless_node_name/path

where diskless_node_name is the name of the diskless node and path is the path to the application. For example, if the binaries are installed in the /opt/mySvc/bin/myapp directory, the application binaries are installed in the /export/root/NetraDiskless1/opt/mySvc/bin/myapp directory for the NetraDiskless1 diskless node.

If your application will run on a dataless node, binaries can be installed either on the local disk (as in the master-eligible node case) or on the shared partition (as in the diskless node case), but they should be installed on the local disk.

For definitions of the terms diskfull, diskless, and dataless nodes, see Cluster Model in the Netra High Availability Suite 3.0 1/08 Foundation Services Overview.


Setting Up a Makefile

To enable the compiler and linker to locate the required header files and libraries, specify the following entries in your makefile:

For the Solaris OS:


CFLAGS  += -I/opt/SUNWcgha/includeLDFLAGS += -L/opt/SUNWcgha/lib \
-R/opt/SUNWcgha/lib

For the Linux OS:

Building a 32-bit application:


CFLAGS  += -I/opt/sun/includeLDFLAGS += -L/opt/sun/lib

Building a 64-bit application:


CFLAGS  += -I/opt/sun/includeLDFLAGS += -L/opt/sun/lib64



Note - These entries apply only if the developer package header files and libraries are installed in their default locations. For information on installing the header files and libraries required by developers, see Installing Libraries and Header Files.



For an example makefile that uses a specified code example, see EXAMPLE 4-4.


Compiling Applications

The applications you develop using the CMM API can be compiled using the Suntrademark Studio software compiler. For more information, refer to the documentation supplied with Sun Studio software.


Including Applications in a Startup Script

Applications that are to run on a deployed Foundation Services cluster can be started automatically when the node is booted. For this, you can supply a startup script for the application. The startup script should be located in the /etc/init.d/ directory. Link the script to an entry in either the /etc/rc2.d/ directory or the /etc/rc3.d/ directory, and the script will be executed when the node boots. For more information, see the init(1M) man page.

If you require fast performance from a program, ensure that shared objects linked with the program are bound at startup. To prebind the shared objects, set the LD_BIND_NOW environment variable. For more information on this variable, see the Solaris documentation about Runtime Linker Linker and Libraries Guide, or for the Linux OS, refer to the ld(1) man page.

For better performance from programs running on diskless nodes, you can set the mlockall function within your program to lock address space. For more information, see the mlockall(3C) man page.


Running Your Applications on the Cluster

Applications that you develop on your development host can be tested on a cluster. For information about supported cluster configurations and how to connect your development host to a cluster, see the Netra High Availability Suite 3.0 1/08 Foundation Services Getting Started Guide.

To transfer applications from your development host to a cluster, use one of the following commands:


ftp The ftp command is the user interface to the Internet standard File Transfer Protocol. For more information, see the ftp(1) man page.
rcp The rcp command is used to copy files between machines. For more information, see the rcp(1) man page.
mount The mount command is used to mount file systems and remote resources. For more information, see the mount(1M) man page, or for the Linux OS, refer to the mount(8) man page.


Application Examples

The code fragment shown in EXAMPLE 4-1 provides functions to display peer node membership information. This code is provided in the module common.c of the examples c and should be included when you run the CMM API examples provided in this guide. These functions use the set of functions cmm_member_isXXXX, which are explained in Chapter 5.


EXAMPLE 4-1   common.c  
#include <stdio.h>
#include <cmm/cmm.h>
 
#include “common.h”
 
static const char *get_role_str(cmm_member_t const *P_Member)
{
    const char *L_Result;
 
    if (cmm_member_ismaster(P_Member))
        L_Result = "MASTER";
    else if (cmm_member_isvicemaster(P_Member))
        L_Result = "VICE-MASTER";
    else if (cmm_member_isoutofcluster(P_Member))
        L_Result = "OUT";
    else
        L_Result = "IN";
 
        return L_Result;
}
 
void print_member (cmm_member_t const *P_Member)
{
    printf("------------------------------\n");
    printf("node_id     = %d\n", P_Member->nodeid);
    printf("domain_id   = %d\n", P_Member->domainid);
    printf("name        = %s\n", P_Member->name) ;
    printf("role        = %s\n", get_role_str(P_Member));
    printf("qualified   = %s\n",
        (cmm_member_isdisqualified(P_Member)) ? "NO" : "YES") ;
    printf("synchro.    = %s\n",
        (cmm_member_isdesynchronized(P_Member)) ? "NEEDED" :
         "READY");
    printf("frozen      = %s\n",
        (cmm_member_isfrozen(P_Member)) ? "NO" : "YES");
    printf("excluded    = %s\n",
        (cmm_member_isexcluded(P_Member)) ? "NO" : "YES");
    printf("eligible    = %s\n",
        (cmm_member_iseligible(P_Member)) ? "NO" : "YES");
    printf("incarn.     = %d\n", P_Member->incarnation_number);
    printf("swload_id   = %s\n", P_Member->software_load_id) ;
    printf("CGTP @      = %s\n", P_Member->addr);
    printf("init.election = %d\n", P_Member->inital_election);
    printf("------------------------------\n");
}

EXAMPLE 4-1 is accompanied by a common.h header file. The code in the common.h header file is used in many of the code samples in this guide and is shown in EXAMPLE 4-2.


EXAMPLE 4-2   The common.h Header File
#ifndef __CMM_COMMON__
#define __CMM_COMMON__
 
#include <cmm/cmm.h>
 
extern void print_member(cmm_member_t const *P_Member) ;
 
 
#endif

For instructions on installing header files, see Installing Libraries and Header Files.

To check that your development host and cluster are running correctly and that you are able to compile and run code using the CMM API, an example, master_node.c, is provided. This example uses:

EXAMPLE 4-3 shows a sampling of this program.


EXAMPLE 4-3   Example master_node.c Program  
#include <stdio.h>
#include <cmm/cmm.h>
 
#include "common.h"
 
 
int main(void)
{
    cmm_member_t member_info;
    cmm_error_t result;
 
    result = cmm_master_getinfo(&member_info);
    if (result != CMM_OK) {
        printf("Couldn’t get master node information: %s\n",
                cmm_strerror(result));
    else
        print_member(&member_info);
    return (result == CMM_OK ? 0 : 1);
}

An example makefile is also provided in this section. This example makefile enables you to compile the master_node.c code in EXAMPLE 4-3, using:

This example makefile is shown in EXAMPLE 4-4.


EXAMPLE 4-4   Makefile for the master_node.c Program
NHAS_DIR = /opt/SUNWcgha
 
CFLAGS = -I$(NHAS_DIR)/include
LDFLAGS = -L$(NHAS_DIR)/lib \
          -R$(NHAS_DIR)/lib \
          -lcgha_cmm
 
all: master_node
 
master_node: master_node.o common.o
        $(CC) $(LDFLAGS) -o $@ master_node.o common.o
 
master_node.o: master_node.c common.h
 
common.o: common.c common.h
 
.c.o:
        $(CC) $(CFLAGS) -c $<
 
clean:
        rm -rf *.o master_node

Building a 64-bit application for use with the the Linux OS:



Note - The preceding example is for the Solaris OS. If your system uses the Linux OS, modify the makefile as described in Setting Up a Makefile.



The Netra HA Suite software is supplied with source code examples in the SUNWnhcmd developer package. These examples are installed in subdirectories of the /opt/SUNWcgha/examples/ directory for Solaris-based systems and in the /opt/sun/nhas/examples/ directory for Linux-based systems.