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