(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 <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ha/ham.h>

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值