日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

使用libscf.so實現(xiàn)SMF服務refresh方法( 二 )


使用libscf.so的API實現(xiàn)refresh方法
正如前文提到的,配置有多種存在形式,不同存在形式讀取方法也不同 。另外,實現(xiàn)SMF的refresh方法也有多種方式,比如通過信號(singal)通知服務進程讀取最新配置就是一種簡單實用的方法 。本節(jié)下文將結合一個例子講述如何利用libscf.so提供的API讀取保存在SMF全局資源庫中的配置,并通過信號方式通知服務進程重新讀取配置,從而實現(xiàn)SMF所需的refresh方法 。
服務程序
表1是一個簡單的程序myapp.c,它運行后將成為后臺守護進程存在于系統(tǒng)中,并每間隔5秒鐘向日志文件插入一行記錄以報告自己的存在 。雖然它實際上不向外提供任何服務,但該程序模擬了一般服務程序的設計結構和運行模式 。即,程序運行后以守護進程形式存在于系統(tǒng),程序頭部有服務配置讀取邏輯read_config()和服務初始化邏輯app_init(),中部使用sleep(5)模擬等待服務請求邏輯,通過向日志插入記錄模擬服務請求處理邏輯,然后返回至循環(huán)開始處sleep(5)繼續(xù)等待下一個服務請求等 。只要在此結構上修改和擴充相應的邏輯就可以將此程序修改為一個真正的服務程序 。
表1. /export/home/smfdemo/myapp.c
/*****************************************************************************/
/* myapp.c */
/*****************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* global exit status code */
#define RUN_OK 0
#define CONFIG_ERROR 1
#define FATAL_ERROR 2
/* global variable declaration */
char filename[1025];
scf_handle_t *handle = NULL;
scf_scope_t *sc = NULL;
scf_service_t *svc = NULL;
scf_propertygroup_t *pg = NULL;
scf_property_t *prop = NULL;
scf_value_t *value = https://www.rkxy.com.cn/dnjc/NULL;
/* function declaration */
int read_config(void);
int app_init(void);
void daemonize(void);
void sig_usr(int signo);
void destroy_scf(void);
int main(int argc, char **argv)
{
FILE *fp;
time_t t;
/* Read the app configuration here. */
/* If error occurred, return non-zero. */
if (read_config() != RUN_OK)
{
printf("%s: read configuration failuren", argv[0]);
exit(CONFIG_ERROR);
}
/* Initialize application. Any error, return non-zero. */
if (app_init() != RUN_OK)
{
printf("%s: initialization failuren", argv[0]);
exit(FATAL_ERROR);
}
daemonize(); /* Make the application a daemon. */
/* Service logic is placed here. */
while (1)
{
sleep(5); /* Sleep for 5 seconds. In a real application, it could be */
/* waiting for service requests, e.g. waiting on message */
/* queue, etc. */
/* service logic */
if ((fp = fopen(filename,"a")) != NULL)
{
t = time(0);
fprintf(fp, "myapp is running at %sn",asctime(localtime(&t)));
fclose(fp);
}
else
{
exit(FATAL_ERROR);
}
}
}
/* make the application a daemon */
void daemonize(void)
{
int pid;
int i;
if (pid = fork())
exit(RUN_OK); /* parent exits */
else if (pid < 0)
exit(FATAL_ERROR); /* fork() failed */
/* first child process */
setsid();
if (pid = fork())
exit(RUN_OK); /* first child exits */
else if(pid < 0)
exit(FATAL_ERROR); /* fork() failed */
/* second child */
for (i = 0; i < NOFILE;i) /* close all files */
close(i);
chdir("/"); /* change Directory to root(/) directory */
umask(0); /* setup proper file mask */
}
/* read configurations in SMF repository */

推薦閱讀