2017-11-28 1 views
1

커널 4.0에서 커널 소스를 통해 sysfs_create_bin_file을 실행하면, sysfs_add_file(kobj->sd, &attr->attr, true);&attr->attrbin_attribute struct 내에있는 struct attribute 구조체가됩니다.바이너리 sysfs 생성시 혼란 스럽습니다

내가 직접 sysfs_add_file에서 호출 sysfs_add_file_mode_ns를 방문 할 때까지이 말이하고 line #277 세트의 임시 변수에 stuct bin_attribute *battr = (void*)attr;

이 시점에서 struct attribute이 가리키는 것이 아니라, 적절한이 해결 방법 구조체 (sysfs_add_file에 대한 호출로 &attr->attrline #483에 사용)?

코드

int sysfs_create_bin_file(struct kobject *kobj, 
       const struct bin_attribute *attr) 
{ 
    BUG_ON(!kobj || !kobj->sd || !attr); 

    return sysfs_add_file(kobj->sd, &attr->attr, true); 
} 

int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr, 
      bool is_bin) 
{ 
    return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL); 
} 

int sysfs_add_file_mode_ns(struct kernfs_node *parent, 
       const struct attribute *attr, bool is_bin, 
       umode_t mode, const void *ns) 
{ 
    struct lock_class_key *key = NULL; 
    const struct kernfs_ops *ops; 
    struct kernfs_node *kn; 
    loff_t size; 

    if (!is_bin) { 
     ... 
    } else { 


     struct bin_attribute *battr = (void *)attr; 
     ... 
    } 
+0

죄송합니다, 지금 시도하십시오 – d4r3llo5

+0

바이너리 파일 속성을 사용하는 모듈을 만들었습니다. 커널이 구조체를 제대로 다운 스트림에 연결하는 방법을 보지 못했습니다. – d4r3llo5

+1

'이것은 이 시점에서 구조체 속성'- 예, 그렇습니다. 그러나'struct attribute' 타입의'attr' 필드는 ** struct'bin_attribute' 컨테이너의 ** 첫 번째 필드이므로, 필드에 대한 포인터는 동시에 컨테이너에 대한 포인터입니다. 따라서'battr'은 실제로'sysfs_create_bin_file' 함수에 넘겨주는'bin_attribute' 구조체를 가리 킵니다. – Tsyvarev

답변

0

라인

stuct bin_attribute *battr = (void*)attr; 

올바르게 첫 번째 필드의 타입 struct attributeattr 포인터로부터 bin_attribute 구조에 대한 포인터를 취득한다.


일반적으로, 리눅스 커널 개발자는 해당 분야에 대한 포인터를 아는 구조 유형에 포인터를 얻기 위해 container_of 매크로를 사용하는 경향이있다. 변환에 대한 더 많은 "표준"방법은 위의 것 :

stuct bin_attribute *battr = container_of(attr, struct device_attribute, attr); 

(이에서 첫 번째 attr 인수가 포인터을 의미 호출하고 세 번째 attr 인수는 필드의 이름을 의미한다).