2013-07-03 15 views
2

리눅스 워치 독 드라이버를 사용하여 워치 독 장치에 핑하는 서비스를 작성하려고합니다. 'LoadConfigurationFile'이라는 함수에서 위에 정의 된 구조체에 대한 포인터를 전달합니다. 그런 다음이 함수는 문자열을 가져 와서 라이브러리 호출 (libconfig)을 사용하여 구조체의 변수 주소에 저장합니다. 그러나 변수 'printf ("% s \ n", options.devicepath)에 액세스하면 1을 반환합니다.' 구성 파일의 내용을 예상대로 인쇄하는 대신 "/ dev/watchdog", 프로그램에 "/watchdog"이 표시됩니다.라이브러리 함수를 사용하여 값을 저장할 때 구조체의 변수가 손상되었습니다.

#include <errno.h> 
#include <getopt.h> 
#include <limits.h> 
#include <linux/types.h> 
#include <linux/watchdog.h> 
#include <signal.h> 
#include <stdbool.h> 
#include <stddef.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/ioctl.h> 
#include <syslog.h> 
#include <sys/stat.h> 
#include <sys/time.h> 
#include <sys/types.h> 
#include <sys/mman.h> 
#include <sys/wait.h> 
#include <time.h> 
#include <unistd.h> 
#include <libconfig.h> 

struct cfgoptions { 
    const char* devicepath; 
}; 

struct cfgoptions options; 
char *confile = "/etc/watchdogd.cfg"; 

    int LoadConfigurationFile(struct cfgoptions *s); 

int main(int argc, char *argv[]) 
{ 

struct cfgoptions *st_ptr; 
    st_ptr = &options; 

    if (LoadConfigurationFile(st_ptr) < 0) 
     /*exit(EXIT_FAILURE);*/ 
/**/ 

printf("%s\n", options.devicepath);return 1; 

/**/ 

int LoadConfigurationFile(struct cfgoptions *s) 
{ 
    config_t cfg; 
    config_init(&cfg); 

    if (!config_read_file(&cfg, confile) && config_error_file(&cfg) == NULL) 
    { 
     fprintf(stderr, "daemon: cannot open configuration file: %s\n", config_error_file(&cfg)); 
     config_destroy(&cfg); 
     return -1; 
    } else if (!config_read_file(&cfg, confile)) { 
     fprintf(stderr, "daemon:%s:%d: %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); 
     config_destroy(&cfg); 
     config_destroy(&cfg); 
     return -1; 
    } 

    if (!config_lookup_string(&cfg, "watchdog-device", &s->devicepath)) 
     s->devicepath = "/dev/watchdog"; 

    config_destroy(&cfg); 

    return 0; 
} 

/etc/watchdogd.cfg :

워치 독 디바이스 = "는/dev/감시"

int config_setting_lookup_string(const config_setting_t *setting, 
           const char *name, const char **value) 
{ 
    config_setting_t *member = config_setting_get_member(setting, name); 
    if(! member) 
    return(CONFIG_FALSE); 

    if(config_setting_type(member) != CONFIG_TYPE_STRING) 
    return(CONFIG_FALSE); 

    *value = config_setting_get_string(member); 
    return(CONFIG_TRUE); 
} 

config_setting_t *config_setting_get_member(const config_setting_t *setting, 
              const char *name) 
{ 
    if(setting->type != CONFIG_TYPE_GROUP) 
    return(NULL); 

    return(__config_list_search(setting->value.list, name, NULL)); 
} 

const char *config_setting_get_string(const config_setting_t *setting) 
{ 
    return((setting->type == CONFIG_TYPE_STRING) ? setting->value.sval : NULL); 
} 
+0

config_lookup_string에 대한 코드를 제공해야합니다. – xaxxon

답변

2

문제가 당신이 config_destroy를 호출하는 것입니다. 조회 도중 할당 된 모든 데이터가 손실됩니다.

- 기능 : void config_destroy (config_t * config) 이 함수는 구성 객체 config를 초기화하고 소멸시킵니다.

config_init()는 config가 가리키는 config_t 구조체를 새로운 빈 구성으로 초기화합니다.

config_destroy()는 구성과 관련된 모든 메모리의 할당을 해제하지만 config_t 구조 자체의 할당을 해제하지 않습니다.

정말이에요.

해당 줄을 제거하고 어떤 현상이 나타나는지보십시오.

기타 : libconfig가 전달하는 포인터에 대해 메모리를 할당하면 나중에 참조를 지울 수 있도록 메모리 참조를 어딘가에 유지해야합니다. 이것이 cfg 객체입니다. 라이브러리가 할당 한 모든 것을 객체로 유지하는 큰 기록 일뿐입니다. 그것을 파괴하면 라이브러리는 할당 된 모든 메모리를 해제해야합니다. 그렇지 않으면 영원히 유출됩니다.

+0

도움을 주신 데, 문제가 해결되었습니다. – clockley1

+0

@ user1450181 언제든지 손상된 데이터가 표시되는 즉시 손상 여부를 확인하십시오. 그렇게하지 않으면, 그것이 손상된 곳을 찾을 수 있고 그 전화가 있은 직후에 그것을 발견했을 것입니다. – xaxxon

+0

죄송하지만 신용을주지는 못했지만 해결책은 내가보고있는 오류를 수정합니다. – clockley1