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