kore load 模块的一些功能

发布时间 2023-11-29 13:08:47作者: 荣锋亮

目前此玩法官方文档暂时没介绍,但是示例中包含,感觉比较有意思,所以说明下

参考使用

  • 配置
    如下,就是包含了一个共享模块的路径以及一个字符串,这个字符串实际上是模块中的一个方法,可以实现一个当模块加载时候的任务
 
load        ./memtag.so init
  • 参考代码
#include <kore/kore.h>
#include <kore/http.h>
 
/*
 * This example demonstrates how dynamically reloadable modules
 * can use the memory tagging system in Kore in order to restore
 * the global pointers in the module.
 */
 
/* Some unique value. */
#define MEM_TAG_HELLO        100
 
int        init(int);
int        page(struct http_request *);
 
/* Global pointer, gets initialized to NULL when module loads/reloads. */
char        *fixed_ptr = NULL;
// 属于标准的签名格式
int
init(int state)
{
    /* Ignore unload(s). */
    if (state == KORE_MODULE_UNLOAD)
        return (KORE_RESULT_OK);
 
    printf("fixed_ptr: %p\n", (void *)fixed_ptr);
 
    /* Attempt to lookup the original pointer. */
    if ((fixed_ptr = kore_mem_lookup(MEM_TAG_HELLO)) == NULL) {
        /* Failed, grab a new chunk of memory and tag it. */
        printf("  allocating fixed_ptr for the first time\n");
        fixed_ptr = kore_malloc_tagged(6, MEM_TAG_HELLO);
        kore_strlcpy(fixed_ptr, "hello", 6);
    } else {
        printf("  fixed_ptr address resolved\n");
    }
 
    printf("  fixed_ptr: %p\n", (void *)fixed_ptr);
    printf("  value    : %s\n", fixed_ptr);
 
    return (KORE_RESULT_OK);
}
 
int
page(struct http_request *req)
{
    http_response(req, 200, fixed_ptr, strlen(fixed_ptr));
    return (KORE_RESULT_OK);
}

参考加载处理

  • config.c
static int
configure_load(char *options)
{
    char        *argv[3];
 
    kore_split_string(options, " ", argv, 3);
    if (argv[0] == NULL)
        return (KORE_RESULT_ERROR);
    // 此处会构建kore 的模块,特别类似nginx 的module
    kore_module_load(argv[0], argv[1], KORE_MODULE_NATIVE);
    return (KORE_RESULT_OK);
}

参考资料

https://github.com/jorisvink/kore/blob/master/src/module.c#L69
https://github.com/jorisvink/kore/blob/master/examples/memtag/conf/memtag.conf#L7