#include "cups-private.h"
static _cups_threadkey_t cups_globals_key = _CUPS_THREADKEY_INITIALIZER;
#ifdef HAVE_PTHREAD_H
static pthread_once_t cups_globals_key_once = PTHREAD_ONCE_INIT;
#endif
#if defined(HAVE_PTHREAD_H) || defined(WIN32)
static _cups_mutex_t cups_global_mutex = _CUPS_MUTEX_INITIALIZER;
#endif
#ifdef WIN32
static void cups_fix_path(char *path);
#endif
static _cups_globals_t *cups_globals_alloc(void);
#if defined(HAVE_PTHREAD_H) || defined(WIN32)
static void cups_globals_free(_cups_globals_t *g);
#endif
#ifdef HAVE_PTHREAD_H
static void cups_globals_init(void);
#endif
void
_cupsGlobalLock(void)
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&cups_global_mutex);
#elif defined(WIN32)
EnterCriticalSection(&cups_global_mutex.m_criticalSection);
#endif
}
_cups_globals_t *
_cupsGlobals(void)
{
_cups_globals_t *cg;
#ifdef HAVE_PTHREAD_H
pthread_once(&cups_globals_key_once, cups_globals_init);
#endif
if ((cg = (_cups_globals_t *)_cupsThreadGetData(cups_globals_key)) == NULL)
{
if ((cg = cups_globals_alloc()) != NULL)
_cupsThreadSetData(cups_globals_key, cg);
}
return (cg);
}
void
_cupsGlobalUnlock(void)
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&cups_global_mutex);
#elif defined(WIN32)
LeaveCriticalSection(&cups_global_mutex.m_criticalSection);
#endif
}
#ifdef WIN32
BOOL WINAPI
DllMain(HINSTANCE hinst,
DWORD reason,
LPVOID reserved)
{
_cups_globals_t *cg;
(void)hinst;
(void)reserved;
switch (reason)
{
case DLL_PROCESS_ATTACH :
InitializeCriticalSection(&cups_global_mutex.m_criticalSection);
if ((cups_globals_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
return (FALSE);
break;
case DLL_THREAD_DETACH :
if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL)
cups_globals_free(cg);
break;
case DLL_PROCESS_DETACH :
if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL)
cups_globals_free(cg);
TlsFree(cups_globals_key);
DeleteCriticalSection(&cups_global_mutex.m_criticalSection);
break;
default:
break;
}
return (TRUE);
}
#endif
static _cups_globals_t *
cups_globals_alloc(void)
{
_cups_globals_t *cg = malloc(sizeof(_cups_globals_t));
#ifdef WIN32
HKEY key;
DWORD size;
static char installdir[1024] = "",
confdir[1024] = "",
localedir[1024] = "";
#endif
if (!cg)
return (NULL);
memset(cg, 0, sizeof(_cups_globals_t));
cg->encryption = (http_encryption_t)-1;
cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
cg->any_root = 1;
cg->expired_certs = 1;
cg->expired_root = 1;
cg->server_version = 20;
#ifdef WIN32
if (!installdir[0])
{
strcpy(installdir, "C:/Program Files/cups.org");
if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ,
&key))
{
char *ptr;
size = sizeof(installdir);
RegQueryValueEx(key, "installdir", NULL, NULL, installdir, &size);
RegCloseKey(key);
for (ptr = installdir; *ptr;)
{
if (*ptr == '\\')
{
if (ptr[1])
*ptr++ = '/';
else
*ptr = '\0';
}
else if (*ptr == '/' && !ptr[1])
*ptr = '\0';
else
ptr ++;
}
}
snprintf(confdir, sizeof(confdir), "%s/conf", installdir);
snprintf(localedir, sizeof(localedir), "%s/locale", installdir);
}
if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
cg->cups_datadir = installdir;
if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
cg->cups_serverbin = installdir;
if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
cg->cups_serverroot = confdir;
if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
cg->cups_statedir = confdir;
if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
cg->localedir = localedir;
#else
# ifdef HAVE_GETEUID
if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
# else
if (!getuid())
# endif
{
cg->cups_datadir = CUPS_DATADIR;
cg->cups_serverbin = CUPS_SERVERBIN;
cg->cups_serverroot = CUPS_SERVERROOT;
cg->cups_statedir = CUPS_STATEDIR;
cg->localedir = CUPS_LOCALEDIR;
}
else
{
if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
cg->cups_datadir = CUPS_DATADIR;
if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
cg->cups_serverbin = CUPS_SERVERBIN;
if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
cg->cups_serverroot = CUPS_SERVERROOT;
if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
cg->cups_statedir = CUPS_STATEDIR;
if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
cg->localedir = CUPS_LOCALEDIR;
}
#endif
return (cg);
}
#if defined(HAVE_PTHREAD_H) || defined(WIN32)
static void
cups_globals_free(_cups_globals_t *cg)
{
_cups_buffer_t *buffer,
*next;
if (cg->last_status_message)
_cupsStrFree(cg->last_status_message);
for (buffer = cg->cups_buffers; buffer; buffer = next)
{
next = buffer->next;
free(buffer);
}
cupsArrayDelete(cg->leg_size_lut);
cupsArrayDelete(cg->ppd_size_lut);
cupsArrayDelete(cg->pwg_size_lut);
httpClose(cg->http);
_httpFreeCredentials(cg->tls_credentials);
cupsFileClose(cg->stdio_files[0]);
cupsFileClose(cg->stdio_files[1]);
cupsFileClose(cg->stdio_files[2]);
cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
free(cg);
}
#endif
#ifdef HAVE_PTHREAD_H
static void
cups_globals_init(void)
{
pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free);
}
#endif