(qnx系统)HAM在实际场景中的实例

  1. 启动ham;
  2. 在需要监测的模块中实现以下两个函数: 
  • int example_ham_stop(void);
  • int example_ham(char *process_name, char *script_name);

备注:libham.so和libham.so.2 是ham依赖的库,如果系统中没有这两个库,运行时会报错。

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define SUCCESS          0
#define FAILED           -1

#define __DEBUG__  
#ifdef __DEBUG__  
#define DEBUG(format, ...) printf (format, ##__VA_ARGS__)  
#else  
#define DEBUG(format,...)  
#endif

int example_ham_stop(void);
int example_ham(char *process_name, char *script_name);

/*****************************************************
* process_name 需要监测的进程名
* script_name 启动该进程的命令
******************************************************/
int example_ham(char *process_name, char *script_name)
{

	char path[64] = {0};
    //char* env = NULL;
    #define env "/etc/"
    if(access("/proc/ham",F_OK) != 0) 
    {
        DEBUG(stderr, "ham not start, quit! %s \n", process_name);
		fflush(stdout);
        return 0;
    }

    //env = getenv("ETC_PATH");
    sprintf(path, "%s/%s", env, script_name);

	fprintf(stderr, "rc_ham: path: %s %s\n", process_name, path);
    ham_entity_t *ehdl;
    ham_condition_t *chdl;
    ham_action_t *ahdl;

    ham_connect(0);

    ehdl = ham_attach_self(process_name,0,0,0,0);
    if( ehdl )
    {
        chdl = ham_condition(ehdl,CONDDEATH, "death", HREARMAFTERRESTART);
        if(chdl)
        {
            ahdl = ham_action_restart(chdl, "restart", path, HREARMAFTERRESTART);
            if( ahdl )
            {
                ham_action_handle_free( ahdl );
            }
            ham_condition_handle_free( chdl );
        }else
        {
            fprintf(stderr,"add action failed.");
        }
        ham_entity_handle_free(ehdl);
    }
    else
    {
        fprintf(stderr,"ham_attach_self failed.");
        return -1;
    }
    return EOK;
}


/**********************************
return val:
       0: ham not start or stop successfully
	   -1 : error 
**********************************/
int example_ham_stop(void)
{
	int ret = 0;

    if(access("/proc/ham",F_OK) != 0) 
    {
        fprintf(stderr, " ==example== ham not start\n");
    	return 0;
	}

	ret = system_call("hamctrl -stop");
	fprintf(stderr ,  "==example== stop ham\n");
	return ret;
}

你可能感兴趣的:(嵌入式开发,开发语言,qnx,ubuntu,软件开发)