4 * Global variable access routines for CUPS.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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.
23 * cups_fix_path() - Fix a file path to use forward slashes consistently.
24 * cups_globals_alloc() - Allocate and initialize global data.
25 * cups_globals_free() - Free global data.
26 * cups_globals_init() - Initialize environment variables.
30 * Include necessary headers...
33 #include "cups-private.h"
42 static int cups_global_index
= 0;
43 /* Next thread number */
45 static _cups_threadkey_t cups_globals_key
= _CUPS_THREADKEY_INITIALIZER
;
46 /* Thread local storage key */
48 static pthread_once_t cups_globals_key_once
= PTHREAD_ONCE_INIT
;
49 /* One-time initialization object */
50 #endif /* HAVE_PTHREAD_H */
51 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
52 static _cups_mutex_t cups_global_mutex
= _CUPS_MUTEX_INITIALIZER
;
53 /* Global critical section */
54 #endif /* HAVE_PTHREAD_H || WIN32 */
62 static void cups_fix_path(char *path
);
64 static _cups_globals_t
*cups_globals_alloc(void);
65 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
66 static void cups_globals_free(_cups_globals_t
*g
);
67 #endif /* HAVE_PTHREAD_H || WIN32 */
69 static void cups_globals_init(void);
70 #endif /* HAVE_PTHREAD_H */
74 * '_cupsGlobalLock()' - Lock the global mutex.
81 pthread_mutex_lock(&cups_global_mutex
);
83 EnterCriticalSection(&cups_global_mutex
.m_criticalSection
);
84 #endif /* HAVE_PTHREAD_H */
89 * '_cupsGlobals()' - Return a pointer to thread local storage
92 _cups_globals_t
* /* O - Pointer to global data */
95 _cups_globals_t
*cg
; /* Pointer to global data */
100 * Initialize the global data exactly once...
103 pthread_once(&cups_globals_key_once
, cups_globals_init
);
104 #endif /* HAVE_PTHREAD_H */
107 * See if we have allocated the data yet...
110 if ((cg
= (_cups_globals_t
*)_cupsThreadGetData(cups_globals_key
)) == NULL
)
113 * No, allocate memory as set the pointer for the key...
116 if ((cg
= cups_globals_alloc()) != NULL
)
117 _cupsThreadSetData(cups_globals_key
, cg
);
121 * Return the pointer to the data...
129 * '_cupsGlobalUnlock()' - Unlock the global mutex.
133 _cupsGlobalUnlock(void)
135 #ifdef HAVE_PTHREAD_H
136 pthread_mutex_unlock(&cups_global_mutex
);
138 LeaveCriticalSection(&cups_global_mutex
.m_criticalSection
);
139 #endif /* HAVE_PTHREAD_H */
145 * 'DllMain()' - Main entry for library.
148 BOOL WINAPI
/* O - Success/failure */
149 DllMain(HINSTANCE hinst
, /* I - DLL module handle */
150 DWORD reason
, /* I - Reason */
151 LPVOID reserved
) /* I - Unused */
153 _cups_globals_t
*cg
; /* Global data */
161 case DLL_PROCESS_ATTACH
: /* Called on library initialization */
162 InitializeCriticalSection(&cups_global_mutex
.m_criticalSection
);
164 if ((cups_globals_key
= TlsAlloc()) == TLS_OUT_OF_INDEXES
)
168 case DLL_THREAD_DETACH
: /* Called when a thread terminates */
169 if ((cg
= (_cups_globals_t
*)TlsGetValue(cups_globals_key
)) != NULL
)
170 cups_globals_free(cg
);
173 case DLL_PROCESS_DETACH
: /* Called when library is unloaded */
174 if ((cg
= (_cups_globals_t
*)TlsGetValue(cups_globals_key
)) != NULL
)
175 cups_globals_free(cg
);
177 TlsFree(cups_globals_key
);
178 DeleteCriticalSection(&cups_global_mutex
.m_criticalSection
);
191 * 'cups_globals_alloc()' - Allocate and initialize global data.
194 static _cups_globals_t
* /* O - Pointer to global data */
195 cups_globals_alloc(void)
197 _cups_globals_t
*cg
= malloc(sizeof(_cups_globals_t
));
198 /* Pointer to global data */
200 HKEY key
; /* Registry key */
201 DWORD size
; /* Size of string */
202 static char installdir
[1024] = "", /* Install directory */
203 confdir
[1024] = "", /* Server root directory */
204 localedir
[1024] = ""; /* Locale directory */
212 * Clear the global storage and set the default encryption and password
216 memset(cg
, 0, sizeof(_cups_globals_t
));
217 cg
->encryption
= (http_encryption_t
)-1;
218 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
220 cg
->expired_certs
= 1;
221 cg
->validate_certs
= 0;
225 * Friendly thread ID for debugging...
228 cg
->thread_id
= ++ cups_global_index
;
232 * Then set directories as appropriate...
239 * Open the registry...
242 strlcpy(installdir
, "C:/Program Files/cups.org", sizeof(installdir
));
244 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE
, "SOFTWARE\\cups.org", 0, KEY_READ
,
248 * Grab the installation directory...
251 char *ptr
; /* Pointer into installdir */
253 size
= sizeof(installdir
);
254 RegQueryValueEx(key
, "installdir", NULL
, NULL
, installdir
, &size
);
257 for (ptr
= installdir
; *ptr
;)
264 *ptr
= '\0'; /* Strip trailing \ */
266 else if (*ptr
== '/' && !ptr
[1])
267 *ptr
= '\0'; /* Strip trailing / */
273 snprintf(confdir
, sizeof(confdir
), "%s/conf", installdir
);
274 snprintf(localedir
, sizeof(localedir
), "%s/locale", installdir
);
277 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
278 cg
->cups_datadir
= installdir
;
280 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
281 cg
->cups_serverbin
= installdir
;
283 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
284 cg
->cups_serverroot
= confdir
;
286 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
287 cg
->cups_statedir
= confdir
;
289 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
290 cg
->localedir
= localedir
;
294 if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
297 # endif /* HAVE_GETEUID */
300 * When running setuid/setgid, don't allow environment variables to override
304 cg
->cups_datadir
= CUPS_DATADIR
;
305 cg
->cups_serverbin
= CUPS_SERVERBIN
;
306 cg
->cups_serverroot
= CUPS_SERVERROOT
;
307 cg
->cups_statedir
= CUPS_STATEDIR
;
308 cg
->localedir
= CUPS_LOCALEDIR
;
313 * Allow directories to be overridden by environment variables.
316 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
317 cg
->cups_datadir
= CUPS_DATADIR
;
319 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
320 cg
->cups_serverbin
= CUPS_SERVERBIN
;
322 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
323 cg
->cups_serverroot
= CUPS_SERVERROOT
;
325 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
326 cg
->cups_statedir
= CUPS_STATEDIR
;
328 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
329 cg
->localedir
= CUPS_LOCALEDIR
;
338 * 'cups_globals_free()' - Free global data.
341 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
343 cups_globals_free(_cups_globals_t
*cg
) /* I - Pointer to global data */
345 _cups_buffer_t
*buffer
, /* Current read/write buffer */
346 *next
; /* Next buffer */
349 if (cg
->last_status_message
)
350 _cupsStrFree(cg
->last_status_message
);
352 for (buffer
= cg
->cups_buffers
; buffer
; buffer
= next
)
358 cupsArrayDelete(cg
->leg_size_lut
);
359 cupsArrayDelete(cg
->ppd_size_lut
);
360 cupsArrayDelete(cg
->pwg_size_lut
);
365 _httpFreeCredentials(cg
->tls_credentials
);
366 #endif /* HAVE_SSL */
368 cupsFileClose(cg
->stdio_files
[0]);
369 cupsFileClose(cg
->stdio_files
[1]);
370 cupsFileClose(cg
->stdio_files
[2]);
372 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
376 #endif /* HAVE_PTHREAD_H || WIN32 */
379 #ifdef HAVE_PTHREAD_H
381 * 'cups_globals_init()' - Initialize environment variables.
385 cups_globals_init(void)
388 * Register the global data for this thread...
391 pthread_key_create(&cups_globals_key
, (void (*)(void *))cups_globals_free
);
393 #endif /* HAVE_PTHREAD_H */