Apache HTTPD
start.c
Go to the documentation of this file.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "apr_private.h"
18#include "apr_general.h"
19#include "apr_pools.h"
20#include "apr_signal.h"
21#include "shellapi.h"
22
23#include "apr_arch_misc.h" /* for WSAHighByte / WSALowByte */
24#include "wchar.h"
25#include "apr_arch_file_io.h" /* bring in unicode-ness */
26#include "apr_arch_threadproc.h" /* bring in apr_threadproc_init */
27#include "assert.h"
28
29/* This symbol is _private_, although it must be exported.
30 */
32
33#if !defined(_WIN32_WCE)
34/* Used by apr_app_initialize to reprocess the environment
35 *
36 * An internal apr function to convert a double-null terminated set
37 * of single-null terminated strings from wide Unicode to narrow utf-8
38 * as a list of strings. These are allocated from the MSVCRT's
39 * _CRT_BLOCK to trick the system into trusting our store.
40 */
41static int warrsztoastr(const char * const * *retarr,
42 const wchar_t * arrsz)
43{
44 const apr_wchar_t *wch;
48 char **env;
49 char *pstrs;
50 char *strs;
51 int arg, args;
52
53 for (args = 1, wch = arrsz; wch[0] || wch[1]; ++wch)
54 if (!*wch)
55 ++args;
56 wsize = 1 + wch - arrsz;
57
58 /* This is a safe max allocation, we will alloc each
59 * string exactly after processing and return this
60 * temporary buffer to the free store.
61 * 3 ucs bytes hold any single wchar_t value (16 bits)
62 * 4 ucs bytes will hold a wchar_t pair value (20 bits)
63 */
64 newlen = totlen = wsize * 3 + 1;
65 pstrs = strs = apr_malloc_dbg(newlen * sizeof(char),
67
69
70 assert(newlen && !wsize);
71
72 *retarr = env = apr_malloc_dbg((args + 1) * sizeof(char*),
74 for (arg = 0; arg < args; ++arg) {
75 char* p = pstrs;
76 int len = 0;
77 while (*p++)
78 ++len;
79 len += 1;
80
81 *env = apr_malloc_dbg(len * sizeof(char),
83 memcpy(*env, pstrs, len * sizeof(char));
84
85 pstrs += len;
86 ++env;
87 }
88
89 *env = NULL;
90 free(strs);
91
92 return args;
93}
94#endif
95
96/* Reprocess the arguments to main() for a completely apr-ized application
97 */
98
100 const char * const * *argv,
101 const char * const * *env)
102{
104
105 if (rv != APR_SUCCESS) {
106 return rv;
107 }
108
109#if defined(_WIN32_WCE)
111#elif APR_HAS_UNICODE_FS
113 {
116 int wstrc;
117 int dupenv;
118
120 return rv;
121 }
122
124
126 if (sysstr) {
128 if (wstrs) {
131 }
132 }
133
136
137 if (env) {
138 *env = apr_malloc_dbg((dupenv + 1) * sizeof (char *),
140 memcpy((void*)*env, _environ, (dupenv + 1) * sizeof (char *));
141 }
142 else {
143 }
144
146
147 /* MSVCRT will attempt to maintain the wide environment calls
148 * on _putenv(), which is bogus if we've passed a non-ascii
149 * string to _putenv(), since they use MultiByteToWideChar
150 * and breaking the implicit utf-8 assumption we've built.
151 *
152 * Reset _wenviron for good measure.
153 */
154 if (_wenviron) {
156 _wenviron = NULL;
157 free(wenv);
158 }
159
160 }
161#endif
162 return rv;
163}
164
165static int initialized = 0;
166
167/* Provide to win32/thread.c */
168extern DWORD tls_apr_thread;
169
171{
176 int err;
178
179 if (initialized++) {
180 return APR_SUCCESS;
181 }
182
183 /* Initialize apr_os_level global */
185 return APR_EEXIST;
186 }
187
190 return status;
191
193 return APR_ENOPOOL;
194 }
195
196 apr_pool_tag(pool, "apr_initialize");
197
200 if (err) {
201 return err;
202 }
203 if (LOBYTE(wsaData.wVersion) != WSAHighByte ||
204 HIBYTE(wsaData.wVersion) != WSALowByte) {
205 WSACleanup();
206 return APR_EEXIST;
207 }
208
210
212
213 return APR_SUCCESS;
214}
215
217{
218 initialized--;
219 if (initialized) {
220 return;
221 }
223
224 WSACleanup();
225
227}
228
230{
232}
const char apr_size_t len
Definition ap_regex.h:187
apr_uint16_t apr_wchar_t
APR Miscellaneous library routines.
APR memory allocation.
APR Signal Handling.
void const char * arg
Definition http_vhost.h:63
OSVERSIONINFO osver
#define APR_ENOPOOL
Definition apr_errno.h:290
#define APR_EEXIST
Definition apr_errno.h:648
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_pool_t int argc
Definition apr_getopt.h:104
char const *const char const *const ** env
APR_DECLARE(void)
Definition start.c:200
APR_DECLARE_NONSTD(void)
Definition start.c:173
#define apr_pool_create(newpool, parent)
Definition apr_pools.h:322
void apr_signal_init(apr_pool_t *pglobal)
Definition signals.c:34
apr_int32_t apr_int32_t apr_int32_t err
const char const char *const * args
int int status
apr_pool_t * p
Definition md_event.c:32
const char * argv[3]
return NULL
Definition mod_so.c:359
static int initialized
Definition start.c:39
#define WSAHighByte
apr_oslevel_e
#define IF_WIN_OS_IS_UNICODE
#define WSALowByte
static APR_INLINE void * apr_malloc_dbg(size_t size, const char *filename, int linenumber)
#define CommandLineToArgvW
apr_status_t apr_get_oslevel(apr_oslevel_e *)
Definition misc.c:26
typedef DWORD(WINAPI *apr_winapi_fpt_GetCompressedFileSizeA)(IN LPCSTR lpFileName
int apr_wastrtoastr(char const *const **retarr, wchar_t const *const *arr, int args)
Definition internal.c:40
apr_status_t apr_threadproc_init(apr_pool_t *pool)
Definition proc.c:441
DWORD tls_apr_thread
Definition thread.c:29
int APR_DECLARE_DATA apr_app_init_complete
Definition start.c:31
static int warrsztoastr(const char *const **retarr, const wchar_t *arrsz)
Definition start.c:41