2 * Global variable access routines for CUPS.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cups-private.h"
26 static int cups_global_index = 0;
27 /* Next thread number */
29 static _cups_threadkey_t cups_globals_key = _CUPS_THREADKEY_INITIALIZER;
30 /* Thread local storage key */
32 static pthread_once_t cups_globals_key_once = PTHREAD_ONCE_INIT;
33 /* One-time initialization object */
34 #endif /* HAVE_PTHREAD_H */
35 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
36 static _cups_mutex_t cups_global_mutex = _CUPS_MUTEX_INITIALIZER;
37 /* Global critical section */
38 #endif /* HAVE_PTHREAD_H || _WIN32 */
46 static void cups_fix_path(char *path);
48 static _cups_globals_t *cups_globals_alloc(void);
49 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
50 static void cups_globals_free(_cups_globals_t *g);
51 #endif /* HAVE_PTHREAD_H || _WIN32 */
53 static void cups_globals_init(void);
54 #endif /* HAVE_PTHREAD_H */
58 * '_cupsGlobalLock()' - Lock the global mutex.
65 pthread_mutex_lock(&cups_global_mutex);
67 EnterCriticalSection(&cups_global_mutex.m_criticalSection);
68 #endif /* HAVE_PTHREAD_H */
73 * '_cupsGlobals()' - Return a pointer to thread local storage
76 _cups_globals_t * /* O - Pointer to global data */
79 _cups_globals_t *cg; /* Pointer to global data */
84 * Initialize the global data exactly once...
87 pthread_once(&cups_globals_key_once, cups_globals_init);
88 #endif /* HAVE_PTHREAD_H */
91 * See if we have allocated the data yet...
94 if ((cg = (_cups_globals_t *)_cupsThreadGetData(cups_globals_key)) == NULL)
97 * No, allocate memory as set the pointer for the key...
100 if ((cg = cups_globals_alloc()) != NULL)
101 _cupsThreadSetData(cups_globals_key, cg);
105 * Return the pointer to the data...
113 * '_cupsGlobalUnlock()' - Unlock the global mutex.
117 _cupsGlobalUnlock(void)
119 #ifdef HAVE_PTHREAD_H
120 pthread_mutex_unlock(&cups_global_mutex);
121 #elif defined(_WIN32)
122 LeaveCriticalSection(&cups_global_mutex.m_criticalSection);
123 #endif /* HAVE_PTHREAD_H */
129 * 'DllMain()' - Main entry for library.
132 BOOL WINAPI /* O - Success/failure */
133 DllMain(HINSTANCE hinst, /* I - DLL module handle */
134 DWORD reason, /* I - Reason */
135 LPVOID reserved) /* I - Unused */
137 _cups_globals_t *cg; /* Global data */
145 case DLL_PROCESS_ATTACH : /* Called on library initialization */
146 InitializeCriticalSection(&cups_global_mutex.m_criticalSection);
148 if ((cups_globals_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
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);
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);
161 TlsFree(cups_globals_key);
162 DeleteCriticalSection(&cups_global_mutex.m_criticalSection);
175 * 'cups_globals_alloc()' - Allocate and initialize global data.
178 static _cups_globals_t * /* O - Pointer to global data */
179 cups_globals_alloc(void)
181 _cups_globals_t *cg = malloc(sizeof(_cups_globals_t));
182 /* Pointer to global data */
184 HKEY key; /* Registry key */
185 DWORD size; /* Size of string */
186 static char installdir[1024] = "", /* Install directory */
187 confdir[1024] = "", /* Server root directory */
188 localedir[1024] = ""; /* Locale directory */
196 * Clear the global storage and set the default encryption and password
200 memset(cg, 0, sizeof(_cups_globals_t));
201 cg->encryption = (http_encryption_t)-1;
202 cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
203 cg->trust_first = -1;
205 cg->expired_certs = -1;
206 cg->validate_certs = -1;
210 * Friendly thread ID for debugging...
213 cg->thread_id = ++ cups_global_index;
217 * Then set directories as appropriate...
224 * Open the registry...
227 strlcpy(installdir, "C:/Program Files/cups.org", sizeof(installdir));
229 if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, &key))
232 * Grab the installation directory...
235 char *ptr; /* Pointer into installdir */
237 size = sizeof(installdir);
238 RegQueryValueExA(key, "installdir", NULL, NULL, installdir, &size);
241 for (ptr = installdir; *ptr;)
248 *ptr = '\0'; /* Strip trailing \ */
250 else if (*ptr == '/' && !ptr[1])
251 *ptr = '\0'; /* Strip trailing / */
257 snprintf(confdir, sizeof(confdir), "%s/conf", installdir);
258 snprintf(localedir, sizeof(localedir), "%s/locale", installdir);
261 if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
262 cg->cups_datadir = installdir;
264 if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
265 cg->cups_serverbin = installdir;
267 if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
268 cg->cups_serverroot = confdir;
270 if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
271 cg->cups_statedir = confdir;
273 if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
274 cg->localedir = localedir;
276 cg->home = getenv("HOME");
280 if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
283 # endif /* HAVE_GETEUID */
286 * When running setuid/setgid, don't allow environment variables to override
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;
299 * Allow directories to be overridden by environment variables.
302 if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
303 cg->cups_datadir = CUPS_DATADIR;
305 if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
306 cg->cups_serverbin = CUPS_SERVERBIN;
308 if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
309 cg->cups_serverroot = CUPS_SERVERROOT;
311 if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
312 cg->cups_statedir = CUPS_STATEDIR;
314 if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
315 cg->localedir = CUPS_LOCALEDIR;
317 cg->home = getenv("HOME");
319 # ifdef __APPLE__ /* Sandboxing now exposes the container as the home directory */
320 if (cg->home && strstr(cg->home, "/Library/Containers/"))
322 # endif /* !__APPLE__ */
327 struct passwd *pw; /* User info */
329 if ((pw = getpwuid(getuid())) != NULL)
330 cg->home = _cupsStrAlloc(pw->pw_dir);
339 * 'cups_globals_free()' - Free global data.
342 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
344 cups_globals_free(_cups_globals_t *cg) /* I - Pointer to global data */
346 _cups_buffer_t *buffer, /* Current read/write buffer */
347 *next; /* Next buffer */
350 if (cg->last_status_message)
351 _cupsStrFree(cg->last_status_message);
353 for (buffer = cg->cups_buffers; buffer; buffer = next)
359 cupsArrayDelete(cg->leg_size_lut);
360 cupsArrayDelete(cg->ppd_size_lut);
361 cupsArrayDelete(cg->pwg_size_lut);
366 _httpFreeCredentials(cg->tls_credentials);
367 #endif /* HAVE_SSL */
369 cupsFileClose(cg->stdio_files[0]);
370 cupsFileClose(cg->stdio_files[1]);
371 cupsFileClose(cg->stdio_files[2]);
373 cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
375 if (cg->raster_error.start)
376 free(cg->raster_error.start);
380 #endif /* HAVE_PTHREAD_H || _WIN32 */
383 #ifdef HAVE_PTHREAD_H
385 * 'cups_globals_init()' - Initialize environment variables.
389 cups_globals_init(void)
392 * Register the global data for this thread...
395 pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free);
397 #endif /* HAVE_PTHREAD_H */