如何解决在C语言中,即使没有执行,任何地方的cURL调用都会使服务崩溃
我正在构建一个小型Windows服务,该服务使用cURL进行小型Web呼叫。
我可以毫无问题地链接cURL,但是如果代码中包含任何curl_*
函数-即使没有调用过-代码也会崩溃:
Program received signal SIGSEGV,Segmentation fault.
0x00007ff8bb75f9f8 in StartServiceCtrlDispatcherW () from C:\WINDOWS\System32\sechost.dll
这是令人反感的功能-仅注释掉调用curl_*
函数的行即可解决问题:
void phone_home(identity id,char* agent_filename,char* action)
{
CURL *curl;
CURLcode res;
char service_url[MAX_PATH_LEN],username[MAX_PATH_LEN];
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
get_username(username,MAX_PATH_LEN);
sprintf(service_url,"%s/%s?identity=%lu&user=%s&agent=%s",LISTENER_HOST,action,id,username,agent_filename);
if(curl) {
curl_easy_setopt(curl,CURLOPT_URL,service_url);
#ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L);
#endif
/* Perform the request,res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr,"curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
这是SvcInit
函数,从不调用该函数!
VOID SvcInit( DWORD dwArgc,LPTSTR *lpszArgv)
{
int id;
identity last_id = 0;
ghSvcStopEvent = CreateEvent(
NULL,/* default security attributes */
TRUE,/* manual reset event */
FALSE,/* not signaled */
NULL); /* no name */
if ( ghSvcStopEvent == NULL)
{
ReportSvcStatus( SERVICE_STOPPED,NO_ERROR,0 );
return;
}
ReportSvcStatus( SERVICE_RUNNING,0 );
while(1)
{
WaitForSingleObject(ghSvcStopEvent,INFINITE);
ReportSvcStatus( SERVICE_STOPPED,0 );
if ((id = scan_for_devices())) {
if (id != last_id) {
last_id = id;
printf("Found marked device 0x%lX!\n",id);
//phone_home(id,"",ACTION_USBINSERT);
}
} else {
last_id = 0;
}
Sleep(5000);
}
}
其余的服务定义代码:
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#include "include/identities.h"
#include "include/identity_agent.h"
#define SVCNAME TEXT("Test")
#define SVCSHOW TEXT("Test Agent")
#define SVCDESC TEXT("Test")
SERVICE_STATUS gSvcStatus;
SERVICE_STATUS_HANDLE gSvcStatusHandle;
HANDLE ghSvcStopEvent = NULL;
VOID WINAPI SvcCtrlHandler( DWORD );
VOID WINAPI SvcMain( DWORD,LPTSTR * );
VOID ReportSvcStatus( DWORD,DWORD,DWORD );
VOID SvcInit( DWORD,LPTSTR * );
VOID SvcReportEvent( LPTSTR );
int __cdecl _tmain(int argc,TCHAR *argv[])
{
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ SVCNAME,(LPSERVICE_MAIN_FUNCTION) SvcMain },{ NULL,NULL }
};
if (!StartServiceCtrlDispatcher( DispatchTable ))
{
SvcReportEvent(TEXT("StartServiceCtrlDispatcher"));
}
return 0;
}
VOID WINAPI SvcMain( DWORD dwArgc,LPTSTR *lpszArgv )
{
/* Register the handler function for the service */
gSvcStatusHandle = RegisterServiceCtrlHandler(
SVCNAME,SvcCtrlHandler);
if( !gSvcStatusHandle )
{
SvcReportEvent(TEXT("RegisterServiceCtrlHandler"));
return;
}
gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
gSvcStatus.dwServiceSpecificExitCode = 0;
ReportSvcStatus( SERVICE_START_PENDING,3000 );
SvcInit( dwArgc,lpszArgv );
}
VOID SvcInit( DWORD dwArgc,/* manual reset event */
FALSE,/* not signaled */
NULL); /* no name */
if ( ghSvcStopEvent == NULL)
{
ReportSvcStatus( SERVICE_STOPPED,0 );
return;
}
ReportSvcStatus( SERVICE_RUNNING,0 );
while(1)
{
WaitForSingleObject(ghSvcStopEvent,INFINITE);
ReportSvcStatus( SERVICE_STOPPED,0 );
if ((id = scan_for_devices())) {
if (id != last_id) {
last_id = id;
printf("Found marked device 0x%lX!\n",id);
//phone_home(id,ACTION_USBINSERT);
}
} else {
last_id = 0;
}
Sleep(5000);
}
}
VOID ReportSvcStatus( DWORD dwCurrentState,DWORD dwWin32ExitCode,DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 1;
/* Fill in the SERVICE_STATUS structure. */
gSvcStatus.dwCurrentState = dwCurrentState;
gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
gSvcStatus.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING)
gSvcStatus.dwControlsAccepted = 0;
else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
if ( (dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED) )
gSvcStatus.dwCheckPoint = 0;
else gSvcStatus.dwCheckPoint = dwCheckPoint++;
/* Report the status of the service to the SCM. */
SetServiceStatus( gSvcStatusHandle,&gSvcStatus );
}
VOID WINAPI SvcCtrlHandler( DWORD dwCtrl )
{
/* Handle the requested control code. */
switch(dwCtrl)
{
case SERVICE_CONTROL_STOP:
ReportSvcStatus(SERVICE_STOP_PENDING,0);
/* Signal the service to stop. */
SetEvent(ghSvcStopEvent);
ReportSvcStatus(gSvcStatus.dwCurrentState,0);
return;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
break;
}
}
VOID SvcReportEvent(LPTSTR szFunction)
{
HANDLE hEventSource;
LPCTSTR lpszStrings[2];
TCHAR Buffer[80];
hEventSource = RegisterEventSource(NULL,SVCNAME);
if( NULL != hEventSource )
{
StringCchPrintf(Buffer,80,TEXT("%s failed with %d"),szFunction,GetLastError());
lpszStrings[0] = SVCNAME;
lpszStrings[1] = Buffer;
ReportEvent(hEventSource,EVENTLOG_ERROR_TYPE,/* event type */
0,/* event category */
/*SVC_ERROR*/0,/* event identifier */
NULL,/* no security identifier */
2,/* size of lpszStrings array */
0,/* no binary data */
lpszStrings,/* array of strings */
NULL); /* no binary data */
DeregisterEventSource(hEventSource);
}
}
我正在使用gcc -o bin/identity_service identity_agent.c identities.c -lcurl
解决方法
事实证明,我没有静态链接cURL,因此它正在寻找cURL DLL-位于mingw bin目录中....位于我的用户路径中....哪个服务在系统下运行看不到
我将-curl与-DCURL_STATICLIB静态链接,并且一切正常。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。