KHMEXP khm_int32 KHMAPI kmm_get_plugin_info_i(kmm_plugin p, kmm_plugin_info * info) { khm_int32 rv = KHM_ERROR_SUCCESS; kmm_plugin_i * pi; khm_handle csp_plugin; if (!info) return KHM_ERROR_INVALID_PARAM; EnterCriticalSection(&cs_kmm); if (!kmm_is_plugin(p)) { rv = KHM_ERROR_INVALID_PARAM; goto _cleanup; } pi = kmm_plugin_from_handle(p); ZeroMemory(info, sizeof(*info)); info->reg = pi->p; info->reg.msg_proc = NULL; if (KHM_FAILED(kmm_get_plugin_config(pi->p.name, KHM_PERM_READ, &csp_plugin))) { info->failure_count = 0; *((khm_int64 *)&info->failure_time) = 0; info->failure_reason = 0; } else { if (KHM_FAILED(khc_read_int32(csp_plugin, L"FailureCount", &info->failure_count))) info->failure_count = 0; if (KHM_FAILED(khc_read_int64(csp_plugin, L"FailureTime", (khm_int64 *) &info->failure_time))) *((khm_int64 *) &info->failure_time) = 0; if (KHM_FAILED(khc_read_int32(csp_plugin, L"FailureReason", &info->failure_reason))) info->failure_reason = 0; khc_close_space(csp_plugin); } info->state = pi->state; kmm_hold_plugin(p); info->h_plugin = p; info->flags = (pi->flags & KMM_PLUGIN_FLAG_DISABLED); _cleanup: LeaveCriticalSection(&cs_kmm); return rv; }
KHMEXP khm_int32 KHMAPI kmm_get_next_plugin(kmm_plugin p, kmm_plugin * p_next) { khm_int32 rv = KHM_ERROR_SUCCESS; kmm_plugin_i * pi; kmm_plugin_i * pi_next = NULL; kmm_module_i * m; EnterCriticalSection(&cs_kmm); if (p == NULL) { if (kmm_listed_plugins) pi_next = kmm_listed_plugins; else { for (m = kmm_all_modules; m; m = LNEXT(m)) { if (m->plugins) { pi_next = m->plugins; break; } } } } else if (kmm_is_plugin(p)) { pi = kmm_plugin_from_handle(p); pi_next = LNEXT(pi); if (!pi_next) { /* we have either exhausted the listed plugins or we are at the end of the module's plugin list */ if (pi->module) { m = LNEXT(pi->module); } else { m = kmm_all_modules; } for(; m; m = LNEXT(m)) { if (m->plugins) { pi_next = m->plugins; break; } } } } if (pi_next) { *p_next = kmm_handle_from_plugin(pi_next); kmm_hold_plugin(*p_next); } else { *p_next = NULL; rv = KHM_ERROR_NOT_FOUND; } LeaveCriticalSection(&cs_kmm); return rv; }
KHMEXP void KHMAPI kmm_exit(void) { kmm_module_i * m; kmm_plugin_i * p; EnterCriticalSection(&cs_kmm); p = kmm_listed_plugins; while(p) { kmm_plugin_i * pn; pn = LNEXT(p); /* plugins that were never resolved should be kicked off the list. Flipping the refcount will do that if no other references exist for the plugin. The plugins that were waiting for unresolved dependencies will automatically get freed when the placeholders and other plugins get freed. */ if(p->state == KMM_PLUGIN_STATE_PLACEHOLDER) { kmm_hold_plugin(kmm_handle_from_plugin(p)); kmm_release_plugin(kmm_handle_from_plugin(p)); } p = pn; } m = kmm_all_modules; while(m) { kmm_unload_module(kmm_handle_from_module(m)); m = LNEXT(m); } LeaveCriticalSection(&cs_kmm); WaitForSingleObject(evt_exit, INFINITE); EnterCriticalSection(&cs_kmm); kmq_post_thread_quit_message(tid_registrar, 0, NULL); hash_del_hashtable(hash_plugins); hash_del_hashtable(hash_modules); LeaveCriticalSection(&cs_kmm); TlsFree(tls_kmm); tls_kmm = 0; }
KHMEXP khm_int32 KHMAPI kmm_provide_plugin(kmm_module module, kmm_plugin_reg * plugin) { kmm_module_i * m; kmm_plugin_i * p; size_t cb_name = 0; size_t cb_desc = 0; size_t cb_dep = 0; m = kmm_module_from_handle(module); /* can only called when handing init_module() */ if(m->state != KMM_MODULE_STATE_INIT) return KHM_ERROR_INVALID_OPERATION; if(!plugin || FAILED(StringCbLength(plugin->name, KMM_MAXCB_NAME - sizeof(wchar_t), &cb_name)) || (plugin->description && FAILED(StringCbLength(plugin->description, KMM_MAXCB_DESC - sizeof(wchar_t), &cb_desc))) || (plugin->dependencies && KHM_FAILED(multi_string_length_cb(plugin->dependencies, KMM_MAXCB_DEPS, &cb_dep)))) { return KHM_ERROR_INVALID_PARAM; } cb_name += sizeof(wchar_t); cb_desc += sizeof(wchar_t); p = kmmint_get_plugin_i(plugin->name); /* released below or in kmmint_init_module() */ kmm_hold_plugin(kmm_handle_from_plugin(p)); if(p->state != KMM_PLUGIN_STATE_NONE && p->state != KMM_PLUGIN_STATE_PLACEHOLDER) { kmm_release_plugin(kmm_handle_from_plugin(p)); return KHM_ERROR_DUPLICATE; } /* released when the plugin quits */ kmm_hold_module(module); p->module = m; p->p.flags = plugin->flags; p->p.msg_proc = plugin->msg_proc; p->p.type = plugin->type; if(plugin->description) { p->p.description = PMALLOC(cb_desc); StringCbCopy(p->p.description, cb_desc, plugin->description); } else p->p.description = NULL; if(plugin->dependencies) { p->p.dependencies = PMALLOC(cb_dep); multi_string_copy_cb(p->p.dependencies, cb_dep, plugin->dependencies); } else p->p.dependencies = NULL; p->p.module = p->module->name; p->p.icon = plugin->icon; p->state = KMM_PLUGIN_STATE_REG; kmmint_delist_plugin(p); EnterCriticalSection(&cs_kmm); LPUSH(&(m->plugins), p); p->flags |= KMM_PLUGIN_FLAG_IN_MODLIST; LeaveCriticalSection(&cs_kmm); /* leave the plugin held because it is in the module's plugin list */ return KHM_ERROR_SUCCESS; }
/*! \internal \brief Uninitializes a module \note Should only be called from the context of the registrar thread */ void kmmint_exit_module(kmm_module_i * m) { kmm_plugin_i * p; /* Exiting a module happens in two stages. If the module state is running (there are active plugins) then those plugins must be exited. This has to be done from the plugin threads. The signal for the plugins to exit must be issued from the registrar. Therefore, we post messages to the registrar for each plugin we want to remove and exit kmmint_exit_module(). When the last plugin is exited, the plugin management code automatically signalls the registrar to remove the module. kmmint_exit_module() gets called again. This is the second stage, where we call exit_module() for the module and start unloading everything. */ EnterCriticalSection(&cs_kmm); /* get rid of any dangling uninitialized plugins */ LPOP(&(m->plugins), &p); while(p) { p->flags &= ~KMM_PLUGIN_FLAG_IN_MODLIST; kmmint_exit_plugin(p); /* release hold from kmm_provide_plugin() */ kmm_release_plugin(kmm_handle_from_plugin(p)); LPOP(&(m->plugins), &p); } if(m->state == KMM_MODULE_STATE_RUNNING) { int np = 0; m->state = KMM_MODULE_STATE_EXITPLUG; p = kmm_listed_plugins; while(p) { if(p->module == m && (p->flags & KMM_PLUGIN_FLAG_IN_MODCOUNT)) { kmm_hold_plugin(kmm_handle_from_plugin(p)); kmq_post_message(KMSG_KMM, KMSG_KMM_I_REG, KMM_REG_EXIT_PLUGIN, (void *) p); np++; } p = LNEXT(p); } #ifdef DEBUG assert(np == m->plugin_count); #endif if(np > 0) { /* we have to go back and wait for the plugins to exit. when the last plugin exits, it automatically posts EXIT_MODULE. We can pick up from there when this happens. */ LeaveCriticalSection(&cs_kmm); return; } } else { #ifdef DEBUG assert(m->plugin_count == 0 || m->state == KMM_MODULE_STATE_EXITPLUG); #endif /* if there are still plug-ins waiting to be unloaded, then we have to go back and wait for them to finish. Once they are done, kmmint_exit_module() will get called again. */ if (m->plugin_count > 0) { LeaveCriticalSection(&cs_kmm); return; } } if(m->flags & KMM_MODULE_FLAG_INITP) { exit_module_t p_exit_module; if(m->state > 0) m->state = KMM_MODULE_STATE_EXIT; p_exit_module = (exit_module_t) GetProcAddress(m->h_module, EXP_EXIT_MODULE); if(p_exit_module) { LeaveCriticalSection(&cs_kmm); (*p_exit_module)(kmm_handle_from_module(m)); EnterCriticalSection(&cs_kmm); } } if(m->state > 0) m->state = KMM_MODULE_STATE_EXITED; LeaveCriticalSection(&cs_kmm); if(!(m->flags & KMM_MODULE_FLAG_NOUNLOAD) && m->h_module) { FreeLibrary(m->h_module); } if(!(m->flags & KMM_MODULE_FLAG_NOUNLOAD) && m->h_resource && (m->h_resource != m->h_module)) { FreeLibrary(m->h_resource); } m->h_module = NULL; m->h_resource = NULL; if (m->flags & KMM_MODULE_FLAG_LOADED) { #ifdef DEBUG assert(kmm_active_modules > 0); #endif kmm_active_modules--; } m->flags = 0; /* release the hold obtained in kmmint_init_module() */ kmm_release_module(kmm_handle_from_module(m)); /* Last but not least, now see if there are any modules left that are running. If not, we can safely signal an exit. */ if (kmm_active_modules == 0) { SetEvent(evt_exit); } }
/*! \internal \brief Initialize a plugin \note If kmmint_init_plugin() is called on a plugin, then kmmint_exit_plugin() \b must be called for the plugin. \note Should only be called from the context of the registrar thread */ void kmmint_init_plugin(kmm_plugin_i * p) { DWORD dummy; khm_handle csp_plugin = NULL; khm_handle csp_plugins = NULL; khm_int32 t; /* the following will be undone in kmmint_exit_plugin() */ kmm_hold_plugin(kmm_handle_from_plugin(p)); EnterCriticalSection(&cs_kmm); if(p->state != KMM_PLUGIN_STATE_REG && p->state != KMM_PLUGIN_STATE_HOLD) { LeaveCriticalSection(&cs_kmm); goto _exit; } _begin_task(0); _report_mr1(KHERR_NONE, MSG_IP_TASK_DESC, _cstr(p->p.name)); _describe(); if(p->state == KMM_PLUGIN_STATE_HOLD) { /* if this plugin was held, then we already had a hold from the initial attempt to start the plugin. Undo the hold we did a few lines earlier. */ kmm_release_plugin(kmm_handle_from_plugin(p)); /* same for the plugin count for the module. */ #ifdef DEBUG assert(p->flags & KMM_PLUGIN_FLAG_IN_MODCOUNT); #endif p->module->plugin_count--; p->flags &= ~KMM_PLUGIN_FLAG_IN_MODCOUNT; } p->state = KMM_PLUGIN_STATE_PREINIT; kmmint_delist_plugin(p); kmmint_list_plugin(p); LeaveCriticalSection(&cs_kmm); if(KHM_FAILED(kmm_get_plugins_config(0, &csp_plugins))) { _report_mr0(KHERR_ERROR, MSG_IP_GET_CONFIG); p->state = KMM_PLUGIN_STATE_FAIL_UNKNOWN; goto _exit; } if(KHM_FAILED(kmm_get_plugin_config(p->p.name, 0, &csp_plugin))) { if(KHM_FAILED(kmm_register_plugin(&(p->p), 0))) { _report_mr0(KHERR_ERROR, MSG_IP_NOT_REGISTERED); p->state = KMM_PLUGIN_STATE_FAIL_NOT_REGISTERED; goto _exit; } if(KHM_FAILED(kmm_get_plugin_config(p->p.name, 0, &csp_plugin))) { _report_mr0(KHERR_ERROR, MSG_IP_NOT_REGISTERED); p->state = KMM_PLUGIN_STATE_FAIL_NOT_REGISTERED; goto _exit; } } if (KHM_SUCCEEDED(khc_read_int32(csp_plugin, L"Disabled", &t)) && t) { p->flags |= KMM_PLUGIN_FLAG_DISABLED; p->state = KMM_PLUGIN_STATE_FAIL_DISABLED; goto _exit; } #if 0 /*TODO: check the failure count and act accordingly */ if(KHM_SUCCEEDED(khc_read_int32(csp_plugin, L"FailureCount", &t)) && (t > 0)) { } #endif EnterCriticalSection(&cs_kmm); p->n_depends = 0; p->n_unresolved = 0; do { wchar_t * deps = NULL; wchar_t * d; khm_size sz = 0; if(khc_read_multi_string(csp_plugin, L"Dependencies", NULL, &sz) != KHM_ERROR_TOO_LONG) break; deps = PMALLOC(sz); if(KHM_FAILED(khc_read_multi_string(csp_plugin, L"Dependencies", deps, &sz))) { if(deps) PFREE(deps); break; } for(d = deps; d && *d; d = multi_string_next(d)) { kmm_plugin_i * pd; int i; pd = kmmint_get_plugin_i(d); if(pd->state == KMM_PLUGIN_STATE_NONE) { /* the dependant was not previously known */ pd->state = KMM_PLUGIN_STATE_PLACEHOLDER; } for(i=0; i < pd->n_dependants; i++) { if(pd->dependants[i] == p) break; } if(i >= pd->n_dependants) { if( pd->n_dependants >= KMM_MAX_DEPENDANTS ) { /*TODO: handle this gracefully */ RaiseException(1, EXCEPTION_NONCONTINUABLE, 0, NULL); } /* released in kmmint_free_plugin() */ kmm_hold_plugin(kmm_handle_from_plugin(p)); pd->dependants[pd->n_dependants] = p; pd->n_dependants++; } p->n_depends++; if(pd->state != KMM_PLUGIN_STATE_RUNNING) { p->n_unresolved++; } } if(p->n_unresolved > 0) { p->state = KMM_PLUGIN_STATE_HOLD; } PFREE(deps); } while(FALSE); #ifdef DEBUG assert(!(p->flags & KMM_PLUGIN_FLAG_IN_MODCOUNT)); #endif p->flags |= KMM_PLUGIN_FLAG_IN_MODCOUNT; p->module->plugin_count++; LeaveCriticalSection(&cs_kmm); if(p->state == KMM_PLUGIN_STATE_HOLD) { _report_mr1(KHERR_INFO, MSG_IP_HOLD, _dupstr(p->p.name)); goto _exit_post; } kmmint_add_to_plugin_queue(p); p->ht_thread = CreateThread(NULL, 0, kmmint_plugin_broker, (LPVOID) p, CREATE_SUSPENDED, &dummy); p->state = KMM_PLUGIN_STATE_INIT; ResumeThread(p->ht_thread); _exit_post: if(csp_plugin != NULL) khc_close_space(csp_plugin); if(csp_plugins != NULL) khc_close_space(csp_plugins); _report_mr2(KHERR_INFO, MSG_IP_STATE, _dupstr(p->p.name), _int32(p->state)); _end_task(); return; /* jump here if an error condition happens before the plugin broker thread starts and the plugin should be unloaded */ _exit: if(csp_plugin != NULL) khc_close_space(csp_plugin); if(csp_plugins != NULL) khc_close_space(csp_plugins); _report_mr2(KHERR_WARNING, MSG_IP_EXITING, _dupstr(p->p.name), _int32(p->state)); _end_task(); #ifdef ASYNC_PLUGIN_UNLOAD_ON_FAILURE kmm_hold_plugin(kmm_handle_from_plugin(p)); kmq_post_message(KMSG_KMM, KMSG_KMM_I_REG, KMM_REG_EXIT_PLUGIN, (void *) p); #else kmmint_exit_plugin(p); #endif }
/*! \internal \brief Manages a plugin message thread. Each plugin gets its own plugin thread which is used to dispatch messages to the plugin. This acts as the thread function for the plugin thread.*/ DWORD WINAPI kmmint_plugin_broker(LPVOID lpParameter) { DWORD rv = 0; kmm_plugin_i * p = (kmm_plugin_i *) lpParameter; PDESCTHREAD(p->p.name, L"KMM"); _begin_task(0); _report_mr1(KHERR_NONE, MSG_PB_START, _cstr(p->p.name)); _describe(); TlsSetValue(tls_kmm, (LPVOID) p); kmm_hold_plugin(kmm_handle_from_plugin(p)); p->tid_thread = GetCurrentThreadId(); if (IsBadCodePtr(p->p.msg_proc)) { _report_mr0(KHERR_WARNING, MSG_PB_INVALID_CODE_PTR); rv = KHM_ERROR_INVALID_PARAM; } else { rv = (*p->p.msg_proc)(KMSG_SYSTEM, KMSG_SYSTEM_INIT, 0, (void *) &(p->p)); _report_mr1(KHERR_INFO, MSG_PB_INIT_RV, _int32(rv)); } /* if it fails to initialize, we exit the plugin */ if(KHM_FAILED(rv)) { kherr_report(KHERR_ERROR, (wchar_t *) MSG_PB_INIT_FAIL_S, (wchar_t *) KHERR_FACILITY, NULL, (wchar_t *) MSG_PB_INIT_FAIL, (wchar_t *) MSG_PB_INIT_FAIL_G, KHERR_FACILITY_ID, KHERR_SUGGEST_NONE, _cstr(p->p.name), _cstr(p->p.description), _cstr(p->module->path), _cstr(p->module->support), KHERR_RF_MSG_SHORT_DESC | KHERR_RF_MSG_LONG_DESC | KHERR_RF_MSG_SUGGEST #ifdef _WIN32 ,KHERR_HMODULE #endif ); _resolve(); /* exit the plugin first. Otherwise it may not uninitialize correctly */ (*p->p.msg_proc)(KMSG_SYSTEM, KMSG_SYSTEM_EXIT, 0, (void *) &(p->p)); kmmint_remove_from_plugin_queue(p); rv = 1; _end_task(); p->state = KMM_PLUGIN_STATE_FAIL_INIT; goto _exit; } /* subscribe to default message classes by plugin type */ if(p->p.type == KHM_PITYPE_CRED) { kmq_subscribe(KMSG_SYSTEM, p->p.msg_proc); kmq_subscribe(KMSG_KCDB, p->p.msg_proc); kmq_subscribe(KMSG_CRED, p->p.msg_proc); } else if(p->p.type == KHM_PITYPE_IDENT) { khm_handle h = NULL; kmq_subscribe(KMSG_SYSTEM, p->p.msg_proc); kmq_subscribe(KMSG_KCDB, p->p.msg_proc); kmq_create_subscription(p->p.msg_proc, &h); kcdb_identity_set_provider(h); /* kcdb deletes the subscription when it's done with it */ } else if(p->p.type == KHM_PITYPE_CONFIG) { /*TODO: subscribe to configuration provider messages here */ } p->state = KMM_PLUGIN_STATE_RUNNING; _report_mr0(KHERR_INFO, MSG_PB_INIT_DONE); _end_task(); /* if there were any plugins that were waiting for this one to start, we should start them too */ EnterCriticalSection(&cs_kmm); do { kmm_plugin_i * pd; int i; for(i=0; i < p->n_dependants; i++) { pd = p->dependants[i]; pd->n_unresolved--; if(pd->n_unresolved == 0) { kmmint_add_to_plugin_queue(pd); kmm_hold_plugin(kmm_handle_from_plugin(pd)); kmq_post_message(KMSG_KMM, KMSG_KMM_I_REG, KMM_REG_INIT_PLUGIN, (void *) pd); } } } while(FALSE); LeaveCriticalSection(&cs_kmm); kmmint_remove_from_plugin_queue(p); /* main message loop */ while(KHM_SUCCEEDED(kmq_dispatch(INFINITE))); /* unsubscribe from default message classes by plugin type */ if(p->p.type == KHM_PITYPE_CRED) { kmq_unsubscribe(KMSG_SYSTEM, p->p.msg_proc); kmq_unsubscribe(KMSG_KCDB, p->p.msg_proc); kmq_unsubscribe(KMSG_CRED, p->p.msg_proc); } else if (p->p.type == KHM_PITYPE_IDENT) { kmq_unsubscribe(KMSG_KCDB, p->p.msg_proc); kmq_unsubscribe(KMSG_SYSTEM, p->p.msg_proc); kcdb_identity_set_provider(NULL); } else if(p->p.type == KHM_PITYPE_CONFIG) { /*TODO: unsubscribe from configuration provider messages here */ } p->p.msg_proc(KMSG_SYSTEM, KMSG_SYSTEM_EXIT, 0, (void *) &(p->p)); _exit: if (p->state >= 0) p->state = KMM_PLUGIN_STATE_EXITED; /* the following call will automatically release the plugin */ kmq_post_message(KMSG_KMM, KMSG_KMM_I_REG, KMM_REG_EXIT_PLUGIN, (void *) p); TlsSetValue(tls_kmm, (LPVOID) 0); ExitThread(rv); /* not reached */ return rv; }
INT_PTR CALLBACK khm_cfg_plugins_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { plugin_dlg_data * d; switch(uMsg) { case WM_INITDIALOG: { kmm_plugin p; kmm_plugin pn; kmm_module m; khm_size i; LVCOLUMN lvc; RECT r; HWND hw; wchar_t buf[256]; HIMAGELIST h_ilist; HICON h_icon; d = PMALLOC(sizeof(*d)); #ifdef DEBUG assert(d); #endif ZeroMemory(d, sizeof(*d)); #pragma warning(push) #pragma warning(disable: 4244) SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR) d); #pragma warning(pop) p = NULL; i = 0; do { if (KHM_FAILED(kmm_get_next_plugin(p, &pn))) break; if (p) kmm_release_plugin(p); p = pn; #ifdef DEBUG assert(d->info[i] == NULL); #endif d->info[i] = PMALLOC(sizeof(*(d->info[i]))); #ifdef DEBUG assert(d->info[i]); #endif ZeroMemory(&d->info[i]->plugin, sizeof(d->info[i]->plugin)); if (KHM_FAILED(kmm_get_plugin_info_i(p, &d->info[i]->plugin))) { PFREE(d->info[i]); d->info[i] = NULL; break; } ZeroMemory(&d->info[i]->module, sizeof(d->info[i]->module)); if (KHM_SUCCEEDED(kmm_load_module(d->info[i]->plugin.reg.module, KMM_LM_FLAG_NOLOAD, &m))) { kmm_get_module_info_i(m, &d->info[i]->module); kmm_release_module(m); } i ++; if (i == MAX_PLUGINS) break; } while(p); if (p) kmm_release_plugin(p); d->n_info = i; /* now populate the list view */ hw = GetDlgItem(hwnd, IDC_CFG_PLUGINS); #ifdef DEBUG assert(hw); #endif h_ilist = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR8, 4, 4); h_icon = LoadImage(khm_hInstance, MAKEINTRESOURCE(IDI_CFG_PLUGIN), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); #ifdef DEBUG assert(h_icon); #endif ImageList_AddIcon(h_ilist, h_icon); DestroyIcon(h_icon); h_icon = LoadImage(khm_hInstance, MAKEINTRESOURCE(IDI_CFG_PLUGIN_DIS), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); #ifdef DEBUG assert(h_icon); #endif ImageList_AddIcon(h_ilist, h_icon); DestroyIcon(h_icon); h_icon = LoadImage(khm_hInstance, MAKEINTRESOURCE(IDI_CFG_PLUGIN_ERR), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); #ifdef DEBUG assert(h_icon); #endif ImageList_AddIcon(h_ilist, h_icon); DestroyIcon(h_icon); ListView_SetImageList(hw, h_ilist, LVSIL_STATE); ZeroMemory(&lvc, sizeof(lvc)); lvc.mask = LVCF_TEXT | LVCF_WIDTH; GetWindowRect(hw, &r); lvc.cx = ((r.right - r.left) * 95) / 100; lvc.pszText = buf; LoadString(khm_hInstance, IDS_CFG_PI_COL_PLUGINS, buf, ARRAYLENGTH(buf)); ListView_InsertColumn(hw, 0, &lvc); for(i=0; i<d->n_info; i++) { LVITEM lvi; ZeroMemory(&lvi, sizeof(lvi)); lvi.mask = LVIF_PARAM | LVIF_TEXT | LVIF_STATE; lvi.lParam = (LPARAM) d->info[i]; lvi.pszText = d->info[i]->plugin.reg.name; if (d->info[i]->plugin.flags & KMM_PLUGIN_FLAG_DISABLED) { lvi.state = INDEXTOSTATEIMAGEMASK(IDX_PLUGIN_DISABLED); } else if (d->info[i]->plugin.state < 0) { lvi.state = INDEXTOSTATEIMAGEMASK(IDX_PLUGIN_ERROR); } else { lvi.state = INDEXTOSTATEIMAGEMASK(IDX_PLUGIN_NORMAL); } ListView_InsertItem(hw, &lvi); } d->plugin_ico = (HICON) LoadImage(khm_hInstance, MAKEINTRESOURCE(IDI_CFG_PLUGIN), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR); } return FALSE; case WM_NOTIFY: { LPNMHDR lpnm; HWND hw; d = (plugin_dlg_data *) (LONG_PTR) GetWindowLongPtr(hwnd, DWLP_USER); if (d == NULL) return FALSE; if (wParam == IDC_CFG_PLUGINS && (lpnm = (LPNMHDR) lParam) && lpnm->code == LVN_ITEMCHANGED) { LVITEM lvi; hw = GetDlgItem(hwnd, IDC_CFG_PLUGINS); #ifdef DEBUG assert(hw); #endif if (ListView_GetSelectedCount(hw) != 1) { SetDlgItemText(hwnd, IDC_CFG_DESC, L""); SetDlgItemText(hwnd, IDC_CFG_STATE, L""); SetDlgItemText(hwnd, IDC_CFG_MODULE, L""); SetDlgItemText(hwnd, IDC_CFG_VENDOR, L""); SetDlgItemText(hwnd, IDC_CFG_VERSION, L""); EnableWindow(GetDlgItem(hwnd, IDC_CFG_ENABLE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_CFG_DISABLE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_CFG_UNREGISTER), FALSE); SendDlgItemMessage(hwnd, IDC_CFG_DEPS, LB_RESETCONTENT, 0, 0); SendDlgItemMessage(hwnd, IDC_CFG_ICON, STM_SETICON, (WPARAM) d->plugin_ico, 0); d->selected = NULL; } else { int idx; plugin_data * info; idx = ListView_GetNextItem(hw, -1, LVNI_SELECTED); #ifdef DEBUG assert(idx != -1); #endif ZeroMemory(&lvi, sizeof(lvi)); lvi.iItem = idx; lvi.iSubItem = 0; lvi.mask = LVIF_PARAM; ListView_GetItem(hw, &lvi); #ifdef DEBUG assert(lvi.lParam != 0); #endif info = (plugin_data *) lvi.lParam; update_dialog_fields(hwnd, d, info); } } } return TRUE; case WM_COMMAND: { d = (plugin_dlg_data *) (LONG_PTR) GetWindowLongPtr(hwnd, DWLP_USER); if (d == NULL) return FALSE; switch (wParam) { case MAKEWPARAM(IDC_CFG_ENABLE, BN_CLICKED): if (d->selected != NULL) { khui_alert * alert = NULL; wchar_t buf[KHUI_MAXCCH_MESSAGE]; wchar_t fmt[KHUI_MAXCCH_MESSAGE]; kmm_plugin p; khui_alert_create_empty(&alert); LoadString(khm_hInstance, IDS_CFG_P_ENBCNFT, fmt, ARRAYLENGTH(fmt)); StringCbPrintf(buf, sizeof(buf), fmt, d->selected->plugin.reg.name); khui_alert_set_title(alert, buf); LoadString(khm_hInstance, IDS_CFG_P_ENBCNFM, fmt, ARRAYLENGTH(fmt)); StringCbPrintf(buf, sizeof(buf), fmt, d->selected->plugin.reg.name); khui_alert_set_message(alert, buf); khui_alert_set_severity(alert, KHERR_INFO); khui_alert_show_modal(alert); kmm_enable_plugin(d->selected->plugin.h_plugin, TRUE); khui_alert_release(alert); p = d->selected->plugin.h_plugin; kmm_hold_plugin(p); kmm_release_plugin_info_i(&d->selected->plugin); kmm_get_plugin_info_i(p, &d->selected->plugin); kmm_release_plugin(p); update_dialog_fields(hwnd, d, d->selected); } break; case MAKEWPARAM(IDC_CFG_DISABLE, BN_CLICKED): if (d->selected != NULL) { khui_alert * alert = NULL; wchar_t buf[KHUI_MAXCCH_MESSAGE]; wchar_t fmt[KHUI_MAXCCH_MESSAGE]; wchar_t depends[KHUI_MAXCCH_MESSAGE]; khm_size i; kmm_plugin p; khui_alert_create_empty(&alert); #ifdef DEBUG assert(alert); #endif if (alert == NULL) break; LoadString(khm_hInstance, IDS_CFG_P_DELCNFT, fmt, ARRAYLENGTH(fmt)); StringCbPrintf(buf, sizeof(buf), fmt, d->selected->plugin.reg.name); khui_alert_set_title(alert, buf); LoadString(khm_hInstance, IDS_CFG_P_DELCNFM, fmt, ARRAYLENGTH(fmt)); StringCbPrintf(buf, sizeof(buf), fmt, d->selected->plugin.reg.name); khui_alert_set_message(alert, buf); depends[0] = L'\0'; for (i=0; i<d->n_info; i++) { wchar_t * t; t = d->info[i]->plugin.reg.dependencies; while(t) { if (!wcscmp(t, d->selected->plugin.reg.name)) { if (depends[0]) StringCbCat(depends, sizeof(depends), L", "); StringCbCat(depends, sizeof(depends), d->info[i]->plugin.reg.name); break; } t = multi_string_next(t); } } if (depends[0]) { LoadString(khm_hInstance, IDS_CFG_P_DELCNFS, fmt, ARRAYLENGTH(fmt)); StringCbPrintf(buf, sizeof(buf), fmt, depends); khui_alert_set_suggestion(alert, buf); } else { LoadString(khm_hInstance, IDS_CFG_P_DELNDEP, buf, ARRAYLENGTH(buf)); khui_alert_set_suggestion(alert, buf); } khui_alert_add_command(alert, KHUI_PACTION_YES); khui_alert_add_command(alert, KHUI_PACTION_NO); khui_alert_set_severity(alert, KHERR_WARNING); if (KHM_SUCCEEDED(khui_alert_show_modal(alert)) && alert->response == KHUI_PACTION_YES) { kmm_enable_plugin(d->selected->plugin.h_plugin, FALSE); } khui_alert_release(alert); p = d->selected->plugin.h_plugin; kmm_hold_plugin(p); kmm_release_plugin_info_i(&d->selected->plugin); kmm_get_plugin_info_i(p, &d->selected->plugin); kmm_release_plugin(p); update_dialog_fields(hwnd, d, d->selected); } break; case MAKEWPARAM(IDC_CFG_UNREGISTER, BN_CLICKED): { khui_alert * alert = NULL; wchar_t buf[KHUI_MAXCCH_MESSAGE]; wchar_t fmt[KHUI_MAXCCH_MESSAGE]; wchar_t plist[KHUI_MAXCCH_MESSAGE]; khm_size i; if (d->selected == NULL) { #ifdef DEBUG assert(FALSE); #endif break; } khui_alert_create_empty(&alert); LoadString(khm_hInstance, IDS_CFG_P_UNRCNFT, fmt, ARRAYLENGTH(fmt)); StringCbPrintf(buf, sizeof(buf), fmt, d->selected->plugin.reg.name); khui_alert_set_title(alert, buf); LoadString(khm_hInstance, IDS_CFG_P_UNRCNFM, fmt, ARRAYLENGTH(fmt)); StringCbPrintf(buf, sizeof(buf), fmt, d->selected->plugin.reg.name); khui_alert_set_message(alert, buf); plist[0] = L'\0'; for (i=0; i < d->n_info; i++) { if (!wcscmp(d->info[i]->module.reg.name, d->selected->module.reg.name)) { if (plist[0]) StringCbCat(plist, sizeof(plist), L", "); StringCbCat(plist, sizeof(plist), d->info[i]->plugin.reg.name); } } #ifdef DEBUG /* there should have been at least one plugin */ assert(plist[0]); #endif LoadString(khm_hInstance, IDS_CFG_P_UNRCNFS, fmt, ARRAYLENGTH(fmt)); StringCbPrintf(buf, sizeof(buf), fmt, plist); khui_alert_set_suggestion(alert, buf); khui_alert_add_command(alert, KHUI_PACTION_YES); khui_alert_add_command(alert, KHUI_PACTION_NO); khui_alert_set_severity(alert, KHERR_WARNING); if (KHM_SUCCEEDED(khui_alert_show_modal(alert)) && alert->response == KHUI_PACTION_YES) { kmm_unregister_module(d->selected->module.reg.name, 0); update_dialog_fields(hwnd, d, d->selected); } } break; case MAKEWPARAM(IDC_CFG_REGISTER, BN_CLICKED): { } break; } } return TRUE; case WM_DESTROY: { khm_size i; d = (plugin_dlg_data *) (LONG_PTR) GetWindowLongPtr(hwnd, DWLP_USER); #ifdef DEBUG assert(d); #endif if (d == NULL) return TRUE; for (i=0; i<d->n_info; i++) { #ifdef DEBUG assert(d->info[i]); #endif kmm_release_plugin_info_i(&d->info[i]->plugin); kmm_release_module_info_i(&d->info[i]->module); PFREE(d->info[i]); } PFREE(d); SetWindowLongPtr(hwnd, DWLP_USER, 0); khm_set_dialog_result(hwnd, 0); } return TRUE; } return FALSE; }
static kcdb_identpro_i * identpro_create_with_plugin(kmm_plugin plugin, khm_handle sub) { kmm_plugin_info pi; kcdb_identpro_i * p; size_t len; ZeroMemory(&pi, sizeof(pi)); if (KHM_FAILED(kmm_get_plugin_info_i(plugin, &pi))) return NULL; if (pi.reg.type != KHM_PITYPE_IDENT) { kmm_release_plugin_info_i(&pi); #ifdef DEBUG assert(FALSE); #endif return NULL; } if (FAILED(StringCbLength(pi.reg.name, KMM_MAXCB_NAME, &len))) return NULL; len += sizeof(wchar_t); /* we need to check if the plug-in is already there, both in the current and the deleted lists */ EnterCriticalSection(&cs_identpro); for (p = QTOP(&id_providers); p; p = QNEXT(p)) { if (p->plugin == plugin) break; } if (p == NULL) for (p = deleted_id_providers; p; p = LNEXT(p)) { if (p->plugin == plugin || !wcscmp(p->name, pi.reg.name)) break; } if (p) identpro_hold(p); if (p == NULL) { p = PMALLOC(sizeof(*p)); ZeroMemory(p, sizeof(*p)); p->magic = KCDB_IDENTPRO_MAGIC; p->name = PMALLOC(len); StringCbCopy(p->name, len, pi.reg.name); p->sub = sub; p->plugin = plugin; kmm_hold_plugin(plugin); p->refcount = 1; /* initially held */ QPUT(&id_providers, p); n_id_providers++; kcdbint_identpro_post_message(KCDB_OP_INSERT, p); } else if (p->flags & KCDB_IDENTPRO_FLAG_DELETED) { LDELETE(&deleted_id_providers, p); p->flags &= ~KCDB_IDENTPRO_FLAG_DELETED; if (p->plugin != plugin) { /* can happen if the plug-in was reloaded */ if (p->plugin) kmm_release_plugin_info_i(p->plugin); p->plugin = plugin; kmm_hold_plugin(plugin); } if (p->sub) { kmq_delete_subscription(p->sub); } p->sub = sub; QPUT(&id_providers, p); n_id_providers++; } identpro_check_and_set_default_provider(); LeaveCriticalSection(&cs_identpro); kmm_release_plugin_info_i(&pi); return p; }