2 * "$Id: globals.c 13138 2016-03-15 14:59:54Z msweet $"
4 * Global variable access routines for CUPS.
6 * Copyright 2007-2015 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 * Include necessary headers...
22 #include "cups-private.h"
30 static int cups_global_index
= 0;
31 /* Next thread number */
33 static _cups_threadkey_t cups_globals_key
= _CUPS_THREADKEY_INITIALIZER
;
34 /* Thread local storage key */
36 static pthread_once_t cups_globals_key_once
= PTHREAD_ONCE_INIT
;
37 /* One-time initialization object */
38 #endif /* HAVE_PTHREAD_H */
39 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
40 static _cups_mutex_t cups_global_mutex
= _CUPS_MUTEX_INITIALIZER
;
41 /* Global critical section */
42 #endif /* HAVE_PTHREAD_H || WIN32 */
50 static void cups_fix_path(char *path
);
52 static _cups_globals_t
*cups_globals_alloc(void);
53 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
54 static void cups_globals_free(_cups_globals_t
*g
);
55 #endif /* HAVE_PTHREAD_H || WIN32 */
57 static void cups_globals_init(void);
58 #endif /* HAVE_PTHREAD_H */
62 * '_cupsGlobalLock()' - Lock the global mutex.
69 pthread_mutex_lock(&cups_global_mutex
);
71 EnterCriticalSection(&cups_global_mutex
.m_criticalSection
);
72 #endif /* HAVE_PTHREAD_H */
77 * '_cupsGlobals()' - Return a pointer to thread local storage
80 _cups_globals_t
* /* O - Pointer to global data */
83 _cups_globals_t
*cg
; /* Pointer to global data */
88 * Initialize the global data exactly once...
91 pthread_once(&cups_globals_key_once
, cups_globals_init
);
92 #endif /* HAVE_PTHREAD_H */
95 * See if we have allocated the data yet...
98 if ((cg
= (_cups_globals_t
*)_cupsThreadGetData(cups_globals_key
)) == NULL
)
101 * No, allocate memory as set the pointer for the key...
104 if ((cg
= cups_globals_alloc()) != NULL
)
105 _cupsThreadSetData(cups_globals_key
, cg
);
109 * Return the pointer to the data...
117 * '_cupsGlobalUnlock()' - Unlock the global mutex.
121 _cupsGlobalUnlock(void)
123 #ifdef HAVE_PTHREAD_H
124 pthread_mutex_unlock(&cups_global_mutex
);
126 LeaveCriticalSection(&cups_global_mutex
.m_criticalSection
);
127 #endif /* HAVE_PTHREAD_H */
133 * 'DllMain()' - Main entry for library.
136 BOOL WINAPI
/* O - Success/failure */
137 DllMain(HINSTANCE hinst
, /* I - DLL module handle */
138 DWORD reason
, /* I - Reason */
139 LPVOID reserved
) /* I - Unused */
141 _cups_globals_t
*cg
; /* Global data */
149 case DLL_PROCESS_ATTACH
: /* Called on library initialization */
150 InitializeCriticalSection(&cups_global_mutex
.m_criticalSection
);
152 if ((cups_globals_key
= TlsAlloc()) == TLS_OUT_OF_INDEXES
)
156 case DLL_THREAD_DETACH
: /* Called when a thread terminates */
157 if ((cg
= (_cups_globals_t
*)TlsGetValue(cups_globals_key
)) != NULL
)
158 cups_globals_free(cg
);
161 case DLL_PROCESS_DETACH
: /* Called when library is unloaded */
162 if ((cg
= (_cups_globals_t
*)TlsGetValue(cups_globals_key
)) != NULL
)
163 cups_globals_free(cg
);
165 TlsFree(cups_globals_key
);
166 DeleteCriticalSection(&cups_global_mutex
.m_criticalSection
);
179 * 'cups_globals_alloc()' - Allocate and initialize global data.
182 static _cups_globals_t
* /* O - Pointer to global data */
183 cups_globals_alloc(void)
185 _cups_globals_t
*cg
= malloc(sizeof(_cups_globals_t
));
186 /* Pointer to global data */
188 HKEY key
; /* Registry key */
189 DWORD size
; /* Size of string */
190 static char installdir
[1024] = "", /* Install directory */
191 confdir
[1024] = "", /* Server root directory */
192 localedir
[1024] = ""; /* Locale directory */
200 * Clear the global storage and set the default encryption and password
204 memset(cg
, 0, sizeof(_cups_globals_t
));
205 cg
->encryption
= (http_encryption_t
)-1;
206 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
208 cg
->expired_certs
= -1;
209 cg
->validate_certs
= -1;
213 * Friendly thread ID for debugging...
216 cg
->thread_id
= ++ cups_global_index
;
220 * Then set directories as appropriate...
227 * Open the registry...
230 strlcpy(installdir
, "C:/Program Files/cups.org", sizeof(installdir
));
232 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE
, "SOFTWARE\\cups.org", 0, KEY_READ
,
236 * Grab the installation directory...
239 char *ptr
; /* Pointer into installdir */
241 size
= sizeof(installdir
);
242 RegQueryValueEx(key
, "installdir", NULL
, NULL
, installdir
, &size
);
245 for (ptr
= installdir
; *ptr
;)
252 *ptr
= '\0'; /* Strip trailing \ */
254 else if (*ptr
== '/' && !ptr
[1])
255 *ptr
= '\0'; /* Strip trailing / */
261 snprintf(confdir
, sizeof(confdir
), "%s/conf", installdir
);
262 snprintf(localedir
, sizeof(localedir
), "%s/locale", installdir
);
265 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
266 cg
->cups_datadir
= installdir
;
268 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
269 cg
->cups_serverbin
= installdir
;
271 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
272 cg
->cups_serverroot
= confdir
;
274 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
275 cg
->cups_statedir
= confdir
;
277 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
278 cg
->localedir
= localedir
;
282 if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
285 # endif /* HAVE_GETEUID */
288 * When running setuid/setgid, don't allow environment variables to override
292 cg
->cups_datadir
= CUPS_DATADIR
;
293 cg
->cups_serverbin
= CUPS_SERVERBIN
;
294 cg
->cups_serverroot
= CUPS_SERVERROOT
;
295 cg
->cups_statedir
= CUPS_STATEDIR
;
296 cg
->localedir
= CUPS_LOCALEDIR
;
301 * Allow directories to be overridden by environment variables.
304 if ((cg
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
305 cg
->cups_datadir
= CUPS_DATADIR
;
307 if ((cg
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
308 cg
->cups_serverbin
= CUPS_SERVERBIN
;
310 if ((cg
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
311 cg
->cups_serverroot
= CUPS_SERVERROOT
;
313 if ((cg
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
314 cg
->cups_statedir
= CUPS_STATEDIR
;
316 if ((cg
->localedir
= getenv("LOCALEDIR")) == NULL
)
317 cg
->localedir
= CUPS_LOCALEDIR
;
326 * 'cups_globals_free()' - Free global data.
329 #if defined(HAVE_PTHREAD_H) || defined(WIN32)
331 cups_globals_free(_cups_globals_t
*cg
) /* I - Pointer to global data */
333 _cups_buffer_t
*buffer
, /* Current read/write buffer */
334 *next
; /* Next buffer */
337 if (cg
->last_status_message
)
338 _cupsStrFree(cg
->last_status_message
);
340 for (buffer
= cg
->cups_buffers
; buffer
; buffer
= next
)
346 cupsArrayDelete(cg
->leg_size_lut
);
347 cupsArrayDelete(cg
->ppd_size_lut
);
348 cupsArrayDelete(cg
->pwg_size_lut
);
353 _httpFreeCredentials(cg
->tls_credentials
);
354 #endif /* HAVE_SSL */
356 cupsFileClose(cg
->stdio_files
[0]);
357 cupsFileClose(cg
->stdio_files
[1]);
358 cupsFileClose(cg
->stdio_files
[2]);
360 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
364 #endif /* HAVE_PTHREAD_H || WIN32 */
367 #ifdef HAVE_PTHREAD_H
369 * 'cups_globals_init()' - Initialize environment variables.
373 cups_globals_init(void)
376 * Register the global data for this thread...
379 pthread_key_create(&cups_globals_key
, (void (*)(void *))cups_globals_free
);
381 #endif /* HAVE_PTHREAD_H */
385 * End of "$Id: globals.c 13138 2016-03-15 14:59:54Z msweet $".