/**\brief 保存一个字符串格式的键值 * \param[in,out] cx 配置输出上下文 * \param[in] key 键名称 * \param[in] str 键值 * \return * - AM_SUCCESS 成功 * - 其他值 错误代码(见am_cfg.h) */ AM_ErrorCode_t AM_CFG_StoreStr(AM_CFG_OutputContext_t *cx, const char *key, const char *str) { assert(cx && key); cfg_prefix(cx); fprintf(cx->fp, "%s=\"", key); if(str) { const char *pch=str; while(*pch) { switch(*pch) { case '\n': fputc('\\', cx->fp); fputc('n', cx->fp); break; case '\r': fputc('\\', cx->fp); fputc('r', cx->fp); break; case '\t': fputc('\\', cx->fp); fputc('t', cx->fp); break; case '\v': fputc('\\', cx->fp); fputc('v', cx->fp); break; case '\f': fputc('\\', cx->fp); fputc('f', cx->fp); break; case '\a': fputc('\\', cx->fp); fputc('a', cx->fp); break; case '\b': fputc('\\', cx->fp); fputc('b', cx->fp); break; case '\\': fputc('\\', cx->fp); fputc('\\', cx->fp); break; default: fputc(*pch, cx->fp); break; } pch++; } } fprintf(cx->fp, "\"\n"); return AM_SUCCESS; }
/**\brief 开始保存一个section *保存一个Section到栈中,此后所有的Store操作都在Section中进行,直到调用AM_CFG_EndSection * \param[in,out] cx 配置输出上下文 * \param[in] sec Section名称 * \return * - AM_SUCCESS 成功 * - 其他值 错误代码(见am_cfg.h) */ AM_ErrorCode_t AM_CFG_BeginSection(AM_CFG_OutputContext_t *cx, const char *sec) { assert(cx && sec); cfg_prefix(cx); fprintf(cx->fp, "%s{\n", sec); cx->sec_level++; return AM_SUCCESS; }
/**\brief 结束一个section的保存。此后的store操作在上一级section中进行 * \param[in,out] cx 配置输出上下文 * \return * - AM_SUCCESS 成功 * - 其他值 错误代码(见am_cfg.h) */ AM_ErrorCode_t AM_CFG_EndSection(AM_CFG_OutputContext_t *cx) { assert(cx); if(!cx->sec_level) { AM_DEBUG(1, "section mismatch"); return AM_CFG_ERR_SYNTAX; } cx->sec_level--; cfg_prefix(cx); fprintf(cx->fp, "}\n"); return AM_SUCCESS; }
int linux_kernel_modules_run (enum lkm_run_code code) { pthread_t **threads = NULL; if (code == lkm_pre_dev) { char dwait; char **modules = linux_kernel_modules_get_by_subsystem ("storage", &dwait); if (modules) { pthread_t *threadid = emalloc (sizeof (pthread_t)); if (ethread_create (threadid, NULL, (void *(*)(void *))linux_kernel_modules_load, modules)) { linux_kernel_modules_load (modules); } else { if (dwait) threads = (pthread_t **)setadd ((void **)threads, threadid, SET_NOALLOC); } } } else if (code == lkm_post_dev) { struct stree *linux_kernel_modules_nodes = cfg_prefix(MPREFIX); char have_generic = 0; char have_audio = 0; if (linux_kernel_modules_nodes) { struct stree *cur = linux_kernel_modules_nodes; while (cur) { char *subsystem = cur->key + sizeof (MPREFIX) -1; struct cfgnode *nod = cur->value; if (nod && nod->arbattrs) { size_t i; for (i = 0; nod->arbattrs[i]; i+=2) { if (strmatch (nod->arbattrs[i], "provide-service") && parse_boolean (nod->arbattrs[i+1])) { goto nextgroup; } } } if (strmatch (subsystem, "storage")) { } else { struct cfgnode *node = cur->value; if (strmatch (subsystem, "generic") || strmatch (subsystem, "arbitrary")) { have_generic = 1; } else if (strmatch (subsystem, "alsa") || strmatch (subsystem, "audio") || strmatch (subsystem, "sound")) { have_audio = 1; } if (node && node->svalue) { char **modules = str2set (':', node->svalue); if (modules) { pthread_t *threadid = emalloc (sizeof (pthread_t)); if (ethread_create (threadid, NULL, (void *(*)(void *))linux_kernel_modules_load, modules)) { linux_kernel_modules_load (modules); } else { if (!node->flag) threads = (pthread_t **)setadd ((void **)threads, threadid, SET_NOALLOC); } } } } nextgroup: cur = streenext (cur); } streefree (linux_kernel_modules_nodes); } if (!have_generic) { char dwait; char **modules = linux_kernel_modules_get_by_subsystem ("generic", &dwait); if (modules) { pthread_t *threadid = emalloc (sizeof (pthread_t)); if (ethread_create (threadid, NULL, (void *(*)(void *))linux_kernel_modules_load, modules)) { linux_kernel_modules_load (modules); } else { if (dwait) threads = (pthread_t **)setadd ((void **)threads, threadid, SET_NOALLOC); } } } if (!have_audio) { char dwait; char **modules = linux_kernel_modules_get_by_subsystem ("audio", &dwait); if (modules) { pthread_t *threadid = emalloc (sizeof (pthread_t)); if (ethread_create (threadid, NULL, (void *(*)(void *))linux_kernel_modules_load, modules)) { linux_kernel_modules_load (modules); } else { if (dwait) threads = (pthread_t **)setadd ((void **)threads, threadid, SET_NOALLOC); } } } } if (threads) { int i = 0; for (; threads[i]; i++) { pthread_join (*(threads[i]), NULL); free (threads[i]); } free (threads); } return status_ok; }
/**\brief Output key name*/ static void AM_INLINE cfg_key(AM_CFG_OutputContext_t *cx, const char *name) { cfg_prefix(cx); fprintf(cx->fp, "%s=", name); }