]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f787e1e3 | 2 | * Global variable access routines for CUPS. |
ef416fc2 | 3 | * |
e2eb28cf MS |
4 | * Copyright © 2007-2019 by Apple Inc. |
5 | * Copyright © 1997-2007 by Easy Software Products, all rights reserved. | |
ef416fc2 | 6 | * |
e2eb28cf MS |
7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more |
8 | * information. | |
ef416fc2 | 9 | */ |
10 | ||
11 | /* | |
12 | * Include necessary headers... | |
13 | */ | |
14 | ||
71e16022 | 15 | #include "cups-private.h" |
e2eb28cf MS |
16 | #ifndef _WIN32 |
17 | # include <pwd.h> | |
18 | #endif /* !_WIN32 */ | |
ef416fc2 | 19 | |
20 | ||
21 | /* | |
6d2f911b | 22 | * Local globals... |
ef416fc2 | 23 | */ |
24 | ||
a469f8a5 MS |
25 | #ifdef DEBUG |
26 | static int cups_global_index = 0; | |
27 | /* Next thread number */ | |
28 | #endif /* DEBUG */ | |
6d2f911b MS |
29 | static _cups_threadkey_t cups_globals_key = _CUPS_THREADKEY_INITIALIZER; |
30 | /* Thread local storage key */ | |
31 | #ifdef HAVE_PTHREAD_H | |
32 | static pthread_once_t cups_globals_key_once = PTHREAD_ONCE_INIT; | |
33 | /* One-time initialization object */ | |
34 | #endif /* HAVE_PTHREAD_H */ | |
19dc16f7 | 35 | #if defined(HAVE_PTHREAD_H) || defined(_WIN32) |
6d2f911b MS |
36 | static _cups_mutex_t cups_global_mutex = _CUPS_MUTEX_INITIALIZER; |
37 | /* Global critical section */ | |
19dc16f7 | 38 | #endif /* HAVE_PTHREAD_H || _WIN32 */ |
f8b3a85b | 39 | |
f8b3a85b | 40 | |
6d2f911b MS |
41 | /* |
42 | * Local functions... | |
43 | */ | |
f8b3a85b | 44 | |
19dc16f7 | 45 | #ifdef _WIN32 |
12f89d24 | 46 | static void cups_fix_path(char *path); |
19dc16f7 | 47 | #endif /* _WIN32 */ |
6d2f911b | 48 | static _cups_globals_t *cups_globals_alloc(void); |
19dc16f7 | 49 | #if defined(HAVE_PTHREAD_H) || defined(_WIN32) |
6d2f911b | 50 | static void cups_globals_free(_cups_globals_t *g); |
19dc16f7 | 51 | #endif /* HAVE_PTHREAD_H || _WIN32 */ |
6d2f911b MS |
52 | #ifdef HAVE_PTHREAD_H |
53 | static void cups_globals_init(void); | |
54 | #endif /* HAVE_PTHREAD_H */ | |
f8b3a85b | 55 | |
f8b3a85b | 56 | |
6d2f911b MS |
57 | /* |
58 | * '_cupsGlobalLock()' - Lock the global mutex. | |
59 | */ | |
f8b3a85b | 60 | |
6d2f911b MS |
61 | void |
62 | _cupsGlobalLock(void) | |
63 | { | |
64 | #ifdef HAVE_PTHREAD_H | |
65 | pthread_mutex_lock(&cups_global_mutex); | |
19dc16f7 | 66 | #elif defined(_WIN32) |
cc754834 | 67 | EnterCriticalSection(&cups_global_mutex.m_criticalSection); |
6d2f911b MS |
68 | #endif /* HAVE_PTHREAD_H */ |
69 | } | |
f8b3a85b | 70 | |
f8b3a85b | 71 | |
6d2f911b MS |
72 | /* |
73 | * '_cupsGlobals()' - Return a pointer to thread local storage | |
74 | */ | |
f8b3a85b | 75 | |
6d2f911b MS |
76 | _cups_globals_t * /* O - Pointer to global data */ |
77 | _cupsGlobals(void) | |
78 | { | |
79 | _cups_globals_t *cg; /* Pointer to global data */ | |
f8b3a85b | 80 | |
f8b3a85b | 81 | |
6d2f911b MS |
82 | #ifdef HAVE_PTHREAD_H |
83 | /* | |
84 | * Initialize the global data exactly once... | |
85 | */ | |
86 | ||
87 | pthread_once(&cups_globals_key_once, cups_globals_init); | |
88 | #endif /* HAVE_PTHREAD_H */ | |
89 | ||
90 | /* | |
91 | * See if we have allocated the data yet... | |
92 | */ | |
93 | ||
94 | if ((cg = (_cups_globals_t *)_cupsThreadGetData(cups_globals_key)) == NULL) | |
5180a04c MS |
95 | { |
96 | /* | |
6d2f911b | 97 | * No, allocate memory as set the pointer for the key... |
5180a04c | 98 | */ |
ef416fc2 | 99 | |
6d2f911b MS |
100 | if ((cg = cups_globals_alloc()) != NULL) |
101 | _cupsThreadSetData(cups_globals_key, cg); | |
5180a04c | 102 | } |
ef416fc2 | 103 | |
6d2f911b MS |
104 | /* |
105 | * Return the pointer to the data... | |
106 | */ | |
ef416fc2 | 107 | |
6d2f911b MS |
108 | return (cg); |
109 | } | |
5180a04c | 110 | |
5180a04c | 111 | |
6d2f911b MS |
112 | /* |
113 | * '_cupsGlobalUnlock()' - Unlock the global mutex. | |
114 | */ | |
5180a04c | 115 | |
6d2f911b MS |
116 | void |
117 | _cupsGlobalUnlock(void) | |
118 | { | |
119 | #ifdef HAVE_PTHREAD_H | |
120 | pthread_mutex_unlock(&cups_global_mutex); | |
19dc16f7 | 121 | #elif defined(_WIN32) |
cc754834 | 122 | LeaveCriticalSection(&cups_global_mutex.m_criticalSection); |
6d2f911b | 123 | #endif /* HAVE_PTHREAD_H */ |
ef416fc2 | 124 | } |
125 | ||
126 | ||
19dc16f7 | 127 | #ifdef _WIN32 |
ef416fc2 | 128 | /* |
6d2f911b | 129 | * 'DllMain()' - Main entry for library. |
ef416fc2 | 130 | */ |
131 | ||
6d2f911b MS |
132 | BOOL WINAPI /* O - Success/failure */ |
133 | DllMain(HINSTANCE hinst, /* I - DLL module handle */ | |
134 | DWORD reason, /* I - Reason */ | |
135 | LPVOID reserved) /* I - Unused */ | |
136 | { | |
137 | _cups_globals_t *cg; /* Global data */ | |
ef416fc2 | 138 | |
ef416fc2 | 139 | |
6d2f911b MS |
140 | (void)hinst; |
141 | (void)reserved; | |
ef416fc2 | 142 | |
6d2f911b MS |
143 | switch (reason) |
144 | { | |
145 | case DLL_PROCESS_ATTACH : /* Called on library initialization */ | |
cc754834 | 146 | InitializeCriticalSection(&cups_global_mutex.m_criticalSection); |
6d2f911b MS |
147 | |
148 | if ((cups_globals_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) | |
149 | return (FALSE); | |
150 | break; | |
151 | ||
152 | case DLL_THREAD_DETACH : /* Called when a thread terminates */ | |
153 | if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL) | |
154 | cups_globals_free(cg); | |
155 | break; | |
156 | ||
157 | case DLL_PROCESS_DETACH : /* Called when library is unloaded */ | |
158 | if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL) | |
159 | cups_globals_free(cg); | |
ef416fc2 | 160 | |
6d2f911b | 161 | TlsFree(cups_globals_key); |
cc754834 | 162 | DeleteCriticalSection(&cups_global_mutex.m_criticalSection); |
6d2f911b MS |
163 | break; |
164 | ||
165 | default: | |
166 | break; | |
167 | } | |
168 | ||
169 | return (TRUE); | |
170 | } | |
19dc16f7 | 171 | #endif /* _WIN32 */ |
ef416fc2 | 172 | |
173 | ||
174 | /* | |
6d2f911b | 175 | * 'cups_globals_alloc()' - Allocate and initialize global data. |
ef416fc2 | 176 | */ |
177 | ||
6d2f911b MS |
178 | static _cups_globals_t * /* O - Pointer to global data */ |
179 | cups_globals_alloc(void) | |
ef416fc2 | 180 | { |
6d2f911b MS |
181 | _cups_globals_t *cg = malloc(sizeof(_cups_globals_t)); |
182 | /* Pointer to global data */ | |
19dc16f7 | 183 | #ifdef _WIN32 |
6d2f911b MS |
184 | HKEY key; /* Registry key */ |
185 | DWORD size; /* Size of string */ | |
12f89d24 MS |
186 | static char installdir[1024] = "", /* Install directory */ |
187 | confdir[1024] = "", /* Server root directory */ | |
188 | localedir[1024] = ""; /* Locale directory */ | |
19dc16f7 | 189 | #endif /* _WIN32 */ |
6d2f911b | 190 | |
ef416fc2 | 191 | |
6d2f911b MS |
192 | if (!cg) |
193 | return (NULL); | |
ef416fc2 | 194 | |
5180a04c | 195 | /* |
6d2f911b MS |
196 | * Clear the global storage and set the default encryption and password |
197 | * callback values... | |
ef416fc2 | 198 | */ |
199 | ||
6d2f911b | 200 | memset(cg, 0, sizeof(_cups_globals_t)); |
c1420c87 MS |
201 | cg->encryption = (http_encryption_t)-1; |
202 | cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; | |
08d56b1f | 203 | cg->trust_first = -1; |
3abb875b MS |
204 | cg->any_root = -1; |
205 | cg->expired_certs = -1; | |
206 | cg->validate_certs = -1; | |
ef416fc2 | 207 | |
a469f8a5 MS |
208 | #ifdef DEBUG |
209 | /* | |
210 | * Friendly thread ID for debugging... | |
211 | */ | |
212 | ||
213 | cg->thread_id = ++ cups_global_index; | |
214 | #endif /* DEBUG */ | |
215 | ||
ef416fc2 | 216 | /* |
6d2f911b MS |
217 | * Then set directories as appropriate... |
218 | */ | |
219 | ||
19dc16f7 | 220 | #ifdef _WIN32 |
12f89d24 | 221 | if (!installdir[0]) |
ef416fc2 | 222 | { |
223 | /* | |
12f89d24 | 224 | * Open the registry... |
ef416fc2 | 225 | */ |
226 | ||
5a9febac | 227 | strlcpy(installdir, "C:/Program Files/cups.org", sizeof(installdir)); |
12f89d24 | 228 | |
104c5283 | 229 | if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, &key)) |
12f89d24 MS |
230 | { |
231 | /* | |
232 | * Grab the installation directory... | |
233 | */ | |
234 | ||
235 | char *ptr; /* Pointer into installdir */ | |
236 | ||
237 | size = sizeof(installdir); | |
104c5283 | 238 | RegQueryValueExA(key, "installdir", NULL, NULL, installdir, &size); |
12f89d24 MS |
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 | ||
e2eb28cf MS |
276 | cg->home = getenv("HOME"); |
277 | ||
6d2f911b MS |
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; | |
e2eb28cf MS |
316 | |
317 | # ifndef __APPLE__ /* Sandboxing now exposes the container as the home directory */ | |
318 | cg->home = getenv("HOME"); | |
319 | #endif /* !__APPLE__ */ | |
320 | } | |
321 | ||
322 | if (!cg->home) | |
323 | { | |
324 | struct passwd *pw; /* User info */ | |
325 | ||
326 | if ((pw = getpwuid(getuid())) != NULL) | |
327 | cg->home = _cupsStrAlloc(pw->pw_dir); | |
6d2f911b | 328 | } |
19dc16f7 | 329 | #endif /* _WIN32 */ |
6d2f911b | 330 | |
e2eb28cf MS |
331 | fprintf(stderr, "Using \"%s\" as home directory.\n", cg->home); |
332 | ||
6d2f911b | 333 | return (cg); |
ef416fc2 | 334 | } |
335 | ||
336 | ||
337 | /* | |
6d2f911b | 338 | * 'cups_globals_free()' - Free global data. |
ef416fc2 | 339 | */ |
340 | ||
19dc16f7 | 341 | #if defined(HAVE_PTHREAD_H) || defined(_WIN32) |
ef416fc2 | 342 | static void |
6d2f911b | 343 | cups_globals_free(_cups_globals_t *cg) /* I - Pointer to global data */ |
ef416fc2 | 344 | { |
dcb445bc | 345 | _cups_buffer_t *buffer, /* Current read/write buffer */ |
1f6f3dbc | 346 | *next; /* Next buffer */ |
fa73b229 | 347 | |
348 | ||
e53920b9 | 349 | if (cg->last_status_message) |
5f64df29 | 350 | _cupsStrFree(cg->last_status_message); |
e53920b9 | 351 | |
dcb445bc | 352 | for (buffer = cg->cups_buffers; buffer; buffer = next) |
1f6f3dbc MS |
353 | { |
354 | next = buffer->next; | |
355 | free(buffer); | |
356 | } | |
357 | ||
c168a833 | 358 | cupsArrayDelete(cg->leg_size_lut); |
f14324a7 MS |
359 | cupsArrayDelete(cg->ppd_size_lut); |
360 | cupsArrayDelete(cg->pwg_size_lut); | |
c168a833 | 361 | |
6d2f911b | 362 | httpClose(cg->http); |
ef416fc2 | 363 | |
7d5824d6 | 364 | #ifdef HAVE_SSL |
7cf5915e | 365 | _httpFreeCredentials(cg->tls_credentials); |
7d5824d6 | 366 | #endif /* HAVE_SSL */ |
7cf5915e | 367 | |
6d2f911b MS |
368 | cupsFileClose(cg->stdio_files[0]); |
369 | cupsFileClose(cg->stdio_files[1]); | |
370 | cupsFileClose(cg->stdio_files[2]); | |
ef416fc2 | 371 | |
6d2f911b | 372 | cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings); |
ef416fc2 | 373 | |
dd204f7a MS |
374 | if (cg->raster_error.start) |
375 | free(cg->raster_error.start); | |
376 | ||
6d2f911b MS |
377 | free(cg); |
378 | } | |
19dc16f7 | 379 | #endif /* HAVE_PTHREAD_H || _WIN32 */ |
6d2f911b MS |
380 | |
381 | ||
382 | #ifdef HAVE_PTHREAD_H | |
ef416fc2 | 383 | /* |
6d2f911b | 384 | * 'cups_globals_init()' - Initialize environment variables. |
ef416fc2 | 385 | */ |
386 | ||
6d2f911b MS |
387 | static void |
388 | cups_globals_init(void) | |
ef416fc2 | 389 | { |
ef416fc2 | 390 | /* |
6d2f911b | 391 | * Register the global data for this thread... |
ef416fc2 | 392 | */ |
393 | ||
6d2f911b | 394 | pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free); |
ef416fc2 | 395 | } |
396 | #endif /* HAVE_PTHREAD_H */ |