Moved apache code into a folder to help prepare for packaging where we dont want...
[httpd-crcsyncproxy.git] / apache / os / win32 / util_win32.c
blobab96ff113675c17398284550a12762ee81dea97d
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_strings.h"
18 #include "arch/win32/apr_arch_file_io.h"
19 #include "arch/win32/apr_arch_misc.h"
21 #include "httpd.h"
22 #include "http_log.h"
24 #include <stdarg.h>
25 #include <time.h>
26 #include <stdlib.h>
29 AP_DECLARE(apr_status_t) ap_os_proc_filepath(char **binpath, apr_pool_t *p)
31 apr_wchar_t wbinpath[APR_PATH_MAX];
33 #if APR_HAS_UNICODE_FS
34 IF_WIN_OS_IS_UNICODE
36 apr_size_t binlen;
37 apr_size_t wbinlen;
38 apr_status_t rv;
39 if (!GetModuleFileNameW(NULL, wbinpath, sizeof(wbinpath)
40 / sizeof(apr_wchar_t))) {
41 return apr_get_os_error();
43 wbinlen = wcslen(wbinpath) + 1;
44 binlen = (wbinlen - 1) * 3 + 1;
45 *binpath = apr_palloc(p, binlen);
46 rv = apr_conv_ucs2_to_utf8(wbinpath, &wbinlen, *binpath, &binlen);
47 if (rv != APR_SUCCESS)
48 return rv;
49 else if (wbinlen)
50 return APR_ENAMETOOLONG;
52 #endif /* APR_HAS_UNICODE_FS */
53 #if APR_HAS_ANSI_FS
54 ELSE_WIN_OS_IS_ANSI
56 /* share the same scratch buffer */
57 char *pathbuf = (char*) wbinpath;
58 if (!GetModuleFileName(NULL, pathbuf, sizeof(wbinpath))) {
59 return apr_get_os_error();
61 *binpath = apr_pstrdup(p, pathbuf);
63 #endif
64 return APR_SUCCESS;
68 AP_DECLARE(apr_status_t) ap_os_create_privileged_process(
69 const request_rec *r,
70 apr_proc_t *newproc, const char *progname,
71 const char * const *args,
72 const char * const *env,
73 apr_procattr_t *attr, apr_pool_t *p)
75 return apr_proc_create(newproc, progname, args, env, attr, p);
79 /* This code is stolen from misc/win32/misc.c and apr_private.h
80 * This helper code resolves late bound entry points
81 * missing from one or more releases of the Win32 API...
82 * but it sure would be nice if we didn't duplicate this code
83 * from the APR ;-)
85 static const char* const lateDllName[DLL_defined] = {
86 "kernel32", "advapi32", "mswsock", "ws2_32" };
87 static HMODULE lateDllHandle[DLL_defined] = {
88 NULL, NULL, NULL, NULL };
91 FARPROC ap_load_dll_func(ap_dlltoken_e fnLib, char* fnName, int ordinal)
93 if (!lateDllHandle[fnLib]) {
94 lateDllHandle[fnLib] = LoadLibrary(lateDllName[fnLib]);
95 if (!lateDllHandle[fnLib])
96 return NULL;
98 if (ordinal)
99 return GetProcAddress(lateDllHandle[fnLib], (char *) ordinal);
100 else
101 return GetProcAddress(lateDllHandle[fnLib], fnName);
105 /* To share the semaphores with other processes, we need a NULL ACL
106 * Code from MS KB Q106387
108 PSECURITY_ATTRIBUTES GetNullACL()
110 PSECURITY_DESCRIPTOR pSD;
111 PSECURITY_ATTRIBUTES sa;
113 sa = (PSECURITY_ATTRIBUTES) LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES));
114 sa->nLength = sizeof(sizeof(SECURITY_ATTRIBUTES));
116 pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
117 sa->lpSecurityDescriptor = pSD;
119 if (pSD == NULL || sa == NULL) {
120 return NULL;
122 apr_set_os_error(0);
123 if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
124 || apr_get_os_error()) {
125 LocalFree( pSD );
126 LocalFree( sa );
127 return NULL;
129 if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE)
130 || apr_get_os_error()) {
131 LocalFree( pSD );
132 LocalFree( sa );
133 return NULL;
136 sa->bInheritHandle = FALSE;
137 return sa;
141 void CleanNullACL(void *sa)
143 if (sa) {
144 LocalFree(((PSECURITY_ATTRIBUTES)sa)->lpSecurityDescriptor);
145 LocalFree(sa);