即使 svc_start() 成功傳回,基礎應用程式可能也會無法啟動。因此,svc_start() 必須探測應用程式,以驗證該應用程式在傳回成功訊息之前已在執行。探測必須考慮到,由於應用程式需要花費一些時間來啟動,因此其不會立即可用。svc_start() 方法呼叫 svc_wait() (在 xfnts.c 檔案中定義) 以確定應用程式正在執行。
/* Wait for the service to start up fully */
scds_syslog_debug(DBG_LEVEL_HIGH,
"Calling svc_wait to verify that service has started.");
rc = svc_wait(scds_handle);
scds_syslog_debug(DBG_LEVEL_HIGH,
"Returned from svc_wait");
if (rc == 0) {
scds_syslog(LOG_INFO, "Successfully started the service.");
} else {
scds_syslog(LOG_ERR, "Failed to start the service.");
}
svc_wait() 函數呼叫 scds_get_netaddr_list() 以取得探測應用程式所需的網路位址資源。
/* obtain the network resource to use for probing */
if (scds_get_netaddr_list(scds_handle, &netaddr)) {
scds_syslog(LOG_ERR,
"No network address resources found in resource group.");
return (1);
}
/* Return an error if there are no network resources */
if (netaddr == NULL || netaddr->num_netaddrs == 0) {
scds_syslog(LOG_ERR,
"No network address resource in resource group.");
return (1);
}
svc_wait() 函數取得 Start_timeout 和 Stop_timeout 值。
svc_start_timeout = scds_get_rs_start_timeout(scds_handle) probe_timeout = scds_get_ext_probe_timeout(scds_handle)
若要計算伺服器用來啟動的時間,svc_wait() 將呼叫 scds_svc_wait(),並傳送一個相當於 Start_timeout 值百分之三的逾時值。svc_wait() 函數呼叫 svc_probe() 函數以驗證應用程式已啟動。svc_probe() 方法在指定通訊埠上建立與伺服器的簡單套接字連接。如果無法連接至連接埠,svc_probe() 將傳回一個為 100 的值,表示完全失敗。如果連接成功,但無法中斷與連接埠的連接,則 svc_probe() 將傳回一個為 50 的值。
svc_probe() 失敗或部分失敗時,svc_wait() 將以逾時值 5 呼叫 scds_svc_wait()。scds_svc_wait() 方法會將探測頻率限定為每五秒一次。此方法也將計算嘗試啟動服務的次數。如果嘗試的次數超出此期間 (由資源的 Retry_interval 特性指定) 內資源的 Retry_count 特性的值,則 scds_svc_wait() 函數傳回失敗。在此情況下,svc_start() 函數也將傳回故障。
#define SVC_CONNECT_TIMEOUT_PCT 95
#define SVC_WAIT_PCT 3
if (scds_svc_wait(scds_handle, (svc_start_timeout * SVC_WAIT_PCT)/100)
!= SCHA_ERR_NOERR) {
scds_syslog(LOG_ERR, "Service failed to start.");
return (1);
}
do {
/*
* probe the data service on the IP address of the
* network resource and the portname
*/
rc = svc_probe(scds_handle,
netaddr->netaddrs[0].hostname,
netaddr->netaddrs[0].port_proto.port, probe_timeout);
if (rc == SCHA_ERR_NOERR) {
/* Success. Free up resources and return */
scds_free_netaddr_list(netaddr);
return (0);
}
/* Call scds_svc_wait() so that if service fails too
if (scds_svc_wait(scds_handle, SVC_WAIT_TIME)
!= SCHA_ERR_NOERR) {
scds_syslog(LOG_ERR, "Service failed to start.");
return (1);
}
/* Rely on RGM to timeout and terminate the program */
} while (1);
結束之前,xfnts_start 方法將呼叫 scds_close(),以收回由 scds_initialize() 配置的資源。scds_initialize() 函數 和 scds_close(3HA) 線上手冊包含更多資訊。