]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f787e1e3 | 2 | * Global variable access routines for CUPS. |
ef416fc2 | 3 | * |
f787e1e3 MS |
4 | * Copyright 2007-2015 by Apple Inc. |
5 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. | |
ef416fc2 | 6 | * |
f787e1e3 MS |
7 | * These coded instructions, statements, and computer programs are the |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
11 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 12 | * |
f787e1e3 | 13 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include "cups-private.h" |
ef416fc2 | 21 | |
22 | ||
23 | /* | |
6d2f911b | 24 | * Local globals... |
ef416fc2 | 25 | */ |
26 | ||
a469f8a5 MS |
27 | #ifdef DEBUG |
28 | static int cups_global_index = 0; | |
29 | /* Next thread number */ | |
30 | #endif /* DEBUG */ | |
6d2f911b MS |
31 | static _cups_threadkey_t cups_globals_key = _CUPS_THREADKEY_INITIALIZER; |
32 | /* Thread local storage key */ | |
33 | #ifdef HAVE_PTHREAD_H | |
34 | static pthread_once_t cups_globals_key_once = PTHREAD_ONCE_INIT; | |
35 | /* One-time initialization object */ | |
36 | #endif /* HAVE_PTHREAD_H */ | |
f3c17241 | 37 | #if defined(HAVE_PTHREAD_H) || defined(WIN32) |
6d2f911b MS |
38 | static _cups_mutex_t cups_global_mutex = _CUPS_MUTEX_INITIALIZER; |
39 | /* Global critical section */ | |
f3c17241 | 40 | #endif /* HAVE_PTHREAD_H || WIN32 */ |
f8b3a85b | 41 | |
f8b3a85b | 42 | |
6d2f911b MS |
43 | /* |
44 | * Local functions... | |
45 | */ | |
f8b3a85b | 46 | |
12f89d24 MS |
47 | #ifdef WIN32 |
48 | static void cups_fix_path(char *path); | |
49 | #endif /* WIN32 */ | |
6d2f911b | 50 | static _cups_globals_t *cups_globals_alloc(void); |
f3c17241 | 51 | #if defined(HAVE_PTHREAD_H) || defined(WIN32) |
6d2f911b | 52 | static void cups_globals_free(_cups_globals_t *g); |
f3c17241 | 53 | #endif /* HAVE_PTHREAD_H || WIN32 */ |
6d2f911b MS |
54 | #ifdef HAVE_PTHREAD_H |
55 | static void cups_globals_init(void); | |
56 | #endif /* HAVE_PTHREAD_H */ | |
f8b3a85b | 57 | |
f8b3a85b | 58 | |
6d2f911b MS |
59 | /* |
60 | * '_cupsGlobalLock()' - Lock the global mutex. | |
61 | */ | |
f8b3a85b | 62 | |
6d2f911b MS |
63 | void |
64 | _cupsGlobalLock(void) | |
65 | { | |
66 | #ifdef HAVE_PTHREAD_H | |
67 | pthread_mutex_lock(&cups_global_mutex); | |
68 | #elif defined(WIN32) | |
cc754834 | 69 | EnterCriticalSection(&cups_global_mutex.m_criticalSection); |
6d2f911b MS |
70 | #endif /* HAVE_PTHREAD_H */ |
71 | } | |
f8b3a85b | 72 | |
f8b3a85b | 73 | |
6d2f911b MS |
74 | /* |
75 | * '_cupsGlobals()' - Return a pointer to thread local storage | |
76 | */ | |
f8b3a85b | 77 | |
6d2f911b MS |
78 | _cups_globals_t * /* O - Pointer to global data */ |
79 | _cupsGlobals(void) | |
80 | { | |
81 | _cups_globals_t *cg; /* Pointer to global data */ | |
f8b3a85b | 82 | |
f8b3a85b | 83 | |
6d2f911b MS |
84 | #ifdef HAVE_PTHREAD_H |
85 | /* | |
86 | * Initialize the global data exactly once... | |
87 | */ | |
88 | ||
89 | pthread_once(&cups_globals_key_once, cups_globals_init); | |
90 | #endif /* HAVE_PTHREAD_H */ | |
91 | ||
92 | /* | |
93 | * See if we have allocated the data yet... | |
94 | */ | |
95 | ||
96 | if ((cg = (_cups_globals_t *)_cupsThreadGetData(cups_globals_key)) == NULL) | |
5180a04c MS |
97 | { |
98 | /* | |
6d2f911b | 99 | * No, allocate memory as set the pointer for the key... |
5180a04c | 100 | */ |
ef416fc2 | 101 | |
6d2f911b MS |
102 | if ((cg = cups_globals_alloc()) != NULL) |
103 | _cupsThreadSetData(cups_globals_key, cg); | |
5180a04c | 104 | } |
ef416fc2 | 105 | |
6d2f911b MS |
106 | /* |
107 | * Return the pointer to the data... | |
108 | */ | |
ef416fc2 | 109 | |
6d2f911b MS |
110 | return (cg); |
111 | } | |
5180a04c | 112 | |
5180a04c | 113 | |
6d2f911b MS |
114 | /* |
115 | * '_cupsGlobalUnlock()' - Unlock the global mutex. | |
116 | */ | |
5180a04c | 117 | |
6d2f911b MS |
118 | void |
119 | _cupsGlobalUnlock(void) | |
120 | { | |
121 | #ifdef HAVE_PTHREAD_H | |
122 | pthread_mutex_unlock(&cups_global_mutex); | |
123 | #elif defined(WIN32) | |
cc754834 | 124 | LeaveCriticalSection(&cups_global_mutex.m_criticalSection); |
6d2f911b | 125 | #endif /* HAVE_PTHREAD_H */ |
ef416fc2 | 126 | } |
127 | ||
128 | ||
6d2f911b | 129 | #ifdef WIN32 |
ef416fc2 | 130 | /* |
6d2f911b | 131 | * 'DllMain()' - Main entry for library. |
ef416fc2 | 132 | */ |
133 | ||
6d2f911b MS |
134 | BOOL WINAPI /* O - Success/failure */ |
135 | DllMain(HINSTANCE hinst, /* I - DLL module handle */ | |
136 | DWORD reason, /* I - Reason */ | |
137 | LPVOID reserved) /* I - Unused */ | |
138 | { | |
139 | _cups_globals_t *cg; /* Global data */ | |
ef416fc2 | 140 | |
ef416fc2 | 141 | |
6d2f911b MS |
142 | (void)hinst; |
143 | (void)reserved; | |
ef416fc2 | 144 | |
6d2f911b MS |
145 | switch (reason) |
146 | { | |
147 | case DLL_PROCESS_ATTACH : /* Called on library initialization */ | |
cc754834 | 148 | InitializeCriticalSection(&cups_global_mutex.m_criticalSection); |
6d2f911b MS |
149 | |
150 | if ((cups_globals_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) | |
151 | return (FALSE); | |
152 | break; | |
153 | ||
154 | case DLL_THREAD_DETACH : /* Called when a thread terminates */ | |
155 | if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL) | |
156 | cups_globals_free(cg); | |
157 | break; | |
158 | ||
159 | case DLL_PROCESS_DETACH : /* Called when library is unloaded */ | |
160 | if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL) | |
161 | cups_globals_free(cg); | |
ef416fc2 | 162 | |
6d2f911b | 163 | TlsFree(cups_globals_key); |
cc754834 | 164 | DeleteCriticalSection(&cups_global_mutex.m_criticalSection); |
6d2f911b MS |
165 | break; |
166 | ||
167 | default: | |
168 | break; | |
169 | } | |
170 | ||
171 | return (TRUE); | |
172 | } | |
173 | #endif /* WIN32 */ | |
ef416fc2 | 174 | |
175 | ||
176 | /* | |
6d2f911b | 177 | * 'cups_globals_alloc()' - Allocate and initialize global data. |
ef416fc2 | 178 | */ |
179 | ||
6d2f911b MS |
180 | static _cups_globals_t * /* O - Pointer to global data */ |
181 | cups_globals_alloc(void) | |
ef416fc2 | 182 | { |
6d2f911b MS |
183 | _cups_globals_t *cg = malloc(sizeof(_cups_globals_t)); |
184 | /* Pointer to global data */ | |
185 | #ifdef WIN32 | |
186 | HKEY key; /* Registry key */ | |
187 | DWORD size; /* Size of string */ | |
12f89d24 MS |
188 | static char installdir[1024] = "", /* Install directory */ |
189 | confdir[1024] = "", /* Server root directory */ | |
190 | localedir[1024] = ""; /* Locale directory */ | |
6d2f911b MS |
191 | #endif /* WIN32 */ |
192 | ||
ef416fc2 | 193 | |
6d2f911b MS |
194 | if (!cg) |
195 | return (NULL); | |
ef416fc2 | 196 | |
5180a04c | 197 | /* |
6d2f911b MS |
198 | * Clear the global storage and set the default encryption and password |
199 | * callback values... | |
ef416fc2 | 200 | */ |
201 | ||
6d2f911b | 202 | memset(cg, 0, sizeof(_cups_globals_t)); |
c1420c87 MS |
203 | cg->encryption = (http_encryption_t)-1; |
204 | cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; | |
3abb875b MS |
205 | cg->any_root = -1; |
206 | cg->expired_certs = -1; | |
207 | cg->validate_certs = -1; | |
ef416fc2 | 208 | |
a469f8a5 MS |
209 | #ifdef DEBUG |
210 | /* | |
211 | * Friendly thread ID for debugging... | |
212 | */ | |
213 | ||
214 | cg->thread_id = ++ cups_global_index; | |
215 | #endif /* DEBUG */ | |
216 | ||
ef416fc2 | 217 | /* |
6d2f911b MS |
218 | * Then set directories as appropriate... |
219 | */ | |
220 | ||
221 | #ifdef WIN32 | |
12f89d24 | 222 | if (!installdir[0]) |
ef416fc2 | 223 | { |
224 | /* | |
12f89d24 | 225 | * Open the registry... |
ef416fc2 | 226 | */ |
227 | ||
5a9febac | 228 | strlcpy(installdir, "C:/Program Files/cups.org", sizeof(installdir)); |
12f89d24 MS |
229 | |
230 | if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, | |
231 | &key)) | |
232 | { | |
233 | /* | |
234 | * Grab the installation directory... | |
235 | */ | |
236 | ||
237 | char *ptr; /* Pointer into installdir */ | |
238 | ||
239 | size = sizeof(installdir); | |
240 | RegQueryValueEx(key, "installdir", NULL, NULL, installdir, &size); | |
241 | RegCloseKey(key); | |
242 | ||
243 | for (ptr = installdir; *ptr;) | |
244 | { | |
245 | if (*ptr == '\\') | |
246 | { | |
247 | if (ptr[1]) | |
248 | *ptr++ = '/'; | |
249 | else | |
250 | *ptr = '\0'; /* Strip trailing \ */ | |
251 | } | |
252 | else if (*ptr == '/' && !ptr[1]) | |
253 | *ptr = '\0'; /* Strip trailing / */ | |
254 | else | |
255 | ptr ++; | |
256 | } | |
257 | } | |
258 | ||
259 | snprintf(confdir, sizeof(confdir), "%s/conf", installdir); | |
260 | snprintf(localedir, sizeof(localedir), "%s/locale", installdir); | |
6d2f911b MS |
261 | } |
262 | ||
6d2f911b MS |
263 | if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL) |
264 | cg->cups_datadir = installdir; | |
ef416fc2 | 265 | |
6d2f911b MS |
266 | if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) |
267 | cg->cups_serverbin = installdir; | |
268 | ||
269 | if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) | |
270 | cg->cups_serverroot = confdir; | |
271 | ||
272 | if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL) | |
273 | cg->cups_statedir = confdir; | |
274 | ||
275 | if ((cg->localedir = getenv("LOCALEDIR")) == NULL) | |
276 | cg->localedir = localedir; | |
277 | ||
278 | #else | |
279 | # ifdef HAVE_GETEUID | |
280 | if ((geteuid() != getuid() && getuid()) || getegid() != getgid()) | |
281 | # else | |
282 | if (!getuid()) | |
283 | # endif /* HAVE_GETEUID */ | |
284 | { | |
ef416fc2 | 285 | /* |
6d2f911b MS |
286 | * When running setuid/setgid, don't allow environment variables to override |
287 | * the directories... | |
ef416fc2 | 288 | */ |
289 | ||
6d2f911b MS |
290 | cg->cups_datadir = CUPS_DATADIR; |
291 | cg->cups_serverbin = CUPS_SERVERBIN; | |
292 | cg->cups_serverroot = CUPS_SERVERROOT; | |
293 | cg->cups_statedir = CUPS_STATEDIR; | |
294 | cg->localedir = CUPS_LOCALEDIR; | |
ef416fc2 | 295 | } |
6d2f911b MS |
296 | else |
297 | { | |
298 | /* | |
299 | * Allow directories to be overridden by environment variables. | |
300 | */ | |
ef416fc2 | 301 | |
6d2f911b MS |
302 | if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL) |
303 | cg->cups_datadir = CUPS_DATADIR; | |
ef416fc2 | 304 | |
6d2f911b MS |
305 | if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) |
306 | cg->cups_serverbin = CUPS_SERVERBIN; | |
ef416fc2 | 307 | |
6d2f911b MS |
308 | if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) |
309 | cg->cups_serverroot = CUPS_SERVERROOT; | |
ef416fc2 | 310 | |
6d2f911b MS |
311 | if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL) |
312 | cg->cups_statedir = CUPS_STATEDIR; | |
ef416fc2 | 313 | |
6d2f911b MS |
314 | if ((cg->localedir = getenv("LOCALEDIR")) == NULL) |
315 | cg->localedir = CUPS_LOCALEDIR; | |
316 | } | |
317 | #endif /* WIN32 */ | |
318 | ||
319 | return (cg); | |
ef416fc2 | 320 | } |
321 | ||
322 | ||
323 | /* | |
6d2f911b | 324 | * 'cups_globals_free()' - Free global data. |
ef416fc2 | 325 | */ |
326 | ||
f3c17241 | 327 | #if defined(HAVE_PTHREAD_H) || defined(WIN32) |
ef416fc2 | 328 | static void |
6d2f911b | 329 | cups_globals_free(_cups_globals_t *cg) /* I - Pointer to global data */ |
ef416fc2 | 330 | { |
dcb445bc | 331 | _cups_buffer_t *buffer, /* Current read/write buffer */ |
1f6f3dbc | 332 | *next; /* Next buffer */ |
fa73b229 | 333 | |
334 | ||
e53920b9 | 335 | if (cg->last_status_message) |
5f64df29 | 336 | _cupsStrFree(cg->last_status_message); |
e53920b9 | 337 | |
dcb445bc | 338 | for (buffer = cg->cups_buffers; buffer; buffer = next) |
1f6f3dbc MS |
339 | { |
340 | next = buffer->next; | |
341 | free(buffer); | |
342 | } | |
343 | ||
c168a833 | 344 | cupsArrayDelete(cg->leg_size_lut); |
f14324a7 MS |
345 | cupsArrayDelete(cg->ppd_size_lut); |
346 | cupsArrayDelete(cg->pwg_size_lut); | |
c168a833 | 347 | |
6d2f911b | 348 | httpClose(cg->http); |
ef416fc2 | 349 | |
7d5824d6 | 350 | #ifdef HAVE_SSL |
7cf5915e | 351 | _httpFreeCredentials(cg->tls_credentials); |
7d5824d6 | 352 | #endif /* HAVE_SSL */ |
7cf5915e | 353 | |
6d2f911b MS |
354 | cupsFileClose(cg->stdio_files[0]); |
355 | cupsFileClose(cg->stdio_files[1]); | |
356 | cupsFileClose(cg->stdio_files[2]); | |
ef416fc2 | 357 | |
6d2f911b | 358 | cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings); |
ef416fc2 | 359 | |
6d2f911b MS |
360 | free(cg); |
361 | } | |
f3c17241 | 362 | #endif /* HAVE_PTHREAD_H || WIN32 */ |
6d2f911b MS |
363 | |
364 | ||
365 | #ifdef HAVE_PTHREAD_H | |
ef416fc2 | 366 | /* |
6d2f911b | 367 | * 'cups_globals_init()' - Initialize environment variables. |
ef416fc2 | 368 | */ |
369 | ||
6d2f911b MS |
370 | static void |
371 | cups_globals_init(void) | |
ef416fc2 | 372 | { |
ef416fc2 | 373 | /* |
6d2f911b | 374 | * Register the global data for this thread... |
ef416fc2 | 375 | */ |
376 | ||
6d2f911b | 377 | pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free); |
ef416fc2 | 378 | } |
379 | #endif /* HAVE_PTHREAD_H */ |