📄 registry.c
字号:
* ap_registry_store_key_int() if it cannot open this key. This * function is intended to be called by ap_registry_store_key_int() if * the Apache key does not exist when it comes to store a data item. * * Returns 0 on success or -1 on error. If -1 is returned, the error will * already have been logged. */static int ap_registry_create_key(char *longkey){ int index; HKEY hKey; HKEY hKeyNext; int retval; int rv; char *key; hKey = HKEY_LOCAL_MACHINE; index = 0; retval = 0; /* Walk the tree, creating at each stage if necessary */ while (key=ap_registry_parse_key(index,longkey)) { int result; rv = RegCreateKeyEx(hKey, key, /* subkey */ 0, /* reserved */ NULL, /* class */ REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKeyNext, &result); if (rv != ERROR_SUCCESS) { do_error(rv, "RegCreateKeyEx(%s)", longkey); retval = -4; } /* Close the old key */ rv = RegCloseKey(hKey); if (rv != ERROR_SUCCESS) { do_error(rv, "RegCloseKey", NULL); if (retval == 0) { /* Keep error status from RegCreateKeyEx, if any */ retval = -4; } } if (retval) { break; } free(key); hKey = hKeyNext; index++; } if (!key) { /* Close the final key we opened, if we walked the entire * tree */ rv = RegCloseKey(hKey); if (rv != ERROR_SUCCESS) { do_error(rv, "RegCloseKey", NULL); if (retval == 0) { /* Keep error status from RegCreateKeyEx, if any */ retval = -4; } } } else free(key); return retval;}/* * ap_registry_store_key_int() stores a value name and value under the * Apache registry key. If the Apache key does not exist it is created * first. This function is intended to be called from a wrapper function * in this file to set particular data values, such as * ap_registry_set_server_root() below. * * Returns 0 if the value name and data was stored successfully, or * returns -1 if the Apache key does not exist (since we try to create * this key, this should never happen), or -4 if any other error occurred * (these values are consistent with ap_registry_get_key_int()). * If the return value is negative then the error will already have been * logged via aplog_error(). */static int ap_registry_store_key_int(char *key, char *name, DWORD type, void *value, int value_size){ long rv; HKEY hKey; int retval; rv = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_WRITE, &hKey); if (rv == ERROR_FILE_NOT_FOUND) { /* Key could not be opened -- try to create it */ if (ap_registry_create_key(key) < 0) { /* Creation failed (error already reported) */ return -4; } /* Now it has been created we should be able to open it */ rv = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_WRITE, &hKey); if (rv == ERROR_FILE_NOT_FOUND) { ap_log_error(APLOG_MARK,APLOG_WARNING|APLOG_NOERRNO,NULL, "Registry does not contain key %s after creation",key); return -1; } } if (rv != ERROR_SUCCESS) { do_error(rv, "RegOpenKeyEx HKLM\\%s", key); return -4; } /* Now set the value and data */ rv = RegSetValueEx(hKey, name, /* value key name */ 0, /* reserved */ type, /* type */ value, /* value data */ (DWORD)value_size); /* for size of "value" */ retval = 0; /* Return value */ if (rv != ERROR_SUCCESS) { do_error(rv, "RegQueryValueEx(key %s)", key); retval = -4; } else { ap_log_error(APLOG_MARK,APLOG_INFO|APLOG_NOERRNO,NULL, "Registry stored HKLM\\" REGKEY "\\%s value %s", key, type == REG_SZ ? value : "(not displayable)"); } /* Make sure we close the key even if there was an error storing * the data */ rv = RegCloseKey(hKey); if (rv != ERROR_SUCCESS) { do_error(rv, "RegCloseKey HKLM\\%s", key); if (retval == 0) { /* Keep error status from RegQueryValueEx, if any */ retval = -4; } } return retval;}/* * Sets the serverroot value within the registry. Returns 0 on success * or -1 on error. If -1 is return the error will already have been * logged via aplog_error(). */int ap_registry_set_server_root(char *dir){ int rv; rv = ap_registry_store_key_int(REGKEY, "ServerRoot", REG_SZ, dir, strlen(dir)+1); return rv < 0 ? -1 : 0;}/* Creates and fills array pointed to by parray with the requested registry string * * Returns 0 on success, machine specific error code on error */int ap_registry_get_array(pool *p, char *key, char *name, array_header **pparray){ char *pValue; char *tmp; char **newelem; int ret; int nSize = 0; ret = ap_registry_get_key_int(p, key, name, NULL, 0, &pValue); if (ret < 0) return ret; tmp = pValue; if ((ret > 2) && (tmp[0] || tmp[1])) nSize = 1; /* Element Count */ while ((tmp < pValue + ret) && (tmp[0] || tmp[1])) { if (!tmp[0]) ++nSize; ++tmp; } *pparray = ap_make_array(p, nSize, sizeof(char *)); tmp = pValue; if (tmp[0] || tmp[1]) { newelem = (char **) ap_push_array(*pparray); *newelem = tmp; } while ((tmp < pValue + ret) && (tmp[0] || tmp[1])) { if (!tmp[0]) { newelem = (char **) ap_push_array(*pparray); *newelem = tmp + 1; } ++tmp; } return nSize;}int ap_registry_get_service_args(pool *p, int *argc, char ***argv, char *display_name){ int ret; array_header *parray; char *key = ap_get_service_key(display_name); ret = ap_registry_get_array(p, key, "ConfigArgs", &parray); if (ret > 0) { *argc = parray->nelts; *argv = (char**) parray->elts; } else { *argc = 0; *argv = NULL; } free(key); return ret;}int ap_registry_store_array(pool *p, char *key, char *name, int nelts, char **elts){ int bufsize, i; char *buf, *tmp; bufsize = 1; /* For trailing second null */ for (i = 0; i < nelts; ++i) { bufsize += strlen(elts[i]) + 1; } if (!nelts) ++bufsize; buf = ap_palloc(p, bufsize); tmp = buf; for (i = 0; i < nelts; ++i) { strcpy(tmp, elts[i]); tmp += strlen(elts[i]) + 1; } if (!nelts) *(tmp++) = '\0'; *(tmp++) = '\0'; /* Trailing second null */ return ap_registry_store_key_int(key, name, REG_MULTI_SZ, buf, tmp - buf);}int ap_registry_set_service_args(pool *p, int argc, char **argv, char *display_name){ int ret; char *key = ap_get_service_key(display_name); ret = ap_registry_store_array(p, key, "ConfigArgs", argc, argv); free(key); return ret;}#endif /* WIN32 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -