即使 svc_start() 成功返回,底层应用程序也可能无法启动。因此,svc_start() 必须探测应用程序以验证在返回成功消息前运行了此应用程序。探测还必须考虑到应用程序可能无法立即可用,因为它需要一些时间才能启动。svc_start() 方法将调用在 xfnts.c 文件中定义的 svc_wait() 以验证应用程序是否正在运行。
/* 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) 手册页包含更多信息。