2 * "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $"
4 * Global variable access routines for CUPS.
6 * Copyright 2007-2012 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"
41 static _cups_threadkey_t cups_globals_key
= _CUPS_THREADKEY_INITIALIZER
;
42 /* Thread local storage key */
44 static pthread_once_t cups_globals_key_once
= PTHREAD_ONCE_INIT
;
45 /* One-time initialization object */
46 #endif /* HAVE_PTHREAD_H */
47 static _cups_mutex_t cups_global_mutex
= _CUPS_MUTEX_INITIALIZER
;
48 /* Global critical section */
56 static void cups_fix_path(char *path
);
58 static _cups_globals_t
*cups_globals_alloc(void);
59 static void cups_globals_free(_cups_globals_t
*g
);
61 static void cups_globals_init(void);
62 #endif /* HAVE_PTHREAD_H */
66 * '_cupsGlobalLock()' - Lock the global mutex.
73 pthread_mutex_lock(&cups_global_mutex
);
75 EnterCriticalSection(&cups_global_mutex
.m_criticalSection
);
76 #endif /* HAVE_PTHREAD_H */
81 * '_cupsGlobals()' - Return a pointer to thread local storage
84 _cups_globals_t
* /* O - Pointer to global data */
87 _cups_globals_t
*cg
; /* Pointer to global data */
92 * Initialize the global data exactly once...
95 pthread_once(&cups_globals_key_once
, cups_globals_init
);
96 #endif /* HAVE_PTHREAD_H */
99 * See if we have allocated the data yet...
102 if ((cg
= (_cups_globals_t
*)_cupsThreadGetData(cups_globals_key
)) == NULL
)
105 * No, allocate memory as set the pointer for the key...
108 if ((cg
= cups_globals_alloc()) != NULL
)
109 _cupsThreadSetData(cups_globals_key
, cg
);
113 * Return the pointer to the data...
121 * '_cupsGlobalUnlock()' - Unlock the global mutex.
125 _cupsGlobalUnlock(void)
127 #ifdef HAVE_PTHREAD_H
128 pthread_mutex_unlock(&cups_global_mutex
);
130 LeaveCriticalSection(&cups_global_mutex
.m_criticalSection
);
131 #endif /* HAVE_PTHREAD_H */
137 * 'DllMain()' - Main entry for library.
140 BOOL WINAPI
/* O - Success/failure */
141 DllMain(HINSTANCE hinst
, /* I - DLL module handle */
142 DWORD reason
, /* I - Reason */
143 LPVOID reserved
) /* I - Unused */
145 _cups_globals_t
*cg
; /* Global data */
153 case DLL_PROCESS_ATTACH
: /* Called on library initialization */
154 InitializeCriticalSection(&cups_global_mutex
.m_criticalSection
);
156 if ((cups_globals_key
= TlsAlloc()) == TLS_OUT_OF_INDEXES
)
160 case DLL_THREAD_DETACH
: /* Called when a thread terminates */
161 if ((cg
= (_cups_globals_t
*)TlsGetValue(cups_globals_key
)) != NULL
)
162 cups_globals_free(cg
);
165 case DLL_PROCESS_DETACH
: /* Called when library is unloaded */
166 if ((cg
= (_cups_globals_t
*)TlsGetValue(cups_globals_key
)) != NULL
)
167 cups_globals_free(cg
);
169 TlsFree(cups_globals_key
);
170 DeleteCriticalSection(&cups_global_mutex
.m_criticalSection
);
183 * 'cups_globals_alloc()' - Allocate and initialize global data.
186 static _cups_globals_t
* /* O - Pointer to global data */
187 cups_globals_alloc(void)
189 _cups_globals_t
*cg
= malloc(sizeof(_cups_globals_t
));
190 /* Pointer to global data */
192 HKEY key
; /* Registry key */
193 DWORD size
; /* Size of string */
194 static char installdir
[1024] = "", /* Install directory */
195 confdir
[1024] = "", /* Server root directory */
196 localedir
[1024] = ""; /* Locale directory */
204 * Clear the global storage and set the default encryption and password
208 memset(cg
, 0, sizeof(_cups_globals_t
));
209 cg
->encryption
= (http_encryption_t
)-1;
210 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
212 cg
->expired_certs
= 1;
213 cg
->expired_root
= 1;
216 * Then set directories as appropriate...
223 * Open the registry...
226 strcpy(installdir
, "C:/Program Files/cups.org");
228 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE
, "SOFTWARE\\cups.org", 0, KEY_READ
,
232 * Grab the installation directory...
235 char *ptr
; /* Pointer into installdir */
237 size
= sizeof(installdir
);
238 RegQueryValueEx(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
;
278 if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
281 # endif /* HAVE_GETEUID */
284 * When running setuid/setgid, don't allow environment variables to override
288 cg
->cups_datadir
= CUPS_DATADIR
;
289 cg
->cups_serverbin
= CUPS_SERVERBIN
;
290 cg
->cups_serverroot
= CUPS_SERVERROOT
;
291 cg
->cups_statedir
= CUPS_STATEDIR
;
292 cg
->localedir
= CUPS_LOCALEDIR
;
297 * Allow directories to be overridden by environment variables.
300 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
301 cg
->cups_datadir
= CUPS_DATADIR
;
303 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
304 cg
->cups_serverbin
= CUPS_SERVERBIN
;
306 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
307 cg
->cups_serverroot
= CUPS_SERVERROOT
;
309 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
310 cg
->cups_statedir
= CUPS_STATEDIR
;
312 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
313 cg
->localedir
= CUPS_LOCALEDIR
;
322 * 'cups_globals_free()' - Free global data.
326 cups_globals_free(_cups_globals_t
*cg
) /* I - Pointer to global data */
328 _cups_buffer_t
*buffer
, /* Current read/write buffer */
329 *next
; /* Next buffer */
332 if (cg
->last_status_message
)
333 _cupsStrFree(cg
->last_status_message
);
335 for (buffer
= cg
->cups_buffers
; buffer
; buffer
= next
)
341 cupsArrayDelete(cg
->leg_size_lut
);
342 cupsArrayDelete(cg
->ppd_size_lut
);
343 cupsArrayDelete(cg
->pwg_size_lut
);
347 _httpFreeCredentials(cg
->tls_credentials
);
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
);
359 #ifdef HAVE_PTHREAD_H
361 * 'cups_globals_init()' - Initialize environment variables.
365 cups_globals_init(void)
368 * Register the global data for this thread...
371 pthread_key_create(&cups_globals_key
, (void (*)(void *))cups_globals_free
);
373 #endif /* HAVE_PTHREAD_H */
377 * End of "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $".