2 * Global variable access routines for CUPS.
4 * Copyright 2007-2015 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 information.
11 * Include necessary headers...
14 #include "cups-private.h"
22 static int cups_global_index
= 0;
23 /* Next thread number */
25 static _cups_threadkey_t cups_globals_key
= _CUPS_THREADKEY_INITIALIZER
;
26 /* Thread local storage key */
28 static pthread_once_t cups_globals_key_once
= PTHREAD_ONCE_INIT
;
29 /* One-time initialization object */
30 #endif /* HAVE_PTHREAD_H */
31 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
32 static _cups_mutex_t cups_global_mutex
= _CUPS_MUTEX_INITIALIZER
;
33 /* Global critical section */
34 #endif /* HAVE_PTHREAD_H || WIN32 */
42 static void cups_fix_path(char *path
);
44 static _cups_globals_t
*cups_globals_alloc(void);
45 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
46 static void cups_globals_free(_cups_globals_t
*g
);
47 #endif /* HAVE_PTHREAD_H || WIN32 */
49 static void cups_globals_init(void);
50 #endif /* HAVE_PTHREAD_H */
54 * '_cupsGlobalLock()' - Lock the global mutex.
61 pthread_mutex_lock(&cups_global_mutex
);
63 EnterCriticalSection(&cups_global_mutex
.m_criticalSection
);
64 #endif /* HAVE_PTHREAD_H */
69 * '_cupsGlobals()' - Return a pointer to thread local storage
72 _cups_globals_t
* /* O - Pointer to global data */
75 _cups_globals_t
*cg
; /* Pointer to global data */
80 * Initialize the global data exactly once...
83 pthread_once(&cups_globals_key_once
, cups_globals_init
);
84 #endif /* HAVE_PTHREAD_H */
87 * See if we have allocated the data yet...
90 if ((cg
= (_cups_globals_t
*)_cupsThreadGetData(cups_globals_key
)) == NULL
)
93 * No, allocate memory as set the pointer for the key...
96 if ((cg
= cups_globals_alloc()) != NULL
)
97 _cupsThreadSetData(cups_globals_key
, cg
);
101 * Return the pointer to the data...
109 * '_cupsGlobalUnlock()' - Unlock the global mutex.
113 _cupsGlobalUnlock(void)
115 #ifdef HAVE_PTHREAD_H
116 pthread_mutex_unlock(&cups_global_mutex
);
118 LeaveCriticalSection(&cups_global_mutex
.m_criticalSection
);
119 #endif /* HAVE_PTHREAD_H */
125 * 'DllMain()' - Main entry for library.
128 BOOL WINAPI
/* O - Success/failure */
129 DllMain(HINSTANCE hinst
, /* I - DLL module handle */
130 DWORD reason
, /* I - Reason */
131 LPVOID reserved
) /* I - Unused */
133 _cups_globals_t
*cg
; /* Global data */
141 case DLL_PROCESS_ATTACH
: /* Called on library initialization */
142 InitializeCriticalSection(&cups_global_mutex
.m_criticalSection
);
144 if ((cups_globals_key
= TlsAlloc()) == TLS_OUT_OF_INDEXES
)
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
);
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
);
157 TlsFree(cups_globals_key
);
158 DeleteCriticalSection(&cups_global_mutex
.m_criticalSection
);
171 * 'cups_globals_alloc()' - Allocate and initialize global data.
174 static _cups_globals_t
* /* O - Pointer to global data */
175 cups_globals_alloc(void)
177 _cups_globals_t
*cg
= malloc(sizeof(_cups_globals_t
));
178 /* Pointer to global data */
180 HKEY key
; /* Registry key */
181 DWORD size
; /* Size of string */
182 static char installdir
[1024] = "", /* Install directory */
183 confdir
[1024] = "", /* Server root directory */
184 localedir
[1024] = ""; /* Locale directory */
192 * Clear the global storage and set the default encryption and password
196 memset(cg
, 0, sizeof(_cups_globals_t
));
197 cg
->encryption
= (http_encryption_t
)-1;
198 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
199 cg
->trust_first
= -1;
201 cg
->expired_certs
= -1;
202 cg
->validate_certs
= -1;
206 * Friendly thread ID for debugging...
209 cg
->thread_id
= ++ cups_global_index
;
213 * Then set directories as appropriate...
220 * Open the registry...
223 strlcpy(installdir
, "C:/Program Files/cups.org", sizeof(installdir
));
225 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE
, "SOFTWARE\\cups.org", 0, KEY_READ
,
229 * Grab the installation directory...
232 char *ptr
; /* Pointer into installdir */
234 size
= sizeof(installdir
);
235 RegQueryValueEx(key
, "installdir", NULL
, NULL
, installdir
, &size
);
238 for (ptr
= installdir
; *ptr
;)
245 *ptr
= '\0'; /* Strip trailing \ */
247 else if (*ptr
== '/' && !ptr
[1])
248 *ptr
= '\0'; /* Strip trailing / */
254 snprintf(confdir
, sizeof(confdir
), "%s/conf", installdir
);
255 snprintf(localedir
, sizeof(localedir
), "%s/locale", installdir
);
258 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
259 cg
->cups_datadir
= installdir
;
261 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
262 cg
->cups_serverbin
= installdir
;
264 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
265 cg
->cups_serverroot
= confdir
;
267 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
268 cg
->cups_statedir
= confdir
;
270 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
271 cg
->localedir
= localedir
;
275 if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
278 # endif /* HAVE_GETEUID */
281 * When running setuid/setgid, don't allow environment variables to override
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
;
294 * Allow directories to be overridden by environment variables.
297 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
298 cg
->cups_datadir
= CUPS_DATADIR
;
300 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
301 cg
->cups_serverbin
= CUPS_SERVERBIN
;
303 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
304 cg
->cups_serverroot
= CUPS_SERVERROOT
;
306 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
307 cg
->cups_statedir
= CUPS_STATEDIR
;
309 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
310 cg
->localedir
= CUPS_LOCALEDIR
;
319 * 'cups_globals_free()' - Free global data.
322 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
324 cups_globals_free(_cups_globals_t
*cg
) /* I - Pointer to global data */
326 _cups_buffer_t
*buffer
, /* Current read/write buffer */
327 *next
; /* Next buffer */
330 if (cg
->last_status_message
)
331 _cupsStrFree(cg
->last_status_message
);
333 for (buffer
= cg
->cups_buffers
; buffer
; buffer
= next
)
339 cupsArrayDelete(cg
->leg_size_lut
);
340 cupsArrayDelete(cg
->ppd_size_lut
);
341 cupsArrayDelete(cg
->pwg_size_lut
);
346 _httpFreeCredentials(cg
->tls_credentials
);
347 #endif /* HAVE_SSL */
349 cupsFileClose(cg
->stdio_files
[0]);
350 cupsFileClose(cg
->stdio_files
[1]);
351 cupsFileClose(cg
->stdio_files
[2]);
353 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
357 #endif /* HAVE_PTHREAD_H || WIN32 */
360 #ifdef HAVE_PTHREAD_H
362 * 'cups_globals_init()' - Initialize environment variables.
366 cups_globals_init(void)
369 * Register the global data for this thread...
372 pthread_key_create(&cups_globals_key
, (void (*)(void *))cups_globals_free
);
374 #endif /* HAVE_PTHREAD_H */