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 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
28 static int cups_global_index
= 0;
29 /* Next thread number */
31 static _cups_threadkey_t cups_globals_key
= _CUPS_THREADKEY_INITIALIZER
;
32 /* Thread local storage key */
34 static pthread_once_t cups_globals_key_once
= PTHREAD_ONCE_INIT
;
35 /* One-time initialization object */
36 #endif /* HAVE_PTHREAD_H */
37 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
38 static _cups_mutex_t cups_global_mutex
= _CUPS_MUTEX_INITIALIZER
;
39 /* Global critical section */
40 #endif /* HAVE_PTHREAD_H || WIN32 */
48 static void cups_fix_path(char *path
);
50 static _cups_globals_t
*cups_globals_alloc(void);
51 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
52 static void cups_globals_free(_cups_globals_t
*g
);
53 #endif /* HAVE_PTHREAD_H || WIN32 */
55 static void cups_globals_init(void);
56 #endif /* HAVE_PTHREAD_H */
60 * '_cupsGlobalLock()' - Lock the global mutex.
67 pthread_mutex_lock(&cups_global_mutex
);
69 EnterCriticalSection(&cups_global_mutex
.m_criticalSection
);
70 #endif /* HAVE_PTHREAD_H */
75 * '_cupsGlobals()' - Return a pointer to thread local storage
78 _cups_globals_t
* /* O - Pointer to global data */
81 _cups_globals_t
*cg
; /* Pointer to global data */
86 * Initialize the global data exactly once...
89 pthread_once(&cups_globals_key_once
, cups_globals_init
);
90 #endif /* HAVE_PTHREAD_H */
93 * See if we have allocated the data yet...
96 if ((cg
= (_cups_globals_t
*)_cupsThreadGetData(cups_globals_key
)) == NULL
)
99 * No, allocate memory as set the pointer for the key...
102 if ((cg
= cups_globals_alloc()) != NULL
)
103 _cupsThreadSetData(cups_globals_key
, cg
);
107 * Return the pointer to the data...
115 * '_cupsGlobalUnlock()' - Unlock the global mutex.
119 _cupsGlobalUnlock(void)
121 #ifdef HAVE_PTHREAD_H
122 pthread_mutex_unlock(&cups_global_mutex
);
124 LeaveCriticalSection(&cups_global_mutex
.m_criticalSection
);
125 #endif /* HAVE_PTHREAD_H */
131 * 'DllMain()' - Main entry for library.
134 BOOL WINAPI
/* O - Success/failure */
135 DllMain(HINSTANCE hinst
, /* I - DLL module handle */
136 DWORD reason
, /* I - Reason */
137 LPVOID reserved
) /* I - Unused */
139 _cups_globals_t
*cg
; /* Global data */
147 case DLL_PROCESS_ATTACH
: /* Called on library initialization */
148 InitializeCriticalSection(&cups_global_mutex
.m_criticalSection
);
150 if ((cups_globals_key
= TlsAlloc()) == TLS_OUT_OF_INDEXES
)
154 case DLL_THREAD_DETACH
: /* Called when a thread terminates */
155 if ((cg
= (_cups_globals_t
*)TlsGetValue(cups_globals_key
)) != NULL
)
156 cups_globals_free(cg
);
159 case DLL_PROCESS_DETACH
: /* Called when library is unloaded */
160 if ((cg
= (_cups_globals_t
*)TlsGetValue(cups_globals_key
)) != NULL
)
161 cups_globals_free(cg
);
163 TlsFree(cups_globals_key
);
164 DeleteCriticalSection(&cups_global_mutex
.m_criticalSection
);
177 * 'cups_globals_alloc()' - Allocate and initialize global data.
180 static _cups_globals_t
* /* O - Pointer to global data */
181 cups_globals_alloc(void)
183 _cups_globals_t
*cg
= malloc(sizeof(_cups_globals_t
));
184 /* Pointer to global data */
186 HKEY key
; /* Registry key */
187 DWORD size
; /* Size of string */
188 static char installdir
[1024] = "", /* Install directory */
189 confdir
[1024] = "", /* Server root directory */
190 localedir
[1024] = ""; /* Locale directory */
198 * Clear the global storage and set the default encryption and password
202 memset(cg
, 0, sizeof(_cups_globals_t
));
203 cg
->encryption
= (http_encryption_t
)-1;
204 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
205 cg
->trust_first
= -1;
207 cg
->expired_certs
= -1;
208 cg
->validate_certs
= -1;
212 * Friendly thread ID for debugging...
215 cg
->thread_id
= ++ cups_global_index
;
219 * Then set directories as appropriate...
226 * Open the registry...
229 strlcpy(installdir
, "C:/Program Files/cups.org", sizeof(installdir
));
231 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE
, "SOFTWARE\\cups.org", 0, KEY_READ
,
235 * Grab the installation directory...
238 char *ptr
; /* Pointer into installdir */
240 size
= sizeof(installdir
);
241 RegQueryValueEx(key
, "installdir", NULL
, NULL
, installdir
, &size
);
244 for (ptr
= installdir
; *ptr
;)
251 *ptr
= '\0'; /* Strip trailing \ */
253 else if (*ptr
== '/' && !ptr
[1])
254 *ptr
= '\0'; /* Strip trailing / */
260 snprintf(confdir
, sizeof(confdir
), "%s/conf", installdir
);
261 snprintf(localedir
, sizeof(localedir
), "%s/locale", installdir
);
264 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
265 cg
->cups_datadir
= installdir
;
267 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
268 cg
->cups_serverbin
= installdir
;
270 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
271 cg
->cups_serverroot
= confdir
;
273 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
274 cg
->cups_statedir
= confdir
;
276 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
277 cg
->localedir
= localedir
;
281 if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
284 # endif /* HAVE_GETEUID */
287 * When running setuid/setgid, don't allow environment variables to override
291 cg
->cups_datadir
= CUPS_DATADIR
;
292 cg
->cups_serverbin
= CUPS_SERVERBIN
;
293 cg
->cups_serverroot
= CUPS_SERVERROOT
;
294 cg
->cups_statedir
= CUPS_STATEDIR
;
295 cg
->localedir
= CUPS_LOCALEDIR
;
300 * Allow directories to be overridden by environment variables.
303 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
304 cg
->cups_datadir
= CUPS_DATADIR
;
306 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
307 cg
->cups_serverbin
= CUPS_SERVERBIN
;
309 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
310 cg
->cups_serverroot
= CUPS_SERVERROOT
;
312 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
313 cg
->cups_statedir
= CUPS_STATEDIR
;
315 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
316 cg
->localedir
= CUPS_LOCALEDIR
;
325 * 'cups_globals_free()' - Free global data.
328 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
330 cups_globals_free(_cups_globals_t
*cg
) /* I - Pointer to global data */
332 _cups_buffer_t
*buffer
, /* Current read/write buffer */
333 *next
; /* Next buffer */
336 if (cg
->last_status_message
)
337 _cupsStrFree(cg
->last_status_message
);
339 for (buffer
= cg
->cups_buffers
; buffer
; buffer
= next
)
345 cupsArrayDelete(cg
->leg_size_lut
);
346 cupsArrayDelete(cg
->ppd_size_lut
);
347 cupsArrayDelete(cg
->pwg_size_lut
);
352 _httpFreeCredentials(cg
->tls_credentials
);
353 #endif /* HAVE_SSL */
355 cupsFileClose(cg
->stdio_files
[0]);
356 cupsFileClose(cg
->stdio_files
[1]);
357 cupsFileClose(cg
->stdio_files
[2]);
359 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
363 #endif /* HAVE_PTHREAD_H || WIN32 */
366 #ifdef HAVE_PTHREAD_H
368 * 'cups_globals_init()' - Initialize environment variables.
372 cups_globals_init(void)
375 * Register the global data for this thread...
378 pthread_key_create(&cups_globals_key
, (void (*)(void *))cups_globals_free
);
380 #endif /* HAVE_PTHREAD_H */