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

使用libscf.so實(shí)現(xiàn)SMF服務(wù)refresh方法( 三 )


int read_config(void)
{
memset(filename, 0, sizeof(filename));
/* connect to the current SMF global repository */
handle = scf_handle_create(SCF_VERSION);
/* allocate scf resources */
sc = scf_scope_create(handle);
svc = scf_service_create(handle);
pg = scf_pg_create(handle);
prop = scf_property_create(handle);
value = https://www.rkxy.com.cn/dnjc/scf_value_create(handle);
/* if failed to allocate resources, exit */
if (handle == NULL || sc == NULL || svc == NULL ||
pg == NULL || prop == NULL || value =https://www.rkxy.com.cn/dnjc/= NULL)
{
destroy_scf();
return CONFIG_ERROR;
}
/* bind scf handle to the running svc.configd daemon */
if ((scf_handle_bind(handle)) == -1)
{
destroy_scf();
return CONFIG_ERROR;
}
/* get the scope of the localhost in the current repository */
if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, sc) == -1)
{
destroy_scf();
return CONFIG_ERROR;
}
/* get the service of "application/myapp" within the scope */
if (scf_scope_get_service(sc, "application/myapp", svc) == -1)
{
destroy_scf();
return CONFIG_ERROR;
}
/* get the property group of "myapp" within the given service */
if (scf_service_get_pg(svc, "myapp", pg) == -1)
{
destroy_scf();
return CONFIG_ERROR;
}
/* get the propery of "log_filename" within the given property group */
if (scf_pg_get_property(pg, "log_filename", prop) == -1)
{
destroy_scf();
return CONFIG_ERROR;
}
/* get the value of the given property */
if (scf_property_get_value(prop, value) == -1)
{
destroy_scf();
return CONFIG_ERROR;
}
/* retrIEve the value as a string */
if (scf_value_get_astring(value, filename, sizeof(filename)) == -1)
{
destroy_scf();
return CONFIG_ERROR;
}
/* free all resources */
destroy_scf();
return RUN_OK; /* OK */
}
/* free all allocated scf resources */
void destroy_scf(void)
{
if (value != NULL) scf_value_destroy(value);
if (prop != NULL) scf_property_destroy(prop);
if (pg != NULL) scf_pg_destroy(pg);
if (svc != NULL) scf_service_destroy(svc);
if (sc != NULL) scf_scope_destroy(sc);
if (handle != NULL) scf_handle_destroy(handle);
}
/* initialize application */
int app_init(void)
{
/* setup the SIGUSR1 signal handler */
signal(SIGUSR1, sig_usr);
return RUN_OK; /* OK */
}
/* signal handler for SIGUSR1 */
void sig_usr(int signo)
{
/* re-read the configuration when signal is received */
if (read_config() != RUN_OK) exit(CONFIG_ERROR);
}
在myapp.c程序中,read_config()就是讀取保存在SMF全局資源庫配置的過程 。除了在main()過程第43行服務(wù)啟動(dòng)時(shí)被調(diào)用外,還在sig_usr()過程中第206行被調(diào)用 。而sig_usr()過程在app_init()初始化過程中被設(shè)定為SIGUSR1信號的處理程序 。這意味著,此服務(wù)在運(yùn)行過程中只要收到SIGUSR1信號后,就會(huì)重新讀取存在SMF資源庫中的配置信息 。
使用libscf.so的API讀取SMF全局資源庫/etc/svc/repository.db服務(wù)配置屬性的一般流程如下圖所示 。
本例中,服務(wù)名字為application/myapp 。屬性組myapp內(nèi)只有一個(gè)屬性log_filename,它指定了日志文件的全路徑文件名 。由于此屬性為字符型,所以使用scf_value_get_astring()將屬性值value轉(zhuǎn)換字符串置于filename 。如果服務(wù)有更多的屬性,可以多次執(zhí)行循環(huán)1,甚至如果用戶服務(wù)有多個(gè)屬性組,可以多次執(zhí)行循環(huán)2,直至將所有屬性全部讀出 。
read_config()過程的第114至118行邏輯是申請scf資源,其目的是先得到保存相關(guān)scf資源的內(nèi)存 。相應(yīng)的destroy_scf()過程是釋放scf資源 。當(dāng)配置讀取完畢后,應(yīng)將所有申請的scf資源釋放 。

推薦閱讀